blob: 76932f5882bab7c02e794c051542a03ed2d05e45 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
238
239#else
240#error "Check if your OS uses bitfields for fd_sets"
241#endif
242
Willy Tarreau80587432006-12-24 17:47:20 +0100243void init_proto_http()
244{
Willy Tarreau42250582007-04-01 01:30:43 +0200245 int i;
246 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100247 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200248
Willy Tarreau80587432006-12-24 17:47:20 +0100249 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
250 if (!http_err_msgs[msg]) {
251 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
252 abort();
253 }
254
255 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
256 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
257 }
Willy Tarreau42250582007-04-01 01:30:43 +0200258
259 /* initialize the log header encoding map : '{|}"#' should be encoded with
260 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
261 * URL encoding only requires '"', '#' to be encoded as well as non-
262 * printable characters above.
263 */
264 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
265 memset(url_encode_map, 0, sizeof(url_encode_map));
266 for (i = 0; i < 32; i++) {
267 FD_SET(i, hdr_encode_map);
268 FD_SET(i, url_encode_map);
269 }
270 for (i = 127; i < 256; i++) {
271 FD_SET(i, hdr_encode_map);
272 FD_SET(i, url_encode_map);
273 }
274
275 tmp = "\"#{|}";
276 while (*tmp) {
277 FD_SET(*tmp, hdr_encode_map);
278 tmp++;
279 }
280
281 tmp = "\"#";
282 while (*tmp) {
283 FD_SET(*tmp, url_encode_map);
284 tmp++;
285 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200286
287 /* memory allocations */
288 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100289 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100290}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau53b6c742006-12-17 13:37:46 +0100292/*
293 * We have 26 list of methods (1 per first letter), each of which can have
294 * up to 3 entries (2 valid, 1 null).
295 */
296struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100297 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100298 int len;
299 const char text[8];
300};
301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100302const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100303 ['C' - 'A'] = {
304 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
305 },
306 ['D' - 'A'] = {
307 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
308 },
309 ['G' - 'A'] = {
310 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
311 },
312 ['H' - 'A'] = {
313 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
314 },
315 ['P' - 'A'] = {
316 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
317 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
318 },
319 ['T' - 'A'] = {
320 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
321 },
322 /* rest is empty like this :
323 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
324 */
325};
326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100327/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100329 * RFC2616 for those chars.
330 */
331
332const char http_is_spht[256] = {
333 [' '] = 1, ['\t'] = 1,
334};
335
336const char http_is_crlf[256] = {
337 ['\r'] = 1, ['\n'] = 1,
338};
339
340const char http_is_lws[256] = {
341 [' '] = 1, ['\t'] = 1,
342 ['\r'] = 1, ['\n'] = 1,
343};
344
345const char http_is_sep[256] = {
346 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
347 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
348 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
349 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
350 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
351};
352
353const char http_is_ctl[256] = {
354 [0 ... 31] = 1,
355 [127] = 1,
356};
357
358/*
359 * A token is any ASCII char that is neither a separator nor a CTL char.
360 * Do not overwrite values in assignment since gcc-2.95 will not handle
361 * them correctly. Instead, define every non-CTL char's status.
362 */
363const char http_is_token[256] = {
364 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
365 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
366 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
367 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
369 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
370 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
371 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
372 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
373 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
374 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
375 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
376 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
377 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
378 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
379 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
380 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
381 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
382 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
383 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
384 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
385 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
386 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
387 ['|'] = 1, ['}'] = 0, ['~'] = 1,
388};
389
390
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100391/*
392 * An http ver_token is any ASCII which can be found in an HTTP version,
393 * which includes 'H', 'T', 'P', '/', '.' and any digit.
394 */
395const char http_is_ver_token[256] = {
396 ['.'] = 1, ['/'] = 1,
397 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
398 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
399 ['H'] = 1, ['P'] = 1, ['T'] = 1,
400};
401
402
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100403/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100404 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
405 */
406#if defined(DEBUG_FSM)
407static void http_silent_debug(int line, struct session *s)
408{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100409 chunk_printf(&trash,
410 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
411 line,
412 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
413 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
414 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100415
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416 chunk_printf(&trash,
417 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
418 line,
419 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
420 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
421 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100422}
423#else
424#define http_silent_debug(l,s) do { } while (0)
425#endif
426
427/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100428 * Adds a header and its CRLF at the tail of the message's buffer, just before
429 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100430 * The header is also automatically added to the index <hdr_idx>, and the end
431 * of headers is automatically adjusted. The number of bytes added is returned
432 * on success, otherwise <0 is returned indicating an error.
433 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100434int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435{
436 int bytes, len;
437
438 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200439 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440 if (!bytes)
441 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100442 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
444}
445
446/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100447 * Adds a header and its CRLF at the tail of the message's buffer, just before
448 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100449 * the buffer is only opened and the space reserved, but nothing is copied.
450 * The header is also automatically added to the index <hdr_idx>, and the end
451 * of headers is automatically adjusted. The number of bytes added is returned
452 * on success, otherwise <0 is returned indicating an error.
453 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100454int http_header_add_tail2(struct http_msg *msg,
455 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456{
457 int bytes;
458
Willy Tarreau9b28e032012-10-12 23:49:43 +0200459 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100460 if (!bytes)
461 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100462 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
466/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100467 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
468 * If so, returns the position of the first non-space character relative to
469 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
470 * to return a pointer to the place after the first space. Returns 0 if the
471 * header name does not match. Checks are case-insensitive.
472 */
473int http_header_match2(const char *hdr, const char *end,
474 const char *name, int len)
475{
476 const char *val;
477
478 if (hdr + len >= end)
479 return 0;
480 if (hdr[len] != ':')
481 return 0;
482 if (strncasecmp(hdr, name, len) != 0)
483 return 0;
484 val = hdr + len + 1;
485 while (val < end && HTTP_IS_SPHT(*val))
486 val++;
487 if ((val >= end) && (len + 2 <= end - hdr))
488 return len + 2; /* we may replace starting from second space */
489 return val - hdr;
490}
491
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200492/* Find the first or next occurrence of header <name> in message buffer <sol>
493 * using headers index <idx>, and return it in the <ctx> structure. This
494 * structure holds everything necessary to use the header and find next
495 * occurrence. If its <idx> member is 0, the header is searched from the
496 * beginning. Otherwise, the next occurrence is returned. The function returns
497 * 1 when it finds a value, and 0 when there is no more. It is very similar to
498 * http_find_header2() except that it is designed to work with full-line headers
499 * whose comma is not a delimiter but is part of the syntax. As a special case,
500 * if ctx->val is NULL when searching for a new values of a header, the current
501 * header is rescanned. This allows rescanning after a header deletion.
502 */
503int http_find_full_header2(const char *name, int len,
504 char *sol, struct hdr_idx *idx,
505 struct hdr_ctx *ctx)
506{
507 char *eol, *sov;
508 int cur_idx, old_idx;
509
510 cur_idx = ctx->idx;
511 if (cur_idx) {
512 /* We have previously returned a header, let's search another one */
513 sol = ctx->line;
514 eol = sol + idx->v[cur_idx].len;
515 goto next_hdr;
516 }
517
518 /* first request for this header */
519 sol += hdr_idx_first_pos(idx);
520 old_idx = 0;
521 cur_idx = hdr_idx_first_idx(idx);
522 while (cur_idx) {
523 eol = sol + idx->v[cur_idx].len;
524
525 if (len == 0) {
526 /* No argument was passed, we want any header.
527 * To achieve this, we simply build a fake request. */
528 while (sol + len < eol && sol[len] != ':')
529 len++;
530 name = sol;
531 }
532
533 if ((len < eol - sol) &&
534 (sol[len] == ':') &&
535 (strncasecmp(sol, name, len) == 0)) {
536 ctx->del = len;
537 sov = sol + len + 1;
538 while (sov < eol && http_is_lws[(unsigned char)*sov])
539 sov++;
540
541 ctx->line = sol;
542 ctx->prev = old_idx;
543 ctx->idx = cur_idx;
544 ctx->val = sov - sol;
545 ctx->tws = 0;
546 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
547 eol--;
548 ctx->tws++;
549 }
550 ctx->vlen = eol - sov;
551 return 1;
552 }
553 next_hdr:
554 sol = eol + idx->v[cur_idx].cr + 1;
555 old_idx = cur_idx;
556 cur_idx = idx->v[cur_idx].next;
557 }
558 return 0;
559}
560
Willy Tarreau68085d82010-01-18 14:54:04 +0100561/* Find the end of the header value contained between <s> and <e>. See RFC2616,
562 * par 2.2 for more information. Note that it requires a valid header to return
563 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100565char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200566{
567 int quoted, qdpair;
568
569 quoted = qdpair = 0;
570 for (; s < e; s++) {
571 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200572 else if (quoted) {
573 if (*s == '\\') qdpair = 1;
574 else if (*s == '"') quoted = 0;
575 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 else if (*s == '"') quoted = 1;
577 else if (*s == ',') return s;
578 }
579 return s;
580}
581
582/* Find the first or next occurrence of header <name> in message buffer <sol>
583 * using headers index <idx>, and return it in the <ctx> structure. This
584 * structure holds everything necessary to use the header and find next
585 * occurrence. If its <idx> member is 0, the header is searched from the
586 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100587 * 1 when it finds a value, and 0 when there is no more. It is designed to work
588 * with headers defined as comma-separated lists. As a special case, if ctx->val
589 * is NULL when searching for a new values of a header, the current header is
590 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
592int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 struct hdr_ctx *ctx)
595{
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 char *eol, *sov;
597 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200598
Willy Tarreau68085d82010-01-18 14:54:04 +0100599 cur_idx = ctx->idx;
600 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 /* We have previously returned a value, let's search
602 * another one on the same line.
603 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200605 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 eol = sol + idx->v[cur_idx].len;
608
609 if (sov >= eol)
610 /* no more values in this header */
611 goto next_hdr;
612
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* values remaining for this header, skip the comma but save it
614 * for later use (eg: for header deletion).
615 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 sov++;
617 while (sov < eol && http_is_lws[(unsigned char)*sov])
618 sov++;
619
620 goto return_hdr;
621 }
622
623 /* first request for this header */
624 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627 while (cur_idx) {
628 eol = sol + idx->v[cur_idx].len;
629
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200630 if (len == 0) {
631 /* No argument was passed, we want any header.
632 * To achieve this, we simply build a fake request. */
633 while (sol + len < eol && sol[len] != ':')
634 len++;
635 name = sol;
636 }
637
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 if ((len < eol - sol) &&
639 (sol[len] == ':') &&
640 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 sov = sol + len + 1;
643 while (sov < eol && http_is_lws[(unsigned char)*sov])
644 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100645
Willy Tarreau33a7e692007-06-10 19:45:56 +0200646 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100647 ctx->prev = old_idx;
648 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200649 ctx->idx = cur_idx;
650 ctx->val = sov - sol;
651
652 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200654 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200655 eol--;
656 ctx->tws++;
657 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 ctx->vlen = eol - sov;
659 return 1;
660 }
661 next_hdr:
662 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200664 cur_idx = idx->v[cur_idx].next;
665 }
666 return 0;
667}
668
669int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 struct hdr_ctx *ctx)
672{
673 return http_find_header2(name, strlen(name), sol, idx, ctx);
674}
675
Willy Tarreau68085d82010-01-18 14:54:04 +0100676/* Remove one value of a header. This only works on a <ctx> returned by one of
677 * the http_find_header functions. The value is removed, as well as surrounding
678 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100679 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * message <msg>. The new index is returned. If it is zero, it means there is
681 * no more header, so any processing may stop. The ctx is always left in a form
682 * that can be handled by http_find_header2() to find next occurrence.
683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100685{
686 int cur_idx = ctx->idx;
687 char *sol = ctx->line;
688 struct hdr_idx_elem *hdr;
689 int delta, skip_comma;
690
691 if (!cur_idx)
692 return 0;
693
694 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200697 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 http_msg_move_end(msg, delta);
699 idx->used--;
700 hdr->len = 0; /* unused entry */
701 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100702 if (idx->tail == ctx->idx)
703 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100704 ctx->idx = ctx->prev; /* walk back to the end of previous header */
705 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
706 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200707 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 return ctx->idx;
709 }
710
711 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200712 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
713 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 */
715
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200716 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200717 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200718 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 NULL, 0);
720 hdr->len += delta;
721 http_msg_move_end(msg, delta);
722 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725}
726
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727/* This function handles a server error at the stream interface level. The
728 * stream interface is assumed to be already in a closed state. An optional
729 * message is copied into the input buffer, and an HTTP status code stored.
730 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100731 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733static void http_server_error(struct session *t, struct stream_interface *si,
734 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200736 channel_auto_read(si->ob);
737 channel_abort(si->ob);
738 channel_auto_close(si->ob);
739 channel_erase(si->ob);
740 channel_auto_close(si->ib);
741 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100742 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100743 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200744 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 }
746 if (!(t->flags & SN_ERR_MASK))
747 t->flags |= err;
748 if (!(t->flags & SN_FINST_MASK))
749 t->flags |= finst;
750}
751
Willy Tarreau80587432006-12-24 17:47:20 +0100752/* This function returns the appropriate error location for the given session
753 * and message.
754 */
755
Willy Tarreau783f2582012-09-04 12:19:04 +0200756struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100757{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200758 if (s->be->errmsg[msgnum].str)
759 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100760 else if (s->fe->errmsg[msgnum].str)
761 return &s->fe->errmsg[msgnum];
762 else
763 return &http_err_chunks[msgnum];
764}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765
Willy Tarreau53b6c742006-12-17 13:37:46 +0100766/*
767 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
768 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
769 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100770static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100771{
772 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100773 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100774
775 m = ((unsigned)*str - 'A');
776
777 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100778 for (h = http_methods[m]; h->len > 0; h++) {
779 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100780 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100781 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100783 };
784 return HTTP_METH_OTHER;
785 }
786 return HTTP_METH_NONE;
787
788}
789
Willy Tarreau21d2af32008-02-14 20:25:24 +0100790/* Parse the URI from the given transaction (which is assumed to be in request
791 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
792 * It is returned otherwise.
793 */
794static char *
795http_get_path(struct http_txn *txn)
796{
797 char *ptr, *end;
798
Willy Tarreau9b28e032012-10-12 23:49:43 +0200799 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100800 end = ptr + txn->req.sl.rq.u_l;
801
802 if (ptr >= end)
803 return NULL;
804
805 /* RFC2616, par. 5.1.2 :
806 * Request-URI = "*" | absuri | abspath | authority
807 */
808
809 if (*ptr == '*')
810 return NULL;
811
812 if (isalpha((unsigned char)*ptr)) {
813 /* this is a scheme as described by RFC3986, par. 3.1 */
814 ptr++;
815 while (ptr < end &&
816 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
817 ptr++;
818 /* skip '://' */
819 if (ptr == end || *ptr++ != ':')
820 return NULL;
821 if (ptr == end || *ptr++ != '/')
822 return NULL;
823 if (ptr == end || *ptr++ != '/')
824 return NULL;
825 }
826 /* skip [user[:passwd]@]host[:[port]] */
827
828 while (ptr < end && *ptr != '/')
829 ptr++;
830
831 if (ptr == end)
832 return NULL;
833
834 /* OK, we got the '/' ! */
835 return ptr;
836}
837
Willy Tarreau71241ab2012-12-27 11:30:54 +0100838/* Returns a 302 for a redirectable request that reaches a server working in
839 * in redirect mode. This may only be called just after the stream interface
840 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
841 * follow normal proxy processing. NOTE: this function is designed to support
842 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100844void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845{
846 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200849 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850
851 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100852 trash.len = strlen(HTTP_302);
853 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100855 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100856
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100858 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859 return;
860
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100861 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100862 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100863 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
864 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100865 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100866
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200867 /* 3: add the request URI. Since it was already forwarded, we need
868 * to temporarily rewind the buffer.
869 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200871 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200872
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200874 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200875
Willy Tarreau9b28e032012-10-12 23:49:43 +0200876 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200877
Willy Tarreauefb453c2008-10-26 20:49:47 +0100878 if (!path)
879 return;
880
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100881 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100882 return;
883
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100884 memcpy(trash.str + trash.len, path, len);
885 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100886
887 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100888 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
889 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100890 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100891 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
892 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100893 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100894
895 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200896 si_shutr(si);
897 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100898 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 si->state = SI_ST_CLO;
900
901 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200902 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100903
904 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100905 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906}
907
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100908/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 * that the server side is closed. Note that err_type is actually a
910 * bitmask, where almost only aborts may be cumulated with other
911 * values. We consider that aborted operations are more important
912 * than timeouts or errors due to the fact that nobody else in the
913 * logs might explain incomplete retries. All others should avoid
914 * being cumulated. It should normally not be possible to have multiple
915 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100916 * Note that connection errors appearing on the second request of a keep-alive
917 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100921 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922
923 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100924 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200925 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100927 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100928 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
929 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200932 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200935 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100937 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100938 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
939 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100940 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100941 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100942 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
943 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200944 else if (err_type & SI_ET_CONN_RES)
945 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100946 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
947 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100948 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100949 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200950 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951}
952
Willy Tarreau42250582007-04-01 01:30:43 +0200953extern const char sess_term_cond[8];
954extern const char sess_fin_state[8];
955extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200956struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100957struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100958struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100959
Willy Tarreau117f59e2007-03-04 18:17:17 +0100960/*
961 * Capture headers from message starting at <som> according to header list
962 * <cap_hdr>, and fill the <idx> structure appropriately.
963 */
964void capture_headers(char *som, struct hdr_idx *idx,
965 char **cap, struct cap_hdr *cap_hdr)
966{
967 char *eol, *sol, *col, *sov;
968 int cur_idx;
969 struct cap_hdr *h;
970 int len;
971
972 sol = som + hdr_idx_first_pos(idx);
973 cur_idx = hdr_idx_first_idx(idx);
974
975 while (cur_idx) {
976 eol = sol + idx->v[cur_idx].len;
977
978 col = sol;
979 while (col < eol && *col != ':')
980 col++;
981
982 sov = col + 1;
983 while (sov < eol && http_is_lws[(unsigned char)*sov])
984 sov++;
985
986 for (h = cap_hdr; h; h = h->next) {
987 if ((h->namelen == col - sol) &&
988 (strncasecmp(sol, h->name, h->namelen) == 0)) {
989 if (cap[h->index] == NULL)
990 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200991 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100992
993 if (cap[h->index] == NULL) {
994 Alert("HTTP capture : out of memory.\n");
995 continue;
996 }
997
998 len = eol - sov;
999 if (len > h->len)
1000 len = h->len;
1001
1002 memcpy(cap[h->index], sov, len);
1003 cap[h->index][len]=0;
1004 }
1005 }
1006 sol = eol + idx->v[cur_idx].cr + 1;
1007 cur_idx = idx->v[cur_idx].next;
1008 }
1009}
1010
1011
Willy Tarreau42250582007-04-01 01:30:43 +02001012/* either we find an LF at <ptr> or we jump to <bad>.
1013 */
1014#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1015
1016/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1017 * otherwise to <http_msg_ood> with <state> set to <st>.
1018 */
1019#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1020 ptr++; \
1021 if (likely(ptr < end)) \
1022 goto good; \
1023 else { \
1024 state = (st); \
1025 goto http_msg_ood; \
1026 } \
1027 } while (0)
1028
1029
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001031 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001032 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1033 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1034 * will give undefined results.
1035 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1036 * and that msg->sol points to the beginning of the response.
1037 * If a complete line is found (which implies that at least one CR or LF is
1038 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1039 * returned indicating an incomplete line (which does not mean that parts have
1040 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1041 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1042 * upon next call.
1043 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001044 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1046 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001047 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001049const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001050 enum ht_state state, const char *ptr, const char *end,
1051 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001052{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001053 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001054
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001056 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001057 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001058 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1060
1061 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001062 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001063 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1064 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001065 state = HTTP_MSG_ERROR;
1066 break;
1067
Willy Tarreau8973c702007-01-21 23:58:29 +01001068 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001069 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001070 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001071 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001072 goto http_msg_rpcode;
1073 }
1074 if (likely(HTTP_IS_SPHT(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1076 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001077 state = HTTP_MSG_ERROR;
1078 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001079
Willy Tarreau8973c702007-01-21 23:58:29 +01001080 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001081 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001082 if (likely(!HTTP_IS_LWS(*ptr)))
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1084
1085 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001087 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1088 }
1089
1090 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 http_msg_rsp_reason:
1093 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001095 msg->sl.st.r_l = 0;
1096 goto http_msg_rpline_eol;
1097
Willy Tarreau8973c702007-01-21 23:58:29 +01001098 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001099 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001101 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 goto http_msg_rpreason;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1106 /* so it's a CR/LF, so there is no reason phrase */
1107 goto http_msg_rsp_reason;
1108
Willy Tarreau8973c702007-01-21 23:58:29 +01001109 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001110 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001111 if (likely(!HTTP_IS_CRLF(*ptr)))
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001113 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 http_msg_rpline_eol:
1115 /* We have seen the end of line. Note that we do not
1116 * necessarily have the \n yet, but at least we know that we
1117 * have EITHER \r OR \n, otherwise the response would not be
1118 * complete. We can then record the response length and return
1119 * to the caller which will be able to register it.
1120 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001121 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 return ptr;
1123
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001125#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1127 exit(1);
1128#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001129 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001130 }
1131
1132 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001133 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001134 if (ret_state)
1135 *ret_state = state;
1136 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001137 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001139}
1140
Willy Tarreau8973c702007-01-21 23:58:29 +01001141/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 * This function parses a request line between <ptr> and <end>, starting with
1143 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1144 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1145 * will give undefined results.
1146 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1147 * and that msg->sol points to the beginning of the request.
1148 * If a complete line is found (which implies that at least one CR or LF is
1149 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1150 * returned indicating an incomplete line (which does not mean that parts have
1151 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1152 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1153 * upon next call.
1154 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001155 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1157 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001158 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001159 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001160const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001161 enum ht_state state, const char *ptr, const char *end,
1162 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001164 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001167 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001168 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 if (likely(HTTP_IS_TOKEN(*ptr)))
1170 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001171
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 if (likely(HTTP_IS_CRLF(*ptr))) {
1178 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001179 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001181 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001185 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 msg->sl.rq.v_l = 0;
1187 goto http_msg_rqline_eol;
1188 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001189 state = HTTP_MSG_ERROR;
1190 break;
1191
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001192 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001193 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001195 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 goto http_msg_rquri;
1197 }
1198 if (likely(HTTP_IS_SPHT(*ptr)))
1199 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1200 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1201 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001203 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001204 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001205 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001207
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001208 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001209 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1211 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001212
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001213 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001214 /* non-ASCII chars are forbidden unless option
1215 * accept-invalid-http-request is enabled in the frontend.
1216 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001217 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001218 if (msg->err_pos < -1)
1219 goto invalid_char;
1220 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001221 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001222 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1223 }
1224
1225 if (likely(HTTP_IS_CRLF(*ptr))) {
1226 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1227 goto http_msg_req09_uri_e;
1228 }
1229
1230 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001231 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001232 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001233 state = HTTP_MSG_ERROR;
1234 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001237 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001239 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 goto http_msg_rqver;
1241 }
1242 if (likely(HTTP_IS_SPHT(*ptr)))
1243 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1244 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1245 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001248 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001249 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001251
1252 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001254 http_msg_rqline_eol:
1255 /* We have seen the end of line. Note that we do not
1256 * necessarily have the \n yet, but at least we know that we
1257 * have EITHER \r OR \n, otherwise the request would not be
1258 * complete. We can then record the request length and return
1259 * to the caller which will be able to register it.
1260 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001261 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001262 return ptr;
1263 }
1264
1265 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001266 state = HTTP_MSG_ERROR;
1267 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001270#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1272 exit(1);
1273#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001274 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001278 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 if (ret_state)
1280 *ret_state = state;
1281 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001282 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001286/*
1287 * Returns the data from Authorization header. Function may be called more
1288 * than once so data is stored in txn->auth_data. When no header is found
1289 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1290 * searching again for something we are unable to find anyway.
1291 */
1292
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001293char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001294
1295int
1296get_http_auth(struct session *s)
1297{
1298
1299 struct http_txn *txn = &s->txn;
1300 struct chunk auth_method;
1301 struct hdr_ctx ctx;
1302 char *h, *p;
1303 int len;
1304
1305#ifdef DEBUG_AUTH
1306 printf("Auth for session %p: %d\n", s, txn->auth.method);
1307#endif
1308
1309 if (txn->auth.method == HTTP_AUTH_WRONG)
1310 return 0;
1311
1312 if (txn->auth.method)
1313 return 1;
1314
1315 txn->auth.method = HTTP_AUTH_WRONG;
1316
1317 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001318
1319 if (txn->flags & TX_USE_PX_CONN) {
1320 h = "Proxy-Authorization";
1321 len = strlen(h);
1322 } else {
1323 h = "Authorization";
1324 len = strlen(h);
1325 }
1326
Willy Tarreau9b28e032012-10-12 23:49:43 +02001327 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001328 return 0;
1329
1330 h = ctx.line + ctx.val;
1331
1332 p = memchr(h, ' ', ctx.vlen);
1333 if (!p || p == h)
1334 return 0;
1335
1336 chunk_initlen(&auth_method, h, 0, p-h);
1337 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1338
1339 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1340
1341 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001342 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001343
1344 if (len < 0)
1345 return 0;
1346
1347
1348 get_http_auth_buff[len] = '\0';
1349
1350 p = strchr(get_http_auth_buff, ':');
1351
1352 if (!p)
1353 return 0;
1354
1355 txn->auth.user = get_http_auth_buff;
1356 *p = '\0';
1357 txn->auth.pass = p+1;
1358
1359 txn->auth.method = HTTP_AUTH_BASIC;
1360 return 1;
1361 }
1362
1363 return 0;
1364}
1365
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau8973c702007-01-21 23:58:29 +01001367/*
1368 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001369 * depending on the initial msg->msg_state. The caller is responsible for
1370 * ensuring that the message does not wrap. The function can be preempted
1371 * everywhere when data are missing and recalled at the exact same location
1372 * with no information loss. The message may even be realigned between two
1373 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001374 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001375 * fields. Note that msg->sol will be initialized after completing the first
1376 * state, so that none of the msg pointers has to be initialized prior to the
1377 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001379void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380{
Willy Tarreau3770f232013-12-07 00:01:53 +01001381 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001383 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001384
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001385 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001386 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001387 ptr = buf->p + msg->next;
1388 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(ptr >= end))
1391 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 /*
1395 * First, states that are specific to the response only.
1396 * We check them first so that request and headers are
1397 * closer to each other (accessed more often).
1398 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001400 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001402 /* we have a start of message, but we have to check
1403 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001404 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001405 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001406 if (unlikely(ptr != buf->p)) {
1407 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001408 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001409 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001410 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 }
Willy Tarreau26927362012-05-18 23:22:52 +02001412 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001413 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 hdr_idx_init(idx);
1415 state = HTTP_MSG_RPVER;
1416 goto http_msg_rpver;
1417 }
1418
1419 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1420 goto http_msg_invalid;
1421
1422 if (unlikely(*ptr == '\n'))
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1425 /* stop here */
1426
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001428 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 EXPECT_LF_HERE(ptr, http_msg_invalid);
1430 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1431 /* stop here */
1432
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001434 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 case HTTP_MSG_RPVER_SP:
1436 case HTTP_MSG_RPCODE:
1437 case HTTP_MSG_RPCODE_SP:
1438 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001439 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001440 state, ptr, end,
1441 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 if (unlikely(!ptr))
1443 return;
1444
1445 /* we have a full response and we know that we have either a CR
1446 * or an LF at <ptr>.
1447 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1449
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001450 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 if (likely(*ptr == '\r'))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1453 goto http_msg_rpline_end;
1454
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001457 /* msg->sol must point to the first of CR or LF. */
1458 EXPECT_LF_HERE(ptr, http_msg_invalid);
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1460 /* stop here */
1461
1462 /*
1463 * Second, states that are specific to the request only
1464 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001468 /* we have a start of message, but we have to check
1469 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001470 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001471 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001472 if (likely(ptr != buf->p)) {
1473 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001475 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 }
Willy Tarreau26927362012-05-18 23:22:52 +02001478 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001479 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 state = HTTP_MSG_RQMETH;
1481 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1485 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 if (unlikely(*ptr == '\n'))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 EXPECT_LF_HERE(ptr, http_msg_invalid);
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001496 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001499 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 case HTTP_MSG_RQMETH_SP:
1501 case HTTP_MSG_RQURI:
1502 case HTTP_MSG_RQURI_SP:
1503 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001504 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001505 state, ptr, end,
1506 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (unlikely(!ptr))
1508 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 /* we have a full request and we know that we have either a CR
1511 * or an LF at <ptr>.
1512 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001515 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 /* check for HTTP/0.9 request : no version information available.
1523 * msg->sol must point to the first of CR or LF.
1524 */
1525 if (unlikely(msg->sl.rq.v_l == 0))
1526 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 EXPECT_LF_HERE(ptr, http_msg_invalid);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8973c702007-01-21 23:58:29 +01001532 /*
1533 * Common states below
1534 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001537 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 if (likely(!HTTP_IS_CRLF(*ptr))) {
1539 goto http_msg_hdr_name;
1540 }
1541
1542 if (likely(*ptr == '\r'))
1543 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1544 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001547 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 /* assumes msg->sol points to the first char */
1549 if (likely(HTTP_IS_TOKEN(*ptr)))
1550 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001551
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001552 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001555 if (likely(msg->err_pos < -1) || *ptr == '\n')
1556 goto http_msg_invalid;
1557
1558 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001560
1561 /* and we still accept this non-token character */
1562 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001566 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(HTTP_IS_SPHT(*ptr)))
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001571 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 if (likely(!HTTP_IS_CRLF(*ptr))) {
1574 goto http_msg_hdr_val;
1575 }
1576
1577 if (likely(*ptr == '\r'))
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1579 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 EXPECT_LF_HERE(ptr, http_msg_invalid);
1584 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001587 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(HTTP_IS_SPHT(*ptr))) {
1589 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001590 for (; buf->p + msg->sov < ptr; msg->sov++)
1591 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 goto http_msg_hdr_l1_sp;
1593 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001594 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001595 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 goto http_msg_complete_header;
1597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001599 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001600 /* assumes msg->sol points to the first char, and msg->sov
1601 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 */
1603 if (likely(!HTTP_IS_CRLF(*ptr)))
1604 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 /* Note: we could also copy eol into ->eoh so that we have the
1608 * real header end in case it ends with lots of LWS, but is this
1609 * really needed ?
1610 */
1611 if (likely(*ptr == '\r'))
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1613 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001616 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 EXPECT_LF_HERE(ptr, http_msg_invalid);
1618 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001621 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1623 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001624 for (; buf->p + msg->eol < ptr; msg->eol++)
1625 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 goto http_msg_hdr_val;
1627 }
1628 http_msg_complete_header:
1629 /*
1630 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001631 * Assumes msg->sol points to the first char, msg->sov points
1632 * to the first character of the value and msg->eol to the
1633 * first CR or LF so we know how the line ends. We insert last
1634 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001636 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 idx, idx->tail) < 0))
1638 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001639
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001640 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 if (likely(!HTTP_IS_CRLF(*ptr))) {
1642 goto http_msg_hdr_name;
1643 }
1644
1645 if (likely(*ptr == '\r'))
1646 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1647 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 /* Assumes msg->sol points to the first of either CR or LF */
1652 EXPECT_LF_HERE(ptr, http_msg_invalid);
1653 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001654 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001655 msg->eoh = msg->sol;
1656 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001657 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001659
1660 case HTTP_MSG_ERROR:
1661 /* this may only happen if we call http_msg_analyser() twice with an error */
1662 break;
1663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001665#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1667 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001668#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001669 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 }
1671 http_msg_ood:
1672 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001674 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 http_msg_invalid:
1678 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001679 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001680 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 return;
1682}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001683
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001684/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1685 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1686 * nothing is done and 1 is returned.
1687 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001688static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001689{
1690 int delta;
1691 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001692 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001693
1694 if (msg->sl.rq.v_l != 0)
1695 return 1;
1696
Willy Tarreau9b28e032012-10-12 23:49:43 +02001697 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001698 delta = 0;
1699
1700 if (msg->sl.rq.u_l == 0) {
1701 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001702 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001703 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001704 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001705 }
1706 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001707 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001708 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001709 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001710 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001711 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001712 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001713 NULL, NULL);
1714 if (unlikely(!cur_end))
1715 return 0;
1716
1717 /* we have a full HTTP/1.0 request now and we know that
1718 * we have either a CR or an LF at <ptr>.
1719 */
1720 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1721 return 1;
1722}
1723
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001724/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 * and "keep-alive" values. If we already know that some headers may safely
1726 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001727 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1728 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001729 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1731 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1732 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001733 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001734 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001735void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001736{
Willy Tarreau5b154472009-12-21 20:11:07 +01001737 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001738 const char *hdr_val = "Connection";
1739 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001740
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001742 return;
1743
Willy Tarreau88d349d2010-01-25 12:15:43 +01001744 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1745 hdr_val = "Proxy-Connection";
1746 hdr_len = 16;
1747 }
1748
Willy Tarreau5b154472009-12-21 20:11:07 +01001749 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001750 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001751 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1753 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001754 if (to_del & 2)
1755 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001756 else
1757 txn->flags |= TX_CON_KAL_SET;
1758 }
1759 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1760 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001761 if (to_del & 1)
1762 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001763 else
1764 txn->flags |= TX_CON_CLO_SET;
1765 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001766 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1767 txn->flags |= TX_HDR_CONN_UPG;
1768 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001769 }
1770
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001771 txn->flags |= TX_HDR_CONN_PRS;
1772 return;
1773}
Willy Tarreau5b154472009-12-21 20:11:07 +01001774
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001775/* Apply desired changes on the Connection: header. Values may be removed and/or
1776 * added depending on the <wanted> flags, which are exclusively composed of
1777 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1778 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1779 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001780void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001781{
1782 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001783 const char *hdr_val = "Connection";
1784 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001785
1786 ctx.idx = 0;
1787
Willy Tarreau88d349d2010-01-25 12:15:43 +01001788
1789 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1790 hdr_val = "Proxy-Connection";
1791 hdr_len = 16;
1792 }
1793
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001794 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001795 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001796 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1797 if (wanted & TX_CON_KAL_SET)
1798 txn->flags |= TX_CON_KAL_SET;
1799 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001801 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1803 if (wanted & TX_CON_CLO_SET)
1804 txn->flags |= TX_CON_CLO_SET;
1805 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001806 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001807 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001808 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001809
1810 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1811 return;
1812
1813 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1814 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001815 hdr_val = "Connection: close";
1816 hdr_len = 17;
1817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1818 hdr_val = "Proxy-Connection: close";
1819 hdr_len = 23;
1820 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001821 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001822 }
1823
1824 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1825 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001826 hdr_val = "Connection: keep-alive";
1827 hdr_len = 22;
1828 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1829 hdr_val = "Proxy-Connection: keep-alive";
1830 hdr_len = 28;
1831 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001833 }
1834 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001835}
1836
Willy Tarreaua458b672012-03-05 11:17:50 +01001837/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001838 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001839 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001841 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001842 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001843static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001844{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001845 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001846 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001847 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 const char *end = buf->data + buf->size;
1849 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001850 unsigned int chunk = 0;
1851
1852 /* The chunk size is in the following form, though we are only
1853 * interested in the size and CRLF :
1854 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1855 */
1856 while (1) {
1857 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001858 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001859 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001861 if (c < 0) /* not a hex digit anymore */
1862 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001863 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001865 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001866 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 chunk = (chunk << 4) + c;
1868 }
1869
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001871 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001872 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873
1874 while (http_is_spht[(unsigned char)*ptr]) {
1875 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001876 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001877 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001878 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001879 }
1880
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 /* Up to there, we know that at least one byte is present at *ptr. Check
1882 * for the end of chunk size.
1883 */
1884 while (1) {
1885 if (likely(HTTP_IS_CRLF(*ptr))) {
1886 /* we now have a CR or an LF at ptr */
1887 if (likely(*ptr == '\r')) {
1888 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001889 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001890 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 return 0;
1892 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001893
Willy Tarreaud98cf932009-12-27 22:54:55 +01001894 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001895 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898 /* done */
1899 break;
1900 }
1901 else if (*ptr == ';') {
1902 /* chunk extension, ends at next CRLF */
1903 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001905 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001906 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907
1908 while (!HTTP_IS_CRLF(*ptr)) {
1909 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001911 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 return 0;
1913 }
1914 /* we have a CRLF now, loop above */
1915 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001916 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001918 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001919 }
1920
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001922 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001924 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001925 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001927 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001929 msg->chunk_len = chunk;
1930 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001932 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001933 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001934 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001935 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001936}
1937
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001938/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 * the trailers is found, it is automatically scheduled to be forwarded,
1941 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1942 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001943 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001944 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001945 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001946 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1947 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001948 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001949 * matches the length of trailers already parsed and not forwarded. It is also
1950 * important to note that this function is designed to be able to parse wrapped
1951 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001953static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001955 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001956
Willy Tarreaua458b672012-03-05 11:17:50 +01001957 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001959 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001960 const char *ptr = b_ptr(buf, msg->next);
1961 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001962 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963
1964 /* scan current line and stop at LF or CRLF */
1965 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001966 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 return 0;
1968
1969 if (*ptr == '\n') {
1970 if (!p1)
1971 p1 = ptr;
1972 p2 = ptr;
1973 break;
1974 }
1975
1976 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001977 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001978 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001980 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 p1 = ptr;
1982 }
1983
1984 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 if (ptr >= buf->data + buf->size)
1986 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987 }
1988
1989 /* after LF; point to beginning of next line */
1990 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 if (p2 >= buf->data + buf->size)
1992 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001993
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001995 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001996 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001997
1998 /* schedule this line for forwarding */
1999 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002000 if (msg->sov >= buf->size)
2001 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002003 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002004 /* LF/CRLF at beginning of line => end of trailers at p2.
2005 * Everything was scheduled for forwarding, there's nothing
2006 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002007 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 msg->msg_state = HTTP_MSG_DONE;
2010 return 1;
2011 }
2012 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002013 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 }
2015}
2016
Willy Tarreau54d23df2012-10-25 19:04:45 +02002017/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002019 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002020 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002021 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2022 * not enough data are available, the function does not change anything and
2023 * returns zero. If a parse error is encountered, the function returns < 0 and
2024 * does not change anything. Note: this function is designed to parse wrapped
2025 * CRLF at the end of the buffer.
2026 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002027static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002028{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002029 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002030 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 int bytes;
2032
2033 /* NB: we'll check data availabilty at the end. It's not a
2034 * problem because whatever we match first will be checked
2035 * against the correct length.
2036 */
2037 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002038 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039 if (*ptr == '\r') {
2040 bytes++;
2041 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002042 if (ptr >= buf->data + buf->size)
2043 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 }
2045
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002046 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 return 0;
2048
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002049 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002052 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053
2054 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002055 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002056 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002057 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2058 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002059 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2061 return 1;
2062}
Willy Tarreau5b154472009-12-21 20:11:07 +01002063
William Lallemand82fe75c2012-10-23 10:25:10 +02002064
2065/*
2066 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002067 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002068int select_compression_request_header(struct session *s, struct buffer *req)
2069{
2070 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002071 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002072 struct hdr_ctx ctx;
2073 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002074 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002075
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002076 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2077 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002078 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2079 */
2080 ctx.idx = 0;
2081 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2082 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002083 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2084 (ctx.vlen < 31 ||
2085 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2086 ctx.line[ctx.val + 30] < '6' ||
2087 (ctx.line[ctx.val + 30] == '6' &&
2088 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2089 s->comp_algo = NULL;
2090 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002091 }
2092
William Lallemand82fe75c2012-10-23 10:25:10 +02002093 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002094 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 +02002095 ctx.idx = 0;
2096 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002097 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002098 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2099 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002100
2101 /* remove all occurrences of the header when "compression offload" is set */
2102
2103 if ((s->be->comp && s->be->comp->offload) ||
2104 (s->fe->comp && s->fe->comp->offload)) {
2105 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2106 ctx.idx = 0;
2107 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2108 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2109 }
2110 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002111 return 1;
2112 }
2113 }
2114 }
2115 }
2116
2117 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002118 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2119 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002120 if (comp_algo->add_data == identity_add_data) {
2121 s->comp_algo = comp_algo;
2122 return 1;
2123 }
2124 }
2125 }
2126
2127 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002128 return 0;
2129}
2130
2131/*
2132 * Selects a comression algorithm depending of the server response.
2133 */
2134int select_compression_response_header(struct session *s, struct buffer *res)
2135{
2136 struct http_txn *txn = &s->txn;
2137 struct http_msg *msg = &txn->rsp;
2138 struct hdr_ctx ctx;
2139 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002140
2141 /* no common compression algorithm was found in request header */
2142 if (s->comp_algo == NULL)
2143 goto fail;
2144
2145 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002146 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002147 goto fail;
2148
William Lallemandd3002612012-11-26 14:34:47 +01002149 /* 200 only */
2150 if (txn->status != 200)
2151 goto fail;
2152
William Lallemand82fe75c2012-10-23 10:25:10 +02002153 /* Content-Length is null */
2154 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2155 goto fail;
2156
Willy Tarreau667c2a32013-04-09 08:13:58 +02002157 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2158 if (msg->flags & HTTP_MSGF_TE_CHNK)
2159 goto fail;
2160
William Lallemand82fe75c2012-10-23 10:25:10 +02002161 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002162 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002163 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2164 goto fail;
2165
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002166 /* no compression when Cache-Control: no-transform is present in the message */
2167 ctx.idx = 0;
2168 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2169 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2170 goto fail;
2171 }
2172
William Lallemand82fe75c2012-10-23 10:25:10 +02002173 comp_type = NULL;
2174
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002175 /* we don't want to compress multipart content-types, nor content-types that are
2176 * not listed in the "compression type" directive if any. If no content-type was
2177 * found but configuration requires one, we don't compress either. Backend has
2178 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002180 ctx.idx = 0;
2181 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2182 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2183 goto fail;
2184
2185 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2186 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002187 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002188 if (ctx.vlen >= comp_type->name_len &&
2189 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002190 /* this Content-Type should be compressed */
2191 break;
2192 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002193 /* this Content-Type should not be compressed */
2194 if (comp_type == NULL)
2195 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002196 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002197 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002198 else { /* no content-type header */
2199 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2200 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002201 }
2202
William Lallemandd85f9172012-11-09 17:05:39 +01002203 /* limit compression rate */
2204 if (global.comp_rate_lim > 0)
2205 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2206 goto fail;
2207
William Lallemand072a2bf2012-11-20 17:01:01 +01002208 /* limit cpu usage */
2209 if (idle_pct < compress_min_idle)
2210 goto fail;
2211
William Lallemand4c49fae2012-11-07 15:00:23 +01002212 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002213 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002214 goto fail;
2215
William Lallemandec3e3892012-11-12 17:02:18 +01002216 s->flags |= SN_COMP_READY;
2217
William Lallemand82fe75c2012-10-23 10:25:10 +02002218 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002219 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002220 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2221 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2222
2223 /* add Transfer-Encoding header */
2224 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2225 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2226
2227 /*
2228 * Add Content-Encoding header when it's not identity encoding.
2229 * RFC 2616 : Identity encoding: This content-coding is used only in the
2230 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2231 * header.
2232 */
2233 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002234 trash.len = 18;
2235 memcpy(trash.str, "Content-Encoding: ", trash.len);
2236 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2237 trash.len += s->comp_algo->name_len;
2238 trash.str[trash.len] = '\0';
2239 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002240 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002241 return 1;
2242
2243fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002244 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002245 return 0;
2246}
2247
2248
Willy Tarreaud787e662009-07-07 10:14:51 +02002249/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2250 * processing can continue on next analysers, or zero if it either needs more
2251 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2252 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2253 * when it has nothing left to do, and may remove any analyser when it wants to
2254 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002255 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002256int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002257{
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 /*
2259 * We will parse the partial (or complete) lines.
2260 * We will check the request syntax, and also join multi-line
2261 * headers. An index of all the lines will be elaborated while
2262 * parsing.
2263 *
2264 * For the parsing, we use a 28 states FSM.
2265 *
2266 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002267 * req->buf->p = beginning of request
2268 * req->buf->p + msg->eoh = end of processed headers / start of current one
2269 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002270 * msg->eol = end of current header or line (LF or CRLF)
2271 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002272 *
2273 * At end of parsing, we may perform a capture of the error (if any), and
2274 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2275 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2276 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002278
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002280 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 struct http_txn *txn = &s->txn;
2282 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002283 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002284
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002285 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 +01002286 now_ms, __FUNCTION__,
2287 s,
2288 req,
2289 req->rex, req->wex,
2290 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002291 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002292 req->analysers);
2293
Willy Tarreau52a0c602009-08-16 22:45:38 +02002294 /* we're speaking HTTP here, so let's speak HTTP to the client */
2295 s->srv_error = http_return_srv_error;
2296
Willy Tarreau83e3af02009-12-28 17:39:57 +01002297 /* There's a protected area at the end of the buffer for rewriting
2298 * purposes. We don't want to start to parse the request if the
2299 * protected area is affected, because we may have to move processed
2300 * data later, which is much more complicated.
2301 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002302 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002303 if (txn->flags & TX_NOT_FIRST) {
2304 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002305 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002306 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002307 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002308 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002309 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002310 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002311 return 0;
2312 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002313 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2314 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2315 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002316 }
2317
Willy Tarreau065e8332010-01-08 00:30:20 +01002318 /* Note that we have the same problem with the response ; we
2319 * may want to send a redirect, error or anything which requires
2320 * some spare space. So we'll ensure that we have at least
2321 * maxrewrite bytes available in the response buffer before
2322 * processing that one. This will only affect pipelined
2323 * keep-alive requests.
2324 */
2325 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002326 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002327 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2328 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2329 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002330 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002331 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002332 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002333 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002334 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002335 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002336 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002337 return 0;
2338 }
2339 }
2340
Willy Tarreau9b28e032012-10-12 23:49:43 +02002341 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002342 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002343 }
2344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 /* 1: we might have to print this header in debug mode */
2346 if (unlikely((global.mode & MODE_DEBUG) &&
2347 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002348 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002350
Willy Tarreau9b28e032012-10-12 23:49:43 +02002351 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002352 /* this is a bit complex : in case of error on the request line,
2353 * we know that rq.l is still zero, so we display only the part
2354 * up to the end of the line (truncated by debug_hdr).
2355 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002356 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 sol += hdr_idx_first_pos(&txn->hdr_idx);
2360 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002361
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 while (cur_idx) {
2363 eol = sol + txn->hdr_idx.v[cur_idx].len;
2364 debug_hdr("clihdr", s, sol, eol);
2365 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2366 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002367 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 }
2369
Willy Tarreau58f10d72006-12-04 02:26:12 +01002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 /*
2372 * Now we quickly check if we have found a full valid request.
2373 * If not so, we check the FD and buffer states before leaving.
2374 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002375 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002376 * requests are checked first. When waiting for a second request
2377 * on a keep-alive session, if we encounter and error, close, t/o,
2378 * we note the error in the session flags but don't set any state.
2379 * Since the error will be noted there, it will not be counted by
2380 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002381 * Last, we may increase some tracked counters' http request errors on
2382 * the cases that are deliberately the client's fault. For instance,
2383 * a timeout or connection reset is not counted as an error. However
2384 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002386
Willy Tarreau655dce92009-11-08 13:10:58 +01002387 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002388 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002390 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002391 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002392 session_inc_http_req_ctr(s);
2393 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002394 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002397
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 /* 1: Since we are in header mode, if there's no space
2399 * left for headers, we won't be able to free more
2400 * later, so the session will never terminate. We
2401 * must terminate it now.
2402 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002403 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 /* FIXME: check if URI is set and return Status
2405 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002407 session_inc_http_req_ctr(s);
2408 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002409 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002410 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002411 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 goto return_bad_req;
2413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002414
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002416 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002417 if (!(s->flags & SN_ERR_MASK))
2418 s->flags |= SN_ERR_CLICL;
2419
Willy Tarreaufcffa692010-01-10 14:21:19 +01002420 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002421 goto failed_keep_alive;
2422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002424 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002425 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002426 session_inc_http_err_ctr(s);
2427 }
2428
Willy Tarreaudc979f22012-12-04 10:39:01 +01002429 txn->status = 400;
2430 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 msg->msg_state = HTTP_MSG_ERROR;
2432 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002433
Willy Tarreauda7ff642010-06-23 11:44:09 +02002434 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002435 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002436 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002437 if (s->listener->counters)
2438 s->listener->counters->failed_req++;
2439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 if (!(s->flags & SN_FINST_MASK))
2441 s->flags |= SN_FINST_R;
2442 return 0;
2443 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002446 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002447 if (!(s->flags & SN_ERR_MASK))
2448 s->flags |= SN_ERR_CLITO;
2449
Willy Tarreaufcffa692010-01-10 14:21:19 +01002450 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002451 goto failed_keep_alive;
2452
Willy Tarreau59234e92008-11-30 23:51:27 +01002453 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002454 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002455 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002456 session_inc_http_err_ctr(s);
2457 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002459 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 msg->msg_state = HTTP_MSG_ERROR;
2461 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002462
Willy Tarreauda7ff642010-06-23 11:44:09 +02002463 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002464 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002465 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002466 if (s->listener->counters)
2467 s->listener->counters->failed_req++;
2468
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 if (!(s->flags & SN_FINST_MASK))
2470 s->flags |= SN_FINST_R;
2471 return 0;
2472 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002473
Willy Tarreau59234e92008-11-30 23:51:27 +01002474 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002475 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002476 if (!(s->flags & SN_ERR_MASK))
2477 s->flags |= SN_ERR_CLICL;
2478
Willy Tarreaufcffa692010-01-10 14:21:19 +01002479 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002480 goto failed_keep_alive;
2481
Willy Tarreau4076a152009-04-02 15:18:36 +02002482 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002483 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002485 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002486 msg->msg_state = HTTP_MSG_ERROR;
2487 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002488
Willy Tarreauda7ff642010-06-23 11:44:09 +02002489 session_inc_http_err_ctr(s);
2490 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002491 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002492 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002493 if (s->listener->counters)
2494 s->listener->counters->failed_req++;
2495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 if (!(s->flags & SN_FINST_MASK))
2497 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002498 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002499 }
2500
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002501 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002502 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2503 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002504#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002505 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 +01002506 /* We need more data, we have to re-enable quick-ack in case we
2507 * previously disabled it, otherwise we might cause the client
2508 * to delay next data.
2509 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002510 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002511 }
2512#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002513
Willy Tarreaufcffa692010-01-10 14:21:19 +01002514 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2515 /* If the client starts to talk, let's fall back to
2516 * request timeout processing.
2517 */
2518 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002519 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002520 }
2521
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002523 if (!tick_isset(req->analyse_exp)) {
2524 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2525 (txn->flags & TX_WAIT_NEXT_RQ) &&
2526 tick_isset(s->be->timeout.httpka))
2527 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2528 else
2529 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2530 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002531
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 /* we're not ready yet */
2533 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534
2535 failed_keep_alive:
2536 /* Here we process low-level errors for keep-alive requests. In
2537 * short, if the request is not the first one and it experiences
2538 * a timeout, read error or shutdown, we just silently close so
2539 * that the client can try again.
2540 */
2541 txn->status = 0;
2542 msg->msg_state = HTTP_MSG_RQBEFORE;
2543 req->analysers = 0;
2544 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002545 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002546 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002547 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002548 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002550
Willy Tarreaud787e662009-07-07 10:14:51 +02002551 /* OK now we have a complete HTTP request with indexed headers. Let's
2552 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002553 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002554 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002555 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002556 * byte after the last LF. msg->sov points to the first byte of data.
2557 * msg->eol cannot be trusted because it may have been left uninitialized
2558 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002559 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002560
Willy Tarreauda7ff642010-06-23 11:44:09 +02002561 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002562 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2563
Willy Tarreaub16a5742010-01-10 14:46:16 +01002564 if (txn->flags & TX_WAIT_NEXT_RQ) {
2565 /* kill the pending keep-alive timeout */
2566 txn->flags &= ~TX_WAIT_NEXT_RQ;
2567 req->analyse_exp = TICK_ETERNITY;
2568 }
2569
2570
Willy Tarreaud787e662009-07-07 10:14:51 +02002571 /* Maybe we found in invalid header name while we were configured not
2572 * to block on that, so we have to capture it now.
2573 */
2574 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002575 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002576
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 /*
2578 * 1: identify the method
2579 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002581
2582 /* we can make use of server redirect on GET and HEAD */
2583 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2584 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /*
2587 * 2: check if the URI matches the monitor_uri.
2588 * We have to do this for every request which gets in, because
2589 * the monitor-uri is defined by the frontend.
2590 */
2591 if (unlikely((s->fe->monitor_uri_len != 0) &&
2592 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002593 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 s->fe->monitor_uri,
2595 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002598 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002602 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002605 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002606 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 ret = acl_pass(ret);
2609 if (cond->pol == ACL_COND_UNLESS)
2610 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002611
Willy Tarreau59234e92008-11-30 23:51:27 +01002612 if (ret) {
2613 /* we fail this request, let's return 503 service unavail */
2614 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002615 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002616 if (!(s->flags & SN_ERR_MASK))
2617 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002618 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002619 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002620 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002621
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 /* nothing to fail, let's reply normaly */
2623 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002624 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002625 if (!(s->flags & SN_ERR_MASK))
2626 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 goto return_prx_cond;
2628 }
2629
2630 /*
2631 * 3: Maybe we have to copy the original REQURI for the logs ?
2632 * Note: we cannot log anymore if the request has been
2633 * classified as invalid.
2634 */
2635 if (unlikely(s->logs.logwait & LW_REQ)) {
2636 /* we have a complete HTTP request that we must log */
2637 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2638 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 if (urilen >= REQURI_LEN)
2641 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002642 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002644
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002645 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 s->do_log(s);
2647 } else {
2648 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002649 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 }
Willy Tarreau06619262006-12-17 08:37:22 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002653 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002654 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002655
Willy Tarreau5b154472009-12-21 20:11:07 +01002656 /* ... and check if the request is HTTP/1.1 or above */
2657 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002658 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2659 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2660 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002661 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002662
2663 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002664 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 +01002665
Willy Tarreau88d349d2010-01-25 12:15:43 +01002666 /* if the frontend has "option http-use-proxy-header", we'll check if
2667 * we have what looks like a proxied connection instead of a connection,
2668 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2669 * Note that this is *not* RFC-compliant, however browsers and proxies
2670 * happen to do that despite being non-standard :-(
2671 * We consider that a request not beginning with either '/' or '*' is
2672 * a proxied connection, which covers both "scheme://location" and
2673 * CONNECT ip:port.
2674 */
2675 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002677 txn->flags |= TX_USE_PX_CONN;
2678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002680 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002683 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002684 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002686
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002687 /* 6: determine the transfer-length.
2688 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2689 * the presence of a message-body in a REQUEST and its transfer length
2690 * must be determined that way (in order of precedence) :
2691 * 1. The presence of a message-body in a request is signaled by the
2692 * inclusion of a Content-Length or Transfer-Encoding header field
2693 * in the request's header fields. When a request message contains
2694 * both a message-body of non-zero length and a method that does
2695 * not define any semantics for that request message-body, then an
2696 * origin server SHOULD either ignore the message-body or respond
2697 * with an appropriate error message (e.g., 413). A proxy or
2698 * gateway, when presented the same request, SHOULD either forward
2699 * the request inbound with the message- body or ignore the
2700 * message-body when determining a response.
2701 *
2702 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2703 * and the "chunked" transfer-coding (Section 6.2) is used, the
2704 * transfer-length is defined by the use of this transfer-coding.
2705 * If a Transfer-Encoding header field is present and the "chunked"
2706 * transfer-coding is not present, the transfer-length is defined
2707 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002708 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002709 * 3. If a Content-Length header field is present, its decimal value in
2710 * OCTETs represents both the entity-length and the transfer-length.
2711 * If a message is received with both a Transfer-Encoding header
2712 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002713 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002714 * 4. By the server closing the connection. (Closing the connection
2715 * cannot be used to indicate the end of a request body, since that
2716 * would leave no possibility for the server to send back a response.)
2717 *
2718 * Whenever a transfer-coding is applied to a message-body, the set of
2719 * transfer-codings MUST include "chunked", unless the message indicates
2720 * it is terminated by closing the connection. When the "chunked"
2721 * transfer-coding is used, it MUST be the last transfer-coding applied
2722 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002723 */
2724
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002726 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002727 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002728 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002730 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002731 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2732 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002733 /* bad transfer-encoding (chunked followed by something else) */
2734 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002735 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002736 break;
2737 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002738 }
2739
Willy Tarreau32b47f42009-10-18 20:55:02 +02002740 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002741 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002742 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002743 signed long long cl;
2744
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002746 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002747 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002748 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002751 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002752 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002753 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754
Willy Tarreauad14f752011-09-02 20:33:27 +02002755 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002756 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002757 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002758 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002759
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002760 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002761 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002762 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002763 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002764
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002765 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002766 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002767 }
2768
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 /* bodyless requests have a known length */
2770 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002771 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002772
Willy Tarreaud787e662009-07-07 10:14:51 +02002773 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002774 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002775 req->analyse_exp = TICK_ETERNITY;
2776 return 1;
2777
2778 return_bad_req:
2779 /* We centralize bad requests processing here */
2780 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2781 /* we detected a parsing error. We want to archive this request
2782 * in the dedicated proxy area for later troubleshooting.
2783 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002784 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002785 }
2786
2787 txn->req.msg_state = HTTP_MSG_ERROR;
2788 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002789 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002790
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002791 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002792 if (s->listener->counters)
2793 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002794
2795 return_prx_cond:
2796 if (!(s->flags & SN_ERR_MASK))
2797 s->flags |= SN_ERR_PRXCOND;
2798 if (!(s->flags & SN_FINST_MASK))
2799 s->flags |= SN_FINST_R;
2800
2801 req->analysers = 0;
2802 req->analyse_exp = TICK_ETERNITY;
2803 return 0;
2804}
2805
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002806
Willy Tarreau347a35d2013-11-22 17:51:09 +01002807/* This function prepares an applet to handle the stats. It can deal with the
2808 * "100-continue" expectation, check that admin rules are met for POST requests,
2809 * and program a response message if something was unexpected. It cannot fail
2810 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002811 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002812 * s->target which is supposed to already point to the stats applet. The caller
2813 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002814 */
2815int http_handle_stats(struct session *s, struct channel *req)
2816{
2817 struct stats_admin_rule *stats_admin_rule;
2818 struct stream_interface *si = s->rep->prod;
2819 struct http_txn *txn = &s->txn;
2820 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002821 struct uri_auth *uri_auth = s->be->uri_auth;
2822 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002823 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002825 appctx = si_appctx(si);
2826 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2827 appctx->st1 = appctx->st2 = 0;
2828 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2829 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002830
2831 uri = msg->chn->buf->p + msg->sl.rq.u;
2832 lookup = uri + uri_auth->uri_len;
2833
2834 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2835 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002836 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002837 break;
2838 }
2839 }
2840
2841 if (uri_auth->refresh) {
2842 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2843 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002844 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002845 break;
2846 }
2847 }
2848 }
2849
2850 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2851 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002852 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002853 break;
2854 }
2855 }
2856
2857 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2858 if (memcmp(h, ";st=", 4) == 0) {
2859 int i;
2860 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002861 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002862 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2863 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002865 break;
2866 }
2867 }
2868 break;
2869 }
2870 }
2871
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002872 appctx->ctx.stats.scope_str = 0;
2873 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002874 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2875 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2876 int itx = 0;
2877 const char *h2;
2878 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2879 const char *err;
2880
2881 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2882 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002884 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2885 itx++;
2886 h++;
2887 }
2888
2889 if (itx > STAT_SCOPE_TXT_MAXLEN)
2890 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002892
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002893 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002894 memcpy(scope_txt, h2, itx);
2895 scope_txt[itx] = '\0';
2896 err = invalid_char(scope_txt);
2897 if (err) {
2898 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002899 appctx->ctx.stats.scope_str = 0;
2900 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002901 }
2902 break;
2903 }
2904 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002905
2906 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002907 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002908 int ret = 1;
2909
2910 if (stats_admin_rule->cond) {
2911 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2912 ret = acl_pass(ret);
2913 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2914 ret = !ret;
2915 }
2916
2917 if (ret) {
2918 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002919 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002920 break;
2921 }
2922 }
2923
2924 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002925 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002926 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002927 if (msg->msg_state < HTTP_MSG_100_SENT) {
2928 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2929 * send an HTTP/1.1 100 Continue intermediate response.
2930 */
2931 if (msg->flags & HTTP_MSGF_VER_11) {
2932 struct hdr_ctx ctx;
2933 ctx.idx = 0;
2934 /* Expect is allowed in 1.1, look for it */
2935 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2936 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2937 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2938 }
2939 }
2940 msg->msg_state = HTTP_MSG_100_SENT;
2941 s->logs.tv_request = now; /* update the request timer to reflect full request */
2942 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002943 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002944 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002945 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002946 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2947 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002948 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002949 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002950 else {
2951 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002952 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002953 }
2954
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002955 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002956 return 1;
2957}
2958
Lukas Tribus67db8df2013-06-23 17:37:13 +02002959/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2960 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2961 */
2962static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2963{
2964#ifdef IP_TOS
2965 if (from.ss_family == AF_INET)
2966 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2967#endif
2968#ifdef IPV6_TCLASS
2969 if (from.ss_family == AF_INET6) {
2970 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2971 /* v4-mapped addresses need IP_TOS */
2972 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2973 else
2974 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2975 }
2976#endif
2977}
2978
Willy Tarreau20b0de52012-12-24 15:45:22 +01002979/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002980 * transaction <txn>. Returns the first rule that prevents further processing
2981 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2982 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2983 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002984 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002985static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002986http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002987{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002988 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002990 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002991
Willy Tarreauff011f22011-01-06 17:51:27 +01002992 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002993 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002994 continue;
2995
Willy Tarreau96257ec2012-12-27 10:46:37 +01002996 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002997 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002998 int ret;
2999
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003000 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003001 ret = acl_pass(ret);
3002
Willy Tarreauff011f22011-01-06 17:51:27 +01003003 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003004 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003005
3006 if (!ret) /* condition not matched */
3007 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003008 }
3009
Willy Tarreau20b0de52012-12-24 15:45:22 +01003010
Willy Tarreau96257ec2012-12-27 10:46:37 +01003011 switch (rule->action) {
3012 case HTTP_REQ_ACT_ALLOW:
3013 return NULL; /* "allow" rules are OK */
3014
3015 case HTTP_REQ_ACT_DENY:
3016 txn->flags |= TX_CLDENY;
3017 return rule;
3018
Willy Tarreauccbcc372012-12-27 12:37:57 +01003019 case HTTP_REQ_ACT_TARPIT:
3020 txn->flags |= TX_CLTARPIT;
3021 return rule;
3022
Willy Tarreau96257ec2012-12-27 10:46:37 +01003023 case HTTP_REQ_ACT_AUTH:
3024 return rule;
3025
Willy Tarreau81499eb2012-12-27 12:19:02 +01003026 case HTTP_REQ_ACT_REDIR:
3027 return rule;
3028
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003029 case HTTP_REQ_ACT_SET_NICE:
3030 s->task->nice = rule->arg.nice;
3031 break;
3032
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003033 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003034 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003035 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003036 break;
3037
Willy Tarreau51347ed2013-06-11 19:34:13 +02003038 case HTTP_REQ_ACT_SET_MARK:
3039#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003040 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003041 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003042#endif
3043 break;
3044
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003045 case HTTP_REQ_ACT_SET_LOGL:
3046 s->logs.level = rule->arg.loglevel;
3047 break;
3048
Willy Tarreau96257ec2012-12-27 10:46:37 +01003049 case HTTP_REQ_ACT_SET_HDR:
3050 ctx.idx = 0;
3051 /* remove all occurrences of the header */
3052 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3053 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3054 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003055 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003056 /* now fall through to header addition */
3057
3058 case HTTP_REQ_ACT_ADD_HDR:
3059 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3060 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3061 trash.len = rule->arg.hdr_add.name_len;
3062 trash.str[trash.len++] = ':';
3063 trash.str[trash.len++] = ' ';
3064 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3065 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3066 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003067 }
3068 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003069
3070 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003071 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072}
3073
Willy Tarreau71241ab2012-12-27 11:30:54 +01003074
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003075/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3076 * transaction <txn>. Returns the first rule that prevents further processing
3077 * of the response (deny, ...) or NULL if it executed all rules or stopped
3078 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3079 * rule.
3080 */
3081static struct http_res_rule *
3082http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3083{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003084 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003085 struct http_res_rule *rule;
3086 struct hdr_ctx ctx;
3087
3088 list_for_each_entry(rule, rules, list) {
3089 if (rule->action >= HTTP_RES_ACT_MAX)
3090 continue;
3091
3092 /* check optional condition */
3093 if (rule->cond) {
3094 int ret;
3095
3096 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3097 ret = acl_pass(ret);
3098
3099 if (rule->cond->pol == ACL_COND_UNLESS)
3100 ret = !ret;
3101
3102 if (!ret) /* condition not matched */
3103 continue;
3104 }
3105
3106
3107 switch (rule->action) {
3108 case HTTP_RES_ACT_ALLOW:
3109 return NULL; /* "allow" rules are OK */
3110
3111 case HTTP_RES_ACT_DENY:
3112 txn->flags |= TX_SVDENY;
3113 return rule;
3114
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003115 case HTTP_RES_ACT_SET_NICE:
3116 s->task->nice = rule->arg.nice;
3117 break;
3118
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003119 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003120 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003121 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003122 break;
3123
Willy Tarreau51347ed2013-06-11 19:34:13 +02003124 case HTTP_RES_ACT_SET_MARK:
3125#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003126 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003127 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003128#endif
3129 break;
3130
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003131 case HTTP_RES_ACT_SET_LOGL:
3132 s->logs.level = rule->arg.loglevel;
3133 break;
3134
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003135 case HTTP_RES_ACT_SET_HDR:
3136 ctx.idx = 0;
3137 /* remove all occurrences of the header */
3138 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3139 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3140 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3141 }
3142 /* now fall through to header addition */
3143
3144 case HTTP_RES_ACT_ADD_HDR:
3145 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3146 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3147 trash.len = rule->arg.hdr_add.name_len;
3148 trash.str[trash.len++] = ':';
3149 trash.str[trash.len++] = ' ';
3150 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3151 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3152 break;
3153 }
3154 }
3155
3156 /* we reached the end of the rules, nothing to report */
3157 return NULL;
3158}
3159
3160
Willy Tarreau71241ab2012-12-27 11:30:54 +01003161/* Perform an HTTP redirect based on the information in <rule>. The function
3162 * returns non-zero on success, or zero in case of a, irrecoverable error such
3163 * as too large a request to build a valid response.
3164 */
3165static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3166{
3167 struct http_msg *msg = &txn->req;
3168 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003169 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003170
3171 /* build redirect message */
3172 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003173 case 308:
3174 msg_fmt = HTTP_308;
3175 break;
3176 case 307:
3177 msg_fmt = HTTP_307;
3178 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179 case 303:
3180 msg_fmt = HTTP_303;
3181 break;
3182 case 301:
3183 msg_fmt = HTTP_301;
3184 break;
3185 case 302:
3186 default:
3187 msg_fmt = HTTP_302;
3188 break;
3189 }
3190
3191 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3192 return 0;
3193
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003194 location = trash.str + trash.len;
3195
Willy Tarreau71241ab2012-12-27 11:30:54 +01003196 switch(rule->type) {
3197 case REDIRECT_TYPE_SCHEME: {
3198 const char *path;
3199 const char *host;
3200 struct hdr_ctx ctx;
3201 int pathlen;
3202 int hostlen;
3203
3204 host = "";
3205 hostlen = 0;
3206 ctx.idx = 0;
3207 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3208 host = ctx.line + ctx.val;
3209 hostlen = ctx.vlen;
3210 }
3211
3212 path = http_get_path(txn);
3213 /* build message using path */
3214 if (path) {
3215 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3216 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3217 int qs = 0;
3218 while (qs < pathlen) {
3219 if (path[qs] == '?') {
3220 pathlen = qs;
3221 break;
3222 }
3223 qs++;
3224 }
3225 }
3226 } else {
3227 path = "/";
3228 pathlen = 1;
3229 }
3230
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003231 if (rule->rdr_str) { /* this is an old "redirect" rule */
3232 /* check if we can add scheme + "://" + host + path */
3233 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3234 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003236 /* add scheme */
3237 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3238 trash.len += rule->rdr_len;
3239 }
3240 else {
3241 /* add scheme with executing log format */
3242 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003243
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003244 /* check if we can add scheme + "://" + host + path */
3245 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3246 return 0;
3247 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003248 /* add "://" */
3249 memcpy(trash.str + trash.len, "://", 3);
3250 trash.len += 3;
3251
3252 /* add host */
3253 memcpy(trash.str + trash.len, host, hostlen);
3254 trash.len += hostlen;
3255
3256 /* add path */
3257 memcpy(trash.str + trash.len, path, pathlen);
3258 trash.len += pathlen;
3259
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003260 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003261 if (trash.len && trash.str[trash.len - 1] != '/' &&
3262 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3263 if (trash.len > trash.size - 5)
3264 return 0;
3265 trash.str[trash.len] = '/';
3266 trash.len++;
3267 }
3268
3269 break;
3270 }
3271 case REDIRECT_TYPE_PREFIX: {
3272 const char *path;
3273 int pathlen;
3274
3275 path = http_get_path(txn);
3276 /* build message using path */
3277 if (path) {
3278 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3279 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3280 int qs = 0;
3281 while (qs < pathlen) {
3282 if (path[qs] == '?') {
3283 pathlen = qs;
3284 break;
3285 }
3286 qs++;
3287 }
3288 }
3289 } else {
3290 path = "/";
3291 pathlen = 1;
3292 }
3293
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003294 if (rule->rdr_str) { /* this is an old "redirect" rule */
3295 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3296 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003297
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003298 /* add prefix. Note that if prefix == "/", we don't want to
3299 * add anything, otherwise it makes it hard for the user to
3300 * configure a self-redirection.
3301 */
3302 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3303 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3304 trash.len += rule->rdr_len;
3305 }
3306 }
3307 else {
3308 /* add prefix with executing log format */
3309 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3310
3311 /* Check length */
3312 if (trash.len + pathlen > trash.size - 4)
3313 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003314 }
3315
3316 /* add path */
3317 memcpy(trash.str + trash.len, path, pathlen);
3318 trash.len += pathlen;
3319
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003320 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003321 if (trash.len && trash.str[trash.len - 1] != '/' &&
3322 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3323 if (trash.len > trash.size - 5)
3324 return 0;
3325 trash.str[trash.len] = '/';
3326 trash.len++;
3327 }
3328
3329 break;
3330 }
3331 case REDIRECT_TYPE_LOCATION:
3332 default:
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 > trash.size - 4)
3335 return 0;
3336
3337 /* add location */
3338 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3339 trash.len += rule->rdr_len;
3340 }
3341 else {
3342 /* add location with executing log format */
3343 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003345 /* Check left length */
3346 if (trash.len > trash.size - 4)
3347 return 0;
3348 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003349 break;
3350 }
3351
3352 if (rule->cookie_len) {
3353 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3354 trash.len += 14;
3355 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3356 trash.len += rule->cookie_len;
3357 memcpy(trash.str + trash.len, "\r\n", 2);
3358 trash.len += 2;
3359 }
3360
3361 /* add end of headers and the keep-alive/close status.
3362 * We may choose to set keep-alive if the Location begins
3363 * with a slash, because the client will come back to the
3364 * same server.
3365 */
3366 txn->status = rule->code;
3367 /* let's log the request time */
3368 s->logs.tv_request = now;
3369
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003370 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003371 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3372 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3373 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3374 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3375 /* keep-alive possible */
3376 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3377 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3378 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3379 trash.len += 30;
3380 } else {
3381 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3382 trash.len += 24;
3383 }
3384 }
3385 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3386 trash.len += 4;
3387 bo_inject(txn->rsp.chn, trash.str, trash.len);
3388 /* "eat" the request */
3389 bi_fast_delete(txn->req.chn->buf, msg->sov);
3390 msg->sov = 0;
3391 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3392 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3393 txn->req.msg_state = HTTP_MSG_CLOSED;
3394 txn->rsp.msg_state = HTTP_MSG_DONE;
3395 } else {
3396 /* keep-alive not possible */
3397 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3398 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3399 trash.len += 29;
3400 } else {
3401 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3402 trash.len += 23;
3403 }
3404 stream_int_retnclose(txn->req.chn->prod, &trash);
3405 txn->req.chn->analysers = 0;
3406 }
3407
3408 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003409 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003410 if (!(s->flags & SN_FINST_MASK))
3411 s->flags |= SN_FINST_R;
3412
3413 return 1;
3414}
3415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003416/* This stream analyser runs all HTTP request processing which is common to
3417 * frontends and backends, which means blocking ACLs, filters, connection-close,
3418 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003419 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003420 * either needs more data or wants to immediately abort the request (eg: deny,
3421 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003422 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003423int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003424{
Willy Tarreaud787e662009-07-07 10:14:51 +02003425 struct http_txn *txn = &s->txn;
3426 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003427 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003428 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003429 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003430 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003431
Willy Tarreau655dce92009-11-08 13:10:58 +01003432 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003433 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003434 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003435 return 0;
3436 }
3437
Willy Tarreau3a816292009-07-07 10:55:49 +02003438 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003439 req->analyse_exp = TICK_ETERNITY;
3440
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003441 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 +02003442 now_ms, __FUNCTION__,
3443 s,
3444 req,
3445 req->rex, req->wex,
3446 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003447 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003448 req->analysers);
3449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 /* first check whether we have some ACLs set to block this request */
3451 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003452 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003453
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003454 ret = acl_pass(ret);
3455 if (cond->pol == ACL_COND_UNLESS)
3456 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003457
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 if (ret) {
3459 txn->status = 403;
3460 /* let's log the request time */
3461 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003462 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003463 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003465 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003466 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003467
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003468 /* just in case we have some per-backend tracking */
3469 session_inc_be_http_req_ctr(s);
3470
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003471 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003472 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003473
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003474 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003475 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003476 if (stats_check_uri(s->rep->prod, txn, px)) {
3477 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003478 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3479 txn->status = 500;
3480 s->logs.tv_request = now;
3481 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003482
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003483 if (!(s->flags & SN_ERR_MASK))
3484 s->flags |= SN_ERR_RESOURCE;
3485 goto return_prx_cond;
3486 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003487 /* parse the whole stats request and extract the relevant information */
3488 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003489 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 +01003490 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003491 }
3492
Willy Tarreau3b44e722013-11-16 10:28:23 +01003493 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3494 * blocked by an http-request rule.
3495 */
3496 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003497 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003499 }
Willy Tarreau06619262006-12-17 08:37:22 +01003500
Willy Tarreau3b44e722013-11-16 10:28:23 +01003501 /* return a 403 if either rule has blocked */
3502 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003503 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003504 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003506 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003507 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003508 s->fe->fe_counters.denied_req++;
3509 if (s->fe != s->be)
3510 s->be->be_counters.denied_req++;
3511 if (s->listener->counters)
3512 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003513 goto return_prx_cond;
3514 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003515
3516 /* When a connection is tarpitted, we use the tarpit timeout,
3517 * which may be the same as the connect timeout if unspecified.
3518 * If unset, then set it to zero because we really want it to
3519 * eventually expire. We build the tarpit as an analyser.
3520 */
3521 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003522 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003523 /* wipe the request out so that we can drop the connection early
3524 * if the client closes first.
3525 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003526 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003527 req->analysers = 0; /* remove switching rules etc... */
3528 req->analysers |= AN_REQ_HTTP_TARPIT;
3529 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3530 if (!req->analyse_exp)
3531 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003532 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003533 s->fe->fe_counters.denied_req++;
3534 if (s->fe != s->be)
3535 s->be->be_counters.denied_req++;
3536 if (s->listener->counters)
3537 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003538 return 1;
3539 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003540 }
Willy Tarreau06619262006-12-17 08:37:22 +01003541
Willy Tarreau5b154472009-12-21 20:11:07 +01003542 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3543 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003544 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3545 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003546 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3547 * avoid to redo the same work if FE and BE have the same settings (common).
3548 * The method consists in checking if options changed between the two calls
3549 * (implying that either one is non-null, or one of them is non-null and we
3550 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003551 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003552
Willy Tarreaudc008c52010-02-01 16:20:08 +01003553 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3554 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3555 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3556 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01003557 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003558
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003559 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3560 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003561 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003562 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3563 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003564 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003565 tmp = TX_CON_WANT_CLO;
3566
Willy Tarreau5b154472009-12-21 20:11:07 +01003567 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3568 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003569
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3571 /* parse the Connection header and possibly clean it */
3572 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003573 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003574 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3575 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003576 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003577 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003578 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003579 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003580 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003581
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 /* check if client or config asks for explicit close in KAL/SCL */
3583 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3584 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3585 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003586 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003587 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003588 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003589 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003590 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3591 }
Willy Tarreau78599912009-10-17 20:12:21 +02003592
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003593 /* we can be blocked here because the request needs to be authenticated,
3594 * either to pass or to access stats.
3595 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003596 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003597 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003598
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003599 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003600 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003601
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003602 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003603 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003604 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003605 /* on 401 we still count one error, because normal browsing
3606 * won't significantly increase the counter but brute force
3607 * attempts will.
3608 */
3609 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003610 goto return_prx_cond;
3611 }
3612
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003613 /* add request headers from the rule sets in the same order */
3614 list_for_each_entry(wl, &px->req_add, list) {
3615 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003616 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 ret = acl_pass(ret);
3618 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3619 ret = !ret;
3620 if (!ret)
3621 continue;
3622 }
3623
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003624 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003625 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003626 }
3627
3628 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3629 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3630 goto return_bad_req;
3631 req->analyse_exp = TICK_ETERNITY;
3632 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003633 }
3634
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003635 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003636 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003637 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3638 s->fe->fe_counters.intercepted_req++;
3639
3640 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3641 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3642 if (!(s->flags & SN_FINST_MASK))
3643 s->flags |= SN_FINST_R;
3644
3645 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003646 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003647 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003648 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003649
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003650 /* check whether we have some ACLs set to redirect this request */
3651 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003652 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003653 int ret;
3654
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003655 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003656 ret = acl_pass(ret);
3657 if (rule->cond->pol == ACL_COND_UNLESS)
3658 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003659 if (!ret)
3660 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003661 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003662 if (!http_apply_redirect_rule(rule, s, txn))
3663 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003664
Willy Tarreau71241ab2012-12-27 11:30:54 +01003665 req->analyse_exp = TICK_ETERNITY;
3666 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003668
Willy Tarreau2be39392010-01-03 17:24:51 +01003669 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3670 * If this happens, then the data will not come immediately, so we must
3671 * send all what we have without waiting. Note that due to the small gain
3672 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003673 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003674 * itself once used.
3675 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003676 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003677
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003678 /* that's OK for us now, let's move on to next analysers */
3679 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003680
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003681 return_bad_req:
3682 /* We centralize bad requests processing here */
3683 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3684 /* we detected a parsing error. We want to archive this request
3685 * in the dedicated proxy area for later troubleshooting.
3686 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003687 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003688 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003689
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003690 txn->req.msg_state = HTTP_MSG_ERROR;
3691 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003692 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003693
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003694 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003695 if (s->listener->counters)
3696 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003697
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003698 return_prx_cond:
3699 if (!(s->flags & SN_ERR_MASK))
3700 s->flags |= SN_ERR_PRXCOND;
3701 if (!(s->flags & SN_FINST_MASK))
3702 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003703
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003704 req->analysers = 0;
3705 req->analyse_exp = TICK_ETERNITY;
3706 return 0;
3707}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003708
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003709/* This function performs all the processing enabled for the current request.
3710 * It returns 1 if the processing can continue on next analysers, or zero if it
3711 * needs more data, encounters an error, or wants to immediately abort the
3712 * request. It relies on buffers flags, and updates s->req->analysers.
3713 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003714int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003715{
3716 struct http_txn *txn = &s->txn;
3717 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003718 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003719
Willy Tarreau655dce92009-11-08 13:10:58 +01003720 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003721 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003722 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003723 return 0;
3724 }
3725
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003726 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 +02003727 now_ms, __FUNCTION__,
3728 s,
3729 req,
3730 req->rex, req->wex,
3731 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003732 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003733 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003734
William Lallemand82fe75c2012-10-23 10:25:10 +02003735 if (s->fe->comp || s->be->comp)
3736 select_compression_request_header(s, req->buf);
3737
Willy Tarreau59234e92008-11-30 23:51:27 +01003738 /*
3739 * Right now, we know that we have processed the entire headers
3740 * and that unwanted requests have been filtered out. We can do
3741 * whatever we want with the remaining request. Also, now we
3742 * may have separate values for ->fe, ->be.
3743 */
Willy Tarreau06619262006-12-17 08:37:22 +01003744
Willy Tarreau59234e92008-11-30 23:51:27 +01003745 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003746 * If HTTP PROXY is set we simply get remote server address parsing
3747 * incoming request. Note that this requires that a connection is
3748 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003749 */
3750 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003751 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003752 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003753
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003754 /* Note that for now we don't reuse existing proxy connections */
3755 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003756 txn->req.msg_state = HTTP_MSG_ERROR;
3757 txn->status = 500;
3758 req->analysers = 0;
3759 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3760
3761 if (!(s->flags & SN_ERR_MASK))
3762 s->flags |= SN_ERR_RESOURCE;
3763 if (!(s->flags & SN_FINST_MASK))
3764 s->flags |= SN_FINST_R;
3765
3766 return 0;
3767 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003768
3769 path = http_get_path(txn);
3770 url2sa(req->buf->p + msg->sl.rq.u,
3771 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3772 &conn->addr.to);
3773 /* if the path was found, we have to remove everything between
3774 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3775 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3776 * u_l characters by a single "/".
3777 */
3778 if (path) {
3779 char *cur_ptr = req->buf->p;
3780 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3781 int delta;
3782
3783 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3784 http_msg_move_end(&txn->req, delta);
3785 cur_end += delta;
3786 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3787 goto return_bad_req;
3788 }
3789 else {
3790 char *cur_ptr = req->buf->p;
3791 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3792 int delta;
3793
3794 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3795 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3796 http_msg_move_end(&txn->req, delta);
3797 cur_end += delta;
3798 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3799 goto return_bad_req;
3800 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003802
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003804 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 * Note that doing so might move headers in the request, but
3806 * the fields will stay coherent and the URI will not move.
3807 * This should only be performed in the backend.
3808 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003809 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003810 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3811 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003812
Willy Tarreau59234e92008-11-30 23:51:27 +01003813 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003814 * 8: the appsession cookie was looked up very early in 1.2,
3815 * so let's do the same now.
3816 */
3817
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003818 /* It needs to look into the URI unless persistence must be ignored */
3819 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003820 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 +01003821 }
3822
William Lallemanda73203e2012-03-12 12:48:57 +01003823 /* add unique-id if "header-unique-id" is specified */
3824
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003825 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3826 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3827 goto return_bad_req;
3828 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003829 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003830 }
William Lallemanda73203e2012-03-12 12:48:57 +01003831
3832 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003833 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3834 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003835 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003836 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003837 goto return_bad_req;
3838 }
3839
Cyril Bontéb21570a2009-11-29 20:04:48 +01003840 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003841 * 9: add X-Forwarded-For if either the frontend or the backend
3842 * asks for it.
3843 */
3844 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003845 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003846 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003847 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3848 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003849 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003850 /* The header is set to be added only if none is present
3851 * and we found it, so don't do anything.
3852 */
3853 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003854 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 /* Add an X-Forwarded-For header unless the source IP is
3856 * in the 'except' network range.
3857 */
3858 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003859 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003860 != s->fe->except_net.s_addr) &&
3861 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003862 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003863 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003864 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003865 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003866 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003867
3868 /* Note: we rely on the backend to get the header name to be used for
3869 * x-forwarded-for, because the header is really meant for the backends.
3870 * However, if the backend did not specify any option, we have to rely
3871 * on the frontend's header name.
3872 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003873 if (s->be->fwdfor_hdr_len) {
3874 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003875 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003876 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003877 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003878 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003879 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003880 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003881
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003882 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003883 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003884 }
3885 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003886 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003887 /* FIXME: for the sake of completeness, we should also support
3888 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003889 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003890 int len;
3891 char pn[INET6_ADDRSTRLEN];
3892 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003893 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003894 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003895
Willy Tarreau59234e92008-11-30 23:51:27 +01003896 /* Note: we rely on the backend to get the header name to be used for
3897 * x-forwarded-for, because the header is really meant for the backends.
3898 * However, if the backend did not specify any option, we have to rely
3899 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003900 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003901 if (s->be->fwdfor_hdr_len) {
3902 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003903 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003904 } else {
3905 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003906 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003907 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003908 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003909
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003910 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003911 goto return_bad_req;
3912 }
3913 }
3914
3915 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003916 * 10: add X-Original-To if either the frontend or the backend
3917 * asks for it.
3918 */
3919 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3920
3921 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003922 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003923 /* Add an X-Original-To header unless the destination IP is
3924 * in the 'except' network range.
3925 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003926 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003927
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003928 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003929 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003930 (((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 +02003931 != s->fe->except_to.s_addr) &&
3932 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003933 (((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 +02003934 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003935 int len;
3936 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003937 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003938
3939 /* Note: we rely on the backend to get the header name to be used for
3940 * x-original-to, because the header is really meant for the backends.
3941 * However, if the backend did not specify any option, we have to rely
3942 * on the frontend's header name.
3943 */
3944 if (s->be->orgto_hdr_len) {
3945 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003947 } else {
3948 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003949 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003950 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003951 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003952
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003954 goto return_bad_req;
3955 }
3956 }
3957 }
3958
Willy Tarreau50fc7772012-11-11 22:19:57 +01003959 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3960 * If an "Upgrade" token is found, the header is left untouched in order not to have
3961 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3962 * "Upgrade" is present in the Connection header.
3963 */
3964 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3965 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3966 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003967 unsigned int want_flags = 0;
3968
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003969 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003970 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3971 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3972 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003973 want_flags |= TX_CON_CLO_SET;
3974 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003975 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3976 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3977 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003978 want_flags |= TX_CON_KAL_SET;
3979 }
3980
3981 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003982 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003983 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003984
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003985
Willy Tarreau522d6c02009-12-06 18:49:18 +01003986 /* If we have no server assigned yet and we're balancing on url_param
3987 * with a POST request, we may be interested in checking the body for
3988 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 */
3990 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3991 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003992 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003993 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003994 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003995 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003997
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003998 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003999 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004000#ifdef TCP_QUICKACK
4001 /* We expect some data from the client. Unless we know for sure
4002 * we already have a full request, we have to re-enable quick-ack
4003 * in case we previously disabled it, otherwise we might cause
4004 * the client to delay further data.
4005 */
4006 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004007 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004008 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004009 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004010 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004011#endif
4012 }
Willy Tarreau03945942009-12-22 16:50:27 +01004013
Willy Tarreau59234e92008-11-30 23:51:27 +01004014 /*************************************************************
4015 * OK, that's finished for the headers. We have done what we *
4016 * could. Let's switch to the DATA state. *
4017 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004018 req->analyse_exp = TICK_ETERNITY;
4019 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004020
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004021 /* if the server closes the connection, we want to immediately react
4022 * and close the socket to save packets and syscalls.
4023 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004024 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4025 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004026
Willy Tarreau59234e92008-11-30 23:51:27 +01004027 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004028 /* OK let's go on with the BODY now */
4029 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004030
Willy Tarreau59234e92008-11-30 23:51:27 +01004031 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004032 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004033 /* we detected a parsing error. We want to archive this request
4034 * in the dedicated proxy area for later troubleshooting.
4035 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004036 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004037 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004038
Willy Tarreau59234e92008-11-30 23:51:27 +01004039 txn->req.msg_state = HTTP_MSG_ERROR;
4040 txn->status = 400;
4041 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004042 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004043
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004044 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004045 if (s->listener->counters)
4046 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004047
Willy Tarreau59234e92008-11-30 23:51:27 +01004048 if (!(s->flags & SN_ERR_MASK))
4049 s->flags |= SN_ERR_PRXCOND;
4050 if (!(s->flags & SN_FINST_MASK))
4051 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004052 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004053}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004054
Willy Tarreau60b85b02008-11-30 23:28:40 +01004055/* This function is an analyser which processes the HTTP tarpit. It always
4056 * returns zero, at the beginning because it prevents any other processing
4057 * from occurring, and at the end because it terminates the request.
4058 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004059int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004060{
4061 struct http_txn *txn = &s->txn;
4062
4063 /* This connection is being tarpitted. The CLIENT side has
4064 * already set the connect expiration date to the right
4065 * timeout. We just have to check that the client is still
4066 * there and that the timeout has not expired.
4067 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004068 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004069 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004070 !tick_is_expired(req->analyse_exp, now_ms))
4071 return 0;
4072
4073 /* We will set the queue timer to the time spent, just for
4074 * logging purposes. We fake a 500 server error, so that the
4075 * attacker will not suspect his connection has been tarpitted.
4076 * It will not cause trouble to the logs because we can exclude
4077 * the tarpitted connections by filtering on the 'PT' status flags.
4078 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004079 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4080
4081 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004082 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004083 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004084
4085 req->analysers = 0;
4086 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004087
Willy Tarreau60b85b02008-11-30 23:28:40 +01004088 if (!(s->flags & SN_ERR_MASK))
4089 s->flags |= SN_ERR_PRXCOND;
4090 if (!(s->flags & SN_FINST_MASK))
4091 s->flags |= SN_FINST_T;
4092 return 0;
4093}
4094
Willy Tarreaud34af782008-11-30 23:36:37 +01004095/* This function is an analyser which processes the HTTP request body. It looks
4096 * for parameters to be used for the load balancing algorithm (url_param). It
4097 * must only be called after the standard HTTP request processing has occurred,
4098 * because it expects the request to be parsed. It returns zero if it needs to
4099 * read more data, or 1 once it has completed its analysis.
4100 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004101int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004102{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004104 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004105 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004106
4107 /* We have to parse the HTTP request body to find any required data.
4108 * "balance url_param check_post" should have been the only way to get
4109 * into this. We were brought here after HTTP header analysis, so all
4110 * related structures are ready.
4111 */
4112
Willy Tarreau522d6c02009-12-06 18:49:18 +01004113 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4114 goto missing_data;
4115
4116 if (msg->msg_state < HTTP_MSG_100_SENT) {
4117 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4118 * send an HTTP/1.1 100 Continue intermediate response.
4119 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004120 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004121 struct hdr_ctx ctx;
4122 ctx.idx = 0;
4123 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004124 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004125 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004126 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127 }
4128 }
4129 msg->msg_state = HTTP_MSG_100_SENT;
4130 }
4131
4132 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004133 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004134 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004135 * is still null. We must save the body in msg->next because it
4136 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004137 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004138 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004139
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004140 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4142 else
4143 msg->msg_state = HTTP_MSG_DATA;
4144 }
4145
4146 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004147 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004148 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004149 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004150 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004151 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004152
Willy Tarreau115acb92009-12-26 13:56:06 +01004153 if (!ret)
4154 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004155 else if (ret < 0) {
4156 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004157 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004158 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004159 }
4160
Willy Tarreaud98cf932009-12-27 22:54:55 +01004161 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004162 * We have the first data byte is in msg->sov. We're waiting for at
4163 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004164 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004165
Willy Tarreau124d9912011-03-01 20:30:48 +01004166 if (msg->body_len < limit)
4167 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004168
Willy Tarreau9b28e032012-10-12 23:49:43 +02004169 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004170 goto http_end;
4171
4172 missing_data:
4173 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004174 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004175 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004176 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004177 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004178
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004179 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004181 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004182
4183 if (!(s->flags & SN_ERR_MASK))
4184 s->flags |= SN_ERR_CLITO;
4185 if (!(s->flags & SN_FINST_MASK))
4186 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004187 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004188 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004189
4190 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004191 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004192 /* Not enough data. We'll re-use the http-request
4193 * timeout here. Ideally, we should set the timeout
4194 * relative to the accept() date. We just set the
4195 * request timeout once at the beginning of the
4196 * request.
4197 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004198 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004199 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004200 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004201 return 0;
4202 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004203
4204 http_end:
4205 /* The situation will not evolve, so let's give up on the analysis. */
4206 s->logs.tv_request = now; /* update the request timer to reflect full request */
4207 req->analysers &= ~an_bit;
4208 req->analyse_exp = TICK_ETERNITY;
4209 return 1;
4210
4211 return_bad_req: /* let's centralize all bad requests */
4212 txn->req.msg_state = HTTP_MSG_ERROR;
4213 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004214 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004215
Willy Tarreau79ebac62010-06-07 13:47:49 +02004216 if (!(s->flags & SN_ERR_MASK))
4217 s->flags |= SN_ERR_PRXCOND;
4218 if (!(s->flags & SN_FINST_MASK))
4219 s->flags |= SN_FINST_R;
4220
Willy Tarreau522d6c02009-12-06 18:49:18 +01004221 return_err_msg:
4222 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004223 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004224 if (s->listener->counters)
4225 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004226 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004227}
4228
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004229/* send a server's name with an outgoing request over an established connection.
4230 * Note: this function is designed to be called once the request has been scheduled
4231 * for being forwarded. This is the reason why it rewinds the buffer before
4232 * proceeding.
4233 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004234int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004235
4236 struct hdr_ctx ctx;
4237
Mark Lamourinec2247f02012-01-04 13:02:01 -05004238 char *hdr_name = be->server_id_hdr_name;
4239 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004240 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004241 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004242 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004243
William Lallemandd9e90662012-01-30 17:27:17 +01004244 ctx.idx = 0;
4245
Willy Tarreau9b28e032012-10-12 23:49:43 +02004246 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004247 if (old_o) {
4248 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004249 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004250 }
4251
Willy Tarreau9b28e032012-10-12 23:49:43 +02004252 old_i = chn->buf->i;
4253 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 -05004254 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004255 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004256 }
4257
4258 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004259 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004260 memcpy(hdr_val, hdr_name, hdr_name_len);
4261 hdr_val += hdr_name_len;
4262 *hdr_val++ = ':';
4263 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004264 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4265 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004266
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004267 if (old_o) {
4268 /* If this was a forwarded request, we must readjust the amount of
4269 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004270 * variations. Note that if the request was already scheduled for
4271 * forwarding, it had its req->sol pointing to the body, which
4272 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004273 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004274 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004275 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004276 }
4277
Mark Lamourinec2247f02012-01-04 13:02:01 -05004278 return 0;
4279}
4280
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281/* Terminate current transaction and prepare a new one. This is very tricky
4282 * right now but it works.
4283 */
4284void http_end_txn_clean_session(struct session *s)
4285{
Willy Tarreau068621e2013-12-23 15:11:25 +01004286 int prev_status = s->txn.status;
4287
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 /* FIXME: We need a more portable way of releasing a backend's and a
4289 * server's connections. We need a safer way to reinitialize buffer
4290 * flags. We also need a more accurate method for computing per-request
4291 * data.
4292 */
4293 http_silent_debug(__LINE__, s);
4294
Willy Tarreau4213a112013-12-15 10:25:42 +01004295 /* unless we're doing keep-alive, we want to quickly close the connection
4296 * to the server.
4297 */
4298 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4299 !si_conn_ready(s->req->cons)) {
4300 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4301 si_shutr(s->req->cons);
4302 si_shutw(s->req->cons);
4303 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304
4305 http_silent_debug(__LINE__, s);
4306
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004307 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004309 if (unlikely(s->srv_conn))
4310 sess_change_server(s, NULL);
4311 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004312
4313 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4314 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004315 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316
4317 if (s->txn.status) {
4318 int n;
4319
4320 n = s->txn.status / 100;
4321 if (n < 1 || n > 5)
4322 n = 0;
4323
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004324 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004325 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004326 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004327 s->fe->fe_counters.p.http.comp_rsp++;
4328 }
Willy Tarreau24657792010-02-26 10:30:28 +01004329 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004330 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004331 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004332 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004333 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004334 s->be->be_counters.p.http.comp_rsp++;
4335 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 }
4337
4338 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004339 s->logs.bytes_in -= s->req->buf->i;
4340 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341
4342 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004343 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 !(s->flags & SN_MONITOR) &&
4345 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4346 s->do_log(s);
4347 }
4348
4349 s->logs.accept_date = date; /* user-visible date for logging */
4350 s->logs.tv_accept = now; /* corrected date for internal use */
4351 tv_zero(&s->logs.tv_request);
4352 s->logs.t_queue = -1;
4353 s->logs.t_connect = -1;
4354 s->logs.t_data = -1;
4355 s->logs.t_close = 0;
4356 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4357 s->logs.srv_queue_size = 0; /* we will get this number soon */
4358
Willy Tarreau9b28e032012-10-12 23:49:43 +02004359 s->logs.bytes_in = s->req->total = s->req->buf->i;
4360 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361
4362 if (s->pend_pos)
4363 pendconn_free(s->pend_pos);
4364
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004365 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 if (s->flags & SN_CURR_SESS) {
4367 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004368 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004369 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004370 if (may_dequeue_tasks(objt_server(s->target), s->be))
4371 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372 }
4373
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004374 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375
Willy Tarreau4213a112013-12-15 10:25:42 +01004376 /* only release our endpoint if we don't intend to reuse the
4377 * connection.
4378 */
4379 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4380 !si_conn_ready(s->req->cons)) {
4381 si_release_endpoint(s->req->cons);
4382 }
4383
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004386 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004387 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004388 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004389 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4390 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 +02004391 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 +01004392 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004393
4394 if (s->flags & SN_COMP_READY)
4395 s->comp_algo->end(&s->comp_ctx);
4396 s->comp_algo = NULL;
4397 s->flags &= ~SN_COMP_READY;
4398
Willy Tarreau610ecce2010-01-04 21:15:02 +01004399 s->txn.meth = 0;
4400 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004401 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004402
4403 if (prev_status == 401 || prev_status == 407) {
4404 /* In HTTP keep-alive mode, if we receive a 401, we still have
4405 * a chance of being able to send the visitor again to the same
4406 * server over the same connection. This is required by some
4407 * broken protocols such as NTLM, and anyway whenever there is
4408 * an opportunity for sending the challenge to the proper place,
4409 * it's better to do it (at least it helps with debugging).
4410 */
4411 s->txn.flags |= TX_PREFER_LAST;
4412 }
4413
Willy Tarreauee55dc02010-06-01 10:56:34 +02004414 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 s->req->cons->flags |= SI_FL_INDEP_STR;
4416
Willy Tarreau96e31212011-05-30 18:10:30 +02004417 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004418 s->req->flags |= CF_NEVER_WAIT;
4419 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004420 }
4421
Willy Tarreau610ecce2010-01-04 21:15:02 +01004422 /* if the request buffer is not empty, it means we're
4423 * about to process another request, so send pending
4424 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004425 * Just don't do this if the buffer is close to be full,
4426 * because the request will wait for it to flush a little
4427 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004429 if (s->req->buf->i) {
4430 if (s->rep->buf->o &&
4431 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4432 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004433 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004434 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004435
4436 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004437 channel_auto_read(s->req);
4438 channel_auto_close(s->req);
4439 channel_auto_read(s->rep);
4440 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441
Willy Tarreau27375622013-12-17 00:00:28 +01004442 /* we're in keep-alive with an idle connection, monitor it */
4443 si_idle_conn(s->req->cons);
4444
Willy Tarreau342b11c2010-11-24 16:22:09 +01004445 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 s->rep->analysers = 0;
4447
4448 http_silent_debug(__LINE__, s);
4449}
4450
4451
4452/* This function updates the request state machine according to the response
4453 * state machine and buffer flags. It returns 1 if it changes anything (flag
4454 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4455 * it is only used to find when a request/response couple is complete. Both
4456 * this function and its equivalent should loop until both return zero. It
4457 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4458 */
4459int http_sync_req_state(struct session *s)
4460{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004463 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 unsigned int old_state = txn->req.msg_state;
4465
4466 http_silent_debug(__LINE__, s);
4467 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4468 return 0;
4469
4470 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004471 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004472 * We can shut the read side unless we want to abort_on_close,
4473 * or we have a POST request. The issue with POST requests is
4474 * that some browsers still send a CRLF after the request, and
4475 * this CRLF must be read so that it does not remain in the kernel
4476 * buffers, otherwise a close could cause an RST on some systems
4477 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004478 * Note that if we're using keep-alive on the client side, we'd
4479 * rather poll now and keep the polling enabled for the whole
4480 * session's life than enabling/disabling it between each
4481 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004482 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004483 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4484 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4485 !(s->be->options & PR_O_ABRT_CLOSE) &&
4486 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004487 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488
Willy Tarreau40f151a2012-12-20 12:10:09 +01004489 /* if the server closes the connection, we want to immediately react
4490 * and close the socket to save packets and syscalls.
4491 */
4492 chn->cons->flags |= SI_FL_NOHALF;
4493
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4495 goto wait_other_side;
4496
4497 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4498 /* The server has not finished to respond, so we
4499 * don't want to move in order not to upset it.
4500 */
4501 goto wait_other_side;
4502 }
4503
4504 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4505 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004506 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004508 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004509 goto wait_other_side;
4510 }
4511
4512 /* When we get here, it means that both the request and the
4513 * response have finished receiving. Depending on the connection
4514 * mode, we'll have to wait for the last bytes to leave in either
4515 * direction, and sometimes for a close to be effective.
4516 */
4517
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004518 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4519 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004520 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4521 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004522 }
4523 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4524 /* Option forceclose is set, or either side wants to close,
4525 * let's enforce it now that we're not expecting any new
4526 * data to come. The caller knows the session is complete
4527 * once both states are CLOSED.
4528 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004529 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4530 channel_shutr_now(chn);
4531 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004533 }
4534 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004535 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4536 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004537 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004538 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4539 channel_auto_read(chn);
4540 txn->req.msg_state = HTTP_MSG_TUNNEL;
4541 chn->flags |= CF_NEVER_WAIT;
4542 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004543 }
4544
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004545 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004547 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004548
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004549 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 txn->req.msg_state = HTTP_MSG_CLOSING;
4551 goto http_msg_closing;
4552 }
4553 else {
4554 txn->req.msg_state = HTTP_MSG_CLOSED;
4555 goto http_msg_closed;
4556 }
4557 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004558 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 }
4560
4561 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4562 http_msg_closing:
4563 /* nothing else to forward, just waiting for the output buffer
4564 * to be empty and for the shutw_now to take effect.
4565 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 txn->req.msg_state = HTTP_MSG_CLOSED;
4568 goto http_msg_closed;
4569 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004570 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 txn->req.msg_state = HTTP_MSG_ERROR;
4572 goto wait_other_side;
4573 }
4574 }
4575
4576 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4577 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004578 /* see above in MSG_DONE why we only do this in these states */
4579 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4580 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4581 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004582 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 goto wait_other_side;
4584 }
4585
4586 wait_other_side:
4587 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589}
4590
4591
4592/* This function updates the response state machine according to the request
4593 * state machine and buffer flags. It returns 1 if it changes anything (flag
4594 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4595 * it is only used to find when a request/response couple is complete. Both
4596 * this function and its equivalent should loop until both return zero. It
4597 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4598 */
4599int http_sync_res_state(struct session *s)
4600{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604 unsigned int old_state = txn->rsp.msg_state;
4605
4606 http_silent_debug(__LINE__, s);
4607 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4608 return 0;
4609
4610 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4611 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004612 * still monitor the server connection for a possible close
4613 * while the request is being uploaded, so we don't disable
4614 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004616 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617
4618 if (txn->req.msg_state == HTTP_MSG_ERROR)
4619 goto wait_other_side;
4620
4621 if (txn->req.msg_state < HTTP_MSG_DONE) {
4622 /* The client seems to still be sending data, probably
4623 * because we got an error response during an upload.
4624 * We have the choice of either breaking the connection
4625 * or letting it pass through. Let's do the later.
4626 */
4627 goto wait_other_side;
4628 }
4629
4630 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4631 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004632 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004634 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 goto wait_other_side;
4636 }
4637
4638 /* When we get here, it means that both the request and the
4639 * response have finished receiving. Depending on the connection
4640 * mode, we'll have to wait for the last bytes to leave in either
4641 * direction, and sometimes for a close to be effective.
4642 */
4643
4644 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4645 /* Server-close mode : shut read and wait for the request
4646 * side to close its output buffer. The caller will detect
4647 * when we're in DONE and the other is in CLOSED and will
4648 * catch that for the final cleanup.
4649 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004650 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4651 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004652 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004653 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4654 /* Option forceclose is set, or either side wants to close,
4655 * let's enforce it now that we're not expecting any new
4656 * data to come. The caller knows the session is complete
4657 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004659 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4660 channel_shutr_now(chn);
4661 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004662 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663 }
4664 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004665 /* The last possible modes are keep-alive and tunnel. Tunnel will
4666 * need to forward remaining data. Keep-alive will need to monitor
4667 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004669 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004670 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004671 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4672 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 }
4674
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004675 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004677 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4679 goto http_msg_closing;
4680 }
4681 else {
4682 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4683 goto http_msg_closed;
4684 }
4685 }
4686 goto wait_other_side;
4687 }
4688
4689 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4690 http_msg_closing:
4691 /* nothing else to forward, just waiting for the output buffer
4692 * to be empty and for the shutw_now to take effect.
4693 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004694 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004695 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4696 goto http_msg_closed;
4697 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004698 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004699 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004700 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004701 if (objt_server(s->target))
4702 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004703 goto wait_other_side;
4704 }
4705 }
4706
4707 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4708 http_msg_closed:
4709 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004710 bi_erase(chn);
4711 channel_auto_close(chn);
4712 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713 goto wait_other_side;
4714 }
4715
4716 wait_other_side:
4717 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004718 /* We force the response to leave immediately if we're waiting for the
4719 * other side, since there is no pending shutdown to push it out.
4720 */
4721 if (!channel_is_empty(chn))
4722 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004723 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724}
4725
4726
4727/* Resync the request and response state machines. Return 1 if either state
4728 * changes.
4729 */
4730int http_resync_states(struct session *s)
4731{
4732 struct http_txn *txn = &s->txn;
4733 int old_req_state = txn->req.msg_state;
4734 int old_res_state = txn->rsp.msg_state;
4735
4736 http_silent_debug(__LINE__, s);
4737 http_sync_req_state(s);
4738 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004739 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 if (!http_sync_res_state(s))
4741 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004742 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 if (!http_sync_req_state(s))
4744 break;
4745 }
4746 http_silent_debug(__LINE__, s);
4747 /* OK, both state machines agree on a compatible state.
4748 * There are a few cases we're interested in :
4749 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4750 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4751 * directions, so let's simply disable both analysers.
4752 * - HTTP_MSG_CLOSED on the response only means we must abort the
4753 * request.
4754 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4755 * with server-close mode means we've completed one request and we
4756 * must re-initialize the server connection.
4757 */
4758
4759 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4760 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4761 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4762 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4763 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004764 channel_auto_close(s->req);
4765 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004766 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004767 channel_auto_close(s->rep);
4768 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004770 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4771 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004772 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004773 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004774 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004775 channel_auto_close(s->rep);
4776 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004777 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004778 channel_abort(s->req);
4779 channel_auto_close(s->req);
4780 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004781 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004783 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4784 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004786 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4787 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4788 /* server-close/keep-alive: terminate this transaction,
4789 * possibly killing the server connection and reinitialize
4790 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791 */
4792 http_end_txn_clean_session(s);
4793 }
4794
4795 http_silent_debug(__LINE__, s);
4796 return txn->req.msg_state != old_req_state ||
4797 txn->rsp.msg_state != old_res_state;
4798}
4799
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800/* This function is an analyser which forwards request body (including chunk
4801 * sizes if any). It is called as soon as we must forward, even if we forward
4802 * zero byte. The only situation where it must not be called is when we're in
4803 * tunnel mode and we want to forward till the close. It's used both to forward
4804 * remaining data and to resync after end of body. It expects the msg_state to
4805 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4806 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004807 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004808 * bytes of pending data + the headers if not already done (between sol and sov).
4809 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004810 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004811int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812{
4813 struct http_txn *txn = &s->txn;
4814 struct http_msg *msg = &s->txn.req;
4815
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004816 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4817 return 0;
4818
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004819 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004820 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004821 /* Output closed while we were sending data. We must abort and
4822 * wake the other side up.
4823 */
4824 msg->msg_state = HTTP_MSG_ERROR;
4825 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004826 return 1;
4827 }
4828
Willy Tarreau4fe41902010-06-07 22:27:41 +02004829 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004830 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831
4832 /* Note that we don't have to send 100-continue back because we don't
4833 * need the data to complete our job, and it's up to the server to
4834 * decide whether to return 100, 417 or anything else in return of
4835 * an "Expect: 100-continue" header.
4836 */
4837
4838 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004839 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004840 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004841 * is still null. We must save the body in msg->next because it
4842 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004843 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004844 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004845
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004846 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004848 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004849 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004850 }
4851
Willy Tarreaud98cf932009-12-27 22:54:55 +01004852 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004853 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004854
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004856 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004857 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004858 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004859 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004860 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004861 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004862 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004863 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004864
Willy Tarreaucaabe412010-01-03 23:08:28 +01004865 if (msg->msg_state == HTTP_MSG_DATA) {
4866 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004867 if (req->to_forward) {
4868 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004869 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004870 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004871
4872 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004873 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004874 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004876 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004877 }
4878 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004879 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004880 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004881 * TRAILERS state.
4882 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004883 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004884
Willy Tarreau54d23df2012-10-25 19:04:45 +02004885 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004886 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004887 else if (ret < 0) {
4888 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004889 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004890 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004891 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004892 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004893 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004894 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004895 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004896 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004897 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004898
4899 if (ret == 0)
4900 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004901 else if (ret < 0) {
4902 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004903 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004904 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004905 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004906 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004907 /* we're in MSG_CHUNK_SIZE now */
4908 }
4909 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004910 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004911
4912 if (ret == 0)
4913 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004914 else if (ret < 0) {
4915 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004916 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004917 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004919 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004920 /* we're in HTTP_MSG_DONE now */
4921 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004923 int old_state = msg->msg_state;
4924
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004926 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004927 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4928 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004930 if (http_resync_states(s)) {
4931 /* some state changes occurred, maybe the analyser
4932 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004933 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004934 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004935 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004936 /* request errors are most likely due to
4937 * the server aborting the transfer.
4938 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004939 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004940 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004941 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004942 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004943 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004944 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004945 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004946 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004947
4948 /* If "option abortonclose" is set on the backend, we
4949 * want to monitor the client's connection and forward
4950 * any shutdown notification to the server, which will
4951 * decide whether to close or to go on processing the
4952 * request.
4953 */
4954 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004955 channel_auto_read(req);
4956 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004957 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004958 else if (s->txn.meth == HTTP_METH_POST) {
4959 /* POST requests may require to read extra CRLF
4960 * sent by broken browsers and which could cause
4961 * an RST to be sent upon close on some systems
4962 * (eg: Linux).
4963 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004964 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004965 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004966
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004968 }
4969 }
4970
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004972 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004973 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004974 if (!(s->flags & SN_ERR_MASK))
4975 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004976 if (!(s->flags & SN_FINST_MASK)) {
4977 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4978 s->flags |= SN_FINST_H;
4979 else
4980 s->flags |= SN_FINST_D;
4981 }
4982
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004983 s->fe->fe_counters.cli_aborts++;
4984 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004985 if (objt_server(s->target))
4986 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004987
4988 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004989 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004990
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004991 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004992 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004993 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004995 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004996 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004997 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004998 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004999 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005000
Willy Tarreau5c620922011-05-11 19:56:11 +02005001 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005002 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005003 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005004 * modes are already handled by the stream sock layer. We must not do
5005 * this in content-length mode because it could present the MSG_MORE
5006 * flag with the last block of forwarded data, which would cause an
5007 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005008 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005009 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005010 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005011
Willy Tarreau610ecce2010-01-04 21:15:02 +01005012 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005013 return 0;
5014
Willy Tarreaud98cf932009-12-27 22:54:55 +01005015 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005016 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005017 if (s->listener->counters)
5018 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005019 return_bad_req_stats_ok:
5020 txn->req.msg_state = HTTP_MSG_ERROR;
5021 if (txn->status) {
5022 /* Note: we don't send any error if some data were already sent */
5023 stream_int_retnclose(req->prod, NULL);
5024 } else {
5025 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005026 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005027 }
5028 req->analysers = 0;
5029 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005030
5031 if (!(s->flags & SN_ERR_MASK))
5032 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005033 if (!(s->flags & SN_FINST_MASK)) {
5034 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5035 s->flags |= SN_FINST_H;
5036 else
5037 s->flags |= SN_FINST_D;
5038 }
5039 return 0;
5040
5041 aborted_xfer:
5042 txn->req.msg_state = HTTP_MSG_ERROR;
5043 if (txn->status) {
5044 /* Note: we don't send any error if some data were already sent */
5045 stream_int_retnclose(req->prod, NULL);
5046 } else {
5047 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005048 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049 }
5050 req->analysers = 0;
5051 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5052
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005053 s->fe->fe_counters.srv_aborts++;
5054 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005055 if (objt_server(s->target))
5056 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005057
5058 if (!(s->flags & SN_ERR_MASK))
5059 s->flags |= SN_ERR_SRVCL;
5060 if (!(s->flags & SN_FINST_MASK)) {
5061 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5062 s->flags |= SN_FINST_H;
5063 else
5064 s->flags |= SN_FINST_D;
5065 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005066 return 0;
5067}
5068
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005069/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5070 * processing can continue on next analysers, or zero if it either needs more
5071 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5072 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5073 * when it has nothing left to do, and may remove any analyser when it wants to
5074 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005075 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005076int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005077{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005078 struct http_txn *txn = &s->txn;
5079 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005080 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005081 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005083 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005084
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005085 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 +02005086 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005088 rep,
5089 rep->rex, rep->wex,
5090 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005091 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005092 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005093
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005094 /*
5095 * Now parse the partial (or complete) lines.
5096 * We will check the response syntax, and also join multi-line
5097 * headers. An index of all the lines will be elaborated while
5098 * parsing.
5099 *
5100 * For the parsing, we use a 28 states FSM.
5101 *
5102 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005103 * rep->buf->p = beginning of response
5104 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5105 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005106 * msg->eol = end of current header or line (LF or CRLF)
5107 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005108 */
5109
Willy Tarreau83e3af02009-12-28 17:39:57 +01005110 /* There's a protected area at the end of the buffer for rewriting
5111 * purposes. We don't want to start to parse the request if the
5112 * protected area is affected, because we may have to move processed
5113 * data later, which is much more complicated.
5114 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005115 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005116 if (unlikely(!channel_reserved(rep))) {
5117 /* some data has still not left the buffer, wake us once that's done */
5118 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5119 goto abort_response;
5120 channel_dont_close(rep);
5121 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005122 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005123 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005124 }
5125
Willy Tarreau379357a2013-06-08 12:55:46 +02005126 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5127 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5128 buffer_slow_realign(rep->buf);
5129
Willy Tarreau9b28e032012-10-12 23:49:43 +02005130 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005131 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005132 }
5133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 /* 1: we might have to print this header in debug mode */
5135 if (unlikely((global.mode & MODE_DEBUG) &&
5136 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005137 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139
Willy Tarreau9b28e032012-10-12 23:49:43 +02005140 sol = rep->buf->p;
5141 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 sol += hdr_idx_first_pos(&txn->hdr_idx);
5145 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005146
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 while (cur_idx) {
5148 eol = sol + txn->hdr_idx.v[cur_idx].len;
5149 debug_hdr("srvhdr", s, sol, eol);
5150 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5151 cur_idx = txn->hdr_idx.v[cur_idx].next;
5152 }
5153 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005154
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005155 /*
5156 * Now we quickly check if we have found a full valid response.
5157 * If not so, we check the FD and buffer states before leaving.
5158 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005159 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160 * responses are checked first.
5161 *
5162 * Depending on whether the client is still there or not, we
5163 * may send an error response back or not. Note that normally
5164 * we should only check for HTTP status there, and check I/O
5165 * errors somewhere else.
5166 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005167
Willy Tarreau655dce92009-11-08 13:10:58 +01005168 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169 /* Invalid response */
5170 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5171 /* we detected a parsing error. We want to archive this response
5172 * in the dedicated proxy area for later troubleshooting.
5173 */
5174 hdr_response_bad:
5175 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005176 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005177
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005178 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005179 if (objt_server(s->target)) {
5180 objt_server(s->target)->counters.failed_resp++;
5181 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005182 }
Willy Tarreau64648412010-03-05 10:41:54 +01005183 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005184 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 rep->analysers = 0;
5186 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005187 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005188 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005189 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005190
5191 if (!(s->flags & SN_ERR_MASK))
5192 s->flags |= SN_ERR_PRXCOND;
5193 if (!(s->flags & SN_FINST_MASK))
5194 s->flags |= SN_FINST_H;
5195
5196 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005197 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005200 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005201 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005202 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 goto hdr_response_bad;
5204 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005207 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005208 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005209 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005210 else if (txn->flags & TX_NOT_FIRST)
5211 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005212
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005213 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005214 if (objt_server(s->target)) {
5215 objt_server(s->target)->counters.failed_resp++;
5216 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005217 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005218
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005219 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 rep->analysers = 0;
5221 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005222 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005223 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005224 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005225
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226 if (!(s->flags & SN_ERR_MASK))
5227 s->flags |= SN_ERR_SRVCL;
5228 if (!(s->flags & SN_FINST_MASK))
5229 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005230 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005232
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005234 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005236 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005237 else if (txn->flags & TX_NOT_FIRST)
5238 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005239
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005240 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005241 if (objt_server(s->target)) {
5242 objt_server(s->target)->counters.failed_resp++;
5243 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005244 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005245
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005246 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 rep->analysers = 0;
5248 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005249 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005250 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005251 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 if (!(s->flags & SN_ERR_MASK))
5254 s->flags |= SN_ERR_SRVTO;
5255 if (!(s->flags & SN_FINST_MASK))
5256 s->flags |= SN_FINST_H;
5257 return 0;
5258 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005259
Willy Tarreauf003d372012-11-26 13:35:37 +01005260 /* client abort with an abortonclose */
5261 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5262 s->fe->fe_counters.cli_aborts++;
5263 s->be->be_counters.cli_aborts++;
5264 if (objt_server(s->target))
5265 objt_server(s->target)->counters.cli_aborts++;
5266
5267 rep->analysers = 0;
5268 channel_auto_close(rep);
5269
5270 txn->status = 400;
5271 bi_erase(rep);
5272 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5273
5274 if (!(s->flags & SN_ERR_MASK))
5275 s->flags |= SN_ERR_CLICL;
5276 if (!(s->flags & SN_FINST_MASK))
5277 s->flags |= SN_FINST_H;
5278
5279 /* process_session() will take care of the error */
5280 return 0;
5281 }
5282
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005283 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005284 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005285 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005286 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005287 else if (txn->flags & TX_NOT_FIRST)
5288 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005289
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005290 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005291 if (objt_server(s->target)) {
5292 objt_server(s->target)->counters.failed_resp++;
5293 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005294 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005295
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005296 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297 rep->analysers = 0;
5298 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005299 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005300 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005301 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005302
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303 if (!(s->flags & SN_ERR_MASK))
5304 s->flags |= SN_ERR_SRVCL;
5305 if (!(s->flags & SN_FINST_MASK))
5306 s->flags |= SN_FINST_H;
5307 return 0;
5308 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005309
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005311 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005312 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005313 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005314 else if (txn->flags & TX_NOT_FIRST)
5315 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005316
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005317 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005318 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005319 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320
5321 if (!(s->flags & SN_ERR_MASK))
5322 s->flags |= SN_ERR_CLICL;
5323 if (!(s->flags & SN_FINST_MASK))
5324 s->flags |= SN_FINST_H;
5325
5326 /* process_session() will take care of the error */
5327 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005328 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005329
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005330 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005331 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005332 return 0;
5333 }
5334
5335 /* More interesting part now : we know that we have a complete
5336 * response which at least looks like HTTP. We have an indicator
5337 * of each header's length, so we can parse them quickly.
5338 */
5339
5340 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005341 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005343 /*
5344 * 1: get the status code
5345 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005346 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 if (n < 1 || n > 5)
5348 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005349 /* when the client triggers a 4xx from the server, it's most often due
5350 * to a missing object or permission. These events should be tracked
5351 * because if they happen often, it may indicate a brute force or a
5352 * vulnerability scan.
5353 */
5354 if (n == 4)
5355 session_inc_http_err_ctr(s);
5356
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005357 if (objt_server(s->target))
5358 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005359
Willy Tarreau5b154472009-12-21 20:11:07 +01005360 /* check if the response is HTTP/1.1 or above */
5361 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005362 ((rep->buf->p[5] > '1') ||
5363 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005364 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005365
5366 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005367 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 +01005368
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005369 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005370 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005371
Willy Tarreau9b28e032012-10-12 23:49:43 +02005372 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005373
Willy Tarreau39650402010-03-15 19:44:39 +01005374 /* Adjust server's health based on status code. Note: status codes 501
5375 * and 505 are triggered on demand by client request, so we must not
5376 * count them as server failures.
5377 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005378 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005379 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005380 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005381 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005382 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005383 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005384
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005385 /*
5386 * 2: check for cacheability.
5387 */
5388
5389 switch (txn->status) {
5390 case 200:
5391 case 203:
5392 case 206:
5393 case 300:
5394 case 301:
5395 case 410:
5396 /* RFC2616 @13.4:
5397 * "A response received with a status code of
5398 * 200, 203, 206, 300, 301 or 410 MAY be stored
5399 * by a cache (...) unless a cache-control
5400 * directive prohibits caching."
5401 *
5402 * RFC2616 @9.5: POST method :
5403 * "Responses to this method are not cacheable,
5404 * unless the response includes appropriate
5405 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005406 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005408 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005409 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5410 break;
5411 default:
5412 break;
5413 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005414
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415 /*
5416 * 3: we may need to capture headers
5417 */
5418 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005419 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005420 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 txn->rsp.cap, s->fe->rsp_cap);
5422
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005423 /* 4: determine the transfer-length.
5424 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5425 * the presence of a message-body in a RESPONSE and its transfer length
5426 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005427 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005428 * All responses to the HEAD request method MUST NOT include a
5429 * message-body, even though the presence of entity-header fields
5430 * might lead one to believe they do. All 1xx (informational), 204
5431 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5432 * message-body. All other responses do include a message-body,
5433 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005434 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005435 * 1. Any response which "MUST NOT" include a message-body (such as the
5436 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5437 * always terminated by the first empty line after the header fields,
5438 * regardless of the entity-header fields present in the message.
5439 *
5440 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5441 * the "chunked" transfer-coding (Section 6.2) is used, the
5442 * transfer-length is defined by the use of this transfer-coding.
5443 * If a Transfer-Encoding header field is present and the "chunked"
5444 * transfer-coding is not present, the transfer-length is defined by
5445 * the sender closing the connection.
5446 *
5447 * 3. If a Content-Length header field is present, its decimal value in
5448 * OCTETs represents both the entity-length and the transfer-length.
5449 * If a message is received with both a Transfer-Encoding header
5450 * field and a Content-Length header field, the latter MUST be ignored.
5451 *
5452 * 4. If the message uses the media type "multipart/byteranges", and
5453 * the transfer-length is not otherwise specified, then this self-
5454 * delimiting media type defines the transfer-length. This media
5455 * type MUST NOT be used unless the sender knows that the recipient
5456 * can parse it; the presence in a request of a Range header with
5457 * multiple byte-range specifiers from a 1.1 client implies that the
5458 * client can parse multipart/byteranges responses.
5459 *
5460 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005461 */
5462
5463 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005464 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005465 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005466 * FIXME: should we parse anyway and return an error on chunked encoding ?
5467 */
5468 if (txn->meth == HTTP_METH_HEAD ||
5469 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005470 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005471 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005472 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005473 goto skip_content_length;
5474 }
5475
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005476 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005477 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005478 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005479 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005480 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005481 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5482 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005483 /* bad transfer-encoding (chunked followed by something else) */
5484 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005485 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005486 break;
5487 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005488 }
5489
5490 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5491 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005492 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005493 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005494 signed long long cl;
5495
Willy Tarreauad14f752011-09-02 20:33:27 +02005496 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005497 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005498 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005499 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005500
Willy Tarreauad14f752011-09-02 20:33:27 +02005501 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005502 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005503 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005504 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005505
Willy Tarreauad14f752011-09-02 20:33:27 +02005506 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005507 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005508 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005509 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005510
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005511 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005512 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005513 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005514 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005515
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005516 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005517 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005518 }
5519
William Lallemand82fe75c2012-10-23 10:25:10 +02005520 if (s->fe->comp || s->be->comp)
5521 select_compression_response_header(s, rep->buf);
5522
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005523 /* FIXME: we should also implement the multipart/byterange method.
5524 * For now on, we resort to close mode in this case (unknown length).
5525 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005527
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 /* end of job, return OK */
5529 rep->analysers &= ~an_bit;
5530 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005531 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005532 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005533
5534 abort_keep_alive:
5535 /* A keep-alive request to the server failed on a network error.
5536 * The client is required to retry. We need to close without returning
5537 * any other information so that the client retries.
5538 */
5539 txn->status = 0;
5540 rep->analysers = 0;
5541 s->req->analysers = 0;
5542 channel_auto_close(rep);
5543 s->logs.logwait = 0;
5544 s->logs.level = 0;
5545 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5546 bi_erase(rep);
5547 stream_int_retnclose(rep->cons, NULL);
5548 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005549}
5550
5551/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005552 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5553 * and updates t->rep->analysers. It might make sense to explode it into several
5554 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005555 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005556int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005557{
5558 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005559 struct http_msg *msg = &txn->rsp;
5560 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005561 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005562 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005563
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005564 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 +02005565 now_ms, __FUNCTION__,
5566 t,
5567 rep,
5568 rep->rex, rep->wex,
5569 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005570 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005571 rep->analysers);
5572
Willy Tarreau655dce92009-11-08 13:10:58 +01005573 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005574 return 0;
5575
5576 rep->analysers &= ~an_bit;
5577 rep->analyse_exp = TICK_ETERNITY;
5578
Willy Tarreau5b154472009-12-21 20:11:07 +01005579 /* Now we have to check if we need to modify the Connection header.
5580 * This is more difficult on the response than it is on the request,
5581 * because we can have two different HTTP versions and we don't know
5582 * how the client will interprete a response. For instance, let's say
5583 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5584 * HTTP/1.1 response without any header. Maybe it will bound itself to
5585 * HTTP/1.0 because it only knows about it, and will consider the lack
5586 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5587 * the lack of header as a keep-alive. Thus we will use two flags
5588 * indicating how a request MAY be understood by the client. In case
5589 * of multiple possibilities, we'll fix the header to be explicit. If
5590 * ambiguous cases such as both close and keepalive are seen, then we
5591 * will fall back to explicit close. Note that we won't take risks with
5592 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005593 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005594 */
5595
Willy Tarreaudc008c52010-02-01 16:20:08 +01005596 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5597 txn->status == 101)) {
5598 /* Either we've established an explicit tunnel, or we're
5599 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005600 * to understand the next protocols. We have to switch to tunnel
5601 * mode, so that we transfer the request and responses then let
5602 * this protocol pass unmodified. When we later implement specific
5603 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005604 * header which contains information about that protocol for
5605 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005606 */
5607 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5608 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005609 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5610 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5611 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005612 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005613
Willy Tarreau60466522010-01-18 19:08:45 +01005614 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005615 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005616 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5617 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005618
Willy Tarreau60466522010-01-18 19:08:45 +01005619 /* now adjust header transformations depending on current state */
5620 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5621 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5622 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005623 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005624 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005625 }
Willy Tarreau60466522010-01-18 19:08:45 +01005626 else { /* SCL / KAL */
5627 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005628 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005629 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005630 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005631
Willy Tarreau60466522010-01-18 19:08:45 +01005632 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005633 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005634
Willy Tarreau60466522010-01-18 19:08:45 +01005635 /* Some keep-alive responses are converted to Server-close if
5636 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005637 */
Willy Tarreau60466522010-01-18 19:08:45 +01005638 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5639 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005640 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005641 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005642 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005643 }
5644
Willy Tarreau7959a552013-09-23 16:44:27 +02005645 /* we want to have the response time before we start processing it */
5646 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5647
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005648 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005649 /*
5650 * 3: we will have to evaluate the filters.
5651 * As opposed to version 1.2, now they will be evaluated in the
5652 * filters order and not in the header order. This means that
5653 * each filter has to be validated among all headers.
5654 *
5655 * Filters are tried with ->be first, then with ->fe if it is
5656 * different from ->be.
5657 */
5658
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005659 cur_proxy = t->be;
5660 while (1) {
5661 struct proxy *rule_set = cur_proxy;
5662
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005663 /* evaluate http-response rules */
5664 if (!http_res_last_rule)
5665 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5666
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005667 /* try headers filters */
5668 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005669 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005670 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005671 if (objt_server(t->target)) {
5672 objt_server(t->target)->counters.failed_resp++;
5673 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005674 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005675 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005676 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005677 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005678 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005679 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005680 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005681 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005682 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005683 if (!(t->flags & SN_ERR_MASK))
5684 t->flags |= SN_ERR_PRXCOND;
5685 if (!(t->flags & SN_FINST_MASK))
5686 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005687 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005688 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005689 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005691 /* has the response been denied ? */
5692 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005693 if (objt_server(t->target))
5694 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005695
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005696 t->be->be_counters.denied_resp++;
5697 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005698 if (t->listener->counters)
5699 t->listener->counters->denied_resp++;
5700
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005701 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005702 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005703
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005704 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005705 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005706 if (txn->status < 200)
5707 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005708 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005709 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005710 ret = acl_pass(ret);
5711 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5712 ret = !ret;
5713 if (!ret)
5714 continue;
5715 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005716 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005717 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005718 }
5719
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005720 /* check whether we're already working on the frontend */
5721 if (cur_proxy == t->fe)
5722 break;
5723 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005724 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005725
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005726 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005727 * We may be facing a 100-continue response, in which case this
5728 * is not the right response, and we're waiting for the next one.
5729 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005730 * next one.
5731 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005732 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005733 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005734 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005735 msg->msg_state = HTTP_MSG_RPBEFORE;
5736 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005737 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005738 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5739 return 1;
5740 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005741 else if (unlikely(txn->status < 200))
5742 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005743
5744 /* we don't have any 1xx status code now */
5745
5746 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005747 * 4: check for server cookie.
5748 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005749 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5750 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005752
Willy Tarreaubaaee002006-06-26 02:48:02 +02005753
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005755 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756 */
Willy Tarreau67402132012-05-31 20:40:20 +02005757 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005758 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005760 /*
5761 * 6: add server cookie in the response if needed
5762 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005763 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005764 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005765 (!(t->flags & SN_DIRECT) ||
5766 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5767 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5768 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5769 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005770 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005771 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005772 /* the server is known, it's not the one the client requested, or the
5773 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005774 * insert a set-cookie here, except if we want to insert only on POST
5775 * requests and this one isn't. Note that servers which don't have cookies
5776 * (eg: some backup servers) will return a full cookie removal request.
5777 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005778 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005779 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005780 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5781 t->be->cookie_name);
5782 }
5783 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005784 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005785
5786 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5787 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005788 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5789 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5790 trash.len += 5;
5791
Willy Tarreauef4f3912010-10-07 21:00:29 +02005792 if (t->be->cookie_maxlife) {
5793 /* emit first_date, which is either the original one or
5794 * the current date.
5795 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005796 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005797 s30tob64(txn->cookie_first_date ?
5798 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005799 (date.tv_sec+3) >> 2, trash.str + trash.len);
5800 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005801 }
5802 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005803 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005805
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005807 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005808
Willy Tarreau4992dd22012-05-31 21:02:17 +02005809 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005810 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005811
5812 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005813 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005814
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005815 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005817
Willy Tarreauf1348312010-10-07 15:54:11 +02005818 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005819 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005820 /* the server did not change, only the date was updated */
5821 txn->flags |= TX_SCK_UPDATED;
5822 else
5823 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005824
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005825 /* Here, we will tell an eventual cache on the client side that we don't
5826 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5827 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5828 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5829 */
Willy Tarreau67402132012-05-31 20:40:20 +02005830 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005831
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005832 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5833
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005834 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005835 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005836 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837 }
5838 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005839
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005840 /*
5841 * 7: check if result will be cacheable with a cookie.
5842 * We'll block the response if security checks have caught
5843 * nasty things such as a cacheable cookie.
5844 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005845 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5846 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005847 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005848
5849 /* we're in presence of a cacheable response containing
5850 * a set-cookie header. We'll block it as requested by
5851 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005852 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005853 if (objt_server(t->target))
5854 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005855
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005856 t->be->be_counters.denied_resp++;
5857 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005858 if (t->listener->counters)
5859 t->listener->counters->denied_resp++;
5860
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005861 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005862 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863 send_log(t->be, LOG_ALERT,
5864 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005865 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866 goto return_srv_prx_502;
5867 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868
5869 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005870 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005871 * If an "Upgrade" token is found, the header is left untouched in order
5872 * not to have to deal with some client bugs : some of them fail an upgrade
5873 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005874 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005875 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5876 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5877 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005878 unsigned int want_flags = 0;
5879
5880 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5881 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5882 /* we want a keep-alive response here. Keep-alive header
5883 * required if either side is not 1.1.
5884 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005885 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005886 want_flags |= TX_CON_KAL_SET;
5887 }
5888 else {
5889 /* we want a close response here. Close header required if
5890 * the server is 1.1, regardless of the client.
5891 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005892 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005893 want_flags |= TX_CON_CLO_SET;
5894 }
5895
5896 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005897 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005898 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005900 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005901 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005902 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005903 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005904
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005905 /*************************************************************
5906 * OK, that's finished for the headers. We have done what we *
5907 * could. Let's switch to the DATA state. *
5908 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005909
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005910 /* if the user wants to log as soon as possible, without counting
5911 * bytes from the server, then this is the right moment. We have
5912 * to temporarily assign bytes_out to log what we currently have.
5913 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005914 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005915 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5916 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005917 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005918 t->logs.bytes_out = 0;
5919 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005921 /* Note: we must not try to cheat by jumping directly to DATA,
5922 * otherwise we would not let the client side wake up.
5923 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005924
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005925 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005926 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005927 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005928}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005929
Willy Tarreaud98cf932009-12-27 22:54:55 +01005930/* This function is an analyser which forwards response body (including chunk
5931 * sizes if any). It is called as soon as we must forward, even if we forward
5932 * zero byte. The only situation where it must not be called is when we're in
5933 * tunnel mode and we want to forward till the close. It's used both to forward
5934 * remaining data and to resync after end of body. It expects the msg_state to
5935 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5936 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005937 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005938 * bytes of pending data + the headers if not already done (between sol and sov).
5939 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005940 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005941int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942{
5943 struct http_txn *txn = &s->txn;
5944 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005945 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005946 static struct buffer *tmpbuf = NULL;
5947 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005948 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005949 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005950
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005951 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5952 return 0;
5953
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005954 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005955 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005956 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005957 /* Output closed while we were sending data. We must abort and
5958 * wake the other side up.
5959 */
5960 msg->msg_state = HTTP_MSG_ERROR;
5961 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005962 return 1;
5963 }
5964
Willy Tarreau4fe41902010-06-07 22:27:41 +02005965 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005966 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005967
William Lallemand82fe75c2012-10-23 10:25:10 +02005968 /* this is the first time we need the compression buffer */
5969 if (s->comp_algo != NULL && tmpbuf == NULL) {
5970 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5971 goto aborted_xfer; /* no memory */
5972 }
5973
Willy Tarreaud98cf932009-12-27 22:54:55 +01005974 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005975 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005976 * rep->buf.p still points to the beginning of the message and msg->sol
5977 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005978 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005979 channel_forward(res, msg->sov);
5980 msg->next = 0;
5981 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005982
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005983 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005984 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005985 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005986 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005987 }
5988
William Lallemand82fe75c2012-10-23 10:25:10 +02005989 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005990 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005991 if (ret < 0) {
5992 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02005993 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005994 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005995 compressing = 1;
5996 }
5997
Willy Tarreaud98cf932009-12-27 22:54:55 +01005998 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005999 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006000 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006001 if (s->comp_algo == NULL) {
6002 bytes = msg->sov - msg->sol;
6003 if (msg->chunk_len || bytes) {
6004 msg->sol = msg->sov;
6005 msg->next -= bytes; /* will be forwarded */
6006 msg->chunk_len += bytes;
6007 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6008 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006009 }
6010
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006011 switch (msg->msg_state - HTTP_MSG_DATA) {
6012 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006013 if (compressing) {
6014 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6015 if (ret < 0)
6016 goto aborted_xfer;
6017 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006018
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006019 if (res->to_forward || msg->chunk_len) {
6020 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006021 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006022 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006023
6024 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006025 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006026 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006027 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006028 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006029 if (compressing && consumed_data) {
6030 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6031 compressing = 0;
6032 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006033 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006034 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006035 /* fall through for HTTP_MSG_CHUNK_CRLF */
6036
6037 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6038 /* we want the CRLF after the data */
6039
6040 ret = http_skip_chunk_crlf(msg);
6041 if (ret == 0)
6042 goto missing_data;
6043 else if (ret < 0) {
6044 if (msg->err_pos >= 0)
6045 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6046 goto return_bad_res;
6047 }
6048 /* skipping data in buffer for compression */
6049 if (compressing) {
6050 b_adv(res->buf, msg->next);
6051 msg->next = 0;
6052 msg->sov = 0;
6053 msg->sol = 0;
6054 }
6055 /* we're in MSG_CHUNK_SIZE now, fall through */
6056
6057 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006058 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006059 * set ->sov and ->next to point to the body and switch to DATA or
6060 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006061 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006062
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006063 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006064 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006066 else if (ret < 0) {
6067 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006068 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006069 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006070 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006071 if (compressing) {
6072 if (likely(msg->chunk_len > 0)) {
6073 /* skipping data if we are in compression mode */
6074 b_adv(res->buf, msg->next);
6075 msg->next = 0;
6076 msg->sov = 0;
6077 msg->sol = 0;
6078 } else {
6079 if (consumed_data) {
6080 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6081 compressing = 0;
6082 }
6083 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006084 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006085 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006086 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006087
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006088 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6089 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090 if (ret == 0)
6091 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006092 else if (ret < 0) {
6093 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006094 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006095 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006096 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006097 if (s->comp_algo != NULL) {
6098 /* forwarding trailers */
6099 channel_forward(res, msg->next);
6100 msg->next = 0;
6101 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006102 /* we're in HTTP_MSG_DONE now, but we might still have
6103 * some data pending, so let's loop over once.
6104 */
6105 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006106
6107 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006108 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006109
6110 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006111 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006112 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6113 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006114 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006115 if (http_resync_states(s)) {
6116 http_silent_debug(__LINE__, s);
6117 /* some state changes occurred, maybe the analyser
6118 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006119 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006120 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006121 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006122 /* response errors are most likely due to
6123 * the client aborting the transfer.
6124 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006125 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006126 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006127 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006128 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006129 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006131 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006132 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006133 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006134 }
6135 }
6136
Willy Tarreaud98cf932009-12-27 22:54:55 +01006137 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006138 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006139 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6140 compressing = 0;
6141 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006142
6143 if (res->flags & CF_SHUTW)
6144 goto aborted_xfer;
6145
6146 /* stop waiting for data if the input is closed before the end. If the
6147 * client side was already closed, it means that the client has aborted,
6148 * so we don't want to count this as a server abort. Otherwise it's a
6149 * server abort.
6150 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006151 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006152 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6153 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006154 if (!(s->flags & SN_ERR_MASK))
6155 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006156 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006157 if (objt_server(s->target))
6158 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006159 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006160 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006161
Willy Tarreau40dba092010-03-04 18:14:51 +01006162 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006163 if (!s->req->analysers)
6164 goto return_bad_res;
6165
Willy Tarreauea953162012-05-18 23:41:28 +02006166 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006167 if (s->comp_algo == NULL) {
6168 bytes = msg->sov - msg->sol;
6169 if (msg->chunk_len || bytes) {
6170 msg->sol = msg->sov;
6171 msg->next -= bytes; /* will be forwarded */
6172 msg->chunk_len += bytes;
6173 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6174 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006175 }
6176
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006177 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006178 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006179 * Similarly, with keep-alive on the client side, we don't want to forward a
6180 * close.
6181 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006182 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006183 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6184 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006185 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006186
Willy Tarreau5c620922011-05-11 19:56:11 +02006187 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006188 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006189 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006190 * modes are already handled by the stream sock layer. We must not do
6191 * this in content-length mode because it could present the MSG_MORE
6192 * flag with the last block of forwarded data, which would cause an
6193 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006194 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006195 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006196 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006197
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006199 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006200 return 0;
6201
Willy Tarreau40dba092010-03-04 18:14:51 +01006202 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006203 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006204 if (objt_server(s->target))
6205 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006206
6207 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006208 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006209 /* don't send any error message as we're in the body */
6210 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006211 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006212 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006213 if (objt_server(s->target))
6214 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006215
6216 if (!(s->flags & SN_ERR_MASK))
6217 s->flags |= SN_ERR_PRXCOND;
6218 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006219 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006220 return 0;
6221
6222 aborted_xfer:
6223 txn->rsp.msg_state = HTTP_MSG_ERROR;
6224 /* don't send any error message as we're in the body */
6225 stream_int_retnclose(res->cons, NULL);
6226 res->analysers = 0;
6227 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6228
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006229 s->fe->fe_counters.cli_aborts++;
6230 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006231 if (objt_server(s->target))
6232 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006233
6234 if (!(s->flags & SN_ERR_MASK))
6235 s->flags |= SN_ERR_CLICL;
6236 if (!(s->flags & SN_FINST_MASK))
6237 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006238 return 0;
6239}
6240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006241/* Iterate the same filter through all request headers.
6242 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006243 * Since it can manage the switch to another backend, it updates the per-proxy
6244 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006246int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006248 char term;
6249 char *cur_ptr, *cur_end, *cur_next;
6250 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006251 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006253 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 last_hdr = 0;
6256
Willy Tarreau9b28e032012-10-12 23:49:43 +02006257 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 old_idx = 0;
6259
6260 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006261 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006262 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006263 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006264 (exp->action == ACT_ALLOW ||
6265 exp->action == ACT_DENY ||
6266 exp->action == ACT_TARPIT))
6267 return 0;
6268
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006269 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006270 if (!cur_idx)
6271 break;
6272
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006273 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006274 cur_ptr = cur_next;
6275 cur_end = cur_ptr + cur_hdr->len;
6276 cur_next = cur_end + cur_hdr->cr + 1;
6277
6278 /* Now we have one header between cur_ptr and cur_end,
6279 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280 */
6281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 /* The annoying part is that pattern matching needs
6283 * that we modify the contents to null-terminate all
6284 * strings before testing them.
6285 */
6286
6287 term = *cur_end;
6288 *cur_end = '\0';
6289
6290 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6291 switch (exp->action) {
6292 case ACT_SETBE:
6293 /* It is not possible to jump a second time.
6294 * FIXME: should we return an HTTP/500 here so that
6295 * the admin knows there's a problem ?
6296 */
6297 if (t->be != t->fe)
6298 break;
6299
6300 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006301 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006302 last_hdr = 1;
6303 break;
6304
6305 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006306 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006307 last_hdr = 1;
6308 break;
6309
6310 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006311 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006313 break;
6314
6315 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006316 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006318 break;
6319
6320 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006321 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6322 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006323 /* FIXME: if the user adds a newline in the replacement, the
6324 * index will not be recalculated for now, and the new line
6325 * will not be counted as a new header.
6326 */
6327
6328 cur_end += delta;
6329 cur_next += delta;
6330 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006331 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006332 break;
6333
6334 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006335 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 cur_next += delta;
6337
Willy Tarreaufa355d42009-11-29 18:12:29 +01006338 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006339 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6340 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341 cur_hdr->len = 0;
6342 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006343 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006344 break;
6345
6346 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 if (cur_end)
6349 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 /* keep the link from this header to next one in case of later
6352 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354 old_idx = cur_idx;
6355 }
6356 return 0;
6357}
6358
6359
6360/* Apply the filter to the request line.
6361 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6362 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006363 * Since it can manage the switch to another backend, it updates the per-proxy
6364 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006366int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367{
6368 char term;
6369 char *cur_ptr, *cur_end;
6370 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006371 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006372 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373
Willy Tarreau3d300592007-03-18 18:34:41 +01006374 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006376 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 (exp->action == ACT_ALLOW ||
6378 exp->action == ACT_DENY ||
6379 exp->action == ACT_TARPIT))
6380 return 0;
6381 else if (exp->action == ACT_REMOVE)
6382 return 0;
6383
6384 done = 0;
6385
Willy Tarreau9b28e032012-10-12 23:49:43 +02006386 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006387 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006388
6389 /* Now we have the request line between cur_ptr and cur_end */
6390
6391 /* The annoying part is that pattern matching needs
6392 * that we modify the contents to null-terminate all
6393 * strings before testing them.
6394 */
6395
6396 term = *cur_end;
6397 *cur_end = '\0';
6398
6399 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6400 switch (exp->action) {
6401 case ACT_SETBE:
6402 /* It is not possible to jump a second time.
6403 * FIXME: should we return an HTTP/500 here so that
6404 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006405 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006406 if (t->be != t->fe)
6407 break;
6408
6409 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006410 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006411 done = 1;
6412 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006414 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006415 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006416 done = 1;
6417 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006420 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006421 done = 1;
6422 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006425 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 done = 1;
6427 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 case ACT_REPLACE:
6430 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006431 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6432 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 /* FIXME: if the user adds a newline in the replacement, the
6434 * index will not be recalculated for now, and the new line
6435 * will not be counted as a new header.
6436 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006437
Willy Tarreaufa355d42009-11-29 18:12:29 +01006438 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006440 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 HTTP_MSG_RQMETH,
6442 cur_ptr, cur_end + 1,
6443 NULL, NULL);
6444 if (unlikely(!cur_end))
6445 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 /* we have a full request and we know that we have either a CR
6448 * or an LF at <ptr>.
6449 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006450 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6451 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 /* there is no point trying this regex on headers */
6453 return 1;
6454 }
6455 }
6456 *cur_end = term; /* restore the string terminator */
6457 return done;
6458}
Willy Tarreau97de6242006-12-27 17:18:38 +01006459
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006463 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465 * unparsable request. Since it can manage the switch to another backend, it
6466 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006468int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006470 struct http_txn *txn = &s->txn;
6471 struct hdr_exp *exp;
6472
6473 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 /*
6477 * The interleaving of transformations and verdicts
6478 * makes it difficult to decide to continue or stop
6479 * the evaluation.
6480 */
6481
Willy Tarreau6c123b12010-01-28 20:22:06 +01006482 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6483 break;
6484
Willy Tarreau3d300592007-03-18 18:34:41 +01006485 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006487 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006489
6490 /* if this filter had a condition, evaluate it now and skip to
6491 * next filter if the condition does not match.
6492 */
6493 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006494 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006495 ret = acl_pass(ret);
6496 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6497 ret = !ret;
6498
6499 if (!ret)
6500 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 }
6502
6503 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006504 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 if (unlikely(ret < 0))
6506 return -1;
6507
6508 if (likely(ret == 0)) {
6509 /* The filter did not match the request, it can be
6510 * iterated through all headers.
6511 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006512 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006513 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006516}
6517
6518
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006521 * Try to retrieve the server associated to the appsession.
6522 * If the server is found, it's assigned to the session.
6523 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006524void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006525 struct http_txn *txn = &t->txn;
6526 appsess *asession = NULL;
6527 char *sessid_temp = NULL;
6528
Cyril Bontéb21570a2009-11-29 20:04:48 +01006529 if (len > t->be->appsession_len) {
6530 len = t->be->appsession_len;
6531 }
6532
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006533 if (t->be->options2 & PR_O2_AS_REQL) {
6534 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006535 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006536 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006537 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006538 }
6539
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006540 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006541 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6542 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6543 return;
6544 }
6545
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006546 memcpy(txn->sessid, buf, len);
6547 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006548 }
6549
6550 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6551 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6552 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6553 return;
6554 }
6555
Cyril Bontéb21570a2009-11-29 20:04:48 +01006556 memcpy(sessid_temp, buf, len);
6557 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006558
6559 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6560 /* free previously allocated memory */
6561 pool_free2(apools.sessid, sessid_temp);
6562
6563 if (asession != NULL) {
6564 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6565 if (!(t->be->options2 & PR_O2_AS_REQL))
6566 asession->request_count++;
6567
6568 if (asession->serverid != NULL) {
6569 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006570
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006571 while (srv) {
6572 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006573 if ((srv->state & SRV_RUNNING) ||
6574 (t->be->options & PR_O_PERSIST) ||
6575 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006576 /* we found the server and it's usable */
6577 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006578 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006579 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006580 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006581
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006582 break;
6583 } else {
6584 txn->flags &= ~TX_CK_MASK;
6585 txn->flags |= TX_CK_DOWN;
6586 }
6587 }
6588 srv = srv->next;
6589 }
6590 }
6591 }
6592}
6593
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006594/* Find the end of a cookie value contained between <s> and <e>. It works the
6595 * same way as with headers above except that the semi-colon also ends a token.
6596 * See RFC2965 for more information. Note that it requires a valid header to
6597 * return a valid result.
6598 */
6599char *find_cookie_value_end(char *s, const char *e)
6600{
6601 int quoted, qdpair;
6602
6603 quoted = qdpair = 0;
6604 for (; s < e; s++) {
6605 if (qdpair) qdpair = 0;
6606 else if (quoted) {
6607 if (*s == '\\') qdpair = 1;
6608 else if (*s == '"') quoted = 0;
6609 }
6610 else if (*s == '"') quoted = 1;
6611 else if (*s == ',' || *s == ';') return s;
6612 }
6613 return s;
6614}
6615
6616/* Delete a value in a header between delimiters <from> and <next> in buffer
6617 * <buf>. The number of characters displaced is returned, and the pointer to
6618 * the first delimiter is updated if required. The function tries as much as
6619 * possible to respect the following principles :
6620 * - replace <from> delimiter by the <next> one unless <from> points to a
6621 * colon, in which case <next> is simply removed
6622 * - set exactly one space character after the new first delimiter, unless
6623 * there are not enough characters in the block being moved to do so.
6624 * - remove unneeded spaces before the previous delimiter and after the new
6625 * one.
6626 *
6627 * It is the caller's responsibility to ensure that :
6628 * - <from> points to a valid delimiter or the colon ;
6629 * - <next> points to a valid delimiter or the final CR/LF ;
6630 * - there are non-space chars before <from> ;
6631 * - there is a CR/LF at or after <next>.
6632 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006633int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634{
6635 char *prev = *from;
6636
6637 if (*prev == ':') {
6638 /* We're removing the first value, preserve the colon and add a
6639 * space if possible.
6640 */
6641 if (!http_is_crlf[(unsigned char)*next])
6642 next++;
6643 prev++;
6644 if (prev < next)
6645 *prev++ = ' ';
6646
6647 while (http_is_spht[(unsigned char)*next])
6648 next++;
6649 } else {
6650 /* Remove useless spaces before the old delimiter. */
6651 while (http_is_spht[(unsigned char)*(prev-1)])
6652 prev--;
6653 *from = prev;
6654
6655 /* copy the delimiter and if possible a space if we're
6656 * not at the end of the line.
6657 */
6658 if (!http_is_crlf[(unsigned char)*next]) {
6659 *prev++ = *next++;
6660 if (prev + 1 < next)
6661 *prev++ = ' ';
6662 while (http_is_spht[(unsigned char)*next])
6663 next++;
6664 }
6665 }
6666 return buffer_replace2(buf, prev, next, NULL, 0);
6667}
6668
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006669/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006670 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006671 * desirable to call it only when needed. This code is quite complex because
6672 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6673 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006674 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006675void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006676{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006677 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006679 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006680 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6681 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006684 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006685 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006686
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006687 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006688 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006689 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006690
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006691 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006692 hdr_beg = hdr_next;
6693 hdr_end = hdr_beg + cur_hdr->len;
6694 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006695
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006696 /* We have one full header between hdr_beg and hdr_end, and the
6697 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698 * "Cookie:" headers.
6699 */
6700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006701 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006702 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006703 old_idx = cur_idx;
6704 continue;
6705 }
6706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 del_from = NULL; /* nothing to be deleted */
6708 preserve_hdr = 0; /* assume we may kill the whole header */
6709
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710 /* Now look for cookies. Conforming to RFC2109, we have to support
6711 * attributes whose name begin with a '$', and associate them with
6712 * the right cookie, if we want to delete this cookie.
6713 * So there are 3 cases for each cookie read :
6714 * 1) it's a special attribute, beginning with a '$' : ignore it.
6715 * 2) it's a server id cookie that we *MAY* want to delete : save
6716 * some pointers on it (last semi-colon, beginning of cookie...)
6717 * 3) it's an application cookie : we *MAY* have to delete a previous
6718 * "special" cookie.
6719 * At the end of loop, if a "special" cookie remains, we may have to
6720 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 * *MUST* delete it.
6722 *
6723 * Note: RFC2965 is unclear about the processing of spaces around
6724 * the equal sign in the ATTR=VALUE form. A careful inspection of
6725 * the RFC explicitly allows spaces before it, and not within the
6726 * tokens (attrs or values). An inspection of RFC2109 allows that
6727 * too but section 10.1.3 lets one think that spaces may be allowed
6728 * after the equal sign too, resulting in some (rare) buggy
6729 * implementations trying to do that. So let's do what servers do.
6730 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6731 * allowed quoted strings in values, with any possible character
6732 * after a backslash, including control chars and delimitors, which
6733 * causes parsing to become ambiguous. Browsers also allow spaces
6734 * within values even without quotes.
6735 *
6736 * We have to keep multiple pointers in order to support cookie
6737 * removal at the beginning, middle or end of header without
6738 * corrupting the header. All of these headers are valid :
6739 *
6740 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6741 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6742 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6743 * | | | | | | | | |
6744 * | | | | | | | | hdr_end <--+
6745 * | | | | | | | +--> next
6746 * | | | | | | +----> val_end
6747 * | | | | | +-----------> val_beg
6748 * | | | | +--------------> equal
6749 * | | | +----------------> att_end
6750 * | | +---------------------> att_beg
6751 * | +--------------------------> prev
6752 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006753 */
6754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6756 /* Iterate through all cookies on this line */
6757
6758 /* find att_beg */
6759 att_beg = prev + 1;
6760 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6761 att_beg++;
6762
6763 /* find att_end : this is the first character after the last non
6764 * space before the equal. It may be equal to hdr_end.
6765 */
6766 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 while (equal < hdr_end) {
6769 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 if (http_is_spht[(unsigned char)*equal++])
6772 continue;
6773 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774 }
6775
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006776 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6777 * is between <att_beg> and <equal>, both may be identical.
6778 */
6779
6780 /* look for end of cookie if there is an equal sign */
6781 if (equal < hdr_end && *equal == '=') {
6782 /* look for the beginning of the value */
6783 val_beg = equal + 1;
6784 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6785 val_beg++;
6786
6787 /* find the end of the value, respecting quotes */
6788 next = find_cookie_value_end(val_beg, hdr_end);
6789
6790 /* make val_end point to the first white space or delimitor after the value */
6791 val_end = next;
6792 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6793 val_end--;
6794 } else {
6795 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 /* We have nothing to do with attributes beginning with '$'. However,
6799 * they will automatically be removed if a header before them is removed,
6800 * since they're supposed to be linked together.
6801 */
6802 if (*att_beg == '$')
6803 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006804
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006805 /* Ignore cookies with no equal sign */
6806 if (equal == next) {
6807 /* This is not our cookie, so we must preserve it. But if we already
6808 * scheduled another cookie for removal, we cannot remove the
6809 * complete header, but we can remove the previous block itself.
6810 */
6811 preserve_hdr = 1;
6812 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006813 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006814 val_end += delta;
6815 next += delta;
6816 hdr_end += delta;
6817 hdr_next += delta;
6818 cur_hdr->len += delta;
6819 http_msg_move_end(&txn->req, delta);
6820 prev = del_from;
6821 del_from = NULL;
6822 }
6823 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006825
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006826 /* if there are spaces around the equal sign, we need to
6827 * strip them otherwise we'll get trouble for cookie captures,
6828 * or even for rewrites. Since this happens extremely rarely,
6829 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006830 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006831 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6832 int stripped_before = 0;
6833 int stripped_after = 0;
6834
6835 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006836 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006837 equal += stripped_before;
6838 val_beg += stripped_before;
6839 }
6840
6841 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006842 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006843 val_beg += stripped_after;
6844 stripped_before += stripped_after;
6845 }
6846
6847 val_end += stripped_before;
6848 next += stripped_before;
6849 hdr_end += stripped_before;
6850 hdr_next += stripped_before;
6851 cur_hdr->len += stripped_before;
6852 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006853 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 /* First, let's see if we want to capture this cookie. We check
6857 * that we don't already have a client side cookie, because we
6858 * can only capture one. Also as an optimisation, we ignore
6859 * cookies shorter than the declared name.
6860 */
6861 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6862 (val_end - att_beg >= t->fe->capture_namelen) &&
6863 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6864 int log_len = val_end - att_beg;
6865
6866 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6867 Alert("HTTP logging : out of memory.\n");
6868 } else {
6869 if (log_len > t->fe->capture_len)
6870 log_len = t->fe->capture_len;
6871 memcpy(txn->cli_cookie, att_beg, log_len);
6872 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006873 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006875
Willy Tarreaubca99692010-10-06 19:25:55 +02006876 /* Persistence cookies in passive, rewrite or insert mode have the
6877 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006879 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006880 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006881 * For cookies in prefix mode, the form is :
6882 *
6883 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 */
6885 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6886 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6887 struct server *srv = t->be->srv;
6888 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6891 * have the server ID between val_beg and delim, and the original cookie between
6892 * delim+1 and val_end. Otherwise, delim==val_end :
6893 *
6894 * Cookie: NAME=SRV; # in all but prefix modes
6895 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6896 * | || || | |+-> next
6897 * | || || | +--> val_end
6898 * | || || +---------> delim
6899 * | || |+------------> val_beg
6900 * | || +-------------> att_end = equal
6901 * | |+-----------------> att_beg
6902 * | +------------------> prev
6903 * +-------------------------> hdr_beg
6904 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006905
Willy Tarreau67402132012-05-31 20:40:20 +02006906 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 for (delim = val_beg; delim < val_end; delim++)
6908 if (*delim == COOKIE_DELIM)
6909 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006910 } else {
6911 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006913 /* Now check if the cookie contains a date field, which would
6914 * appear after a vertical bar ('|') just after the server name
6915 * and before the delimiter.
6916 */
6917 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6918 if (vbar1) {
6919 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006920 * right is the last seen date. It is a base64 encoded
6921 * 30-bit value representing the UNIX date since the
6922 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006923 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006924 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006925 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006926 if (val_end - vbar1 >= 5) {
6927 val = b64tos30(vbar1);
6928 if (val > 0)
6929 txn->cookie_last_date = val << 2;
6930 }
6931 /* look for a second vertical bar */
6932 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6933 if (vbar1 && (val_end - vbar1 > 5)) {
6934 val = b64tos30(vbar1 + 1);
6935 if (val > 0)
6936 txn->cookie_first_date = val << 2;
6937 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006938 }
6939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreauf64d1412010-10-07 20:06:11 +02006941 /* if the cookie has an expiration date and the proxy wants to check
6942 * it, then we do that now. We first check if the cookie is too old,
6943 * then only if it has expired. We detect strict overflow because the
6944 * time resolution here is not great (4 seconds). Cookies with dates
6945 * in the future are ignored if their offset is beyond one day. This
6946 * allows an admin to fix timezone issues without expiring everyone
6947 * and at the same time avoids keeping unwanted side effects for too
6948 * long.
6949 */
6950 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006951 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6952 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006953 txn->flags &= ~TX_CK_MASK;
6954 txn->flags |= TX_CK_OLD;
6955 delim = val_beg; // let's pretend we have not found the cookie
6956 txn->cookie_first_date = 0;
6957 txn->cookie_last_date = 0;
6958 }
6959 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006960 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6961 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006962 txn->flags &= ~TX_CK_MASK;
6963 txn->flags |= TX_CK_EXPIRED;
6964 delim = val_beg; // let's pretend we have not found the cookie
6965 txn->cookie_first_date = 0;
6966 txn->cookie_last_date = 0;
6967 }
6968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 /* Here, we'll look for the first running server which supports the cookie.
6970 * This allows to share a same cookie between several servers, for example
6971 * to dedicate backup servers to specific servers only.
6972 * However, to prevent clients from sticking to cookie-less backup server
6973 * when they have incidentely learned an empty cookie, we simply ignore
6974 * empty cookies and mark them as invalid.
6975 * The same behaviour is applied when persistence must be ignored.
6976 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006977 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 while (srv) {
6981 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6982 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6983 if ((srv->state & SRV_RUNNING) ||
6984 (t->be->options & PR_O_PERSIST) ||
6985 (t->flags & SN_FORCE_PRST)) {
6986 /* we found the server and we can use it */
6987 txn->flags &= ~TX_CK_MASK;
6988 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6989 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006990 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006991 break;
6992 } else {
6993 /* we found a server, but it's down,
6994 * mark it as such and go on in case
6995 * another one is available.
6996 */
6997 txn->flags &= ~TX_CK_MASK;
6998 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007000 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007001 srv = srv->next;
7002 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003
Willy Tarreauf64d1412010-10-07 20:06:11 +02007004 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007005 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007006 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007007 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7008 txn->flags |= TX_CK_UNUSED;
7009 else
7010 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007011 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007012
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007013 /* depending on the cookie mode, we may have to either :
7014 * - delete the complete cookie if we're in insert+indirect mode, so that
7015 * the server never sees it ;
7016 * - remove the server id from the cookie value, and tag the cookie as an
7017 * application cookie so that it does not get accidentely removed later,
7018 * if we're in cookie prefix mode
7019 */
Willy Tarreau67402132012-05-31 20:40:20 +02007020 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007021 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007022
Willy Tarreau9b28e032012-10-12 23:49:43 +02007023 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007024 val_end += delta;
7025 next += delta;
7026 hdr_end += delta;
7027 hdr_next += delta;
7028 cur_hdr->len += delta;
7029 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007030
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007031 del_from = NULL;
7032 preserve_hdr = 1; /* we want to keep this cookie */
7033 }
7034 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007035 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007036 del_from = prev;
7037 }
7038 } else {
7039 /* This is not our cookie, so we must preserve it. But if we already
7040 * scheduled another cookie for removal, we cannot remove the
7041 * complete header, but we can remove the previous block itself.
7042 */
7043 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007046 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007047 if (att_beg >= del_from)
7048 att_beg += delta;
7049 if (att_end >= del_from)
7050 att_end += delta;
7051 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 val_end += delta;
7053 next += delta;
7054 hdr_end += delta;
7055 hdr_next += delta;
7056 cur_hdr->len += delta;
7057 http_msg_move_end(&txn->req, delta);
7058 prev = del_from;
7059 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 /* Look for the appsession cookie unless persistence must be ignored */
7064 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7065 int cmp_len, value_len;
7066 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007067
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007068 if (t->be->options2 & PR_O2_AS_PFX) {
7069 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7070 value_begin = att_beg + t->be->appsession_name_len;
7071 value_len = val_end - att_beg - t->be->appsession_name_len;
7072 } else {
7073 cmp_len = att_end - att_beg;
7074 value_begin = val_beg;
7075 value_len = val_end - val_beg;
7076 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007077
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 /* let's see if the cookie is our appcookie */
7079 if (cmp_len == t->be->appsession_name_len &&
7080 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7081 manage_client_side_appsession(t, value_begin, value_len);
7082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083 }
7084
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 /* continue with next cookie on this header line */
7086 att_beg = next;
7087 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007089 /* There are no more cookies on this line.
7090 * We may still have one (or several) marked for deletion at the
7091 * end of the line. We must do this now in two ways :
7092 * - if some cookies must be preserved, we only delete from the
7093 * mark to the end of line ;
7094 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007098 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007099 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007100 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101 cur_hdr->len += delta;
7102 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007103 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104
7105 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007106 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7107 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007109 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007111 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007112 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 }
7114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007115 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118}
7119
7120
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121/* Iterate the same filter through all response headers contained in <rtr>.
7122 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7123 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007124int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125{
7126 char term;
7127 char *cur_ptr, *cur_end, *cur_next;
7128 int cur_idx, old_idx, last_hdr;
7129 struct http_txn *txn = &t->txn;
7130 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007131 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132
7133 last_hdr = 0;
7134
Willy Tarreau9b28e032012-10-12 23:49:43 +02007135 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 old_idx = 0;
7137
7138 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007139 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007141 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142 (exp->action == ACT_ALLOW ||
7143 exp->action == ACT_DENY))
7144 return 0;
7145
7146 cur_idx = txn->hdr_idx.v[old_idx].next;
7147 if (!cur_idx)
7148 break;
7149
7150 cur_hdr = &txn->hdr_idx.v[cur_idx];
7151 cur_ptr = cur_next;
7152 cur_end = cur_ptr + cur_hdr->len;
7153 cur_next = cur_end + cur_hdr->cr + 1;
7154
7155 /* Now we have one header between cur_ptr and cur_end,
7156 * and the next header starts at cur_next.
7157 */
7158
7159 /* The annoying part is that pattern matching needs
7160 * that we modify the contents to null-terminate all
7161 * strings before testing them.
7162 */
7163
7164 term = *cur_end;
7165 *cur_end = '\0';
7166
7167 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7168 switch (exp->action) {
7169 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007170 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171 last_hdr = 1;
7172 break;
7173
7174 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 last_hdr = 1;
7177 break;
7178
7179 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007180 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7181 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182 /* FIXME: if the user adds a newline in the replacement, the
7183 * index will not be recalculated for now, and the new line
7184 * will not be counted as a new header.
7185 */
7186
7187 cur_end += delta;
7188 cur_next += delta;
7189 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007190 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191 break;
7192
7193 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007194 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 cur_next += delta;
7196
Willy Tarreaufa355d42009-11-29 18:12:29 +01007197 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7199 txn->hdr_idx.used--;
7200 cur_hdr->len = 0;
7201 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007202 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 break;
7204
7205 }
7206 }
7207 if (cur_end)
7208 *cur_end = term; /* restore the string terminator */
7209
7210 /* keep the link from this header to next one in case of later
7211 * removal of next header.
7212 */
7213 old_idx = cur_idx;
7214 }
7215 return 0;
7216}
7217
7218
7219/* Apply the filter to the status line in the response buffer <rtr>.
7220 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7221 * or -1 if a replacement resulted in an invalid status line.
7222 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007223int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224{
7225 char term;
7226 char *cur_ptr, *cur_end;
7227 int done;
7228 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007229 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230
7231
Willy Tarreau3d300592007-03-18 18:34:41 +01007232 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007234 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 (exp->action == ACT_ALLOW ||
7236 exp->action == ACT_DENY))
7237 return 0;
7238 else if (exp->action == ACT_REMOVE)
7239 return 0;
7240
7241 done = 0;
7242
Willy Tarreau9b28e032012-10-12 23:49:43 +02007243 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007244 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245
7246 /* Now we have the status line between cur_ptr and cur_end */
7247
7248 /* The annoying part is that pattern matching needs
7249 * that we modify the contents to null-terminate all
7250 * strings before testing them.
7251 */
7252
7253 term = *cur_end;
7254 *cur_end = '\0';
7255
7256 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7257 switch (exp->action) {
7258 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007259 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 done = 1;
7261 break;
7262
7263 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007264 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265 done = 1;
7266 break;
7267
7268 case ACT_REPLACE:
7269 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007270 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7271 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 /* FIXME: if the user adds a newline in the replacement, the
7273 * index will not be recalculated for now, and the new line
7274 * will not be counted as a new header.
7275 */
7276
Willy Tarreaufa355d42009-11-29 18:12:29 +01007277 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007278 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007279 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007280 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 cur_ptr, cur_end + 1,
7282 NULL, NULL);
7283 if (unlikely(!cur_end))
7284 return -1;
7285
7286 /* we have a full respnse and we know that we have either a CR
7287 * or an LF at <ptr>.
7288 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007289 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007290 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 /* there is no point trying this regex on headers */
7292 return 1;
7293 }
7294 }
7295 *cur_end = term; /* restore the string terminator */
7296 return done;
7297}
7298
7299
7300
7301/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007302 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7304 * unparsable response.
7305 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007306int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007308 struct http_txn *txn = &s->txn;
7309 struct hdr_exp *exp;
7310
7311 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 int ret;
7313
7314 /*
7315 * The interleaving of transformations and verdicts
7316 * makes it difficult to decide to continue or stop
7317 * the evaluation.
7318 */
7319
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007320 if (txn->flags & TX_SVDENY)
7321 break;
7322
Willy Tarreau3d300592007-03-18 18:34:41 +01007323 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7325 exp->action == ACT_PASS)) {
7326 exp = exp->next;
7327 continue;
7328 }
7329
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007330 /* if this filter had a condition, evaluate it now and skip to
7331 * next filter if the condition does not match.
7332 */
7333 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007334 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007335 ret = acl_pass(ret);
7336 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7337 ret = !ret;
7338 if (!ret)
7339 continue;
7340 }
7341
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007343 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 if (unlikely(ret < 0))
7345 return -1;
7346
7347 if (likely(ret == 0)) {
7348 /* The filter did not match the response, it can be
7349 * iterated through all headers.
7350 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007351 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 }
7354 return 0;
7355}
7356
7357
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007359 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007360 * desirable to call it only when needed. This function is also used when we
7361 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007363void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007364{
7365 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007366 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007367 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007369 char *hdr_beg, *hdr_end, *hdr_next;
7370 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 /* Iterate through the headers.
7373 * we start with the start line.
7374 */
7375 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007376 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377
7378 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7379 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007380 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007381
7382 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007383 hdr_beg = hdr_next;
7384 hdr_end = hdr_beg + cur_hdr->len;
7385 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386
Willy Tarreau24581ba2010-08-31 22:39:35 +02007387 /* We have one full header between hdr_beg and hdr_end, and the
7388 * next header starts at hdr_next. We're only interested in
7389 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007390 */
7391
Willy Tarreau24581ba2010-08-31 22:39:35 +02007392 is_cookie2 = 0;
7393 prev = hdr_beg + 10;
7394 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007395 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007396 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7397 if (!val) {
7398 old_idx = cur_idx;
7399 continue;
7400 }
7401 is_cookie2 = 1;
7402 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 }
7404
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7406 * <prev> points to the colon.
7407 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007408 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409
Willy Tarreau24581ba2010-08-31 22:39:35 +02007410 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7411 * check-cache is enabled) and we are not interested in checking
7412 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007413 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007414 if (t->be->cookie_name == NULL &&
7415 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007416 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 return;
7418
Willy Tarreau24581ba2010-08-31 22:39:35 +02007419 /* OK so now we know we have to process this response cookie.
7420 * The format of the Set-Cookie header is slightly different
7421 * from the format of the Cookie header in that it does not
7422 * support the comma as a cookie delimiter (thus the header
7423 * cannot be folded) because the Expires attribute described in
7424 * the original Netscape's spec may contain an unquoted date
7425 * with a comma inside. We have to live with this because
7426 * many browsers don't support Max-Age and some browsers don't
7427 * support quoted strings. However the Set-Cookie2 header is
7428 * clean.
7429 *
7430 * We have to keep multiple pointers in order to support cookie
7431 * removal at the beginning, middle or end of header without
7432 * corrupting the header (in case of set-cookie2). A special
7433 * pointer, <scav> points to the beginning of the set-cookie-av
7434 * fields after the first semi-colon. The <next> pointer points
7435 * either to the end of line (set-cookie) or next unquoted comma
7436 * (set-cookie2). All of these headers are valid :
7437 *
7438 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7439 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7440 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7441 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7442 * | | | | | | | | | |
7443 * | | | | | | | | +-> next hdr_end <--+
7444 * | | | | | | | +------------> scav
7445 * | | | | | | +--------------> val_end
7446 * | | | | | +--------------------> val_beg
7447 * | | | | +----------------------> equal
7448 * | | | +------------------------> att_end
7449 * | | +----------------------------> att_beg
7450 * | +------------------------------> prev
7451 * +-----------------------------------------> hdr_beg
7452 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 for (; prev < hdr_end; prev = next) {
7455 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
Willy Tarreau24581ba2010-08-31 22:39:35 +02007457 /* find att_beg */
7458 att_beg = prev + 1;
7459 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7460 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007461
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 /* find att_end : this is the first character after the last non
7463 * space before the equal. It may be equal to hdr_end.
7464 */
7465 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007466
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 while (equal < hdr_end) {
7468 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7469 break;
7470 if (http_is_spht[(unsigned char)*equal++])
7471 continue;
7472 att_end = equal;
7473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007474
Willy Tarreau24581ba2010-08-31 22:39:35 +02007475 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7476 * is between <att_beg> and <equal>, both may be identical.
7477 */
7478
7479 /* look for end of cookie if there is an equal sign */
7480 if (equal < hdr_end && *equal == '=') {
7481 /* look for the beginning of the value */
7482 val_beg = equal + 1;
7483 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7484 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485
Willy Tarreau24581ba2010-08-31 22:39:35 +02007486 /* find the end of the value, respecting quotes */
7487 next = find_cookie_value_end(val_beg, hdr_end);
7488
7489 /* make val_end point to the first white space or delimitor after the value */
7490 val_end = next;
7491 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7492 val_end--;
7493 } else {
7494 /* <equal> points to next comma, semi-colon or EOL */
7495 val_beg = val_end = next = equal;
7496 }
7497
7498 if (next < hdr_end) {
7499 /* Set-Cookie2 supports multiple cookies, and <next> points to
7500 * a colon or semi-colon before the end. So skip all attr-value
7501 * pairs and look for the next comma. For Set-Cookie, since
7502 * commas are permitted in values, skip to the end.
7503 */
7504 if (is_cookie2)
7505 next = find_hdr_value_end(next, hdr_end);
7506 else
7507 next = hdr_end;
7508 }
7509
7510 /* Now everything is as on the diagram above */
7511
7512 /* Ignore cookies with no equal sign */
7513 if (equal == val_end)
7514 continue;
7515
7516 /* If there are spaces around the equal sign, we need to
7517 * strip them otherwise we'll get trouble for cookie captures,
7518 * or even for rewrites. Since this happens extremely rarely,
7519 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7522 int stripped_before = 0;
7523 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007526 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007527 equal += stripped_before;
7528 val_beg += stripped_before;
7529 }
7530
7531 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007532 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 val_beg += stripped_after;
7534 stripped_before += stripped_after;
7535 }
7536
7537 val_end += stripped_before;
7538 next += stripped_before;
7539 hdr_end += stripped_before;
7540 hdr_next += stripped_before;
7541 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007542 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007543 }
7544
7545 /* First, let's see if we want to capture this cookie. We check
7546 * that we don't already have a server side cookie, because we
7547 * can only capture one. Also as an optimisation, we ignore
7548 * cookies shorter than the declared name.
7549 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007550 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007551 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007552 (val_end - att_beg >= t->fe->capture_namelen) &&
7553 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7554 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007555 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 Alert("HTTP logging : out of memory.\n");
7557 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007558 else {
7559 if (log_len > t->fe->capture_len)
7560 log_len = t->fe->capture_len;
7561 memcpy(txn->srv_cookie, att_beg, log_len);
7562 txn->srv_cookie[log_len] = 0;
7563 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 }
7565
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007566 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007567 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007568 if (!(t->flags & SN_IGNORE_PRST) &&
7569 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7570 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007571 /* assume passive cookie by default */
7572 txn->flags &= ~TX_SCK_MASK;
7573 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007574
7575 /* If the cookie is in insert mode on a known server, we'll delete
7576 * this occurrence because we'll insert another one later.
7577 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 * a direct access.
7579 */
Willy Tarreau67402132012-05-31 20:40:20 +02007580 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007581 /* The "preserve" flag was set, we don't want to touch the
7582 * server's cookie.
7583 */
7584 }
Willy Tarreau67402132012-05-31 20:40:20 +02007585 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7586 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 /* this cookie must be deleted */
7588 if (*prev == ':' && next == hdr_end) {
7589 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007590 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7592 txn->hdr_idx.used--;
7593 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007594 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007595 hdr_next += delta;
7596 http_msg_move_end(&txn->rsp, delta);
7597 /* note: while both invalid now, <next> and <hdr_end>
7598 * are still equal, so the for() will stop as expected.
7599 */
7600 } else {
7601 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007602 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007603 next = prev;
7604 hdr_end += delta;
7605 hdr_next += delta;
7606 cur_hdr->len += delta;
7607 http_msg_move_end(&txn->rsp, delta);
7608 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007609 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007610 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007611 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007612 }
Willy Tarreau67402132012-05-31 20:40:20 +02007613 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007614 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007615 * with this server since we know it.
7616 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007617 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007618 next += delta;
7619 hdr_end += delta;
7620 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007622 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623
Willy Tarreauf1348312010-10-07 15:54:11 +02007624 txn->flags &= ~TX_SCK_MASK;
7625 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007626 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007627 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007629 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007630 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007631 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 next += delta;
7633 hdr_end += delta;
7634 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007636 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637
Willy Tarreau827aee92011-03-10 16:55:02 +01007638 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007639 txn->flags &= ~TX_SCK_MASK;
7640 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641 }
7642 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007643 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7644 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007645 int cmp_len, value_len;
7646 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647
Cyril Bontéb21570a2009-11-29 20:04:48 +01007648 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007649 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7650 value_begin = att_beg + t->be->appsession_name_len;
7651 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007652 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007653 cmp_len = att_end - att_beg;
7654 value_begin = val_beg;
7655 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007656 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007657
Cyril Bonté17530c32010-04-06 21:11:10 +02007658 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007659 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7660 /* free a possibly previously allocated memory */
7661 pool_free2(apools.sessid, txn->sessid);
7662
Cyril Bontéb21570a2009-11-29 20:04:48 +01007663 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007664 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007665 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7666 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7667 return;
7668 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007669 memcpy(txn->sessid, value_begin, value_len);
7670 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007672 }
7673 /* that's done for this cookie, check the next one on the same
7674 * line when next != hdr_end (only if is_cookie2).
7675 */
7676 }
7677 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007678 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007679 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007680
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007681 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007682 appsess *asession = NULL;
7683 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007684 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007685 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007686 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007687 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7688 Alert("Not enough Memory process_srv():asession:calloc().\n");
7689 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7690 return;
7691 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007692 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7693
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007694 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7695 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7696 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007697 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007698 return;
7699 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007700 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007701 asession->sessid[t->be->appsession_len] = 0;
7702
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007703 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007704 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007705 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007706 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007707 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007708 return;
7709 }
7710 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007711 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007712
7713 asession->request_count = 0;
7714 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7715 }
7716
7717 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7718 asession->request_count++;
7719 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007720}
7721
7722
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723/*
7724 * Check if response is cacheable or not. Updates t->flags.
7725 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007726void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727{
7728 struct http_txn *txn = &t->txn;
7729 char *p1, *p2;
7730
7731 char *cur_ptr, *cur_end, *cur_next;
7732 int cur_idx;
7733
Willy Tarreau5df51872007-11-25 16:20:08 +01007734 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007735 return;
7736
7737 /* Iterate through the headers.
7738 * we start with the start line.
7739 */
7740 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007741 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742
7743 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7744 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007745 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746
7747 cur_hdr = &txn->hdr_idx.v[cur_idx];
7748 cur_ptr = cur_next;
7749 cur_end = cur_ptr + cur_hdr->len;
7750 cur_next = cur_end + cur_hdr->cr + 1;
7751
7752 /* We have one full header between cur_ptr and cur_end, and the
7753 * next header starts at cur_next. We're only interested in
7754 * "Cookie:" headers.
7755 */
7756
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007757 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7758 if (val) {
7759 if ((cur_end - (cur_ptr + val) >= 8) &&
7760 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7761 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7762 return;
7763 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007764 }
7765
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007766 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7767 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007768 continue;
7769
7770 /* OK, right now we know we have a cache-control header at cur_ptr */
7771
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007772 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773
7774 if (p1 >= cur_end) /* no more info */
7775 continue;
7776
7777 /* p1 is at the beginning of the value */
7778 p2 = p1;
7779
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007780 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 p2++;
7782
7783 /* we have a complete value between p1 and p2 */
7784 if (p2 < cur_end && *p2 == '=') {
7785 /* we have something of the form no-cache="set-cookie" */
7786 if ((cur_end - p1 >= 21) &&
7787 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7788 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007789 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 continue;
7791 }
7792
7793 /* OK, so we know that either p2 points to the end of string or to a comma */
7794 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007795 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7797 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7798 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007799 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800 return;
7801 }
7802
7803 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007804 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 continue;
7806 }
7807 }
7808}
7809
7810
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811/*
7812 * Try to retrieve a known appsession in the URI, then the associated server.
7813 * If the server is found, it's assigned to the session.
7814 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007815void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007816{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007817 char *end_params, *first_param, *cur_param, *next_param;
7818 char separator;
7819 int value_len;
7820
7821 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007822
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007823 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007824 (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 +01007825 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007826 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827
Cyril Bontéb21570a2009-11-29 20:04:48 +01007828 first_param = NULL;
7829 switch (mode) {
7830 case PR_O2_AS_M_PP:
7831 first_param = memchr(begin, ';', len);
7832 break;
7833 case PR_O2_AS_M_QS:
7834 first_param = memchr(begin, '?', len);
7835 break;
7836 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007837
Cyril Bontéb21570a2009-11-29 20:04:48 +01007838 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007839 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007841
Cyril Bontéb21570a2009-11-29 20:04:48 +01007842 switch (mode) {
7843 case PR_O2_AS_M_PP:
7844 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7845 end_params = (char *) begin + len;
7846 }
7847 separator = ';';
7848 break;
7849 case PR_O2_AS_M_QS:
7850 end_params = (char *) begin + len;
7851 separator = '&';
7852 break;
7853 default:
7854 /* unknown mode, shouldn't happen */
7855 return;
7856 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007857
Cyril Bontéb21570a2009-11-29 20:04:48 +01007858 cur_param = next_param = end_params;
7859 while (cur_param > first_param) {
7860 cur_param--;
7861 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7862 /* let's see if this is the appsession parameter */
7863 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7864 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7865 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7866 /* Cool... it's the right one */
7867 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7868 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7869 if (value_len > 0) {
7870 manage_client_side_appsession(t, cur_param, value_len);
7871 }
7872 break;
7873 }
7874 next_param = cur_param;
7875 }
7876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007878 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007879 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007880#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007881}
7882
Willy Tarreaub2513902006-12-17 14:52:38 +01007883/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007884 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007885 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007886 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007887 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007888 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007889 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007890 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007891 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007892int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007893{
7894 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007895 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007896 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007897
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007898 if (!uri_auth)
7899 return 0;
7900
Cyril Bonté70be45d2010-10-12 00:14:35 +02007901 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007902 return 0;
7903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007904 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007905 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007906 return 0;
7907
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007908 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007909 return 0;
7910
Willy Tarreaub2513902006-12-17 14:52:38 +01007911 return 1;
7912}
7913
Willy Tarreau4076a152009-04-02 15:18:36 +02007914/*
7915 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007916 * By default it tries to report the error position as msg->err_pos. However if
7917 * this one is not set, it will then report msg->next, which is the last known
7918 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007919 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007920 */
7921void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007922 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007923 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007924{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007925 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007926 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007927
Willy Tarreau9b28e032012-10-12 23:49:43 +02007928 es->len = MIN(chn->buf->i, sizeof(es->buf));
7929 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007930 len1 = MIN(len1, es->len);
7931 len2 = es->len - len1; /* remaining data if buffer wraps */
7932
Willy Tarreau9b28e032012-10-12 23:49:43 +02007933 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007934 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007935 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007936
Willy Tarreau4076a152009-04-02 15:18:36 +02007937 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007938 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007939 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007940 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007941
Willy Tarreau4076a152009-04-02 15:18:36 +02007942 es->when = date; // user-visible date
7943 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007944 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007945 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007946 if (objt_conn(s->req->prod->end))
7947 es->src = __objt_conn(s->req->prod->end)->addr.from;
7948 else
7949 memset(&es->src, 0, sizeof(es->src));
7950
Willy Tarreau078272e2010-12-12 12:46:33 +01007951 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007952 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007953 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007954 es->s_flags = s->flags;
7955 es->t_flags = s->txn.flags;
7956 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007957 es->b_out = chn->buf->o;
7958 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007959 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007960 es->m_clen = msg->chunk_len;
7961 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007962}
Willy Tarreaub2513902006-12-17 14:52:38 +01007963
Willy Tarreau294c4732011-12-16 21:35:50 +01007964/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7965 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7966 * performed over the whole headers. Otherwise it must contain a valid header
7967 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7968 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7969 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7970 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007971 * -1. The value fetch stops at commas, so this function is suited for use with
7972 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007973 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007974 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007975unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007976 struct hdr_idx *idx, int occ,
7977 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007978{
Willy Tarreau294c4732011-12-16 21:35:50 +01007979 struct hdr_ctx local_ctx;
7980 char *ptr_hist[MAX_HDR_HISTORY];
7981 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007982 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007983 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007984
Willy Tarreau294c4732011-12-16 21:35:50 +01007985 if (!ctx) {
7986 local_ctx.idx = 0;
7987 ctx = &local_ctx;
7988 }
7989
Willy Tarreaubce70882009-09-07 11:51:47 +02007990 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007991 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007992 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007993 occ--;
7994 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007995 *vptr = ctx->line + ctx->val;
7996 *vlen = ctx->vlen;
7997 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007998 }
7999 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008000 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008001 }
8002
8003 /* negative occurrence, we scan all the list then walk back */
8004 if (-occ > MAX_HDR_HISTORY)
8005 return 0;
8006
Willy Tarreau294c4732011-12-16 21:35:50 +01008007 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008008 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008009 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8010 len_hist[hist_ptr] = ctx->vlen;
8011 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008012 hist_ptr = 0;
8013 found++;
8014 }
8015 if (-occ > found)
8016 return 0;
8017 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008018 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8019 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8020 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008021 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008022 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008023 if (hist_ptr >= MAX_HDR_HISTORY)
8024 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008025 *vptr = ptr_hist[hist_ptr];
8026 *vlen = len_hist[hist_ptr];
8027 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008028}
8029
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008030/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8031 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8032 * performed over the whole headers. Otherwise it must contain a valid header
8033 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8034 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8035 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8036 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8037 * -1. This function differs from http_get_hdr() in that it only returns full
8038 * line header values and does not stop at commas.
8039 * The return value is 0 if nothing was found, or non-zero otherwise.
8040 */
8041unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8042 struct hdr_idx *idx, int occ,
8043 struct hdr_ctx *ctx, char **vptr, int *vlen)
8044{
8045 struct hdr_ctx local_ctx;
8046 char *ptr_hist[MAX_HDR_HISTORY];
8047 int len_hist[MAX_HDR_HISTORY];
8048 unsigned int hist_ptr;
8049 int found;
8050
8051 if (!ctx) {
8052 local_ctx.idx = 0;
8053 ctx = &local_ctx;
8054 }
8055
8056 if (occ >= 0) {
8057 /* search from the beginning */
8058 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8059 occ--;
8060 if (occ <= 0) {
8061 *vptr = ctx->line + ctx->val;
8062 *vlen = ctx->vlen;
8063 return 1;
8064 }
8065 }
8066 return 0;
8067 }
8068
8069 /* negative occurrence, we scan all the list then walk back */
8070 if (-occ > MAX_HDR_HISTORY)
8071 return 0;
8072
8073 found = hist_ptr = 0;
8074 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8075 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8076 len_hist[hist_ptr] = ctx->vlen;
8077 if (++hist_ptr >= MAX_HDR_HISTORY)
8078 hist_ptr = 0;
8079 found++;
8080 }
8081 if (-occ > found)
8082 return 0;
8083 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8084 * find occurrence -occ, so we have to check [hist_ptr+occ].
8085 */
8086 hist_ptr += occ;
8087 if (hist_ptr >= MAX_HDR_HISTORY)
8088 hist_ptr -= MAX_HDR_HISTORY;
8089 *vptr = ptr_hist[hist_ptr];
8090 *vlen = len_hist[hist_ptr];
8091 return 1;
8092}
8093
Willy Tarreaubaaee002006-06-26 02:48:02 +02008094/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008095 * Print a debug line with a header. Always stop at the first CR or LF char,
8096 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8097 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008098 */
8099void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8100{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008101 int max;
8102 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008103 dir,
8104 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8105 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 +02008106
8107 for (max = 0; start + max < end; max++)
8108 if (start[max] == '\r' || start[max] == '\n')
8109 break;
8110
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008111 UBOUND(max, trash.size - trash.len - 3);
8112 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8113 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008114 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008115}
8116
Willy Tarreau0937bc42009-12-22 15:03:09 +01008117/*
8118 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8119 * the required fields are properly allocated and that we only need to (re)init
8120 * them. This should be used before processing any new request.
8121 */
8122void http_init_txn(struct session *s)
8123{
8124 struct http_txn *txn = &s->txn;
8125 struct proxy *fe = s->fe;
8126
8127 txn->flags = 0;
8128 txn->status = -1;
8129
Willy Tarreauf64d1412010-10-07 20:06:11 +02008130 txn->cookie_first_date = 0;
8131 txn->cookie_last_date = 0;
8132
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008133 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008134 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008135 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008136 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008137 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008138 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008139 txn->req.chunk_len = 0LL;
8140 txn->req.body_len = 0LL;
8141 txn->rsp.chunk_len = 0LL;
8142 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008143 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8144 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008145 txn->req.chn = s->req;
8146 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008147
8148 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008149
8150 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8151 if (fe->options2 & PR_O2_REQBUG_OK)
8152 txn->req.err_pos = -1; /* let buggy requests pass */
8153
Willy Tarreau46023632010-01-07 22:51:47 +01008154 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008155 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8156
Willy Tarreau46023632010-01-07 22:51:47 +01008157 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008158 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8159
8160 if (txn->hdr_idx.v)
8161 hdr_idx_init(&txn->hdr_idx);
8162}
8163
8164/* to be used at the end of a transaction */
8165void http_end_txn(struct session *s)
8166{
8167 struct http_txn *txn = &s->txn;
8168
8169 /* these ones will have been dynamically allocated */
8170 pool_free2(pool2_requri, txn->uri);
8171 pool_free2(pool2_capture, txn->cli_cookie);
8172 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008173 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008174 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008175
William Lallemanda73203e2012-03-12 12:48:57 +01008176 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008177 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008178 txn->uri = NULL;
8179 txn->srv_cookie = NULL;
8180 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008181
8182 if (txn->req.cap) {
8183 struct cap_hdr *h;
8184 for (h = s->fe->req_cap; h; h = h->next)
8185 pool_free2(h->pool, txn->req.cap[h->index]);
8186 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8187 }
8188
8189 if (txn->rsp.cap) {
8190 struct cap_hdr *h;
8191 for (h = s->fe->rsp_cap; h; h = h->next)
8192 pool_free2(h->pool, txn->rsp.cap[h->index]);
8193 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8194 }
8195
Willy Tarreau0937bc42009-12-22 15:03:09 +01008196}
8197
8198/* to be used at the end of a transaction to prepare a new one */
8199void http_reset_txn(struct session *s)
8200{
8201 http_end_txn(s);
8202 http_init_txn(s);
8203
8204 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008205 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008206 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008207 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008208 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008209 /* re-init store persistence */
8210 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008211 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008212
Willy Tarreau0937bc42009-12-22 15:03:09 +01008213 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008214
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008215 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008216
Willy Tarreau739cfba2010-01-25 23:11:14 +01008217 /* We must trim any excess data from the response buffer, because we
8218 * may have blocked an invalid response from a server that we don't
8219 * want to accidentely forward once we disable the analysers, nor do
8220 * we want those data to come along with next response. A typical
8221 * example of such data would be from a buggy server responding to
8222 * a HEAD with some data, or sending more than the advertised
8223 * content-length.
8224 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008225 if (unlikely(s->rep->buf->i))
8226 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008227
Willy Tarreau0937bc42009-12-22 15:03:09 +01008228 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008229 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008230
Willy Tarreaud04e8582010-05-31 12:31:35 +02008231 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008232 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008233
8234 s->req->rex = TICK_ETERNITY;
8235 s->req->wex = TICK_ETERNITY;
8236 s->req->analyse_exp = TICK_ETERNITY;
8237 s->rep->rex = TICK_ETERNITY;
8238 s->rep->wex = TICK_ETERNITY;
8239 s->rep->analyse_exp = TICK_ETERNITY;
8240}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008241
Willy Tarreauff011f22011-01-06 17:51:27 +01008242void free_http_req_rules(struct list *r) {
8243 struct http_req_rule *tr, *pr;
8244
8245 list_for_each_entry_safe(pr, tr, r, list) {
8246 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008247 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008248 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008249
8250 free(pr);
8251 }
8252}
8253
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008254/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008255struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8256{
8257 struct http_req_rule *rule;
8258 int cur_arg;
8259
8260 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8261 if (!rule) {
8262 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008263 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008264 }
8265
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008266 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008267 rule->action = HTTP_REQ_ACT_ALLOW;
8268 cur_arg = 1;
8269 } else if (!strcmp(args[0], "deny")) {
8270 rule->action = HTTP_REQ_ACT_DENY;
8271 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008272 } else if (!strcmp(args[0], "tarpit")) {
8273 rule->action = HTTP_REQ_ACT_TARPIT;
8274 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008275 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008276 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008277 cur_arg = 1;
8278
8279 while(*args[cur_arg]) {
8280 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008281 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008282 cur_arg+=2;
8283 continue;
8284 } else
8285 break;
8286 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008287 } else if (!strcmp(args[0], "set-nice")) {
8288 rule->action = HTTP_REQ_ACT_SET_NICE;
8289 cur_arg = 1;
8290
8291 if (!*args[cur_arg] ||
8292 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8293 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8294 file, linenum, args[0]);
8295 goto out_err;
8296 }
8297 rule->arg.nice = atoi(args[cur_arg]);
8298 if (rule->arg.nice < -1024)
8299 rule->arg.nice = -1024;
8300 else if (rule->arg.nice > 1024)
8301 rule->arg.nice = 1024;
8302 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008303 } else if (!strcmp(args[0], "set-tos")) {
8304#ifdef IP_TOS
8305 char *err;
8306 rule->action = HTTP_REQ_ACT_SET_TOS;
8307 cur_arg = 1;
8308
8309 if (!*args[cur_arg] ||
8310 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8311 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8312 file, linenum, args[0]);
8313 goto out_err;
8314 }
8315
8316 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8317 if (err && *err != '\0') {
8318 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8319 file, linenum, err, args[0]);
8320 goto out_err;
8321 }
8322 cur_arg++;
8323#else
8324 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8325 goto out_err;
8326#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008327 } else if (!strcmp(args[0], "set-mark")) {
8328#ifdef SO_MARK
8329 char *err;
8330 rule->action = HTTP_REQ_ACT_SET_MARK;
8331 cur_arg = 1;
8332
8333 if (!*args[cur_arg] ||
8334 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8335 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8336 file, linenum, args[0]);
8337 goto out_err;
8338 }
8339
8340 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8341 if (err && *err != '\0') {
8342 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8343 file, linenum, err, args[0]);
8344 goto out_err;
8345 }
8346 cur_arg++;
8347 global.last_checks |= LSTCHK_NETADM;
8348#else
8349 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8350 goto out_err;
8351#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008352 } else if (!strcmp(args[0], "set-log-level")) {
8353 rule->action = HTTP_REQ_ACT_SET_LOGL;
8354 cur_arg = 1;
8355
8356 if (!*args[cur_arg] ||
8357 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8358 bad_log_level:
8359 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8360 file, linenum, args[0]);
8361 goto out_err;
8362 }
8363 if (strcmp(args[cur_arg], "silent") == 0)
8364 rule->arg.loglevel = -1;
8365 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8366 goto bad_log_level;
8367 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008368 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8369 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8370 cur_arg = 1;
8371
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008372 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8373 (*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 +01008374 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8375 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008376 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008377 }
8378
8379 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8380 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8381 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008382
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008383 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008384 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8385 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008386 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008387 } else if (strcmp(args[0], "redirect") == 0) {
8388 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008389 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008390
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008391 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008392 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8393 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8394 goto out_err;
8395 }
8396
8397 /* this redirect rule might already contain a parsed condition which
8398 * we'll pass to the http-request rule.
8399 */
8400 rule->action = HTTP_REQ_ACT_REDIR;
8401 rule->arg.redir = redir;
8402 rule->cond = redir->cond;
8403 redir->cond = NULL;
8404 cur_arg = 2;
8405 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008406 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008407 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 +01008408 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008409 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008410 }
8411
8412 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8413 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008414 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008415
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008416 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8417 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8418 file, linenum, args[0], errmsg);
8419 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008420 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008421 }
8422 rule->cond = cond;
8423 }
8424 else if (*args[cur_arg]) {
8425 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8426 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8427 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008428 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008429 }
8430
8431 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008432 out_err:
8433 free(rule);
8434 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008435}
8436
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008437/* parse an "http-respose" rule */
8438struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8439{
8440 struct http_res_rule *rule;
8441 int cur_arg;
8442
8443 rule = calloc(1, sizeof(*rule));
8444 if (!rule) {
8445 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8446 goto out_err;
8447 }
8448
8449 if (!strcmp(args[0], "allow")) {
8450 rule->action = HTTP_RES_ACT_ALLOW;
8451 cur_arg = 1;
8452 } else if (!strcmp(args[0], "deny")) {
8453 rule->action = HTTP_RES_ACT_DENY;
8454 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008455 } else if (!strcmp(args[0], "set-nice")) {
8456 rule->action = HTTP_RES_ACT_SET_NICE;
8457 cur_arg = 1;
8458
8459 if (!*args[cur_arg] ||
8460 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8461 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8462 file, linenum, args[0]);
8463 goto out_err;
8464 }
8465 rule->arg.nice = atoi(args[cur_arg]);
8466 if (rule->arg.nice < -1024)
8467 rule->arg.nice = -1024;
8468 else if (rule->arg.nice > 1024)
8469 rule->arg.nice = 1024;
8470 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008471 } else if (!strcmp(args[0], "set-tos")) {
8472#ifdef IP_TOS
8473 char *err;
8474 rule->action = HTTP_RES_ACT_SET_TOS;
8475 cur_arg = 1;
8476
8477 if (!*args[cur_arg] ||
8478 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8479 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8480 file, linenum, args[0]);
8481 goto out_err;
8482 }
8483
8484 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8485 if (err && *err != '\0') {
8486 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8487 file, linenum, err, args[0]);
8488 goto out_err;
8489 }
8490 cur_arg++;
8491#else
8492 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8493 goto out_err;
8494#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008495 } else if (!strcmp(args[0], "set-mark")) {
8496#ifdef SO_MARK
8497 char *err;
8498 rule->action = HTTP_RES_ACT_SET_MARK;
8499 cur_arg = 1;
8500
8501 if (!*args[cur_arg] ||
8502 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8503 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8504 file, linenum, args[0]);
8505 goto out_err;
8506 }
8507
8508 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8509 if (err && *err != '\0') {
8510 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8511 file, linenum, err, args[0]);
8512 goto out_err;
8513 }
8514 cur_arg++;
8515 global.last_checks |= LSTCHK_NETADM;
8516#else
8517 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8518 goto out_err;
8519#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008520 } else if (!strcmp(args[0], "set-log-level")) {
8521 rule->action = HTTP_RES_ACT_SET_LOGL;
8522 cur_arg = 1;
8523
8524 if (!*args[cur_arg] ||
8525 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8526 bad_log_level:
8527 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8528 file, linenum, args[0]);
8529 goto out_err;
8530 }
8531 if (strcmp(args[cur_arg], "silent") == 0)
8532 rule->arg.loglevel = -1;
8533 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8534 goto bad_log_level;
8535 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008536 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8537 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8538 cur_arg = 1;
8539
8540 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8541 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8542 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8543 file, linenum, args[0]);
8544 goto out_err;
8545 }
8546
8547 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8548 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8549 LIST_INIT(&rule->arg.hdr_add.fmt);
8550
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008551 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008552 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8553 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8554 cur_arg += 2;
8555 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008556 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 +02008557 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8558 goto out_err;
8559 }
8560
8561 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8562 struct acl_cond *cond;
8563 char *errmsg = NULL;
8564
8565 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8566 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8567 file, linenum, args[0], errmsg);
8568 free(errmsg);
8569 goto out_err;
8570 }
8571 rule->cond = cond;
8572 }
8573 else if (*args[cur_arg]) {
8574 Alert("parsing [%s:%d]: 'http-response %s' expects"
8575 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8576 file, linenum, args[0], args[cur_arg]);
8577 goto out_err;
8578 }
8579
8580 return rule;
8581 out_err:
8582 free(rule);
8583 return NULL;
8584}
8585
Willy Tarreau4baae242012-12-27 12:00:31 +01008586/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008587 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8588 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008589 */
8590struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008591 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008592{
8593 struct redirect_rule *rule;
8594 int cur_arg;
8595 int type = REDIRECT_TYPE_NONE;
8596 int code = 302;
8597 const char *destination = NULL;
8598 const char *cookie = NULL;
8599 int cookie_set = 0;
8600 unsigned int flags = REDIRECT_FLAG_NONE;
8601 struct acl_cond *cond = NULL;
8602
8603 cur_arg = 0;
8604 while (*(args[cur_arg])) {
8605 if (strcmp(args[cur_arg], "location") == 0) {
8606 if (!*args[cur_arg + 1])
8607 goto missing_arg;
8608
8609 type = REDIRECT_TYPE_LOCATION;
8610 cur_arg++;
8611 destination = args[cur_arg];
8612 }
8613 else if (strcmp(args[cur_arg], "prefix") == 0) {
8614 if (!*args[cur_arg + 1])
8615 goto missing_arg;
8616
8617 type = REDIRECT_TYPE_PREFIX;
8618 cur_arg++;
8619 destination = args[cur_arg];
8620 }
8621 else if (strcmp(args[cur_arg], "scheme") == 0) {
8622 if (!*args[cur_arg + 1])
8623 goto missing_arg;
8624
8625 type = REDIRECT_TYPE_SCHEME;
8626 cur_arg++;
8627 destination = args[cur_arg];
8628 }
8629 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8630 if (!*args[cur_arg + 1])
8631 goto missing_arg;
8632
8633 cur_arg++;
8634 cookie = args[cur_arg];
8635 cookie_set = 1;
8636 }
8637 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8638 if (!*args[cur_arg + 1])
8639 goto missing_arg;
8640
8641 cur_arg++;
8642 cookie = args[cur_arg];
8643 cookie_set = 0;
8644 }
8645 else if (strcmp(args[cur_arg], "code") == 0) {
8646 if (!*args[cur_arg + 1])
8647 goto missing_arg;
8648
8649 cur_arg++;
8650 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008651 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008652 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008653 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008654 args[cur_arg - 1], args[cur_arg]);
8655 return NULL;
8656 }
8657 }
8658 else if (!strcmp(args[cur_arg],"drop-query")) {
8659 flags |= REDIRECT_FLAG_DROP_QS;
8660 }
8661 else if (!strcmp(args[cur_arg],"append-slash")) {
8662 flags |= REDIRECT_FLAG_APPEND_SLASH;
8663 }
8664 else if (strcmp(args[cur_arg], "if") == 0 ||
8665 strcmp(args[cur_arg], "unless") == 0) {
8666 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8667 if (!cond) {
8668 memprintf(errmsg, "error in condition: %s", *errmsg);
8669 return NULL;
8670 }
8671 break;
8672 }
8673 else {
8674 memprintf(errmsg,
8675 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8676 args[cur_arg]);
8677 return NULL;
8678 }
8679 cur_arg++;
8680 }
8681
8682 if (type == REDIRECT_TYPE_NONE) {
8683 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8684 return NULL;
8685 }
8686
8687 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8688 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008689 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008690
8691 if (!use_fmt) {
8692 /* old-style static redirect rule */
8693 rule->rdr_str = strdup(destination);
8694 rule->rdr_len = strlen(destination);
8695 }
8696 else {
8697 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008698
8699 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8700 * if prefix == "/", we don't want to add anything, otherwise it
8701 * makes it hard for the user to configure a self-redirection.
8702 */
8703 proxy->conf.args.ctx = ARGC_RDR;
8704 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8705 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8706 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8707 }
8708 }
8709
Willy Tarreau4baae242012-12-27 12:00:31 +01008710 if (cookie) {
8711 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8712 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8713 */
8714 rule->cookie_len = strlen(cookie);
8715 if (cookie_set) {
8716 rule->cookie_str = malloc(rule->cookie_len + 10);
8717 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8718 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8719 rule->cookie_len += 9;
8720 } else {
8721 rule->cookie_str = malloc(rule->cookie_len + 21);
8722 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8723 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8724 rule->cookie_len += 20;
8725 }
8726 }
8727 rule->type = type;
8728 rule->code = code;
8729 rule->flags = flags;
8730 LIST_INIT(&rule->list);
8731 return rule;
8732
8733 missing_arg:
8734 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8735 return NULL;
8736}
8737
Willy Tarreau8797c062007-05-07 00:55:35 +02008738/************************************************************************/
8739/* The code below is dedicated to ACL parsing and matching */
8740/************************************************************************/
8741
8742
Willy Tarreau14174bc2012-04-16 14:34:04 +02008743/* This function ensures that the prerequisites for an L7 fetch are ready,
8744 * which means that a request or response is ready. If some data is missing,
8745 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008746 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8747 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008748 *
8749 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008750 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8751 * decide whether or not an HTTP message is present ;
8752 * 0 if the requested data cannot be fetched or if it is certain that
8753 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008754 * 1 if an HTTP message is ready
8755 */
8756static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008757smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008758 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008759{
8760 struct http_txn *txn = l7;
8761 struct http_msg *msg = &txn->req;
8762
8763 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8764 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8765 */
8766
8767 if (unlikely(!s || !txn))
8768 return 0;
8769
8770 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008771 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008772
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008773 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008774 if (unlikely(!s->req))
8775 return 0;
8776
Willy Tarreauaae75e32013-03-29 12:31:49 +01008777 /* If the buffer does not leave enough free space at the end,
8778 * we must first realign it.
8779 */
8780 if (s->req->buf->p > s->req->buf->data &&
8781 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8782 buffer_slow_realign(s->req->buf);
8783
Willy Tarreau14174bc2012-04-16 14:34:04 +02008784 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008785 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008786 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008787
8788 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008789 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008790 http_msg_analyzer(msg, &txn->hdr_idx);
8791
8792 /* Still no valid request ? */
8793 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008794 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008795 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008796 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008797 }
8798 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008799 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008800 return 0;
8801 }
8802
8803 /* OK we just got a valid HTTP request. We have some minor
8804 * preparation to perform so that further checks can rely
8805 * on HTTP tests.
8806 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008807
8808 /* If the request was parsed but was too large, we must absolutely
8809 * return an error so that it is not processed. At the moment this
8810 * cannot happen, but if the parsers are to change in the future,
8811 * we want this check to be maintained.
8812 */
8813 if (unlikely(s->req->buf->i + s->req->buf->p >
8814 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8815 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008816 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008817 return 1;
8818 }
8819
Willy Tarreau9b28e032012-10-12 23:49:43 +02008820 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008821 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8822 s->flags |= SN_REDIRECTABLE;
8823
Willy Tarreau506d0502013-07-06 13:29:24 +02008824 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8825 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008826 }
8827
Willy Tarreau506d0502013-07-06 13:29:24 +02008828 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008829 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008830 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008831
8832 /* otherwise everything's ready for the request */
8833 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008834 else {
8835 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008836 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8837 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008838 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008839 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008840 }
8841
8842 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008843 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008844 return 1;
8845}
Willy Tarreau8797c062007-05-07 00:55:35 +02008846
Willy Tarreauc0239e02012-04-16 14:42:55 +02008847#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008848 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 +02008849
Willy Tarreau24e32d82012-04-23 23:55:44 +02008850#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008851 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 +02008852
Willy Tarreau8797c062007-05-07 00:55:35 +02008853
8854/* 1. Check on METHOD
8855 * We use the pre-parsed method if it is known, and store its number as an
8856 * integer. If it is unknown, we use the pointer and the length.
8857 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008858static 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 +02008859{
8860 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008861 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008862
Willy Tarreauae8b7962007-06-09 23:10:04 +02008863 len = strlen(*text);
8864 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008865
8866 pattern->val.i = meth;
8867 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008868 if (usage == PAT_U_COMPILE) {
8869 pattern->ptr.str = strdup(*text);
8870 if (!pattern->ptr.str) {
8871 memprintf(err, "out of memory while loading pattern");
8872 return 0;
8873 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008874 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008875 else {
8876 trash = get_trash_chunk();
8877 if (trash->size < len) {
8878 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8879 len, trash->size);
8880 return 0;
8881 }
8882 pattern->ptr.str = trash->str;
8883 }
8884 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008885 pattern->len = len;
8886 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008887 else
8888 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008889 return 1;
8890}
8891
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008892/* This function fetches the method of current HTTP request and stores
8893 * it in the global pattern struct as a chunk. There are two possibilities :
8894 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8895 * in <len> and <ptr> is NULL ;
8896 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8897 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008898 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008899 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008900static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008901smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008902 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008903{
8904 int meth;
8905 struct http_txn *txn = l7;
8906
Willy Tarreau24e32d82012-04-23 23:55:44 +02008907 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008908
Willy Tarreau8797c062007-05-07 00:55:35 +02008909 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008910 smp->type = SMP_T_UINT;
8911 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008912 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008913 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8914 /* ensure the indexes are not affected */
8915 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008916 smp->type = SMP_T_CSTR;
8917 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008918 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008919 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008920 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008921 return 1;
8922}
8923
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008924/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008925static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008926{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008927 int icase;
8928
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008929
Willy Tarreauf853c462012-04-23 18:53:56 +02008930 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008931 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008932 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008933 return PAT_MATCH;
8934 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008935 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008936
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008937 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8938 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008939 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008940
8941 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008942 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008943 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008944
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008945 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008946 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8947 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008948 return PAT_NOMATCH;
8949 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008950}
8951
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008952static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008953smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008954 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008955{
8956 struct http_txn *txn = l7;
8957 char *ptr;
8958 int len;
8959
Willy Tarreauc0239e02012-04-16 14:42:55 +02008960 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008961
Willy Tarreau8797c062007-05-07 00:55:35 +02008962 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008963 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008964
8965 while ((len-- > 0) && (*ptr++ != '/'));
8966 if (len <= 0)
8967 return 0;
8968
Willy Tarreauf853c462012-04-23 18:53:56 +02008969 smp->type = SMP_T_CSTR;
8970 smp->data.str.str = ptr;
8971 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008972
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008973 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008974 return 1;
8975}
8976
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008977static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008978smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008979 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008980{
8981 struct http_txn *txn = l7;
8982 char *ptr;
8983 int len;
8984
Willy Tarreauc0239e02012-04-16 14:42:55 +02008985 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008986
Willy Tarreauf26b2522012-12-14 08:33:14 +01008987 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8988 return 0;
8989
Willy Tarreau8797c062007-05-07 00:55:35 +02008990 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008991 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008992
8993 while ((len-- > 0) && (*ptr++ != '/'));
8994 if (len <= 0)
8995 return 0;
8996
Willy Tarreauf853c462012-04-23 18:53:56 +02008997 smp->type = SMP_T_CSTR;
8998 smp->data.str.str = ptr;
8999 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009000
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009001 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009002 return 1;
9003}
9004
9005/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009006static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009007smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009008 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009009{
9010 struct http_txn *txn = l7;
9011 char *ptr;
9012 int len;
9013
Willy Tarreauc0239e02012-04-16 14:42:55 +02009014 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009015
Willy Tarreauf26b2522012-12-14 08:33:14 +01009016 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9017 return 0;
9018
Willy Tarreau8797c062007-05-07 00:55:35 +02009019 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009020 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009021
Willy Tarreauf853c462012-04-23 18:53:56 +02009022 smp->type = SMP_T_UINT;
9023 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009024 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009025 return 1;
9026}
9027
9028/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009029static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009030smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009031 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009032{
9033 struct http_txn *txn = l7;
9034
Willy Tarreauc0239e02012-04-16 14:42:55 +02009035 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009036
Willy Tarreauf853c462012-04-23 18:53:56 +02009037 smp->type = SMP_T_CSTR;
9038 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009039 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009040 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009041 return 1;
9042}
9043
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009044static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009045smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009046 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009047{
9048 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009049 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009050
Willy Tarreauc0239e02012-04-16 14:42:55 +02009051 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009052
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009053 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9054 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009055 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009056
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009057 smp->type = SMP_T_IPV4;
9058 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009059 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009060 return 1;
9061}
9062
9063static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009064smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009065 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009066{
9067 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009068 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009069
Willy Tarreauc0239e02012-04-16 14:42:55 +02009070 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009071
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009072 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9073 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9074 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009075
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009076 smp->type = SMP_T_UINT;
9077 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009078 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009079 return 1;
9080}
9081
Willy Tarreau185b5c42012-04-26 15:11:51 +02009082/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9083 * Accepts an optional argument of type string containing the header field name,
9084 * and an optional argument of type signed or unsigned integer to request an
9085 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009086 * headers are considered from the first one. It does not stop on commas and
9087 * returns full lines instead (useful for User-Agent or Date for example).
9088 */
9089static int
9090smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009091 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009092{
9093 struct http_txn *txn = l7;
9094 struct hdr_idx *idx = &txn->hdr_idx;
9095 struct hdr_ctx *ctx = smp->ctx.a[0];
9096 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9097 int occ = 0;
9098 const char *name_str = NULL;
9099 int name_len = 0;
9100
9101 if (!ctx) {
9102 /* first call */
9103 ctx = &static_hdr_ctx;
9104 ctx->idx = 0;
9105 smp->ctx.a[0] = ctx;
9106 }
9107
9108 if (args) {
9109 if (args[0].type != ARGT_STR)
9110 return 0;
9111 name_str = args[0].data.str.str;
9112 name_len = args[0].data.str.len;
9113
9114 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9115 occ = args[1].data.uint;
9116 }
9117
9118 CHECK_HTTP_MESSAGE_FIRST();
9119
9120 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9121 /* search for header from the beginning */
9122 ctx->idx = 0;
9123
9124 if (!occ && !(opt & SMP_OPT_ITERATE))
9125 /* no explicit occurrence and single fetch => last header by default */
9126 occ = -1;
9127
9128 if (!occ)
9129 /* prepare to report multiple occurrences for ACL fetches */
9130 smp->flags |= SMP_F_NOT_LAST;
9131
9132 smp->type = SMP_T_CSTR;
9133 smp->flags |= SMP_F_VOL_HDR;
9134 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9135 return 1;
9136
9137 smp->flags &= ~SMP_F_NOT_LAST;
9138 return 0;
9139}
9140
9141/* 6. Check on HTTP header count. The number of occurrences is returned.
9142 * Accepts exactly 1 argument of type string. It does not stop on commas and
9143 * returns full lines instead (useful for User-Agent or Date for example).
9144 */
9145static int
9146smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009147 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009148{
9149 struct http_txn *txn = l7;
9150 struct hdr_idx *idx = &txn->hdr_idx;
9151 struct hdr_ctx ctx;
9152 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9153 int cnt;
9154
9155 if (!args || args->type != ARGT_STR)
9156 return 0;
9157
9158 CHECK_HTTP_MESSAGE_FIRST();
9159
9160 ctx.idx = 0;
9161 cnt = 0;
9162 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9163 cnt++;
9164
9165 smp->type = SMP_T_UINT;
9166 smp->data.uint = cnt;
9167 smp->flags = SMP_F_VOL_HDR;
9168 return 1;
9169}
9170
9171/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9172 * Accepts an optional argument of type string containing the header field name,
9173 * and an optional argument of type signed or unsigned integer to request an
9174 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009175 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009176 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009177static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009178smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009179 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009180{
9181 struct http_txn *txn = l7;
9182 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009183 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009184 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009185 int occ = 0;
9186 const char *name_str = NULL;
9187 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009188
Willy Tarreaua890d072013-04-02 12:01:06 +02009189 if (!ctx) {
9190 /* first call */
9191 ctx = &static_hdr_ctx;
9192 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009193 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009194 }
9195
Willy Tarreau185b5c42012-04-26 15:11:51 +02009196 if (args) {
9197 if (args[0].type != ARGT_STR)
9198 return 0;
9199 name_str = args[0].data.str.str;
9200 name_len = args[0].data.str.len;
9201
9202 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9203 occ = args[1].data.uint;
9204 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009205
Willy Tarreaue333ec92012-04-16 16:26:40 +02009206 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009207
Willy Tarreau185b5c42012-04-26 15:11:51 +02009208 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009209 /* search for header from the beginning */
9210 ctx->idx = 0;
9211
Willy Tarreau185b5c42012-04-26 15:11:51 +02009212 if (!occ && !(opt & SMP_OPT_ITERATE))
9213 /* no explicit occurrence and single fetch => last header by default */
9214 occ = -1;
9215
9216 if (!occ)
9217 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009218 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009219
Willy Tarreau185b5c42012-04-26 15:11:51 +02009220 smp->type = SMP_T_CSTR;
9221 smp->flags |= SMP_F_VOL_HDR;
9222 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 +02009223 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009224
Willy Tarreau37406352012-04-23 16:16:37 +02009225 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009226 return 0;
9227}
9228
Willy Tarreauc11416f2007-06-17 16:58:38 +02009229/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009230 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009231 */
9232static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009233smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009234 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009235{
9236 struct http_txn *txn = l7;
9237 struct hdr_idx *idx = &txn->hdr_idx;
9238 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009239 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009240 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009241
Willy Tarreau24e32d82012-04-23 23:55:44 +02009242 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009243 return 0;
9244
Willy Tarreaue333ec92012-04-16 16:26:40 +02009245 CHECK_HTTP_MESSAGE_FIRST();
9246
Willy Tarreau33a7e692007-06-10 19:45:56 +02009247 ctx.idx = 0;
9248 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009249 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 +02009250 cnt++;
9251
Willy Tarreauf853c462012-04-23 18:53:56 +02009252 smp->type = SMP_T_UINT;
9253 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009254 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009255 return 1;
9256}
9257
Willy Tarreau185b5c42012-04-26 15:11:51 +02009258/* Fetch an HTTP header's integer value. The integer value is returned. It
9259 * takes a mandatory argument of type string and an optional one of type int
9260 * to designate a specific occurrence. It returns an unsigned integer, which
9261 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009262 */
9263static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009264smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009265 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009266{
Willy Tarreauef38c392013-07-22 16:29:32 +02009267 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009268
Willy Tarreauf853c462012-04-23 18:53:56 +02009269 if (ret > 0) {
9270 smp->type = SMP_T_UINT;
9271 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9272 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009273
Willy Tarreaud53e2422012-04-16 17:21:11 +02009274 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009275}
9276
Cyril Bonté69fa9922012-10-25 00:01:06 +02009277/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9278 * and an optional one of type int to designate a specific occurrence.
9279 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009280 */
9281static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009282smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009283 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009284{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009285 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009286
Willy Tarreauef38c392013-07-22 16:29:32 +02009287 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009288 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9289 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009290 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009291 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009292 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009293 if (smp->data.str.len < temp->size - 1) {
9294 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9295 temp->str[smp->data.str.len] = '\0';
9296 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9297 smp->type = SMP_T_IPV6;
9298 break;
9299 }
9300 }
9301 }
9302
Willy Tarreaud53e2422012-04-16 17:21:11 +02009303 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009304 if (!(smp->flags & SMP_F_NOT_LAST))
9305 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009306 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009307 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009308}
9309
Willy Tarreau737b0c12007-06-10 21:28:46 +02009310/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9311 * the first '/' after the possible hostname, and ends before the possible '?'.
9312 */
9313static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009314smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009315 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009316{
9317 struct http_txn *txn = l7;
9318 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009319
Willy Tarreauc0239e02012-04-16 14:42:55 +02009320 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009321
Willy Tarreau9b28e032012-10-12 23:49:43 +02009322 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009323 ptr = http_get_path(txn);
9324 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009325 return 0;
9326
9327 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009328 smp->type = SMP_T_CSTR;
9329 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009330
9331 while (ptr < end && *ptr != '?')
9332 ptr++;
9333
Willy Tarreauf853c462012-04-23 18:53:56 +02009334 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009335 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009336 return 1;
9337}
9338
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009339/* This produces a concatenation of the first occurrence of the Host header
9340 * followed by the path component if it begins with a slash ('/'). This means
9341 * that '*' will not be added, resulting in exactly the first Host entry.
9342 * If no Host header is found, then the path is returned as-is. The returned
9343 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009344 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009345 */
9346static int
9347smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009348 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009349{
9350 struct http_txn *txn = l7;
9351 char *ptr, *end, *beg;
9352 struct hdr_ctx ctx;
9353
9354 CHECK_HTTP_MESSAGE_FIRST();
9355
9356 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009357 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 +02009358 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009359 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009360
9361 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009362 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009363 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009364 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009365 smp->data.str.len = ctx.vlen;
9366
9367 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009368 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 +02009369 beg = http_get_path(txn);
9370 if (!beg)
9371 beg = end;
9372
9373 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9374
9375 if (beg < ptr && *beg == '/') {
9376 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9377 smp->data.str.len += ptr - beg;
9378 }
9379
9380 smp->flags = SMP_F_VOL_1ST;
9381 return 1;
9382}
9383
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009384/* This produces a 32-bit hash of the concatenation of the first occurrence of
9385 * the Host header followed by the path component if it begins with a slash ('/').
9386 * This means that '*' will not be added, resulting in exactly the first Host
9387 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009388 * is hashed using the path hash followed by a full avalanche hash and provides a
9389 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009390 * high-traffic sites without having to store whole paths.
9391 */
9392static int
9393smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009394 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009395{
9396 struct http_txn *txn = l7;
9397 struct hdr_ctx ctx;
9398 unsigned int hash = 0;
9399 char *ptr, *beg, *end;
9400 int len;
9401
9402 CHECK_HTTP_MESSAGE_FIRST();
9403
9404 ctx.idx = 0;
9405 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9406 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9407 ptr = ctx.line + ctx.val;
9408 len = ctx.vlen;
9409 while (len--)
9410 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9411 }
9412
9413 /* now retrieve the path */
9414 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9415 beg = http_get_path(txn);
9416 if (!beg)
9417 beg = end;
9418
9419 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9420
9421 if (beg < ptr && *beg == '/') {
9422 while (beg < ptr)
9423 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9424 }
9425 hash = full_hash(hash);
9426
9427 smp->type = SMP_T_UINT;
9428 smp->data.uint = hash;
9429 smp->flags = SMP_F_VOL_1ST;
9430 return 1;
9431}
9432
Willy Tarreau4a550602012-12-09 14:53:32 +01009433/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009434 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9435 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9436 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009437 * that in environments where IPv6 is insignificant, truncating the output to
9438 * 8 bytes would still work.
9439 */
9440static int
9441smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009442 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009443{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009444 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009445 struct connection *cli_conn = objt_conn(l4->si[0].end);
9446
9447 if (!cli_conn)
9448 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009449
Willy Tarreauef38c392013-07-22 16:29:32 +02009450 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009451 return 0;
9452
Willy Tarreau47ca5452012-12-23 20:22:19 +01009453 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009454 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9455 temp->len += sizeof(smp->data.uint);
9456
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009457 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009458 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009459 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009460 temp->len += 4;
9461 break;
9462 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009463 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009464 temp->len += 16;
9465 break;
9466 default:
9467 return 0;
9468 }
9469
9470 smp->data.str = *temp;
9471 smp->type = SMP_T_BIN;
9472 return 1;
9473}
9474
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009475static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009476smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009477 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009478{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009479 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9480 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9481 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009482
Willy Tarreau24e32d82012-04-23 23:55:44 +02009483 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009484
Willy Tarreauf853c462012-04-23 18:53:56 +02009485 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009486 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009487 return 1;
9488}
9489
Willy Tarreau7f18e522010-10-22 20:04:13 +02009490/* return a valid test if the current request is the first one on the connection */
9491static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009492smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009493 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009494{
9495 if (!s)
9496 return 0;
9497
Willy Tarreauf853c462012-04-23 18:53:56 +02009498 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009499 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009500 return 1;
9501}
9502
Willy Tarreau34db1082012-04-19 17:16:54 +02009503/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009504static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009505smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009506 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009507{
9508
Willy Tarreau24e32d82012-04-23 23:55:44 +02009509 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009510 return 0;
9511
Willy Tarreauc0239e02012-04-16 14:42:55 +02009512 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009513
Willy Tarreauc0239e02012-04-16 14:42:55 +02009514 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009515 return 0;
9516
Willy Tarreauf853c462012-04-23 18:53:56 +02009517 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009518 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 +01009519 return 1;
9520}
Willy Tarreau8797c062007-05-07 00:55:35 +02009521
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009522/* Accepts exactly 1 argument of type userlist */
9523static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009524smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009525 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009526{
9527
9528 if (!args || args->type != ARGT_USR)
9529 return 0;
9530
9531 CHECK_HTTP_MESSAGE_FIRST();
9532
9533 if (!get_http_auth(l4))
9534 return 0;
9535
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009536 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009537 smp->ctx.a[0] = args->data.usr; /* user list */
9538 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9539 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9540
9541 /* if the user does not belong to the userlist or has a wrong password,
9542 * report that it unconditionally does not match. Otherwise we return
9543 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009544 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009545 */
9546 smp->type = SMP_T_BOOL;
9547 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9548 if (smp->data.uint)
9549 smp->type = SMP_T_UINT;
9550
9551 return 1;
9552}
9553
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009554/* Try to find the next occurrence of a cookie name in a cookie header value.
9555 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9556 * the cookie value is returned into *value and *value_l, and the function
9557 * returns a pointer to the next pointer to search from if the value was found.
9558 * Otherwise if the cookie was not found, NULL is returned and neither value
9559 * nor value_l are touched. The input <hdr> string should first point to the
9560 * header's value, and the <hdr_end> pointer must point to the first character
9561 * not part of the value. <list> must be non-zero if value may represent a list
9562 * of values (cookie headers). This makes it faster to abort parsing when no
9563 * list is expected.
9564 */
9565static char *
9566extract_cookie_value(char *hdr, const char *hdr_end,
9567 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009568 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009569{
9570 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9571 char *next;
9572
9573 /* we search at least a cookie name followed by an equal, and more
9574 * generally something like this :
9575 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9576 */
9577 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9578 /* Iterate through all cookies on this line */
9579
9580 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9581 att_beg++;
9582
9583 /* find att_end : this is the first character after the last non
9584 * space before the equal. It may be equal to hdr_end.
9585 */
9586 equal = att_end = att_beg;
9587
9588 while (equal < hdr_end) {
9589 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9590 break;
9591 if (http_is_spht[(unsigned char)*equal++])
9592 continue;
9593 att_end = equal;
9594 }
9595
9596 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9597 * is between <att_beg> and <equal>, both may be identical.
9598 */
9599
9600 /* look for end of cookie if there is an equal sign */
9601 if (equal < hdr_end && *equal == '=') {
9602 /* look for the beginning of the value */
9603 val_beg = equal + 1;
9604 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9605 val_beg++;
9606
9607 /* find the end of the value, respecting quotes */
9608 next = find_cookie_value_end(val_beg, hdr_end);
9609
9610 /* make val_end point to the first white space or delimitor after the value */
9611 val_end = next;
9612 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9613 val_end--;
9614 } else {
9615 val_beg = val_end = next = equal;
9616 }
9617
9618 /* We have nothing to do with attributes beginning with '$'. However,
9619 * they will automatically be removed if a header before them is removed,
9620 * since they're supposed to be linked together.
9621 */
9622 if (*att_beg == '$')
9623 continue;
9624
9625 /* Ignore cookies with no equal sign */
9626 if (equal == next)
9627 continue;
9628
9629 /* Now we have the cookie name between att_beg and att_end, and
9630 * its value between val_beg and val_end.
9631 */
9632
9633 if (att_end - att_beg == cookie_name_l &&
9634 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9635 /* let's return this value and indicate where to go on from */
9636 *value = val_beg;
9637 *value_l = val_end - val_beg;
9638 return next + 1;
9639 }
9640
9641 /* Set-Cookie headers only have the name in the first attr=value part */
9642 if (!list)
9643 break;
9644 }
9645
9646 return NULL;
9647}
9648
William Lallemanda43ba4e2014-01-28 18:14:25 +01009649/* Fetch a captured HTTP request header. The index is the position of
9650 * the "capture" option in the configuration file
9651 */
9652static int
9653smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9654 const struct arg *args, struct sample *smp, const char *kw)
9655{
9656 struct proxy *fe = l4->fe;
9657 struct http_txn *txn = l7;
9658 int idx;
9659
9660 if (!args || args->type != ARGT_UINT)
9661 return 0;
9662
9663 idx = args->data.uint;
9664
9665 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9666 return 0;
9667
9668 smp->type = SMP_T_CSTR;
9669 smp->data.str.str = txn->req.cap[idx];
9670 smp->data.str.len = strlen(txn->req.cap[idx]);
9671
9672 return 1;
9673}
9674
9675/* Fetch a captured HTTP response header. The index is the position of
9676 * the "capture" option in the configuration file
9677 */
9678static int
9679smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9680 const struct arg *args, struct sample *smp, const char *kw)
9681{
9682 struct proxy *fe = l4->fe;
9683 struct http_txn *txn = l7;
9684 int idx;
9685
9686 if (!args || args->type != ARGT_UINT)
9687 return 0;
9688
9689 idx = args->data.uint;
9690
9691 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9692 return 0;
9693
9694 smp->type = SMP_T_CSTR;
9695 smp->data.str.str = txn->rsp.cap[idx];
9696 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9697
9698 return 1;
9699}
9700
Willy Tarreaue333ec92012-04-16 16:26:40 +02009701/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009702 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009703 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009704 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009705 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009706 * Accepts exactly 1 argument of type string. If the input options indicate
9707 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009708 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009709 */
9710static int
Willy Tarreau51539362012-05-08 12:46:28 +02009711smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009712 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009713{
9714 struct http_txn *txn = l7;
9715 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009716 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009717 const struct http_msg *msg;
9718 const char *hdr_name;
9719 int hdr_name_len;
9720 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009721 int occ = 0;
9722 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009723
Willy Tarreau24e32d82012-04-23 23:55:44 +02009724 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009725 return 0;
9726
Willy Tarreaua890d072013-04-02 12:01:06 +02009727 if (!ctx) {
9728 /* first call */
9729 ctx = &static_hdr_ctx;
9730 ctx->idx = 0;
9731 smp->ctx.a[2] = ctx;
9732 }
9733
Willy Tarreaue333ec92012-04-16 16:26:40 +02009734 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009735
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009736 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009737 msg = &txn->req;
9738 hdr_name = "Cookie";
9739 hdr_name_len = 6;
9740 } else {
9741 msg = &txn->rsp;
9742 hdr_name = "Set-Cookie";
9743 hdr_name_len = 10;
9744 }
9745
Willy Tarreau28376d62012-04-26 21:26:10 +02009746 if (!occ && !(opt & SMP_OPT_ITERATE))
9747 /* no explicit occurrence and single fetch => last cookie by default */
9748 occ = -1;
9749
9750 /* OK so basically here, either we want only one value and it's the
9751 * last one, or we want to iterate over all of them and we fetch the
9752 * next one.
9753 */
9754
Willy Tarreau9b28e032012-10-12 23:49:43 +02009755 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009756 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009757 /* search for the header from the beginning, we must first initialize
9758 * the search parameters.
9759 */
Willy Tarreau37406352012-04-23 16:16:37 +02009760 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009761 ctx->idx = 0;
9762 }
9763
Willy Tarreau28376d62012-04-26 21:26:10 +02009764 smp->flags |= SMP_F_VOL_HDR;
9765
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009766 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009767 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9768 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009769 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9770 goto out;
9771
Willy Tarreau24e32d82012-04-23 23:55:44 +02009772 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009773 continue;
9774
Willy Tarreau37406352012-04-23 16:16:37 +02009775 smp->ctx.a[0] = ctx->line + ctx->val;
9776 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009777 }
9778
Willy Tarreauf853c462012-04-23 18:53:56 +02009779 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009780 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009781 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009782 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009783 &smp->data.str.str,
9784 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009785 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009786 found = 1;
9787 if (occ >= 0) {
9788 /* one value was returned into smp->data.str.{str,len} */
9789 smp->flags |= SMP_F_NOT_LAST;
9790 return 1;
9791 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009792 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009793 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009794 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009795 /* all cookie headers and values were scanned. If we're looking for the
9796 * last occurrence, we may return it now.
9797 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009798 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009799 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009800 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009801}
9802
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009803/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009804 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009805 * multiple cookies may be parsed on the same line. The returned sample is of
9806 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009807 */
9808static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009809smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009810 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009811{
9812 struct http_txn *txn = l7;
9813 struct hdr_idx *idx = &txn->hdr_idx;
9814 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009815 const struct http_msg *msg;
9816 const char *hdr_name;
9817 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009818 int cnt;
9819 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009820 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009821
Willy Tarreau24e32d82012-04-23 23:55:44 +02009822 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009823 return 0;
9824
Willy Tarreaue333ec92012-04-16 16:26:40 +02009825 CHECK_HTTP_MESSAGE_FIRST();
9826
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009827 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009828 msg = &txn->req;
9829 hdr_name = "Cookie";
9830 hdr_name_len = 6;
9831 } else {
9832 msg = &txn->rsp;
9833 hdr_name = "Set-Cookie";
9834 hdr_name_len = 10;
9835 }
9836
Willy Tarreau9b28e032012-10-12 23:49:43 +02009837 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009838 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009839 ctx.idx = 0;
9840 cnt = 0;
9841
9842 while (1) {
9843 /* Note: val_beg == NULL every time we need to fetch a new header */
9844 if (!val_beg) {
9845 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9846 break;
9847
Willy Tarreau24e32d82012-04-23 23:55:44 +02009848 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009849 continue;
9850
9851 val_beg = ctx.line + ctx.val;
9852 val_end = val_beg + ctx.vlen;
9853 }
9854
Willy Tarreauf853c462012-04-23 18:53:56 +02009855 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009856 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009857 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009858 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009859 &smp->data.str.str,
9860 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009861 cnt++;
9862 }
9863 }
9864
Willy Tarreaub169eba2013-12-16 15:14:43 +01009865 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +02009866 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009867 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009868 return 1;
9869}
9870
Willy Tarreau51539362012-05-08 12:46:28 +02009871/* Fetch an cookie's integer value. The integer value is returned. It
9872 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9873 */
9874static int
9875smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009876 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009877{
Willy Tarreauef38c392013-07-22 16:29:32 +02009878 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009879
9880 if (ret > 0) {
9881 smp->type = SMP_T_UINT;
9882 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9883 }
9884
9885 return ret;
9886}
9887
Willy Tarreau8797c062007-05-07 00:55:35 +02009888/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009889/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009890/************************************************************************/
9891
David Cournapeau16023ee2010-12-23 20:55:41 +09009892/*
9893 * Given a path string and its length, find the position of beginning of the
9894 * query string. Returns NULL if no query string is found in the path.
9895 *
9896 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9897 *
9898 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9899 */
bedis4c75cca2012-10-05 08:38:24 +02009900static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009901{
9902 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009903
bedis4c75cca2012-10-05 08:38:24 +02009904 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009905 return p ? p + 1 : NULL;
9906}
9907
bedis4c75cca2012-10-05 08:38:24 +02009908static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009909{
bedis4c75cca2012-10-05 08:38:24 +02009910 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009911}
9912
9913/*
9914 * Given a url parameter, find the starting position of the first occurence,
9915 * or NULL if the parameter is not found.
9916 *
9917 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9918 * the function will return query_string+8.
9919 */
9920static char*
9921find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009922 char* url_param_name, size_t url_param_name_l,
9923 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009924{
9925 char *pos, *last;
9926
9927 pos = query_string;
9928 last = query_string + query_string_l - url_param_name_l - 1;
9929
9930 while (pos <= last) {
9931 if (pos[url_param_name_l] == '=') {
9932 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9933 return pos;
9934 pos += url_param_name_l + 1;
9935 }
bedis4c75cca2012-10-05 08:38:24 +02009936 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009937 pos++;
9938 pos++;
9939 }
9940 return NULL;
9941}
9942
9943/*
9944 * Given a url parameter name, returns its value and size into *value and
9945 * *value_l respectively, and returns non-zero. If the parameter is not found,
9946 * zero is returned and value/value_l are not touched.
9947 */
9948static int
9949find_url_param_value(char* path, size_t path_l,
9950 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009951 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009952{
9953 char *query_string, *qs_end;
9954 char *arg_start;
9955 char *value_start, *value_end;
9956
bedis4c75cca2012-10-05 08:38:24 +02009957 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009958 if (!query_string)
9959 return 0;
9960
9961 qs_end = path + path_l;
9962 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009963 url_param_name, url_param_name_l,
9964 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009965 if (!arg_start)
9966 return 0;
9967
9968 value_start = arg_start + url_param_name_l + 1;
9969 value_end = value_start;
9970
bedis4c75cca2012-10-05 08:38:24 +02009971 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009972 value_end++;
9973
9974 *value = value_start;
9975 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009976 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009977}
9978
9979static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009980smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009981 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009982{
bedis4c75cca2012-10-05 08:38:24 +02009983 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009984 struct http_txn *txn = l7;
9985 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009986
bedis4c75cca2012-10-05 08:38:24 +02009987 if (!args || args[0].type != ARGT_STR ||
9988 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009989 return 0;
9990
9991 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009992
bedis4c75cca2012-10-05 08:38:24 +02009993 if (args[1].type)
9994 delim = *args[1].data.str.str;
9995
Willy Tarreau9b28e032012-10-12 23:49:43 +02009996 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009997 args->data.str.str, args->data.str.len,
9998 &smp->data.str.str, &smp->data.str.len,
9999 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010000 return 0;
10001
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010002 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010003 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010004 return 1;
10005}
10006
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010007/* Return the signed integer value for the specified url parameter (see url_param
10008 * above).
10009 */
10010static int
10011smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010012 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010013{
Willy Tarreauef38c392013-07-22 16:29:32 +020010014 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010015
10016 if (ret > 0) {
10017 smp->type = SMP_T_UINT;
10018 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10019 }
10020
10021 return ret;
10022}
10023
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010024/* This produces a 32-bit hash of the concatenation of the first occurrence of
10025 * the Host header followed by the path component if it begins with a slash ('/').
10026 * This means that '*' will not be added, resulting in exactly the first Host
10027 * entry. If no Host header is found, then the path is used. The resulting value
10028 * is hashed using the url hash followed by a full avalanche hash and provides a
10029 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10030 * high-traffic sites without having to store whole paths.
10031 * this differs from the base32 functions in that it includes the url parameters
10032 * as well as the path
10033 */
10034static int
10035smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010036 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010037{
10038 struct http_txn *txn = l7;
10039 struct hdr_ctx ctx;
10040 unsigned int hash = 0;
10041 char *ptr, *beg, *end;
10042 int len;
10043
10044 CHECK_HTTP_MESSAGE_FIRST();
10045
10046 ctx.idx = 0;
10047 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10048 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10049 ptr = ctx.line + ctx.val;
10050 len = ctx.vlen;
10051 while (len--)
10052 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10053 }
10054
10055 /* now retrieve the path */
10056 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10057 beg = http_get_path(txn);
10058 if (!beg)
10059 beg = end;
10060
10061 for (ptr = beg; ptr < end ; ptr++);
10062
10063 if (beg < ptr && *beg == '/') {
10064 while (beg < ptr)
10065 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10066 }
10067 hash = full_hash(hash);
10068
10069 smp->type = SMP_T_UINT;
10070 smp->data.uint = hash;
10071 smp->flags = SMP_F_VOL_1ST;
10072 return 1;
10073}
10074
10075/* This concatenates the source address with the 32-bit hash of the Host and
10076 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10077 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10078 * on the source address length. The URL hash is stored before the address so
10079 * that in environments where IPv6 is insignificant, truncating the output to
10080 * 8 bytes would still work.
10081 */
10082static int
10083smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010084 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010085{
10086 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010087 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010088
Willy Tarreaue155ec22013-11-18 18:33:22 +010010089 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010090 return 0;
10091
10092 temp = get_trash_chunk();
10093 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10094 temp->len += sizeof(smp->data.uint);
10095
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010096 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010097 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010098 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010099 temp->len += 4;
10100 break;
10101 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010102 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010103 temp->len += 16;
10104 break;
10105 default:
10106 return 0;
10107 }
10108
10109 smp->data.str = *temp;
10110 smp->type = SMP_T_BIN;
10111 return 1;
10112}
10113
Willy Tarreau185b5c42012-04-26 15:11:51 +020010114/* This function is used to validate the arguments passed to any "hdr" fetch
10115 * keyword. These keywords support an optional positive or negative occurrence
10116 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10117 * is assumed that the types are already the correct ones. Returns 0 on error,
10118 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10119 * error message in case of error, that the caller is responsible for freeing.
10120 * The initial location must either be freeable or NULL.
10121 */
10122static int val_hdr(struct arg *arg, char **err_msg)
10123{
10124 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010125 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010126 return 0;
10127 }
10128 return 1;
10129}
10130
Willy Tarreau276fae92013-07-25 14:36:01 +020010131/* takes an UINT value on input supposed to represent the time since EPOCH,
10132 * adds an optional offset found in args[0] and emits a string representing
10133 * the date in RFC-1123/5322 format.
10134 */
10135static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10136{
10137 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10138 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10139 struct chunk *temp;
10140 struct tm *tm;
10141 time_t curr_date = smp->data.uint;
10142
10143 /* add offset */
10144 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10145 curr_date += args[0].data.sint;
10146
10147 tm = gmtime(&curr_date);
10148
10149 temp = get_trash_chunk();
10150 temp->len = snprintf(temp->str, temp->size - temp->len,
10151 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10152 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10153 tm->tm_hour, tm->tm_min, tm->tm_sec);
10154
10155 smp->data.str = *temp;
10156 smp->type = SMP_T_STR;
10157 return 1;
10158}
10159
Willy Tarreau4a568972010-05-12 08:08:50 +020010160/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010161/* All supported ACL keywords must be declared here. */
10162/************************************************************************/
10163
10164/* Note: must not be declared <const> as its list will be overwritten.
10165 * Please take care of keeping this list alphabetically sorted.
10166 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010167static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010168 { "base", "base", pat_parse_str, pat_match_str },
10169 { "base_beg", "base", pat_parse_str, pat_match_beg },
10170 { "base_dir", "base", pat_parse_str, pat_match_dir },
10171 { "base_dom", "base", pat_parse_str, pat_match_dom },
10172 { "base_end", "base", pat_parse_str, pat_match_end },
10173 { "base_len", "base", pat_parse_int, pat_match_len },
10174 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10175 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010176
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010177 { "cook", "req.cook", pat_parse_str, pat_match_str },
10178 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10179 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10180 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10181 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10182 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10183 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10184 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010185
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010186 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10187 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10188 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10189 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10190 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10191 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10192 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10193 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010194
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010195 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010196
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010197 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010198
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010199 { "path", "path", pat_parse_str, pat_match_str },
10200 { "path_beg", "path", pat_parse_str, pat_match_beg },
10201 { "path_dir", "path", pat_parse_str, pat_match_dir },
10202 { "path_dom", "path", pat_parse_str, pat_match_dom },
10203 { "path_end", "path", pat_parse_str, pat_match_end },
10204 { "path_len", "path", pat_parse_int, pat_match_len },
10205 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10206 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010207
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010208 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10209 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010210
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010211 { "scook", "res.cook", pat_parse_str, pat_match_str },
10212 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10213 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10214 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10215 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10216 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10217 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10218 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010219
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010220 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10221 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10222 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10223 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10224 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10225 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10226 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10227 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010228
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010229 { "url", "url", pat_parse_str, pat_match_str },
10230 { "url_beg", "url", pat_parse_str, pat_match_beg },
10231 { "url_dir", "url", pat_parse_str, pat_match_dir },
10232 { "url_dom", "url", pat_parse_str, pat_match_dom },
10233 { "url_end", "url", pat_parse_str, pat_match_end },
10234 { "url_len", "url", pat_parse_int, pat_match_len },
10235 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10236 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010237
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010238 { "urlp", "urlp", pat_parse_str, pat_match_str },
10239 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10240 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10241 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10242 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10243 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10244 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10245 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010246
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010247 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010248}};
10249
10250/************************************************************************/
10251/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010252/************************************************************************/
10253/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010254static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010255 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10256 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10257 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10258
William Lallemanda43ba4e2014-01-28 18:14:25 +010010259 /* capture are allocated and are permanent in the session */
10260 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10261 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10262
Willy Tarreau409bcde2013-01-08 00:31:00 +010010263 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10264 * are only here to match the ACL's name, are request-only and are used
10265 * for ACL compatibility only.
10266 */
10267 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10268 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10269 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10270 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10271
10272 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10273 * only here to match the ACL's name, are request-only and are used for
10274 * ACL compatibility only.
10275 */
10276 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10277 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10278 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10279 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10280
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010281 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10282 { "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 +010010283 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10284 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10285 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010286
10287 /* HTTP protocol on the request path */
10288 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010289 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010290
10291 /* HTTP version on the request path */
10292 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010293 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010294
10295 /* HTTP version on the response path */
10296 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010297 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10298
Willy Tarreau18ed2562013-01-14 15:56:36 +010010299 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10300 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10301 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10302 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10303
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010304 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10305 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010306 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10307 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10308 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10309 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10310
10311 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10312 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10313 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10314 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10315
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010316 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10317 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010318 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10319 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10320 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10321 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10322
Willy Tarreau409bcde2013-01-08 00:31:00 +010010323 /* scook is valid only on the response and is used for ACL compatibility */
10324 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10325 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10326 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10327 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10328
10329 /* shdr is valid only on the response and is used for ACL compatibility */
10330 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10331 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10332 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10333 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10334
10335 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10336 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010337 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10338 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010339 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10340 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10341 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10342 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10343 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10344 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010345}};
10346
Willy Tarreau8797c062007-05-07 00:55:35 +020010347
Willy Tarreau276fae92013-07-25 14:36:01 +020010348/* Note: must not be declared <const> as its list will be overwritten */
10349static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10350 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10351 { NULL, NULL, 0, 0, 0 },
10352}};
10353
Willy Tarreau8797c062007-05-07 00:55:35 +020010354__attribute__((constructor))
10355static void __http_protocol_init(void)
10356{
10357 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010358 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010359 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010360}
10361
10362
Willy Tarreau58f10d72006-12-04 02:26:12 +010010363/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010364 * Local variables:
10365 * c-indent-level: 8
10366 * c-basic-offset: 8
10367 * End:
10368 */