blob: 721bc36beff5c1e8da82cf6b7003b63d7f38130b [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 */
2146 if (!(msg->flags & HTTP_MSGF_VER_11))
2147 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 Tarreau2ab6eb12010-01-02 22:04:45 +01002310 return 0;
2311 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002312 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2313 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2314 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002315 }
2316
Willy Tarreau065e8332010-01-08 00:30:20 +01002317 /* Note that we have the same problem with the response ; we
2318 * may want to send a redirect, error or anything which requires
2319 * some spare space. So we'll ensure that we have at least
2320 * maxrewrite bytes available in the response buffer before
2321 * processing that one. This will only affect pipelined
2322 * keep-alive requests.
2323 */
2324 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002325 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002326 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2327 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2328 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002329 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002330 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002331 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002332 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002333 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002334 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002335 return 0;
2336 }
2337 }
2338
Willy Tarreau9b28e032012-10-12 23:49:43 +02002339 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002340 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002341 }
2342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 /* 1: we might have to print this header in debug mode */
2344 if (unlikely((global.mode & MODE_DEBUG) &&
2345 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002346 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348
Willy Tarreau9b28e032012-10-12 23:49:43 +02002349 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002350 /* this is a bit complex : in case of error on the request line,
2351 * we know that rq.l is still zero, so we display only the part
2352 * up to the end of the line (truncated by debug_hdr).
2353 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002354 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 sol += hdr_idx_first_pos(&txn->hdr_idx);
2358 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 while (cur_idx) {
2361 eol = sol + txn->hdr_idx.v[cur_idx].len;
2362 debug_hdr("clihdr", s, sol, eol);
2363 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2364 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002365 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 }
2367
Willy Tarreau58f10d72006-12-04 02:26:12 +01002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /*
2370 * Now we quickly check if we have found a full valid request.
2371 * If not so, we check the FD and buffer states before leaving.
2372 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002373 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002374 * requests are checked first. When waiting for a second request
2375 * on a keep-alive session, if we encounter and error, close, t/o,
2376 * we note the error in the session flags but don't set any state.
2377 * Since the error will be noted there, it will not be counted by
2378 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002379 * Last, we may increase some tracked counters' http request errors on
2380 * the cases that are deliberately the client's fault. For instance,
2381 * a timeout or connection reset is not counted as an error. However
2382 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384
Willy Tarreau655dce92009-11-08 13:10:58 +01002385 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002386 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002388 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002389 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002390 session_inc_http_req_ctr(s);
2391 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002392 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002394 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002395
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 /* 1: Since we are in header mode, if there's no space
2397 * left for headers, we won't be able to free more
2398 * later, so the session will never terminate. We
2399 * must terminate it now.
2400 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002401 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 /* FIXME: check if URI is set and return Status
2403 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002404 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002405 session_inc_http_req_ctr(s);
2406 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002407 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002408 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002409 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 goto return_bad_req;
2411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002412
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002414 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002415 if (!(s->flags & SN_ERR_MASK))
2416 s->flags |= SN_ERR_CLICL;
2417
Willy Tarreaufcffa692010-01-10 14:21:19 +01002418 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002419 goto failed_keep_alive;
2420
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002422 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002423 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002424 session_inc_http_err_ctr(s);
2425 }
2426
Willy Tarreaudc979f22012-12-04 10:39:01 +01002427 txn->status = 400;
2428 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 msg->msg_state = HTTP_MSG_ERROR;
2430 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002431
Willy Tarreauda7ff642010-06-23 11:44:09 +02002432 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002433 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002434 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002435 if (s->listener->counters)
2436 s->listener->counters->failed_req++;
2437
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 if (!(s->flags & SN_FINST_MASK))
2439 s->flags |= SN_FINST_R;
2440 return 0;
2441 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002442
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002444 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002445 if (!(s->flags & SN_ERR_MASK))
2446 s->flags |= SN_ERR_CLITO;
2447
Willy Tarreaufcffa692010-01-10 14:21:19 +01002448 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002449 goto failed_keep_alive;
2450
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002452 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002453 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002454 session_inc_http_err_ctr(s);
2455 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002457 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 msg->msg_state = HTTP_MSG_ERROR;
2459 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002460
Willy Tarreauda7ff642010-06-23 11:44:09 +02002461 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002462 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002463 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002464 if (s->listener->counters)
2465 s->listener->counters->failed_req++;
2466
Willy Tarreau59234e92008-11-30 23:51:27 +01002467 if (!(s->flags & SN_FINST_MASK))
2468 s->flags |= SN_FINST_R;
2469 return 0;
2470 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002471
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002473 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002474 if (!(s->flags & SN_ERR_MASK))
2475 s->flags |= SN_ERR_CLICL;
2476
Willy Tarreaufcffa692010-01-10 14:21:19 +01002477 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002478 goto failed_keep_alive;
2479
Willy Tarreau4076a152009-04-02 15:18:36 +02002480 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002481 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002483 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 msg->msg_state = HTTP_MSG_ERROR;
2485 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002486
Willy Tarreauda7ff642010-06-23 11:44:09 +02002487 session_inc_http_err_ctr(s);
2488 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002489 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002490 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491 if (s->listener->counters)
2492 s->listener->counters->failed_req++;
2493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 if (!(s->flags & SN_FINST_MASK))
2495 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002496 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002497 }
2498
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002499 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002500 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2501 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002502#ifdef TCP_QUICKACK
Willy Tarreauf79c8172013-10-21 16:30:56 +02002503 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && (__objt_conn(s->req->prod->end)->flags & CO_FL_CTRL_READY)) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002504 /* We need more data, we have to re-enable quick-ack in case we
2505 * previously disabled it, otherwise we might cause the client
2506 * to delay next data.
2507 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002508 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002509 }
2510#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002511
Willy Tarreaufcffa692010-01-10 14:21:19 +01002512 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2513 /* If the client starts to talk, let's fall back to
2514 * request timeout processing.
2515 */
2516 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002517 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002518 }
2519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002521 if (!tick_isset(req->analyse_exp)) {
2522 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2523 (txn->flags & TX_WAIT_NEXT_RQ) &&
2524 tick_isset(s->be->timeout.httpka))
2525 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2526 else
2527 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002529
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 /* we're not ready yet */
2531 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002532
2533 failed_keep_alive:
2534 /* Here we process low-level errors for keep-alive requests. In
2535 * short, if the request is not the first one and it experiences
2536 * a timeout, read error or shutdown, we just silently close so
2537 * that the client can try again.
2538 */
2539 txn->status = 0;
2540 msg->msg_state = HTTP_MSG_RQBEFORE;
2541 req->analysers = 0;
2542 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002543 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002544 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002545 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002546 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002548
Willy Tarreaud787e662009-07-07 10:14:51 +02002549 /* OK now we have a complete HTTP request with indexed headers. Let's
2550 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002552 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002553 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002554 * byte after the last LF. msg->sov points to the first byte of data.
2555 * msg->eol cannot be trusted because it may have been left uninitialized
2556 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002558
Willy Tarreauda7ff642010-06-23 11:44:09 +02002559 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002560 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2561
Willy Tarreaub16a5742010-01-10 14:46:16 +01002562 if (txn->flags & TX_WAIT_NEXT_RQ) {
2563 /* kill the pending keep-alive timeout */
2564 txn->flags &= ~TX_WAIT_NEXT_RQ;
2565 req->analyse_exp = TICK_ETERNITY;
2566 }
2567
2568
Willy Tarreaud787e662009-07-07 10:14:51 +02002569 /* Maybe we found in invalid header name while we were configured not
2570 * to block on that, so we have to capture it now.
2571 */
2572 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002573 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 /*
2576 * 1: identify the method
2577 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002578 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002579
2580 /* we can make use of server redirect on GET and HEAD */
2581 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2582 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 /*
2585 * 2: check if the URI matches the monitor_uri.
2586 * We have to do this for every request which gets in, because
2587 * the monitor-uri is defined by the frontend.
2588 */
2589 if (unlikely((s->fe->monitor_uri_len != 0) &&
2590 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 s->fe->monitor_uri,
2593 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002594 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002600 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002601
Willy Tarreau59234e92008-11-30 23:51:27 +01002602 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002603 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002604 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002605
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 ret = acl_pass(ret);
2607 if (cond->pol == ACL_COND_UNLESS)
2608 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 if (ret) {
2611 /* we fail this request, let's return 503 service unavail */
2612 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002613 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002614 if (!(s->flags & SN_ERR_MASK))
2615 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002617 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002618 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002619
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 /* nothing to fail, let's reply normaly */
2621 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002622 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002623 if (!(s->flags & SN_ERR_MASK))
2624 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 goto return_prx_cond;
2626 }
2627
2628 /*
2629 * 3: Maybe we have to copy the original REQURI for the logs ?
2630 * Note: we cannot log anymore if the request has been
2631 * classified as invalid.
2632 */
2633 if (unlikely(s->logs.logwait & LW_REQ)) {
2634 /* we have a complete HTTP request that we must log */
2635 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2636 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 if (urilen >= REQURI_LEN)
2639 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002642
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002643 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 s->do_log(s);
2645 } else {
2646 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002647 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 }
Willy Tarreau06619262006-12-17 08:37:22 +01002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002651 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002652 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002653
Willy Tarreau5b154472009-12-21 20:11:07 +01002654 /* ... and check if the request is HTTP/1.1 or above */
2655 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002656 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2657 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2658 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002659 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002660
2661 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002662 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 +01002663
Willy Tarreau88d349d2010-01-25 12:15:43 +01002664 /* if the frontend has "option http-use-proxy-header", we'll check if
2665 * we have what looks like a proxied connection instead of a connection,
2666 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2667 * Note that this is *not* RFC-compliant, however browsers and proxies
2668 * happen to do that despite being non-standard :-(
2669 * We consider that a request not beginning with either '/' or '*' is
2670 * a proxied connection, which covers both "scheme://location" and
2671 * CONNECT ip:port.
2672 */
2673 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002674 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002675 txn->flags |= TX_USE_PX_CONN;
2676
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002677 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002678 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002681 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002682 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002684
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002685 /* 6: determine the transfer-length.
2686 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2687 * the presence of a message-body in a REQUEST and its transfer length
2688 * must be determined that way (in order of precedence) :
2689 * 1. The presence of a message-body in a request is signaled by the
2690 * inclusion of a Content-Length or Transfer-Encoding header field
2691 * in the request's header fields. When a request message contains
2692 * both a message-body of non-zero length and a method that does
2693 * not define any semantics for that request message-body, then an
2694 * origin server SHOULD either ignore the message-body or respond
2695 * with an appropriate error message (e.g., 413). A proxy or
2696 * gateway, when presented the same request, SHOULD either forward
2697 * the request inbound with the message- body or ignore the
2698 * message-body when determining a response.
2699 *
2700 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2701 * and the "chunked" transfer-coding (Section 6.2) is used, the
2702 * transfer-length is defined by the use of this transfer-coding.
2703 * If a Transfer-Encoding header field is present and the "chunked"
2704 * transfer-coding is not present, the transfer-length is defined
2705 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002706 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002707 * 3. If a Content-Length header field is present, its decimal value in
2708 * OCTETs represents both the entity-length and the transfer-length.
2709 * If a message is received with both a Transfer-Encoding header
2710 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002711 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002712 * 4. By the server closing the connection. (Closing the connection
2713 * cannot be used to indicate the end of a request body, since that
2714 * would leave no possibility for the server to send back a response.)
2715 *
2716 * Whenever a transfer-coding is applied to a message-body, the set of
2717 * transfer-codings MUST include "chunked", unless the message indicates
2718 * it is terminated by closing the connection. When the "chunked"
2719 * transfer-coding is used, it MUST be the last transfer-coding applied
2720 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002721 */
2722
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002723 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002724 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002726 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002727 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002728 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002729 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2730 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002731 /* bad transfer-encoding (chunked followed by something else) */
2732 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002733 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002734 break;
2735 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002736 }
2737
Willy Tarreau32b47f42009-10-18 20:55:02 +02002738 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002739 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002740 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741 signed long long cl;
2742
Willy Tarreauad14f752011-09-02 20:33:27 +02002743 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002744 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002745 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002746 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002747
Willy Tarreauad14f752011-09-02 20:33:27 +02002748 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002749 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002750 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002751 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002752
Willy Tarreauad14f752011-09-02 20:33:27 +02002753 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002754 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002755 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002756 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002757
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002758 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002759 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002760 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002761 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002762
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002763 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002764 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002765 }
2766
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002767 /* bodyless requests have a known length */
2768 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002769 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002770
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002772 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002773 req->analyse_exp = TICK_ETERNITY;
2774 return 1;
2775
2776 return_bad_req:
2777 /* We centralize bad requests processing here */
2778 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2779 /* we detected a parsing error. We want to archive this request
2780 * in the dedicated proxy area for later troubleshooting.
2781 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002782 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002783 }
2784
2785 txn->req.msg_state = HTTP_MSG_ERROR;
2786 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002787 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002788
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002789 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002790 if (s->listener->counters)
2791 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002792
2793 return_prx_cond:
2794 if (!(s->flags & SN_ERR_MASK))
2795 s->flags |= SN_ERR_PRXCOND;
2796 if (!(s->flags & SN_FINST_MASK))
2797 s->flags |= SN_FINST_R;
2798
2799 req->analysers = 0;
2800 req->analyse_exp = TICK_ETERNITY;
2801 return 0;
2802}
2803
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002804
Willy Tarreau347a35d2013-11-22 17:51:09 +01002805/* This function prepares an applet to handle the stats. It can deal with the
2806 * "100-continue" expectation, check that admin rules are met for POST requests,
2807 * and program a response message if something was unexpected. It cannot fail
2808 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002809 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002810 * s->target which is supposed to already point to the stats applet. The caller
2811 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002812 */
2813int http_handle_stats(struct session *s, struct channel *req)
2814{
2815 struct stats_admin_rule *stats_admin_rule;
2816 struct stream_interface *si = s->rep->prod;
2817 struct http_txn *txn = &s->txn;
2818 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002819 struct uri_auth *uri_auth = s->be->uri_auth;
2820 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002821 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002823 appctx = si_appctx(si);
2824 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2825 appctx->st1 = appctx->st2 = 0;
2826 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2827 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002828
2829 uri = msg->chn->buf->p + msg->sl.rq.u;
2830 lookup = uri + uri_auth->uri_len;
2831
2832 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2833 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002834 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002835 break;
2836 }
2837 }
2838
2839 if (uri_auth->refresh) {
2840 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2841 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002842 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002843 break;
2844 }
2845 }
2846 }
2847
2848 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2849 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002850 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002851 break;
2852 }
2853 }
2854
2855 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2856 if (memcmp(h, ";st=", 4) == 0) {
2857 int i;
2858 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002859 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002860 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2861 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002862 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002863 break;
2864 }
2865 }
2866 break;
2867 }
2868 }
2869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002870 appctx->ctx.stats.scope_str = 0;
2871 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002872 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2873 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2874 int itx = 0;
2875 const char *h2;
2876 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2877 const char *err;
2878
2879 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2880 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002881 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002882 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2883 itx++;
2884 h++;
2885 }
2886
2887 if (itx > STAT_SCOPE_TXT_MAXLEN)
2888 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002889 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002892 memcpy(scope_txt, h2, itx);
2893 scope_txt[itx] = '\0';
2894 err = invalid_char(scope_txt);
2895 if (err) {
2896 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002897 appctx->ctx.stats.scope_str = 0;
2898 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002899 }
2900 break;
2901 }
2902 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002903
2904 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002905 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002906 int ret = 1;
2907
2908 if (stats_admin_rule->cond) {
2909 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2910 ret = acl_pass(ret);
2911 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2912 ret = !ret;
2913 }
2914
2915 if (ret) {
2916 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002917 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002918 break;
2919 }
2920 }
2921
2922 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002923 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002924 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002925 if (msg->msg_state < HTTP_MSG_100_SENT) {
2926 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2927 * send an HTTP/1.1 100 Continue intermediate response.
2928 */
2929 if (msg->flags & HTTP_MSGF_VER_11) {
2930 struct hdr_ctx ctx;
2931 ctx.idx = 0;
2932 /* Expect is allowed in 1.1, look for it */
2933 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2934 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2935 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2936 }
2937 }
2938 msg->msg_state = HTTP_MSG_100_SENT;
2939 s->logs.tv_request = now; /* update the request timer to reflect full request */
2940 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002941 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002942 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002943 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2945 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002946 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002947 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002948 else {
2949 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002950 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002951 }
2952
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002953 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002954 return 1;
2955}
2956
Lukas Tribus67db8df2013-06-23 17:37:13 +02002957/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2958 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2959 */
2960static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2961{
2962#ifdef IP_TOS
2963 if (from.ss_family == AF_INET)
2964 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2965#endif
2966#ifdef IPV6_TCLASS
2967 if (from.ss_family == AF_INET6) {
2968 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2969 /* v4-mapped addresses need IP_TOS */
2970 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2971 else
2972 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2973 }
2974#endif
2975}
2976
Willy Tarreau20b0de52012-12-24 15:45:22 +01002977/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002978 * transaction <txn>. Returns the first rule that prevents further processing
2979 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2980 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2981 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002982 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002983static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002984http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002985{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002986 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002987 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002988 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002989
Willy Tarreauff011f22011-01-06 17:51:27 +01002990 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002991 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002992 continue;
2993
Willy Tarreau96257ec2012-12-27 10:46:37 +01002994 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002995 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002996 int ret;
2997
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002998 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002999 ret = acl_pass(ret);
3000
Willy Tarreauff011f22011-01-06 17:51:27 +01003001 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003002 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003003
3004 if (!ret) /* condition not matched */
3005 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003006 }
3007
Willy Tarreau20b0de52012-12-24 15:45:22 +01003008
Willy Tarreau96257ec2012-12-27 10:46:37 +01003009 switch (rule->action) {
3010 case HTTP_REQ_ACT_ALLOW:
3011 return NULL; /* "allow" rules are OK */
3012
3013 case HTTP_REQ_ACT_DENY:
3014 txn->flags |= TX_CLDENY;
3015 return rule;
3016
Willy Tarreauccbcc372012-12-27 12:37:57 +01003017 case HTTP_REQ_ACT_TARPIT:
3018 txn->flags |= TX_CLTARPIT;
3019 return rule;
3020
Willy Tarreau96257ec2012-12-27 10:46:37 +01003021 case HTTP_REQ_ACT_AUTH:
3022 return rule;
3023
Willy Tarreau81499eb2012-12-27 12:19:02 +01003024 case HTTP_REQ_ACT_REDIR:
3025 return rule;
3026
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003027 case HTTP_REQ_ACT_SET_NICE:
3028 s->task->nice = rule->arg.nice;
3029 break;
3030
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003031 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003032 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003033 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003034 break;
3035
Willy Tarreau51347ed2013-06-11 19:34:13 +02003036 case HTTP_REQ_ACT_SET_MARK:
3037#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003038 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003039 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003040#endif
3041 break;
3042
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003043 case HTTP_REQ_ACT_SET_LOGL:
3044 s->logs.level = rule->arg.loglevel;
3045 break;
3046
Willy Tarreau96257ec2012-12-27 10:46:37 +01003047 case HTTP_REQ_ACT_SET_HDR:
3048 ctx.idx = 0;
3049 /* remove all occurrences of the header */
3050 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3051 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3052 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003053 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003054 /* now fall through to header addition */
3055
3056 case HTTP_REQ_ACT_ADD_HDR:
3057 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3058 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3059 trash.len = rule->arg.hdr_add.name_len;
3060 trash.str[trash.len++] = ':';
3061 trash.str[trash.len++] = ' ';
3062 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3063 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3064 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003065 }
3066 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003067
3068 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003069 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003070}
3071
Willy Tarreau71241ab2012-12-27 11:30:54 +01003072
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003073/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3074 * transaction <txn>. Returns the first rule that prevents further processing
3075 * of the response (deny, ...) or NULL if it executed all rules or stopped
3076 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3077 * rule.
3078 */
3079static struct http_res_rule *
3080http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3081{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003082 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003083 struct http_res_rule *rule;
3084 struct hdr_ctx ctx;
3085
3086 list_for_each_entry(rule, rules, list) {
3087 if (rule->action >= HTTP_RES_ACT_MAX)
3088 continue;
3089
3090 /* check optional condition */
3091 if (rule->cond) {
3092 int ret;
3093
3094 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3095 ret = acl_pass(ret);
3096
3097 if (rule->cond->pol == ACL_COND_UNLESS)
3098 ret = !ret;
3099
3100 if (!ret) /* condition not matched */
3101 continue;
3102 }
3103
3104
3105 switch (rule->action) {
3106 case HTTP_RES_ACT_ALLOW:
3107 return NULL; /* "allow" rules are OK */
3108
3109 case HTTP_RES_ACT_DENY:
3110 txn->flags |= TX_SVDENY;
3111 return rule;
3112
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003113 case HTTP_RES_ACT_SET_NICE:
3114 s->task->nice = rule->arg.nice;
3115 break;
3116
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003117 case HTTP_RES_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003118 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003119 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003120 break;
3121
Willy Tarreau51347ed2013-06-11 19:34:13 +02003122 case HTTP_RES_ACT_SET_MARK:
3123#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003124 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003125 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003126#endif
3127 break;
3128
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003129 case HTTP_RES_ACT_SET_LOGL:
3130 s->logs.level = rule->arg.loglevel;
3131 break;
3132
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003133 case HTTP_RES_ACT_SET_HDR:
3134 ctx.idx = 0;
3135 /* remove all occurrences of the header */
3136 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3137 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3138 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3139 }
3140 /* now fall through to header addition */
3141
3142 case HTTP_RES_ACT_ADD_HDR:
3143 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3144 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3145 trash.len = rule->arg.hdr_add.name_len;
3146 trash.str[trash.len++] = ':';
3147 trash.str[trash.len++] = ' ';
3148 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3149 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3150 break;
3151 }
3152 }
3153
3154 /* we reached the end of the rules, nothing to report */
3155 return NULL;
3156}
3157
3158
Willy Tarreau71241ab2012-12-27 11:30:54 +01003159/* Perform an HTTP redirect based on the information in <rule>. The function
3160 * returns non-zero on success, or zero in case of a, irrecoverable error such
3161 * as too large a request to build a valid response.
3162 */
3163static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3164{
3165 struct http_msg *msg = &txn->req;
3166 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003167 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003168
3169 /* build redirect message */
3170 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003171 case 308:
3172 msg_fmt = HTTP_308;
3173 break;
3174 case 307:
3175 msg_fmt = HTTP_307;
3176 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003177 case 303:
3178 msg_fmt = HTTP_303;
3179 break;
3180 case 301:
3181 msg_fmt = HTTP_301;
3182 break;
3183 case 302:
3184 default:
3185 msg_fmt = HTTP_302;
3186 break;
3187 }
3188
3189 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3190 return 0;
3191
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003192 location = trash.str + trash.len;
3193
Willy Tarreau71241ab2012-12-27 11:30:54 +01003194 switch(rule->type) {
3195 case REDIRECT_TYPE_SCHEME: {
3196 const char *path;
3197 const char *host;
3198 struct hdr_ctx ctx;
3199 int pathlen;
3200 int hostlen;
3201
3202 host = "";
3203 hostlen = 0;
3204 ctx.idx = 0;
3205 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3206 host = ctx.line + ctx.val;
3207 hostlen = ctx.vlen;
3208 }
3209
3210 path = http_get_path(txn);
3211 /* build message using path */
3212 if (path) {
3213 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3214 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3215 int qs = 0;
3216 while (qs < pathlen) {
3217 if (path[qs] == '?') {
3218 pathlen = qs;
3219 break;
3220 }
3221 qs++;
3222 }
3223 }
3224 } else {
3225 path = "/";
3226 pathlen = 1;
3227 }
3228
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003229 if (rule->rdr_str) { /* this is an old "redirect" rule */
3230 /* check if we can add scheme + "://" + host + path */
3231 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3232 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003233
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003234 /* add scheme */
3235 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3236 trash.len += rule->rdr_len;
3237 }
3238 else {
3239 /* add scheme with executing log format */
3240 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003241
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003242 /* check if we can add scheme + "://" + host + path */
3243 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3244 return 0;
3245 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003246 /* add "://" */
3247 memcpy(trash.str + trash.len, "://", 3);
3248 trash.len += 3;
3249
3250 /* add host */
3251 memcpy(trash.str + trash.len, host, hostlen);
3252 trash.len += hostlen;
3253
3254 /* add path */
3255 memcpy(trash.str + trash.len, path, pathlen);
3256 trash.len += pathlen;
3257
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003258 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003259 if (trash.len && trash.str[trash.len - 1] != '/' &&
3260 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3261 if (trash.len > trash.size - 5)
3262 return 0;
3263 trash.str[trash.len] = '/';
3264 trash.len++;
3265 }
3266
3267 break;
3268 }
3269 case REDIRECT_TYPE_PREFIX: {
3270 const char *path;
3271 int pathlen;
3272
3273 path = http_get_path(txn);
3274 /* build message using path */
3275 if (path) {
3276 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3277 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3278 int qs = 0;
3279 while (qs < pathlen) {
3280 if (path[qs] == '?') {
3281 pathlen = qs;
3282 break;
3283 }
3284 qs++;
3285 }
3286 }
3287 } else {
3288 path = "/";
3289 pathlen = 1;
3290 }
3291
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003292 if (rule->rdr_str) { /* this is an old "redirect" rule */
3293 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3294 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003295
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003296 /* add prefix. Note that if prefix == "/", we don't want to
3297 * add anything, otherwise it makes it hard for the user to
3298 * configure a self-redirection.
3299 */
3300 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3301 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3302 trash.len += rule->rdr_len;
3303 }
3304 }
3305 else {
3306 /* add prefix with executing log format */
3307 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3308
3309 /* Check length */
3310 if (trash.len + pathlen > trash.size - 4)
3311 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003312 }
3313
3314 /* add path */
3315 memcpy(trash.str + trash.len, path, pathlen);
3316 trash.len += pathlen;
3317
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003318 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003319 if (trash.len && trash.str[trash.len - 1] != '/' &&
3320 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3321 if (trash.len > trash.size - 5)
3322 return 0;
3323 trash.str[trash.len] = '/';
3324 trash.len++;
3325 }
3326
3327 break;
3328 }
3329 case REDIRECT_TYPE_LOCATION:
3330 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003331 if (rule->rdr_str) { /* this is an old "redirect" rule */
3332 if (trash.len + rule->rdr_len > trash.size - 4)
3333 return 0;
3334
3335 /* add location */
3336 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3337 trash.len += rule->rdr_len;
3338 }
3339 else {
3340 /* add location with executing log format */
3341 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 /* Check left length */
3344 if (trash.len > trash.size - 4)
3345 return 0;
3346 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003347 break;
3348 }
3349
3350 if (rule->cookie_len) {
3351 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3352 trash.len += 14;
3353 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3354 trash.len += rule->cookie_len;
3355 memcpy(trash.str + trash.len, "\r\n", 2);
3356 trash.len += 2;
3357 }
3358
3359 /* add end of headers and the keep-alive/close status.
3360 * We may choose to set keep-alive if the Location begins
3361 * with a slash, because the client will come back to the
3362 * same server.
3363 */
3364 txn->status = rule->code;
3365 /* let's log the request time */
3366 s->logs.tv_request = now;
3367
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003368 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003369 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3370 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3371 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3372 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3373 /* keep-alive possible */
3374 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3375 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3376 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3377 trash.len += 30;
3378 } else {
3379 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3380 trash.len += 24;
3381 }
3382 }
3383 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3384 trash.len += 4;
3385 bo_inject(txn->rsp.chn, trash.str, trash.len);
3386 /* "eat" the request */
3387 bi_fast_delete(txn->req.chn->buf, msg->sov);
3388 msg->sov = 0;
3389 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3390 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3391 txn->req.msg_state = HTTP_MSG_CLOSED;
3392 txn->rsp.msg_state = HTTP_MSG_DONE;
3393 } else {
3394 /* keep-alive not possible */
3395 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3396 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3397 trash.len += 29;
3398 } else {
3399 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3400 trash.len += 23;
3401 }
3402 stream_int_retnclose(txn->req.chn->prod, &trash);
3403 txn->req.chn->analysers = 0;
3404 }
3405
3406 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003407 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003408 if (!(s->flags & SN_FINST_MASK))
3409 s->flags |= SN_FINST_R;
3410
3411 return 1;
3412}
3413
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003414/* This stream analyser runs all HTTP request processing which is common to
3415 * frontends and backends, which means blocking ACLs, filters, connection-close,
3416 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003417 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003418 * either needs more data or wants to immediately abort the request (eg: deny,
3419 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003420 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003421int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003422{
Willy Tarreaud787e662009-07-07 10:14:51 +02003423 struct http_txn *txn = &s->txn;
3424 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003425 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003426 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003427 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003428 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003429
Willy Tarreau655dce92009-11-08 13:10:58 +01003430 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003431 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003432 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003433 return 0;
3434 }
3435
Willy Tarreau3a816292009-07-07 10:55:49 +02003436 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003437 req->analyse_exp = TICK_ETERNITY;
3438
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003439 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 +02003440 now_ms, __FUNCTION__,
3441 s,
3442 req,
3443 req->rex, req->wex,
3444 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003445 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003446 req->analysers);
3447
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003448 /* first check whether we have some ACLs set to block this request */
3449 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003450 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003452 ret = acl_pass(ret);
3453 if (cond->pol == ACL_COND_UNLESS)
3454 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003455
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 if (ret) {
3457 txn->status = 403;
3458 /* let's log the request time */
3459 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003460 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003461 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003462 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003463 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003465
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003466 /* just in case we have some per-backend tracking */
3467 session_inc_be_http_req_ctr(s);
3468
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003469 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003470 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003471
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003472 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003473 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003474 if (stats_check_uri(s->rep->prod, txn, px)) {
3475 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003476 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3477 txn->status = 500;
3478 s->logs.tv_request = now;
3479 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003480
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003481 if (!(s->flags & SN_ERR_MASK))
3482 s->flags |= SN_ERR_RESOURCE;
3483 goto return_prx_cond;
3484 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003485 /* parse the whole stats request and extract the relevant information */
3486 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003487 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 +01003488 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003489 }
3490
Willy Tarreau3b44e722013-11-16 10:28:23 +01003491 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3492 * blocked by an http-request rule.
3493 */
3494 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003495 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003496 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003497 }
Willy Tarreau06619262006-12-17 08:37:22 +01003498
Willy Tarreau3b44e722013-11-16 10:28:23 +01003499 /* return a 403 if either rule has blocked */
3500 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003501 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003502 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003503 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003504 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003505 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003506 s->fe->fe_counters.denied_req++;
3507 if (s->fe != s->be)
3508 s->be->be_counters.denied_req++;
3509 if (s->listener->counters)
3510 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003511 goto return_prx_cond;
3512 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003513
3514 /* When a connection is tarpitted, we use the tarpit timeout,
3515 * which may be the same as the connect timeout if unspecified.
3516 * If unset, then set it to zero because we really want it to
3517 * eventually expire. We build the tarpit as an analyser.
3518 */
3519 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003520 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003521 /* wipe the request out so that we can drop the connection early
3522 * if the client closes first.
3523 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003524 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003525 req->analysers = 0; /* remove switching rules etc... */
3526 req->analysers |= AN_REQ_HTTP_TARPIT;
3527 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3528 if (!req->analyse_exp)
3529 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003530 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003531 s->fe->fe_counters.denied_req++;
3532 if (s->fe != s->be)
3533 s->be->be_counters.denied_req++;
3534 if (s->listener->counters)
3535 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003536 return 1;
3537 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003538 }
Willy Tarreau06619262006-12-17 08:37:22 +01003539
Willy Tarreau5b154472009-12-21 20:11:07 +01003540 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3541 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003542 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3543 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003544 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3545 * avoid to redo the same work if FE and BE have the same settings (common).
3546 * The method consists in checking if options changed between the two calls
3547 * (implying that either one is non-null, or one of them is non-null and we
3548 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003549 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003550
Willy Tarreaudc008c52010-02-01 16:20:08 +01003551 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3552 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3553 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3554 (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 +01003555 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003556
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003557 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3558 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003559 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003560 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3561 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003562 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003563 tmp = TX_CON_WANT_CLO;
3564
Willy Tarreau5b154472009-12-21 20:11:07 +01003565 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3566 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003567
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003568 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3569 /* parse the Connection header and possibly clean it */
3570 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003571 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003572 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3573 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003574 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003575 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003576 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003577 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003578 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003579
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003580 /* check if client or config asks for explicit close in KAL/SCL */
3581 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3582 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3583 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003584 (!(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 +01003585 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003586 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003587 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003588 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3589 }
Willy Tarreau78599912009-10-17 20:12:21 +02003590
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003591 /* we can be blocked here because the request needs to be authenticated,
3592 * either to pass or to access stats.
3593 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003594 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003595 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003596
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003597 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003598 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003599
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003600 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003601 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003602 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003603 /* on 401 we still count one error, because normal browsing
3604 * won't significantly increase the counter but brute force
3605 * attempts will.
3606 */
3607 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003608 goto return_prx_cond;
3609 }
3610
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003611 /* add request headers from the rule sets in the same order */
3612 list_for_each_entry(wl, &px->req_add, list) {
3613 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003614 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003615 ret = acl_pass(ret);
3616 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3617 ret = !ret;
3618 if (!ret)
3619 continue;
3620 }
3621
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003622 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003623 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003624 }
3625
3626 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3627 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3628 goto return_bad_req;
3629 req->analyse_exp = TICK_ETERNITY;
3630 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003631 }
3632
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003633 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003634 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003635 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3636 s->fe->fe_counters.intercepted_req++;
3637
3638 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3639 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3640 if (!(s->flags & SN_FINST_MASK))
3641 s->flags |= SN_FINST_R;
3642
3643 req->analyse_exp = TICK_ETERNITY;
3644 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003645 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003647
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003648 /* check whether we have some ACLs set to redirect this request */
3649 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003650 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003651 int ret;
3652
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003653 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003654 ret = acl_pass(ret);
3655 if (rule->cond->pol == ACL_COND_UNLESS)
3656 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 if (!ret)
3658 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003659 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003660 if (!http_apply_redirect_rule(rule, s, txn))
3661 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003662
Willy Tarreau71241ab2012-12-27 11:30:54 +01003663 req->analyse_exp = TICK_ETERNITY;
3664 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003666
Willy Tarreau2be39392010-01-03 17:24:51 +01003667 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3668 * If this happens, then the data will not come immediately, so we must
3669 * send all what we have without waiting. Note that due to the small gain
3670 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003671 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003672 * itself once used.
3673 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003674 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003675
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003676 /* that's OK for us now, let's move on to next analysers */
3677 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003678
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003679 return_bad_req:
3680 /* We centralize bad requests processing here */
3681 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3682 /* we detected a parsing error. We want to archive this request
3683 * in the dedicated proxy area for later troubleshooting.
3684 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003685 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003686 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003687
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003688 txn->req.msg_state = HTTP_MSG_ERROR;
3689 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003690 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003691
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003692 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003693 if (s->listener->counters)
3694 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003696 return_prx_cond:
3697 if (!(s->flags & SN_ERR_MASK))
3698 s->flags |= SN_ERR_PRXCOND;
3699 if (!(s->flags & SN_FINST_MASK))
3700 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003701
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003702 req->analysers = 0;
3703 req->analyse_exp = TICK_ETERNITY;
3704 return 0;
3705}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003706
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707/* This function performs all the processing enabled for the current request.
3708 * It returns 1 if the processing can continue on next analysers, or zero if it
3709 * needs more data, encounters an error, or wants to immediately abort the
3710 * request. It relies on buffers flags, and updates s->req->analysers.
3711 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003712int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003713{
3714 struct http_txn *txn = &s->txn;
3715 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003716 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003717
Willy Tarreau655dce92009-11-08 13:10:58 +01003718 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003719 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003720 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003721 return 0;
3722 }
3723
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003724 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 +02003725 now_ms, __FUNCTION__,
3726 s,
3727 req,
3728 req->rex, req->wex,
3729 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003730 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003731 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003732
William Lallemand82fe75c2012-10-23 10:25:10 +02003733 if (s->fe->comp || s->be->comp)
3734 select_compression_request_header(s, req->buf);
3735
Willy Tarreau59234e92008-11-30 23:51:27 +01003736 /*
3737 * Right now, we know that we have processed the entire headers
3738 * and that unwanted requests have been filtered out. We can do
3739 * whatever we want with the remaining request. Also, now we
3740 * may have separate values for ->fe, ->be.
3741 */
Willy Tarreau06619262006-12-17 08:37:22 +01003742
Willy Tarreau59234e92008-11-30 23:51:27 +01003743 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003744 * If HTTP PROXY is set we simply get remote server address parsing
3745 * incoming request. Note that this requires that a connection is
3746 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003747 */
3748 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003749 struct connection *conn;
3750
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003751 /* Note that for now we don't reuse existing proxy connections */
3752 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003753 txn->req.msg_state = HTTP_MSG_ERROR;
3754 txn->status = 500;
3755 req->analysers = 0;
3756 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3757
3758 if (!(s->flags & SN_ERR_MASK))
3759 s->flags |= SN_ERR_RESOURCE;
3760 if (!(s->flags & SN_FINST_MASK))
3761 s->flags |= SN_FINST_R;
3762
3763 return 0;
3764 }
3765 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003766 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767
Willy Tarreau59234e92008-11-30 23:51:27 +01003768 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003769 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003770 * Note that doing so might move headers in the request, but
3771 * the fields will stay coherent and the URI will not move.
3772 * This should only be performed in the backend.
3773 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003774 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003775 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3776 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003777
Willy Tarreau59234e92008-11-30 23:51:27 +01003778 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003779 * 8: the appsession cookie was looked up very early in 1.2,
3780 * so let's do the same now.
3781 */
3782
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003783 /* It needs to look into the URI unless persistence must be ignored */
3784 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003785 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 +01003786 }
3787
William Lallemanda73203e2012-03-12 12:48:57 +01003788 /* add unique-id if "header-unique-id" is specified */
3789
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003790 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3791 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3792 goto return_bad_req;
3793 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003794 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003795 }
William Lallemanda73203e2012-03-12 12:48:57 +01003796
3797 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003798 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3799 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003800 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003801 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003802 goto return_bad_req;
3803 }
3804
Cyril Bontéb21570a2009-11-29 20:04:48 +01003805 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003806 * 9: add X-Forwarded-For if either the frontend or the backend
3807 * asks for it.
3808 */
3809 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003810 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003811 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003812 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3813 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003814 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003815 /* The header is set to be added only if none is present
3816 * and we found it, so don't do anything.
3817 */
3818 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003819 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003820 /* Add an X-Forwarded-For header unless the source IP is
3821 * in the 'except' network range.
3822 */
3823 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003824 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003825 != s->fe->except_net.s_addr) &&
3826 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003827 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003829 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003831 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003832
3833 /* Note: we rely on the backend to get the header name to be used for
3834 * x-forwarded-for, because the header is really meant for the backends.
3835 * However, if the backend did not specify any option, we have to rely
3836 * on the frontend's header name.
3837 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003838 if (s->be->fwdfor_hdr_len) {
3839 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003840 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003841 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003842 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003843 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003844 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003845 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003846
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003847 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003848 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003849 }
3850 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003851 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 /* FIXME: for the sake of completeness, we should also support
3853 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003854 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 int len;
3856 char pn[INET6_ADDRSTRLEN];
3857 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003858 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003859 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003860
Willy Tarreau59234e92008-11-30 23:51:27 +01003861 /* Note: we rely on the backend to get the header name to be used for
3862 * x-forwarded-for, because the header is really meant for the backends.
3863 * However, if the backend did not specify any option, we have to rely
3864 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003865 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003866 if (s->be->fwdfor_hdr_len) {
3867 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003868 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003869 } else {
3870 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003871 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003872 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003873 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003874
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003875 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003876 goto return_bad_req;
3877 }
3878 }
3879
3880 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003881 * 10: add X-Original-To if either the frontend or the backend
3882 * asks for it.
3883 */
3884 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3885
3886 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003887 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003888 /* Add an X-Original-To header unless the destination IP is
3889 * in the 'except' network range.
3890 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003891 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003892
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003893 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003894 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003895 (((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 +02003896 != s->fe->except_to.s_addr) &&
3897 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003898 (((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 +02003899 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003900 int len;
3901 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003902 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003903
3904 /* Note: we rely on the backend to get the header name to be used for
3905 * x-original-to, because the header is really meant for the backends.
3906 * However, if the backend did not specify any option, we have to rely
3907 * on the frontend's header name.
3908 */
3909 if (s->be->orgto_hdr_len) {
3910 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003912 } else {
3913 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003914 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003915 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003916 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003917
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003918 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003919 goto return_bad_req;
3920 }
3921 }
3922 }
3923
Willy Tarreau50fc7772012-11-11 22:19:57 +01003924 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3925 * If an "Upgrade" token is found, the header is left untouched in order not to have
3926 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3927 * "Upgrade" is present in the Connection header.
3928 */
3929 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3930 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3931 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003932 unsigned int want_flags = 0;
3933
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003934 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003935 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3936 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3937 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003938 want_flags |= TX_CON_CLO_SET;
3939 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003940 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3941 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3942 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003943 want_flags |= TX_CON_KAL_SET;
3944 }
3945
3946 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003947 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003948 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003949
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003950
Willy Tarreau522d6c02009-12-06 18:49:18 +01003951 /* If we have no server assigned yet and we're balancing on url_param
3952 * with a POST request, we may be interested in checking the body for
3953 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003954 */
3955 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3956 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003957 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003958 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003959 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003960 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003961 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003962
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003963 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003964 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003965#ifdef TCP_QUICKACK
3966 /* We expect some data from the client. Unless we know for sure
3967 * we already have a full request, we have to re-enable quick-ack
3968 * in case we previously disabled it, otherwise we might cause
3969 * the client to delay further data.
3970 */
3971 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003972 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003973 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003974 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003975 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003976#endif
3977 }
Willy Tarreau03945942009-12-22 16:50:27 +01003978
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 /*************************************************************
3980 * OK, that's finished for the headers. We have done what we *
3981 * could. Let's switch to the DATA state. *
3982 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003983 req->analyse_exp = TICK_ETERNITY;
3984 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003985
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003986 /* if the server closes the connection, we want to immediately react
3987 * and close the socket to save packets and syscalls.
3988 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003989 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3990 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003991
Willy Tarreau59234e92008-11-30 23:51:27 +01003992 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003993 /* OK let's go on with the BODY now */
3994 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003995
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003997 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003998 /* we detected a parsing error. We want to archive this request
3999 * in the dedicated proxy area for later troubleshooting.
4000 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004001 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004002 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004003
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 txn->req.msg_state = HTTP_MSG_ERROR;
4005 txn->status = 400;
4006 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004007 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004008
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004009 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004010 if (s->listener->counters)
4011 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004012
Willy Tarreau59234e92008-11-30 23:51:27 +01004013 if (!(s->flags & SN_ERR_MASK))
4014 s->flags |= SN_ERR_PRXCOND;
4015 if (!(s->flags & SN_FINST_MASK))
4016 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004017 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004018}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004019
Willy Tarreau60b85b02008-11-30 23:28:40 +01004020/* This function is an analyser which processes the HTTP tarpit. It always
4021 * returns zero, at the beginning because it prevents any other processing
4022 * from occurring, and at the end because it terminates the request.
4023 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004024int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004025{
4026 struct http_txn *txn = &s->txn;
4027
4028 /* This connection is being tarpitted. The CLIENT side has
4029 * already set the connect expiration date to the right
4030 * timeout. We just have to check that the client is still
4031 * there and that the timeout has not expired.
4032 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004033 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004034 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004035 !tick_is_expired(req->analyse_exp, now_ms))
4036 return 0;
4037
4038 /* We will set the queue timer to the time spent, just for
4039 * logging purposes. We fake a 500 server error, so that the
4040 * attacker will not suspect his connection has been tarpitted.
4041 * It will not cause trouble to the logs because we can exclude
4042 * the tarpitted connections by filtering on the 'PT' status flags.
4043 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004044 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4045
4046 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004047 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004048 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004049
4050 req->analysers = 0;
4051 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004052
Willy Tarreau60b85b02008-11-30 23:28:40 +01004053 if (!(s->flags & SN_ERR_MASK))
4054 s->flags |= SN_ERR_PRXCOND;
4055 if (!(s->flags & SN_FINST_MASK))
4056 s->flags |= SN_FINST_T;
4057 return 0;
4058}
4059
Willy Tarreaud34af782008-11-30 23:36:37 +01004060/* This function is an analyser which processes the HTTP request body. It looks
4061 * for parameters to be used for the load balancing algorithm (url_param). It
4062 * must only be called after the standard HTTP request processing has occurred,
4063 * because it expects the request to be parsed. It returns zero if it needs to
4064 * read more data, or 1 once it has completed its analysis.
4065 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004066int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004067{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004068 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004069 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004070 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004071
4072 /* We have to parse the HTTP request body to find any required data.
4073 * "balance url_param check_post" should have been the only way to get
4074 * into this. We were brought here after HTTP header analysis, so all
4075 * related structures are ready.
4076 */
4077
Willy Tarreau522d6c02009-12-06 18:49:18 +01004078 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4079 goto missing_data;
4080
4081 if (msg->msg_state < HTTP_MSG_100_SENT) {
4082 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4083 * send an HTTP/1.1 100 Continue intermediate response.
4084 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004085 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 struct hdr_ctx ctx;
4087 ctx.idx = 0;
4088 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004089 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004090 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004091 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004092 }
4093 }
4094 msg->msg_state = HTTP_MSG_100_SENT;
4095 }
4096
4097 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004098 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004099 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004100 * is still null. We must save the body in msg->next because it
4101 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004102 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004103 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004104
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004105 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4107 else
4108 msg->msg_state = HTTP_MSG_DATA;
4109 }
4110
4111 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004112 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004113 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004114 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004115 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004116 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004117
Willy Tarreau115acb92009-12-26 13:56:06 +01004118 if (!ret)
4119 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004120 else if (ret < 0) {
4121 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004122 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004123 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004124 }
4125
Willy Tarreaud98cf932009-12-27 22:54:55 +01004126 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004127 * We have the first data byte is in msg->sov. We're waiting for at
4128 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004129 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004130
Willy Tarreau124d9912011-03-01 20:30:48 +01004131 if (msg->body_len < limit)
4132 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004133
Willy Tarreau9b28e032012-10-12 23:49:43 +02004134 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004135 goto http_end;
4136
4137 missing_data:
4138 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004139 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004140 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004141 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004142 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004143
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004144 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004145 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004146 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004147
4148 if (!(s->flags & SN_ERR_MASK))
4149 s->flags |= SN_ERR_CLITO;
4150 if (!(s->flags & SN_FINST_MASK))
4151 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004153 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004154
4155 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004156 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004157 /* Not enough data. We'll re-use the http-request
4158 * timeout here. Ideally, we should set the timeout
4159 * relative to the accept() date. We just set the
4160 * request timeout once at the beginning of the
4161 * request.
4162 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004163 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004164 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004165 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004166 return 0;
4167 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004168
4169 http_end:
4170 /* The situation will not evolve, so let's give up on the analysis. */
4171 s->logs.tv_request = now; /* update the request timer to reflect full request */
4172 req->analysers &= ~an_bit;
4173 req->analyse_exp = TICK_ETERNITY;
4174 return 1;
4175
4176 return_bad_req: /* let's centralize all bad requests */
4177 txn->req.msg_state = HTTP_MSG_ERROR;
4178 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004179 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180
Willy Tarreau79ebac62010-06-07 13:47:49 +02004181 if (!(s->flags & SN_ERR_MASK))
4182 s->flags |= SN_ERR_PRXCOND;
4183 if (!(s->flags & SN_FINST_MASK))
4184 s->flags |= SN_FINST_R;
4185
Willy Tarreau522d6c02009-12-06 18:49:18 +01004186 return_err_msg:
4187 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004188 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004189 if (s->listener->counters)
4190 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004191 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004192}
4193
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004194/* send a server's name with an outgoing request over an established connection.
4195 * Note: this function is designed to be called once the request has been scheduled
4196 * for being forwarded. This is the reason why it rewinds the buffer before
4197 * proceeding.
4198 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004199int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004200
4201 struct hdr_ctx ctx;
4202
Mark Lamourinec2247f02012-01-04 13:02:01 -05004203 char *hdr_name = be->server_id_hdr_name;
4204 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004205 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004206 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004207 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004208
William Lallemandd9e90662012-01-30 17:27:17 +01004209 ctx.idx = 0;
4210
Willy Tarreau9b28e032012-10-12 23:49:43 +02004211 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004212 if (old_o) {
4213 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004214 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004215 }
4216
Willy Tarreau9b28e032012-10-12 23:49:43 +02004217 old_i = chn->buf->i;
4218 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 -05004219 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004220 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004221 }
4222
4223 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004224 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004225 memcpy(hdr_val, hdr_name, hdr_name_len);
4226 hdr_val += hdr_name_len;
4227 *hdr_val++ = ':';
4228 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004229 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4230 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004231
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004232 if (old_o) {
4233 /* If this was a forwarded request, we must readjust the amount of
4234 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004235 * variations. Note that if the request was already scheduled for
4236 * forwarding, it had its req->sol pointing to the body, which
4237 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004238 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004239 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004240 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004241 }
4242
Mark Lamourinec2247f02012-01-04 13:02:01 -05004243 return 0;
4244}
4245
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246/* Terminate current transaction and prepare a new one. This is very tricky
4247 * right now but it works.
4248 */
4249void http_end_txn_clean_session(struct session *s)
4250{
4251 /* FIXME: We need a more portable way of releasing a backend's and a
4252 * server's connections. We need a safer way to reinitialize buffer
4253 * flags. We also need a more accurate method for computing per-request
4254 * data.
4255 */
4256 http_silent_debug(__LINE__, s);
4257
Willy Tarreau4213a112013-12-15 10:25:42 +01004258 /* unless we're doing keep-alive, we want to quickly close the connection
4259 * to the server.
4260 */
4261 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4262 !si_conn_ready(s->req->cons)) {
4263 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4264 si_shutr(s->req->cons);
4265 si_shutw(s->req->cons);
4266 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267
4268 http_silent_debug(__LINE__, s);
4269
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004270 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004272 if (unlikely(s->srv_conn))
4273 sess_change_server(s, NULL);
4274 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275
4276 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4277 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004278 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279
4280 if (s->txn.status) {
4281 int n;
4282
4283 n = s->txn.status / 100;
4284 if (n < 1 || n > 5)
4285 n = 0;
4286
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004287 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004288 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004289 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004290 s->fe->fe_counters.p.http.comp_rsp++;
4291 }
Willy Tarreau24657792010-02-26 10:30:28 +01004292 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004293 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004294 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004295 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004296 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004297 s->be->be_counters.p.http.comp_rsp++;
4298 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 }
4300
4301 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004302 s->logs.bytes_in -= s->req->buf->i;
4303 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304
4305 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004306 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 !(s->flags & SN_MONITOR) &&
4308 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4309 s->do_log(s);
4310 }
4311
4312 s->logs.accept_date = date; /* user-visible date for logging */
4313 s->logs.tv_accept = now; /* corrected date for internal use */
4314 tv_zero(&s->logs.tv_request);
4315 s->logs.t_queue = -1;
4316 s->logs.t_connect = -1;
4317 s->logs.t_data = -1;
4318 s->logs.t_close = 0;
4319 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4320 s->logs.srv_queue_size = 0; /* we will get this number soon */
4321
Willy Tarreau9b28e032012-10-12 23:49:43 +02004322 s->logs.bytes_in = s->req->total = s->req->buf->i;
4323 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004324
4325 if (s->pend_pos)
4326 pendconn_free(s->pend_pos);
4327
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004328 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 if (s->flags & SN_CURR_SESS) {
4330 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004331 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004333 if (may_dequeue_tasks(objt_server(s->target), s->be))
4334 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 }
4336
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004337 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338
Willy Tarreau4213a112013-12-15 10:25:42 +01004339 /* only release our endpoint if we don't intend to reuse the
4340 * connection.
4341 */
4342 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4343 !si_conn_ready(s->req->cons)) {
4344 si_release_endpoint(s->req->cons);
4345 }
4346
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004348 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004349 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 s->req->cons->exp = TICK_ETERNITY;
4351 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004352 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4353 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 +02004354 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 +01004355 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004356
4357 if (s->flags & SN_COMP_READY)
4358 s->comp_algo->end(&s->comp_ctx);
4359 s->comp_algo = NULL;
4360 s->flags &= ~SN_COMP_READY;
4361
Willy Tarreau610ecce2010-01-04 21:15:02 +01004362 s->txn.meth = 0;
4363 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004364 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004365 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 s->req->cons->flags |= SI_FL_INDEP_STR;
4367
Willy Tarreau96e31212011-05-30 18:10:30 +02004368 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004369 s->req->flags |= CF_NEVER_WAIT;
4370 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004371 }
4372
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 /* if the request buffer is not empty, it means we're
4374 * about to process another request, so send pending
4375 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004376 * Just don't do this if the buffer is close to be full,
4377 * because the request will wait for it to flush a little
4378 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004379 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004380 if (s->req->buf->i) {
4381 if (s->rep->buf->o &&
4382 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4383 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004384 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004385 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004386
4387 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004388 channel_auto_read(s->req);
4389 channel_auto_close(s->req);
4390 channel_auto_read(s->rep);
4391 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392
Willy Tarreau342b11c2010-11-24 16:22:09 +01004393 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004394 s->rep->analysers = 0;
4395
4396 http_silent_debug(__LINE__, s);
4397}
4398
4399
4400/* This function updates the request state machine according to the response
4401 * state machine and buffer flags. It returns 1 if it changes anything (flag
4402 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4403 * it is only used to find when a request/response couple is complete. Both
4404 * this function and its equivalent should loop until both return zero. It
4405 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4406 */
4407int http_sync_req_state(struct session *s)
4408{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004409 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004411 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412 unsigned int old_state = txn->req.msg_state;
4413
4414 http_silent_debug(__LINE__, s);
4415 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4416 return 0;
4417
4418 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004419 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004420 * We can shut the read side unless we want to abort_on_close,
4421 * or we have a POST request. The issue with POST requests is
4422 * that some browsers still send a CRLF after the request, and
4423 * this CRLF must be read so that it does not remain in the kernel
4424 * buffers, otherwise a close could cause an RST on some systems
4425 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004426 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004427 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004428 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429
Willy Tarreau40f151a2012-12-20 12:10:09 +01004430 /* if the server closes the connection, we want to immediately react
4431 * and close the socket to save packets and syscalls.
4432 */
4433 chn->cons->flags |= SI_FL_NOHALF;
4434
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4436 goto wait_other_side;
4437
4438 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4439 /* The server has not finished to respond, so we
4440 * don't want to move in order not to upset it.
4441 */
4442 goto wait_other_side;
4443 }
4444
4445 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4446 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004447 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004449 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004450 goto wait_other_side;
4451 }
4452
4453 /* When we get here, it means that both the request and the
4454 * response have finished receiving. Depending on the connection
4455 * mode, we'll have to wait for the last bytes to leave in either
4456 * direction, and sometimes for a close to be effective.
4457 */
4458
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4460 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4462 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004463 }
4464 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4465 /* Option forceclose is set, or either side wants to close,
4466 * let's enforce it now that we're not expecting any new
4467 * data to come. The caller knows the session is complete
4468 * once both states are CLOSED.
4469 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004470 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4471 channel_shutr_now(chn);
4472 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004473 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004474 }
4475 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004476 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4477 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004478 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004479 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4480 channel_auto_read(chn);
4481 txn->req.msg_state = HTTP_MSG_TUNNEL;
4482 chn->flags |= CF_NEVER_WAIT;
4483 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484 }
4485
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004486 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004488 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004489
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004490 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 txn->req.msg_state = HTTP_MSG_CLOSING;
4492 goto http_msg_closing;
4493 }
4494 else {
4495 txn->req.msg_state = HTTP_MSG_CLOSED;
4496 goto http_msg_closed;
4497 }
4498 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004499 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 }
4501
4502 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4503 http_msg_closing:
4504 /* nothing else to forward, just waiting for the output buffer
4505 * to be empty and for the shutw_now to take effect.
4506 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 txn->req.msg_state = HTTP_MSG_CLOSED;
4509 goto http_msg_closed;
4510 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004511 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 txn->req.msg_state = HTTP_MSG_ERROR;
4513 goto wait_other_side;
4514 }
4515 }
4516
4517 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4518 http_msg_closed:
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004519 if (!(s->be->options & PR_O_ABRT_CLOSE))
4520 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521 goto wait_other_side;
4522 }
4523
4524 wait_other_side:
4525 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004526 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527}
4528
4529
4530/* This function updates the response state machine according to the request
4531 * state machine and buffer flags. It returns 1 if it changes anything (flag
4532 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4533 * it is only used to find when a request/response couple is complete. Both
4534 * this function and its equivalent should loop until both return zero. It
4535 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4536 */
4537int http_sync_res_state(struct session *s)
4538{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004539 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004541 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 unsigned int old_state = txn->rsp.msg_state;
4543
4544 http_silent_debug(__LINE__, s);
4545 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4546 return 0;
4547
4548 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4549 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004550 * still monitor the server connection for a possible close
4551 * while the request is being uploaded, so we don't disable
4552 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004554 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555
4556 if (txn->req.msg_state == HTTP_MSG_ERROR)
4557 goto wait_other_side;
4558
4559 if (txn->req.msg_state < HTTP_MSG_DONE) {
4560 /* The client seems to still be sending data, probably
4561 * because we got an error response during an upload.
4562 * We have the choice of either breaking the connection
4563 * or letting it pass through. Let's do the later.
4564 */
4565 goto wait_other_side;
4566 }
4567
4568 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4569 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004570 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004572 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 goto wait_other_side;
4574 }
4575
4576 /* When we get here, it means that both the request and the
4577 * response have finished receiving. Depending on the connection
4578 * mode, we'll have to wait for the last bytes to leave in either
4579 * direction, and sometimes for a close to be effective.
4580 */
4581
4582 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4583 /* Server-close mode : shut read and wait for the request
4584 * side to close its output buffer. The caller will detect
4585 * when we're in DONE and the other is in CLOSED and will
4586 * catch that for the final cleanup.
4587 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4589 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004591 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4592 /* Option forceclose is set, or either side wants to close,
4593 * let's enforce it now that we're not expecting any new
4594 * data to come. The caller knows the session is complete
4595 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004597 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4598 channel_shutr_now(chn);
4599 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004600 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004601 }
4602 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004603 /* The last possible modes are keep-alive and tunnel. Tunnel will
4604 * need to forward remaining data. Keep-alive will need to monitor
4605 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004607 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004608 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004609 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4610 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 }
4612
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004613 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004615 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004616 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4617 goto http_msg_closing;
4618 }
4619 else {
4620 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4621 goto http_msg_closed;
4622 }
4623 }
4624 goto wait_other_side;
4625 }
4626
4627 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4628 http_msg_closing:
4629 /* nothing else to forward, just waiting for the output buffer
4630 * to be empty and for the shutw_now to take effect.
4631 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004632 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4634 goto http_msg_closed;
4635 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004636 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004638 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004639 if (objt_server(s->target))
4640 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 goto wait_other_side;
4642 }
4643 }
4644
4645 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4646 http_msg_closed:
4647 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004648 bi_erase(chn);
4649 channel_auto_close(chn);
4650 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 goto wait_other_side;
4652 }
4653
4654 wait_other_side:
4655 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004656 /* We force the response to leave immediately if we're waiting for the
4657 * other side, since there is no pending shutdown to push it out.
4658 */
4659 if (!channel_is_empty(chn))
4660 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662}
4663
4664
4665/* Resync the request and response state machines. Return 1 if either state
4666 * changes.
4667 */
4668int http_resync_states(struct session *s)
4669{
4670 struct http_txn *txn = &s->txn;
4671 int old_req_state = txn->req.msg_state;
4672 int old_res_state = txn->rsp.msg_state;
4673
4674 http_silent_debug(__LINE__, s);
4675 http_sync_req_state(s);
4676 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004677 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 if (!http_sync_res_state(s))
4679 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004680 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 if (!http_sync_req_state(s))
4682 break;
4683 }
4684 http_silent_debug(__LINE__, s);
4685 /* OK, both state machines agree on a compatible state.
4686 * There are a few cases we're interested in :
4687 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4688 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4689 * directions, so let's simply disable both analysers.
4690 * - HTTP_MSG_CLOSED on the response only means we must abort the
4691 * request.
4692 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4693 * with server-close mode means we've completed one request and we
4694 * must re-initialize the server connection.
4695 */
4696
4697 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4698 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4699 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4700 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4701 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004702 channel_auto_close(s->req);
4703 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004705 channel_auto_close(s->rep);
4706 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004708 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4709 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004710 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004711 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004712 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004713 channel_auto_close(s->rep);
4714 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004715 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004716 channel_abort(s->req);
4717 channel_auto_close(s->req);
4718 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004719 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004721 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4722 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004724 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4725 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4726 /* server-close/keep-alive: terminate this transaction,
4727 * possibly killing the server connection and reinitialize
4728 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 */
4730 http_end_txn_clean_session(s);
4731 }
4732
4733 http_silent_debug(__LINE__, s);
4734 return txn->req.msg_state != old_req_state ||
4735 txn->rsp.msg_state != old_res_state;
4736}
4737
Willy Tarreaud98cf932009-12-27 22:54:55 +01004738/* This function is an analyser which forwards request body (including chunk
4739 * sizes if any). It is called as soon as we must forward, even if we forward
4740 * zero byte. The only situation where it must not be called is when we're in
4741 * tunnel mode and we want to forward till the close. It's used both to forward
4742 * remaining data and to resync after end of body. It expects the msg_state to
4743 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4744 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004745 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004746 * bytes of pending data + the headers if not already done (between sol and sov).
4747 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004748 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004749int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004750{
4751 struct http_txn *txn = &s->txn;
4752 struct http_msg *msg = &s->txn.req;
4753
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004754 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4755 return 0;
4756
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004757 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004758 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004759 /* Output closed while we were sending data. We must abort and
4760 * wake the other side up.
4761 */
4762 msg->msg_state = HTTP_MSG_ERROR;
4763 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004764 return 1;
4765 }
4766
Willy Tarreau4fe41902010-06-07 22:27:41 +02004767 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004768 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004769
4770 /* Note that we don't have to send 100-continue back because we don't
4771 * need the data to complete our job, and it's up to the server to
4772 * decide whether to return 100, 417 or anything else in return of
4773 * an "Expect: 100-continue" header.
4774 */
4775
4776 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004777 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004778 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004779 * is still null. We must save the body in msg->next because it
4780 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004782 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004783
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004784 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004785 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004786 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004787 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004788 }
4789
Willy Tarreaud98cf932009-12-27 22:54:55 +01004790 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004791 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004792
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004794 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004795 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004796 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004797 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004798 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004799 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004800 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004801 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004802
Willy Tarreaucaabe412010-01-03 23:08:28 +01004803 if (msg->msg_state == HTTP_MSG_DATA) {
4804 /* must still forward */
4805 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004806 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004807
4808 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004809 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004810 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004812 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004813 }
4814 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004815 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004816 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004817 * TRAILERS state.
4818 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004819 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004820
Willy Tarreau54d23df2012-10-25 19:04:45 +02004821 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004823 else if (ret < 0) {
4824 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004825 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004826 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004827 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004828 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004830 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004831 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004832 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004833 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834
4835 if (ret == 0)
4836 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004837 else if (ret < 0) {
4838 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004839 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004840 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004842 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004843 /* we're in MSG_CHUNK_SIZE now */
4844 }
4845 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004846 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847
4848 if (ret == 0)
4849 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004850 else if (ret < 0) {
4851 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004852 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004853 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004854 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004855 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 /* we're in HTTP_MSG_DONE now */
4857 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004859 int old_state = msg->msg_state;
4860
Willy Tarreau610ecce2010-01-04 21:15:02 +01004861 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004862 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004863 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4864 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004865 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 if (http_resync_states(s)) {
4867 /* some state changes occurred, maybe the analyser
4868 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004869 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004870 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004871 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004872 /* request errors are most likely due to
4873 * the server aborting the transfer.
4874 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004876 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004877 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004878 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004879 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004880 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004881 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004882 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004883
4884 /* If "option abortonclose" is set on the backend, we
4885 * want to monitor the client's connection and forward
4886 * any shutdown notification to the server, which will
4887 * decide whether to close or to go on processing the
4888 * request.
4889 */
4890 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_auto_read(req);
4892 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004893 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004894 else if (s->txn.meth == HTTP_METH_POST) {
4895 /* POST requests may require to read extra CRLF
4896 * sent by broken browsers and which could cause
4897 * an RST to be sent upon close on some systems
4898 * (eg: Linux).
4899 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004900 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004901 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004904 }
4905 }
4906
Willy Tarreaud98cf932009-12-27 22:54:55 +01004907 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004908 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004909 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004910 if (!(s->flags & SN_ERR_MASK))
4911 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004912 if (!(s->flags & SN_FINST_MASK)) {
4913 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4914 s->flags |= SN_FINST_H;
4915 else
4916 s->flags |= SN_FINST_D;
4917 }
4918
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004919 s->fe->fe_counters.cli_aborts++;
4920 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004921 if (objt_server(s->target))
4922 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004923
4924 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004925 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004926
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004927 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004928 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004929 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004930
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004931 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004932 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004933 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004934 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004935 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004936
Willy Tarreau5c620922011-05-11 19:56:11 +02004937 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004938 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004939 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004940 * modes are already handled by the stream sock layer. We must not do
4941 * this in content-length mode because it could present the MSG_MORE
4942 * flag with the last block of forwarded data, which would cause an
4943 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004944 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004945 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004946 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004947
Willy Tarreau610ecce2010-01-04 21:15:02 +01004948 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004949 return 0;
4950
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004952 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004953 if (s->listener->counters)
4954 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004955 return_bad_req_stats_ok:
4956 txn->req.msg_state = HTTP_MSG_ERROR;
4957 if (txn->status) {
4958 /* Note: we don't send any error if some data were already sent */
4959 stream_int_retnclose(req->prod, NULL);
4960 } else {
4961 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004962 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 }
4964 req->analysers = 0;
4965 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004966
4967 if (!(s->flags & SN_ERR_MASK))
4968 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004969 if (!(s->flags & SN_FINST_MASK)) {
4970 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4971 s->flags |= SN_FINST_H;
4972 else
4973 s->flags |= SN_FINST_D;
4974 }
4975 return 0;
4976
4977 aborted_xfer:
4978 txn->req.msg_state = HTTP_MSG_ERROR;
4979 if (txn->status) {
4980 /* Note: we don't send any error if some data were already sent */
4981 stream_int_retnclose(req->prod, NULL);
4982 } else {
4983 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004984 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004985 }
4986 req->analysers = 0;
4987 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4988
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004989 s->fe->fe_counters.srv_aborts++;
4990 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004991 if (objt_server(s->target))
4992 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004993
4994 if (!(s->flags & SN_ERR_MASK))
4995 s->flags |= SN_ERR_SRVCL;
4996 if (!(s->flags & SN_FINST_MASK)) {
4997 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4998 s->flags |= SN_FINST_H;
4999 else
5000 s->flags |= SN_FINST_D;
5001 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005002 return 0;
5003}
5004
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005005/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5006 * processing can continue on next analysers, or zero if it either needs more
5007 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5008 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5009 * when it has nothing left to do, and may remove any analyser when it wants to
5010 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005011 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005012int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005013{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014 struct http_txn *txn = &s->txn;
5015 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005016 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005017 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005018 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005019 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005020
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005021 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 +02005022 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005024 rep,
5025 rep->rex, rep->wex,
5026 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005027 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005028 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005029
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005030 /*
5031 * Now parse the partial (or complete) lines.
5032 * We will check the response syntax, and also join multi-line
5033 * headers. An index of all the lines will be elaborated while
5034 * parsing.
5035 *
5036 * For the parsing, we use a 28 states FSM.
5037 *
5038 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005039 * rep->buf->p = beginning of response
5040 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5041 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005042 * msg->eol = end of current header or line (LF or CRLF)
5043 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005044 */
5045
Willy Tarreau83e3af02009-12-28 17:39:57 +01005046 /* There's a protected area at the end of the buffer for rewriting
5047 * purposes. We don't want to start to parse the request if the
5048 * protected area is affected, because we may have to move processed
5049 * data later, which is much more complicated.
5050 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005051 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005052 if (unlikely(!channel_reserved(rep))) {
5053 /* some data has still not left the buffer, wake us once that's done */
5054 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5055 goto abort_response;
5056 channel_dont_close(rep);
5057 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5058 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005059 }
5060
Willy Tarreau379357a2013-06-08 12:55:46 +02005061 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5062 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5063 buffer_slow_realign(rep->buf);
5064
Willy Tarreau9b28e032012-10-12 23:49:43 +02005065 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005066 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005067 }
5068
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005069 /* 1: we might have to print this header in debug mode */
5070 if (unlikely((global.mode & MODE_DEBUG) &&
5071 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005072 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005074
Willy Tarreau9b28e032012-10-12 23:49:43 +02005075 sol = rep->buf->p;
5076 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 sol += hdr_idx_first_pos(&txn->hdr_idx);
5080 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 while (cur_idx) {
5083 eol = sol + txn->hdr_idx.v[cur_idx].len;
5084 debug_hdr("srvhdr", s, sol, eol);
5085 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5086 cur_idx = txn->hdr_idx.v[cur_idx].next;
5087 }
5088 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090 /*
5091 * Now we quickly check if we have found a full valid response.
5092 * If not so, we check the FD and buffer states before leaving.
5093 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005094 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 * responses are checked first.
5096 *
5097 * Depending on whether the client is still there or not, we
5098 * may send an error response back or not. Note that normally
5099 * we should only check for HTTP status there, and check I/O
5100 * errors somewhere else.
5101 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005102
Willy Tarreau655dce92009-11-08 13:10:58 +01005103 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005104 /* Invalid response */
5105 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5106 /* we detected a parsing error. We want to archive this response
5107 * in the dedicated proxy area for later troubleshooting.
5108 */
5109 hdr_response_bad:
5110 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005111 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005112
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005113 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005114 if (objt_server(s->target)) {
5115 objt_server(s->target)->counters.failed_resp++;
5116 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005117 }
Willy Tarreau64648412010-03-05 10:41:54 +01005118 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005119 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 rep->analysers = 0;
5121 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005122 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005123 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005124 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005125
5126 if (!(s->flags & SN_ERR_MASK))
5127 s->flags |= SN_ERR_PRXCOND;
5128 if (!(s->flags & SN_FINST_MASK))
5129 s->flags |= SN_FINST_H;
5130
5131 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005132 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005135 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005136 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005137 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 goto hdr_response_bad;
5139 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005142 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005144 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005145 else if (txn->flags & TX_NOT_FIRST)
5146 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005147
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005148 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005149 if (objt_server(s->target)) {
5150 objt_server(s->target)->counters.failed_resp++;
5151 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005152 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005153
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005154 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005155 rep->analysers = 0;
5156 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005157 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005158 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005159 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161 if (!(s->flags & SN_ERR_MASK))
5162 s->flags |= SN_ERR_SRVCL;
5163 if (!(s->flags & SN_FINST_MASK))
5164 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005165 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005166 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005167
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005169 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005170 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005171 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005172 else if (txn->flags & TX_NOT_FIRST)
5173 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005174
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005175 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005176 if (objt_server(s->target)) {
5177 objt_server(s->target)->counters.failed_resp++;
5178 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005179 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005180
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005181 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 rep->analysers = 0;
5183 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005184 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005185 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005186 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005187
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005188 if (!(s->flags & SN_ERR_MASK))
5189 s->flags |= SN_ERR_SRVTO;
5190 if (!(s->flags & SN_FINST_MASK))
5191 s->flags |= SN_FINST_H;
5192 return 0;
5193 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005194
Willy Tarreauf003d372012-11-26 13:35:37 +01005195 /* client abort with an abortonclose */
5196 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5197 s->fe->fe_counters.cli_aborts++;
5198 s->be->be_counters.cli_aborts++;
5199 if (objt_server(s->target))
5200 objt_server(s->target)->counters.cli_aborts++;
5201
5202 rep->analysers = 0;
5203 channel_auto_close(rep);
5204
5205 txn->status = 400;
5206 bi_erase(rep);
5207 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5208
5209 if (!(s->flags & SN_ERR_MASK))
5210 s->flags |= SN_ERR_CLICL;
5211 if (!(s->flags & SN_FINST_MASK))
5212 s->flags |= SN_FINST_H;
5213
5214 /* process_session() will take care of the error */
5215 return 0;
5216 }
5217
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005218 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005219 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005220 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005221 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005222 else if (txn->flags & TX_NOT_FIRST)
5223 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005224
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005225 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005226 if (objt_server(s->target)) {
5227 objt_server(s->target)->counters.failed_resp++;
5228 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005229 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005230
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005231 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005232 rep->analysers = 0;
5233 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005234 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005235 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005236 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005237
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238 if (!(s->flags & SN_ERR_MASK))
5239 s->flags |= SN_ERR_SRVCL;
5240 if (!(s->flags & SN_FINST_MASK))
5241 s->flags |= SN_FINST_H;
5242 return 0;
5243 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005244
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005245 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005246 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005248 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005249 else if (txn->flags & TX_NOT_FIRST)
5250 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005251
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005252 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005254 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255
5256 if (!(s->flags & SN_ERR_MASK))
5257 s->flags |= SN_ERR_CLICL;
5258 if (!(s->flags & SN_FINST_MASK))
5259 s->flags |= SN_FINST_H;
5260
5261 /* process_session() will take care of the error */
5262 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005263 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005264
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005265 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005266 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 return 0;
5268 }
5269
5270 /* More interesting part now : we know that we have a complete
5271 * response which at least looks like HTTP. We have an indicator
5272 * of each header's length, so we can parse them quickly.
5273 */
5274
5275 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005276 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005278 /*
5279 * 1: get the status code
5280 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005281 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282 if (n < 1 || n > 5)
5283 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005284 /* when the client triggers a 4xx from the server, it's most often due
5285 * to a missing object or permission. These events should be tracked
5286 * because if they happen often, it may indicate a brute force or a
5287 * vulnerability scan.
5288 */
5289 if (n == 4)
5290 session_inc_http_err_ctr(s);
5291
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005292 if (objt_server(s->target))
5293 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294
Willy Tarreau5b154472009-12-21 20:11:07 +01005295 /* check if the response is HTTP/1.1 or above */
5296 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005297 ((rep->buf->p[5] > '1') ||
5298 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005299 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005300
5301 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005302 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 +01005303
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005304 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005305 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005306
Willy Tarreau9b28e032012-10-12 23:49:43 +02005307 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308
Willy Tarreau39650402010-03-15 19:44:39 +01005309 /* Adjust server's health based on status code. Note: status codes 501
5310 * and 505 are triggered on demand by client request, so we must not
5311 * count them as server failures.
5312 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005313 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005314 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005315 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005316 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005317 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005318 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005319
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320 /*
5321 * 2: check for cacheability.
5322 */
5323
5324 switch (txn->status) {
5325 case 200:
5326 case 203:
5327 case 206:
5328 case 300:
5329 case 301:
5330 case 410:
5331 /* RFC2616 @13.4:
5332 * "A response received with a status code of
5333 * 200, 203, 206, 300, 301 or 410 MAY be stored
5334 * by a cache (...) unless a cache-control
5335 * directive prohibits caching."
5336 *
5337 * RFC2616 @9.5: POST method :
5338 * "Responses to this method are not cacheable,
5339 * unless the response includes appropriate
5340 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005341 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005343 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005344 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5345 break;
5346 default:
5347 break;
5348 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005349
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005350 /*
5351 * 3: we may need to capture headers
5352 */
5353 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005354 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005355 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005356 txn->rsp.cap, s->fe->rsp_cap);
5357
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005358 /* 4: determine the transfer-length.
5359 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5360 * the presence of a message-body in a RESPONSE and its transfer length
5361 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005362 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005363 * All responses to the HEAD request method MUST NOT include a
5364 * message-body, even though the presence of entity-header fields
5365 * might lead one to believe they do. All 1xx (informational), 204
5366 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5367 * message-body. All other responses do include a message-body,
5368 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005369 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005370 * 1. Any response which "MUST NOT" include a message-body (such as the
5371 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5372 * always terminated by the first empty line after the header fields,
5373 * regardless of the entity-header fields present in the message.
5374 *
5375 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5376 * the "chunked" transfer-coding (Section 6.2) is used, the
5377 * transfer-length is defined by the use of this transfer-coding.
5378 * If a Transfer-Encoding header field is present and the "chunked"
5379 * transfer-coding is not present, the transfer-length is defined by
5380 * the sender closing the connection.
5381 *
5382 * 3. If a Content-Length header field is present, its decimal value in
5383 * OCTETs represents both the entity-length and the transfer-length.
5384 * If a message is received with both a Transfer-Encoding header
5385 * field and a Content-Length header field, the latter MUST be ignored.
5386 *
5387 * 4. If the message uses the media type "multipart/byteranges", and
5388 * the transfer-length is not otherwise specified, then this self-
5389 * delimiting media type defines the transfer-length. This media
5390 * type MUST NOT be used unless the sender knows that the recipient
5391 * can parse it; the presence in a request of a Range header with
5392 * multiple byte-range specifiers from a 1.1 client implies that the
5393 * client can parse multipart/byteranges responses.
5394 *
5395 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005396 */
5397
5398 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005399 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005400 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005401 * FIXME: should we parse anyway and return an error on chunked encoding ?
5402 */
5403 if (txn->meth == HTTP_METH_HEAD ||
5404 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005405 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005406 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005407 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005408 goto skip_content_length;
5409 }
5410
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005411 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005412 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005413 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005414 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005415 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005416 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5417 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005418 /* bad transfer-encoding (chunked followed by something else) */
5419 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005420 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005421 break;
5422 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005423 }
5424
5425 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5426 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005427 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005428 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005429 signed long long cl;
5430
Willy Tarreauad14f752011-09-02 20:33:27 +02005431 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005432 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005433 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005434 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005435
Willy Tarreauad14f752011-09-02 20:33:27 +02005436 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005437 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005438 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005439 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005440
Willy Tarreauad14f752011-09-02 20:33:27 +02005441 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005442 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005443 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005444 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005445
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005446 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005447 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005448 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005449 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005450
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005451 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005452 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005453 }
5454
William Lallemand82fe75c2012-10-23 10:25:10 +02005455 if (s->fe->comp || s->be->comp)
5456 select_compression_response_header(s, rep->buf);
5457
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005458 /* FIXME: we should also implement the multipart/byterange method.
5459 * For now on, we resort to close mode in this case (unknown length).
5460 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005461skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005462
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 /* end of job, return OK */
5464 rep->analysers &= ~an_bit;
5465 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005466 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005467 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005468
5469 abort_keep_alive:
5470 /* A keep-alive request to the server failed on a network error.
5471 * The client is required to retry. We need to close without returning
5472 * any other information so that the client retries.
5473 */
5474 txn->status = 0;
5475 rep->analysers = 0;
5476 s->req->analysers = 0;
5477 channel_auto_close(rep);
5478 s->logs.logwait = 0;
5479 s->logs.level = 0;
5480 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5481 bi_erase(rep);
5482 stream_int_retnclose(rep->cons, NULL);
5483 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005484}
5485
5486/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005487 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5488 * and updates t->rep->analysers. It might make sense to explode it into several
5489 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005490 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005491int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005492{
5493 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005494 struct http_msg *msg = &txn->rsp;
5495 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005496 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005497 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005498
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005499 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 +02005500 now_ms, __FUNCTION__,
5501 t,
5502 rep,
5503 rep->rex, rep->wex,
5504 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005505 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005506 rep->analysers);
5507
Willy Tarreau655dce92009-11-08 13:10:58 +01005508 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005509 return 0;
5510
5511 rep->analysers &= ~an_bit;
5512 rep->analyse_exp = TICK_ETERNITY;
5513
Willy Tarreau5b154472009-12-21 20:11:07 +01005514 /* Now we have to check if we need to modify the Connection header.
5515 * This is more difficult on the response than it is on the request,
5516 * because we can have two different HTTP versions and we don't know
5517 * how the client will interprete a response. For instance, let's say
5518 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5519 * HTTP/1.1 response without any header. Maybe it will bound itself to
5520 * HTTP/1.0 because it only knows about it, and will consider the lack
5521 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5522 * the lack of header as a keep-alive. Thus we will use two flags
5523 * indicating how a request MAY be understood by the client. In case
5524 * of multiple possibilities, we'll fix the header to be explicit. If
5525 * ambiguous cases such as both close and keepalive are seen, then we
5526 * will fall back to explicit close. Note that we won't take risks with
5527 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005528 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005529 */
5530
Willy Tarreaudc008c52010-02-01 16:20:08 +01005531 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5532 txn->status == 101)) {
5533 /* Either we've established an explicit tunnel, or we're
5534 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005535 * to understand the next protocols. We have to switch to tunnel
5536 * mode, so that we transfer the request and responses then let
5537 * this protocol pass unmodified. When we later implement specific
5538 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005539 * header which contains information about that protocol for
5540 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005541 */
5542 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5543 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005544 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5545 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5546 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005547 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005548
Willy Tarreau60466522010-01-18 19:08:45 +01005549 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005550 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005551 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5552 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005553
Willy Tarreau60466522010-01-18 19:08:45 +01005554 /* now adjust header transformations depending on current state */
5555 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5556 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5557 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005558 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005559 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005560 }
Willy Tarreau60466522010-01-18 19:08:45 +01005561 else { /* SCL / KAL */
5562 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005563 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005564 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005565 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005566
Willy Tarreau60466522010-01-18 19:08:45 +01005567 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005568 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005569
Willy Tarreau60466522010-01-18 19:08:45 +01005570 /* Some keep-alive responses are converted to Server-close if
5571 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005572 */
Willy Tarreau60466522010-01-18 19:08:45 +01005573 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5574 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005575 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005576 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005577 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005578 }
5579
Willy Tarreau7959a552013-09-23 16:44:27 +02005580 /* we want to have the response time before we start processing it */
5581 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5582
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005583 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005584 /*
5585 * 3: we will have to evaluate the filters.
5586 * As opposed to version 1.2, now they will be evaluated in the
5587 * filters order and not in the header order. This means that
5588 * each filter has to be validated among all headers.
5589 *
5590 * Filters are tried with ->be first, then with ->fe if it is
5591 * different from ->be.
5592 */
5593
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005594 cur_proxy = t->be;
5595 while (1) {
5596 struct proxy *rule_set = cur_proxy;
5597
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005598 /* evaluate http-response rules */
5599 if (!http_res_last_rule)
5600 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5601
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005602 /* try headers filters */
5603 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005604 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005605 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005606 if (objt_server(t->target)) {
5607 objt_server(t->target)->counters.failed_resp++;
5608 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005609 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005610 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005611 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005612 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005613 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005614 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005615 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005616 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005617 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005618 if (!(t->flags & SN_ERR_MASK))
5619 t->flags |= SN_ERR_PRXCOND;
5620 if (!(t->flags & SN_FINST_MASK))
5621 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005622 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005623 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005624 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005625
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005626 /* has the response been denied ? */
5627 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005628 if (objt_server(t->target))
5629 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005630
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005631 t->be->be_counters.denied_resp++;
5632 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005633 if (t->listener->counters)
5634 t->listener->counters->denied_resp++;
5635
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005636 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005638
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005639 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005640 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005641 if (txn->status < 200)
5642 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005643 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005644 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005645 ret = acl_pass(ret);
5646 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5647 ret = !ret;
5648 if (!ret)
5649 continue;
5650 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005651 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005652 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005653 }
5654
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005655 /* check whether we're already working on the frontend */
5656 if (cur_proxy == t->fe)
5657 break;
5658 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005659 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005660
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005661 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005662 * We may be facing a 100-continue response, in which case this
5663 * is not the right response, and we're waiting for the next one.
5664 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005665 * next one.
5666 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005667 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005668 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005669 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005670 msg->msg_state = HTTP_MSG_RPBEFORE;
5671 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005672 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005673 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5674 return 1;
5675 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005676 else if (unlikely(txn->status < 200))
5677 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005678
5679 /* we don't have any 1xx status code now */
5680
5681 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005682 * 4: check for server cookie.
5683 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005684 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5685 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005686 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005687
Willy Tarreaubaaee002006-06-26 02:48:02 +02005688
Willy Tarreaua15645d2007-03-18 16:22:39 +01005689 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005690 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005691 */
Willy Tarreau67402132012-05-31 20:40:20 +02005692 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005693 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005695 /*
5696 * 6: add server cookie in the response if needed
5697 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005698 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005699 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005700 (!(t->flags & SN_DIRECT) ||
5701 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5702 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5703 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5704 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005705 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005706 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005707 /* the server is known, it's not the one the client requested, or the
5708 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005709 * insert a set-cookie here, except if we want to insert only on POST
5710 * requests and this one isn't. Note that servers which don't have cookies
5711 * (eg: some backup servers) will return a full cookie removal request.
5712 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005713 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005714 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005715 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5716 t->be->cookie_name);
5717 }
5718 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005719 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005720
5721 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5722 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005723 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5724 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5725 trash.len += 5;
5726
Willy Tarreauef4f3912010-10-07 21:00:29 +02005727 if (t->be->cookie_maxlife) {
5728 /* emit first_date, which is either the original one or
5729 * the current date.
5730 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005731 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005732 s30tob64(txn->cookie_first_date ?
5733 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005734 (date.tv_sec+3) >> 2, trash.str + trash.len);
5735 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005736 }
5737 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005738 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005739 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005740
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005742 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005743
Willy Tarreau4992dd22012-05-31 21:02:17 +02005744 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005745 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005746
5747 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005748 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005749
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005750 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005752
Willy Tarreauf1348312010-10-07 15:54:11 +02005753 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005754 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005755 /* the server did not change, only the date was updated */
5756 txn->flags |= TX_SCK_UPDATED;
5757 else
5758 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005760 /* Here, we will tell an eventual cache on the client side that we don't
5761 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5762 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5763 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5764 */
Willy Tarreau67402132012-05-31 20:40:20 +02005765 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5768
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005769 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005770 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005771 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005772 }
5773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775 /*
5776 * 7: check if result will be cacheable with a cookie.
5777 * We'll block the response if security checks have caught
5778 * nasty things such as a cacheable cookie.
5779 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005780 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5781 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005782 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005783
5784 /* we're in presence of a cacheable response containing
5785 * a set-cookie header. We'll block it as requested by
5786 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005788 if (objt_server(t->target))
5789 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005790
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005791 t->be->be_counters.denied_resp++;
5792 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005793 if (t->listener->counters)
5794 t->listener->counters->denied_resp++;
5795
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005796 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005797 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798 send_log(t->be, LOG_ALERT,
5799 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005800 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005801 goto return_srv_prx_502;
5802 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005803
5804 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005805 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005806 * If an "Upgrade" token is found, the header is left untouched in order
5807 * not to have to deal with some client bugs : some of them fail an upgrade
5808 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005809 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005810 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5811 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5812 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005813 unsigned int want_flags = 0;
5814
5815 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5816 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5817 /* we want a keep-alive response here. Keep-alive header
5818 * required if either side is not 1.1.
5819 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005820 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005821 want_flags |= TX_CON_KAL_SET;
5822 }
5823 else {
5824 /* we want a close response here. Close header required if
5825 * the server is 1.1, regardless of the client.
5826 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005827 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005828 want_flags |= TX_CON_CLO_SET;
5829 }
5830
5831 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005832 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005833 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005834
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005835 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005836 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005837 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005838 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005839
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005840 /*************************************************************
5841 * OK, that's finished for the headers. We have done what we *
5842 * could. Let's switch to the DATA state. *
5843 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005844
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845 /* if the user wants to log as soon as possible, without counting
5846 * bytes from the server, then this is the right moment. We have
5847 * to temporarily assign bytes_out to log what we currently have.
5848 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005849 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005850 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5851 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005852 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005853 t->logs.bytes_out = 0;
5854 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005856 /* Note: we must not try to cheat by jumping directly to DATA,
5857 * otherwise we would not let the client side wake up.
5858 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005860 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005861 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005862 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005864
Willy Tarreaud98cf932009-12-27 22:54:55 +01005865/* This function is an analyser which forwards response body (including chunk
5866 * sizes if any). It is called as soon as we must forward, even if we forward
5867 * zero byte. The only situation where it must not be called is when we're in
5868 * tunnel mode and we want to forward till the close. It's used both to forward
5869 * remaining data and to resync after end of body. It expects the msg_state to
5870 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5871 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005872 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005873 * bytes of pending data + the headers if not already done (between sol and sov).
5874 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005875 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005876int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005877{
5878 struct http_txn *txn = &s->txn;
5879 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005880 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005881 static struct buffer *tmpbuf = NULL;
5882 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005883 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005884 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005885
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005886 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5887 return 0;
5888
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005889 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005890 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005891 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005892 /* Output closed while we were sending data. We must abort and
5893 * wake the other side up.
5894 */
5895 msg->msg_state = HTTP_MSG_ERROR;
5896 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005897 return 1;
5898 }
5899
Willy Tarreau4fe41902010-06-07 22:27:41 +02005900 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005901 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005902
William Lallemand82fe75c2012-10-23 10:25:10 +02005903 /* this is the first time we need the compression buffer */
5904 if (s->comp_algo != NULL && tmpbuf == NULL) {
5905 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5906 goto aborted_xfer; /* no memory */
5907 }
5908
Willy Tarreaud98cf932009-12-27 22:54:55 +01005909 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005910 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005911 * rep->buf.p still points to the beginning of the message and msg->sol
5912 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005913 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005914 channel_forward(res, msg->sov);
5915 msg->next = 0;
5916 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005917
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005918 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005919 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005920 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005921 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005922 }
5923
William Lallemand82fe75c2012-10-23 10:25:10 +02005924 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005925 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005926 if (ret < 0)
5927 goto missing_data; /* not enough spaces in buffers */
5928 compressing = 1;
5929 }
5930
Willy Tarreaud98cf932009-12-27 22:54:55 +01005931 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005932 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005933 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005934 if (s->comp_algo == NULL) {
5935 bytes = msg->sov - msg->sol;
5936 if (msg->chunk_len || bytes) {
5937 msg->sol = msg->sov;
5938 msg->next -= bytes; /* will be forwarded */
5939 msg->chunk_len += bytes;
5940 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5941 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005942 }
5943
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005944 switch (msg->msg_state - HTTP_MSG_DATA) {
5945 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005946 if (compressing) {
5947 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5948 if (ret < 0)
5949 goto aborted_xfer;
5950 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005951
5952 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005953 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005954
5955 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005956 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005957 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005958 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005959 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005960 if (compressing && consumed_data) {
5961 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5962 compressing = 0;
5963 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005964 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005965 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005966 /* fall through for HTTP_MSG_CHUNK_CRLF */
5967
5968 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5969 /* we want the CRLF after the data */
5970
5971 ret = http_skip_chunk_crlf(msg);
5972 if (ret == 0)
5973 goto missing_data;
5974 else if (ret < 0) {
5975 if (msg->err_pos >= 0)
5976 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5977 goto return_bad_res;
5978 }
5979 /* skipping data in buffer for compression */
5980 if (compressing) {
5981 b_adv(res->buf, msg->next);
5982 msg->next = 0;
5983 msg->sov = 0;
5984 msg->sol = 0;
5985 }
5986 /* we're in MSG_CHUNK_SIZE now, fall through */
5987
5988 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005989 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005990 * set ->sov and ->next to point to the body and switch to DATA or
5991 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005992 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005993
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005994 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005995 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005996 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005997 else if (ret < 0) {
5998 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005999 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006000 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006001 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006002 if (compressing) {
6003 if (likely(msg->chunk_len > 0)) {
6004 /* skipping data if we are in compression mode */
6005 b_adv(res->buf, msg->next);
6006 msg->next = 0;
6007 msg->sov = 0;
6008 msg->sol = 0;
6009 } else {
6010 if (consumed_data) {
6011 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6012 compressing = 0;
6013 }
6014 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006015 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006016 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006017 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006018
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006019 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6020 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006021 if (ret == 0)
6022 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006023 else if (ret < 0) {
6024 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006025 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006026 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006027 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006028 if (s->comp_algo != NULL) {
6029 /* forwarding trailers */
6030 channel_forward(res, msg->next);
6031 msg->next = 0;
6032 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006033 /* we're in HTTP_MSG_DONE now, but we might still have
6034 * some data pending, so let's loop over once.
6035 */
6036 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006037
6038 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006039 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006040
6041 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006042 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006043 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6044 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006045 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006046 if (http_resync_states(s)) {
6047 http_silent_debug(__LINE__, s);
6048 /* some state changes occurred, maybe the analyser
6049 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006050 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006051 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006052 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006053 /* response errors are most likely due to
6054 * the client aborting the transfer.
6055 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006056 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006057 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006058 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006059 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006060 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006061 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006062 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006063 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006064 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065 }
6066 }
6067
Willy Tarreaud98cf932009-12-27 22:54:55 +01006068 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006069 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006070 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6071 compressing = 0;
6072 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006073
6074 if (res->flags & CF_SHUTW)
6075 goto aborted_xfer;
6076
6077 /* stop waiting for data if the input is closed before the end. If the
6078 * client side was already closed, it means that the client has aborted,
6079 * so we don't want to count this as a server abort. Otherwise it's a
6080 * server abort.
6081 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006082 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006083 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6084 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006085 if (!(s->flags & SN_ERR_MASK))
6086 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006087 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006088 if (objt_server(s->target))
6089 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006090 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006091 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006092
Willy Tarreau40dba092010-03-04 18:14:51 +01006093 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006094 if (!s->req->analysers)
6095 goto return_bad_res;
6096
Willy Tarreauea953162012-05-18 23:41:28 +02006097 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006098 if (s->comp_algo == NULL) {
6099 bytes = msg->sov - msg->sol;
6100 if (msg->chunk_len || bytes) {
6101 msg->sol = msg->sov;
6102 msg->next -= bytes; /* will be forwarded */
6103 msg->chunk_len += bytes;
6104 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6105 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006106 }
6107
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006108 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006109 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006110 * Similarly, with keep-alive on the client side, we don't want to forward a
6111 * close.
6112 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006113 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6115 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006116 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006117
Willy Tarreau5c620922011-05-11 19:56:11 +02006118 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006119 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006120 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006121 * modes are already handled by the stream sock layer. We must not do
6122 * this in content-length mode because it could present the MSG_MORE
6123 * flag with the last block of forwarded data, which would cause an
6124 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006125 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006126 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006127 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006128
Willy Tarreaud98cf932009-12-27 22:54:55 +01006129 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006130 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006131 return 0;
6132
Willy Tarreau40dba092010-03-04 18:14:51 +01006133 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006134 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006135 if (objt_server(s->target))
6136 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006137
6138 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006139 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006140 /* don't send any error message as we're in the body */
6141 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006142 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006143 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006144 if (objt_server(s->target))
6145 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006146
6147 if (!(s->flags & SN_ERR_MASK))
6148 s->flags |= SN_ERR_PRXCOND;
6149 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006150 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006151 return 0;
6152
6153 aborted_xfer:
6154 txn->rsp.msg_state = HTTP_MSG_ERROR;
6155 /* don't send any error message as we're in the body */
6156 stream_int_retnclose(res->cons, NULL);
6157 res->analysers = 0;
6158 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6159
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006160 s->fe->fe_counters.cli_aborts++;
6161 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006162 if (objt_server(s->target))
6163 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006164
6165 if (!(s->flags & SN_ERR_MASK))
6166 s->flags |= SN_ERR_CLICL;
6167 if (!(s->flags & SN_FINST_MASK))
6168 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006169 return 0;
6170}
6171
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006172/* Iterate the same filter through all request headers.
6173 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006174 * Since it can manage the switch to another backend, it updates the per-proxy
6175 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006177int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006179 char term;
6180 char *cur_ptr, *cur_end, *cur_next;
6181 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006182 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006184 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006186 last_hdr = 0;
6187
Willy Tarreau9b28e032012-10-12 23:49:43 +02006188 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006189 old_idx = 0;
6190
6191 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006192 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006193 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006194 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006195 (exp->action == ACT_ALLOW ||
6196 exp->action == ACT_DENY ||
6197 exp->action == ACT_TARPIT))
6198 return 0;
6199
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006200 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006201 if (!cur_idx)
6202 break;
6203
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006204 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 cur_ptr = cur_next;
6206 cur_end = cur_ptr + cur_hdr->len;
6207 cur_next = cur_end + cur_hdr->cr + 1;
6208
6209 /* Now we have one header between cur_ptr and cur_end,
6210 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211 */
6212
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006213 /* The annoying part is that pattern matching needs
6214 * that we modify the contents to null-terminate all
6215 * strings before testing them.
6216 */
6217
6218 term = *cur_end;
6219 *cur_end = '\0';
6220
6221 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6222 switch (exp->action) {
6223 case ACT_SETBE:
6224 /* It is not possible to jump a second time.
6225 * FIXME: should we return an HTTP/500 here so that
6226 * the admin knows there's a problem ?
6227 */
6228 if (t->be != t->fe)
6229 break;
6230
6231 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006232 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006233 last_hdr = 1;
6234 break;
6235
6236 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006237 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006238 last_hdr = 1;
6239 break;
6240
6241 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006242 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006243 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006244 break;
6245
6246 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006247 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006248 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006249 break;
6250
6251 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006252 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6253 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006254 /* FIXME: if the user adds a newline in the replacement, the
6255 * index will not be recalculated for now, and the new line
6256 * will not be counted as a new header.
6257 */
6258
6259 cur_end += delta;
6260 cur_next += delta;
6261 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006262 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006263 break;
6264
6265 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006266 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006267 cur_next += delta;
6268
Willy Tarreaufa355d42009-11-29 18:12:29 +01006269 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006270 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6271 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006272 cur_hdr->len = 0;
6273 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006274 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006275 break;
6276
6277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006279 if (cur_end)
6280 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 /* keep the link from this header to next one in case of later
6283 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285 old_idx = cur_idx;
6286 }
6287 return 0;
6288}
6289
6290
6291/* Apply the filter to the request line.
6292 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6293 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006294 * Since it can manage the switch to another backend, it updates the per-proxy
6295 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006296 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006297int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298{
6299 char term;
6300 char *cur_ptr, *cur_end;
6301 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006302 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006303 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006304
Willy Tarreau3d300592007-03-18 18:34:41 +01006305 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006307 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006308 (exp->action == ACT_ALLOW ||
6309 exp->action == ACT_DENY ||
6310 exp->action == ACT_TARPIT))
6311 return 0;
6312 else if (exp->action == ACT_REMOVE)
6313 return 0;
6314
6315 done = 0;
6316
Willy Tarreau9b28e032012-10-12 23:49:43 +02006317 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006318 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006319
6320 /* Now we have the request line between cur_ptr and cur_end */
6321
6322 /* The annoying part is that pattern matching needs
6323 * that we modify the contents to null-terminate all
6324 * strings before testing them.
6325 */
6326
6327 term = *cur_end;
6328 *cur_end = '\0';
6329
6330 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6331 switch (exp->action) {
6332 case ACT_SETBE:
6333 /* It is not possible to jump a second time.
6334 * FIXME: should we return an HTTP/500 here so that
6335 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 if (t->be != t->fe)
6338 break;
6339
6340 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006341 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 done = 1;
6343 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006346 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 done = 1;
6348 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006351 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 done = 1;
6353 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006355 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006356 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 done = 1;
6358 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 case ACT_REPLACE:
6361 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006362 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6363 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006364 /* FIXME: if the user adds a newline in the replacement, the
6365 * index will not be recalculated for now, and the new line
6366 * will not be counted as a new header.
6367 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006368
Willy Tarreaufa355d42009-11-29 18:12:29 +01006369 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006371 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006372 HTTP_MSG_RQMETH,
6373 cur_ptr, cur_end + 1,
6374 NULL, NULL);
6375 if (unlikely(!cur_end))
6376 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006378 /* we have a full request and we know that we have either a CR
6379 * or an LF at <ptr>.
6380 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006381 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6382 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006383 /* there is no point trying this regex on headers */
6384 return 1;
6385 }
6386 }
6387 *cur_end = term; /* restore the string terminator */
6388 return done;
6389}
Willy Tarreau97de6242006-12-27 17:18:38 +01006390
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006393/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006394 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006395 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006396 * unparsable request. Since it can manage the switch to another backend, it
6397 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006399int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006401 struct http_txn *txn = &s->txn;
6402 struct hdr_exp *exp;
6403
6404 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006405 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006407 /*
6408 * The interleaving of transformations and verdicts
6409 * makes it difficult to decide to continue or stop
6410 * the evaluation.
6411 */
6412
Willy Tarreau6c123b12010-01-28 20:22:06 +01006413 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6414 break;
6415
Willy Tarreau3d300592007-03-18 18:34:41 +01006416 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006418 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006420
6421 /* if this filter had a condition, evaluate it now and skip to
6422 * next filter if the condition does not match.
6423 */
6424 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006425 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006426 ret = acl_pass(ret);
6427 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6428 ret = !ret;
6429
6430 if (!ret)
6431 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 }
6433
6434 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006435 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 if (unlikely(ret < 0))
6437 return -1;
6438
6439 if (likely(ret == 0)) {
6440 /* The filter did not match the request, it can be
6441 * iterated through all headers.
6442 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006443 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447}
6448
6449
Willy Tarreaua15645d2007-03-18 16:22:39 +01006450
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006452 * Try to retrieve the server associated to the appsession.
6453 * If the server is found, it's assigned to the session.
6454 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006455void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006456 struct http_txn *txn = &t->txn;
6457 appsess *asession = NULL;
6458 char *sessid_temp = NULL;
6459
Cyril Bontéb21570a2009-11-29 20:04:48 +01006460 if (len > t->be->appsession_len) {
6461 len = t->be->appsession_len;
6462 }
6463
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006464 if (t->be->options2 & PR_O2_AS_REQL) {
6465 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006466 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006467 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006468 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006469 }
6470
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006471 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006472 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6473 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6474 return;
6475 }
6476
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006477 memcpy(txn->sessid, buf, len);
6478 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006479 }
6480
6481 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6482 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6483 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6484 return;
6485 }
6486
Cyril Bontéb21570a2009-11-29 20:04:48 +01006487 memcpy(sessid_temp, buf, len);
6488 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006489
6490 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6491 /* free previously allocated memory */
6492 pool_free2(apools.sessid, sessid_temp);
6493
6494 if (asession != NULL) {
6495 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6496 if (!(t->be->options2 & PR_O2_AS_REQL))
6497 asession->request_count++;
6498
6499 if (asession->serverid != NULL) {
6500 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006501
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006502 while (srv) {
6503 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006504 if ((srv->state & SRV_RUNNING) ||
6505 (t->be->options & PR_O_PERSIST) ||
6506 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006507 /* we found the server and it's usable */
6508 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006509 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006510 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006511 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006512
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006513 break;
6514 } else {
6515 txn->flags &= ~TX_CK_MASK;
6516 txn->flags |= TX_CK_DOWN;
6517 }
6518 }
6519 srv = srv->next;
6520 }
6521 }
6522 }
6523}
6524
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525/* Find the end of a cookie value contained between <s> and <e>. It works the
6526 * same way as with headers above except that the semi-colon also ends a token.
6527 * See RFC2965 for more information. Note that it requires a valid header to
6528 * return a valid result.
6529 */
6530char *find_cookie_value_end(char *s, const char *e)
6531{
6532 int quoted, qdpair;
6533
6534 quoted = qdpair = 0;
6535 for (; s < e; s++) {
6536 if (qdpair) qdpair = 0;
6537 else if (quoted) {
6538 if (*s == '\\') qdpair = 1;
6539 else if (*s == '"') quoted = 0;
6540 }
6541 else if (*s == '"') quoted = 1;
6542 else if (*s == ',' || *s == ';') return s;
6543 }
6544 return s;
6545}
6546
6547/* Delete a value in a header between delimiters <from> and <next> in buffer
6548 * <buf>. The number of characters displaced is returned, and the pointer to
6549 * the first delimiter is updated if required. The function tries as much as
6550 * possible to respect the following principles :
6551 * - replace <from> delimiter by the <next> one unless <from> points to a
6552 * colon, in which case <next> is simply removed
6553 * - set exactly one space character after the new first delimiter, unless
6554 * there are not enough characters in the block being moved to do so.
6555 * - remove unneeded spaces before the previous delimiter and after the new
6556 * one.
6557 *
6558 * It is the caller's responsibility to ensure that :
6559 * - <from> points to a valid delimiter or the colon ;
6560 * - <next> points to a valid delimiter or the final CR/LF ;
6561 * - there are non-space chars before <from> ;
6562 * - there is a CR/LF at or after <next>.
6563 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006564int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006565{
6566 char *prev = *from;
6567
6568 if (*prev == ':') {
6569 /* We're removing the first value, preserve the colon and add a
6570 * space if possible.
6571 */
6572 if (!http_is_crlf[(unsigned char)*next])
6573 next++;
6574 prev++;
6575 if (prev < next)
6576 *prev++ = ' ';
6577
6578 while (http_is_spht[(unsigned char)*next])
6579 next++;
6580 } else {
6581 /* Remove useless spaces before the old delimiter. */
6582 while (http_is_spht[(unsigned char)*(prev-1)])
6583 prev--;
6584 *from = prev;
6585
6586 /* copy the delimiter and if possible a space if we're
6587 * not at the end of the line.
6588 */
6589 if (!http_is_crlf[(unsigned char)*next]) {
6590 *prev++ = *next++;
6591 if (prev + 1 < next)
6592 *prev++ = ' ';
6593 while (http_is_spht[(unsigned char)*next])
6594 next++;
6595 }
6596 }
6597 return buffer_replace2(buf, prev, next, NULL, 0);
6598}
6599
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006600/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006601 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 * desirable to call it only when needed. This code is quite complex because
6603 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6604 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006606void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006607{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006608 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006609 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6612 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006614 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006615 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006616 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006618 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006619 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006620 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006621
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006622 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 hdr_beg = hdr_next;
6624 hdr_end = hdr_beg + cur_hdr->len;
6625 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006627 /* We have one full header between hdr_beg and hdr_end, and the
6628 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629 * "Cookie:" headers.
6630 */
6631
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006632 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006633 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634 old_idx = cur_idx;
6635 continue;
6636 }
6637
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006638 del_from = NULL; /* nothing to be deleted */
6639 preserve_hdr = 0; /* assume we may kill the whole header */
6640
Willy Tarreau58f10d72006-12-04 02:26:12 +01006641 /* Now look for cookies. Conforming to RFC2109, we have to support
6642 * attributes whose name begin with a '$', and associate them with
6643 * the right cookie, if we want to delete this cookie.
6644 * So there are 3 cases for each cookie read :
6645 * 1) it's a special attribute, beginning with a '$' : ignore it.
6646 * 2) it's a server id cookie that we *MAY* want to delete : save
6647 * some pointers on it (last semi-colon, beginning of cookie...)
6648 * 3) it's an application cookie : we *MAY* have to delete a previous
6649 * "special" cookie.
6650 * At the end of loop, if a "special" cookie remains, we may have to
6651 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006652 * *MUST* delete it.
6653 *
6654 * Note: RFC2965 is unclear about the processing of spaces around
6655 * the equal sign in the ATTR=VALUE form. A careful inspection of
6656 * the RFC explicitly allows spaces before it, and not within the
6657 * tokens (attrs or values). An inspection of RFC2109 allows that
6658 * too but section 10.1.3 lets one think that spaces may be allowed
6659 * after the equal sign too, resulting in some (rare) buggy
6660 * implementations trying to do that. So let's do what servers do.
6661 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6662 * allowed quoted strings in values, with any possible character
6663 * after a backslash, including control chars and delimitors, which
6664 * causes parsing to become ambiguous. Browsers also allow spaces
6665 * within values even without quotes.
6666 *
6667 * We have to keep multiple pointers in order to support cookie
6668 * removal at the beginning, middle or end of header without
6669 * corrupting the header. All of these headers are valid :
6670 *
6671 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6672 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6673 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6674 * | | | | | | | | |
6675 * | | | | | | | | hdr_end <--+
6676 * | | | | | | | +--> next
6677 * | | | | | | +----> val_end
6678 * | | | | | +-----------> val_beg
6679 * | | | | +--------------> equal
6680 * | | | +----------------> att_end
6681 * | | +---------------------> att_beg
6682 * | +--------------------------> prev
6683 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684 */
6685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006686 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6687 /* Iterate through all cookies on this line */
6688
6689 /* find att_beg */
6690 att_beg = prev + 1;
6691 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6692 att_beg++;
6693
6694 /* find att_end : this is the first character after the last non
6695 * space before the equal. It may be equal to hdr_end.
6696 */
6697 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699 while (equal < hdr_end) {
6700 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006701 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 if (http_is_spht[(unsigned char)*equal++])
6703 continue;
6704 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006705 }
6706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6708 * is between <att_beg> and <equal>, both may be identical.
6709 */
6710
6711 /* look for end of cookie if there is an equal sign */
6712 if (equal < hdr_end && *equal == '=') {
6713 /* look for the beginning of the value */
6714 val_beg = equal + 1;
6715 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6716 val_beg++;
6717
6718 /* find the end of the value, respecting quotes */
6719 next = find_cookie_value_end(val_beg, hdr_end);
6720
6721 /* make val_end point to the first white space or delimitor after the value */
6722 val_end = next;
6723 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6724 val_end--;
6725 } else {
6726 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006728
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006729 /* We have nothing to do with attributes beginning with '$'. However,
6730 * they will automatically be removed if a header before them is removed,
6731 * since they're supposed to be linked together.
6732 */
6733 if (*att_beg == '$')
6734 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006735
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006736 /* Ignore cookies with no equal sign */
6737 if (equal == next) {
6738 /* This is not our cookie, so we must preserve it. But if we already
6739 * scheduled another cookie for removal, we cannot remove the
6740 * complete header, but we can remove the previous block itself.
6741 */
6742 preserve_hdr = 1;
6743 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006744 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006745 val_end += delta;
6746 next += delta;
6747 hdr_end += delta;
6748 hdr_next += delta;
6749 cur_hdr->len += delta;
6750 http_msg_move_end(&txn->req, delta);
6751 prev = del_from;
6752 del_from = NULL;
6753 }
6754 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 /* if there are spaces around the equal sign, we need to
6758 * strip them otherwise we'll get trouble for cookie captures,
6759 * or even for rewrites. Since this happens extremely rarely,
6760 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006761 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6763 int stripped_before = 0;
6764 int stripped_after = 0;
6765
6766 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006767 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 equal += stripped_before;
6769 val_beg += stripped_before;
6770 }
6771
6772 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006773 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 val_beg += stripped_after;
6775 stripped_before += stripped_after;
6776 }
6777
6778 val_end += stripped_before;
6779 next += stripped_before;
6780 hdr_end += stripped_before;
6781 hdr_next += stripped_before;
6782 cur_hdr->len += stripped_before;
6783 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006784 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 /* First, let's see if we want to capture this cookie. We check
6788 * that we don't already have a client side cookie, because we
6789 * can only capture one. Also as an optimisation, we ignore
6790 * cookies shorter than the declared name.
6791 */
6792 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6793 (val_end - att_beg >= t->fe->capture_namelen) &&
6794 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6795 int log_len = val_end - att_beg;
6796
6797 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6798 Alert("HTTP logging : out of memory.\n");
6799 } else {
6800 if (log_len > t->fe->capture_len)
6801 log_len = t->fe->capture_len;
6802 memcpy(txn->cli_cookie, att_beg, log_len);
6803 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006804 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006805 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806
Willy Tarreaubca99692010-10-06 19:25:55 +02006807 /* Persistence cookies in passive, rewrite or insert mode have the
6808 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006809 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006810 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006812 * For cookies in prefix mode, the form is :
6813 *
6814 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006815 */
6816 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6817 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6818 struct server *srv = t->be->srv;
6819 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6822 * have the server ID between val_beg and delim, and the original cookie between
6823 * delim+1 and val_end. Otherwise, delim==val_end :
6824 *
6825 * Cookie: NAME=SRV; # in all but prefix modes
6826 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6827 * | || || | |+-> next
6828 * | || || | +--> val_end
6829 * | || || +---------> delim
6830 * | || |+------------> val_beg
6831 * | || +-------------> att_end = equal
6832 * | |+-----------------> att_beg
6833 * | +------------------> prev
6834 * +-------------------------> hdr_beg
6835 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006836
Willy Tarreau67402132012-05-31 20:40:20 +02006837 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 for (delim = val_beg; delim < val_end; delim++)
6839 if (*delim == COOKIE_DELIM)
6840 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006841 } else {
6842 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006843 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006844 /* Now check if the cookie contains a date field, which would
6845 * appear after a vertical bar ('|') just after the server name
6846 * and before the delimiter.
6847 */
6848 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6849 if (vbar1) {
6850 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006851 * right is the last seen date. It is a base64 encoded
6852 * 30-bit value representing the UNIX date since the
6853 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006854 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006855 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006856 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006857 if (val_end - vbar1 >= 5) {
6858 val = b64tos30(vbar1);
6859 if (val > 0)
6860 txn->cookie_last_date = val << 2;
6861 }
6862 /* look for a second vertical bar */
6863 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6864 if (vbar1 && (val_end - vbar1 > 5)) {
6865 val = b64tos30(vbar1 + 1);
6866 if (val > 0)
6867 txn->cookie_first_date = val << 2;
6868 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006869 }
6870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreauf64d1412010-10-07 20:06:11 +02006872 /* if the cookie has an expiration date and the proxy wants to check
6873 * it, then we do that now. We first check if the cookie is too old,
6874 * then only if it has expired. We detect strict overflow because the
6875 * time resolution here is not great (4 seconds). Cookies with dates
6876 * in the future are ignored if their offset is beyond one day. This
6877 * allows an admin to fix timezone issues without expiring everyone
6878 * and at the same time avoids keeping unwanted side effects for too
6879 * long.
6880 */
6881 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006882 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6883 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006884 txn->flags &= ~TX_CK_MASK;
6885 txn->flags |= TX_CK_OLD;
6886 delim = val_beg; // let's pretend we have not found the cookie
6887 txn->cookie_first_date = 0;
6888 txn->cookie_last_date = 0;
6889 }
6890 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006891 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6892 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006893 txn->flags &= ~TX_CK_MASK;
6894 txn->flags |= TX_CK_EXPIRED;
6895 delim = val_beg; // let's pretend we have not found the cookie
6896 txn->cookie_first_date = 0;
6897 txn->cookie_last_date = 0;
6898 }
6899
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006900 /* Here, we'll look for the first running server which supports the cookie.
6901 * This allows to share a same cookie between several servers, for example
6902 * to dedicate backup servers to specific servers only.
6903 * However, to prevent clients from sticking to cookie-less backup server
6904 * when they have incidentely learned an empty cookie, we simply ignore
6905 * empty cookies and mark them as invalid.
6906 * The same behaviour is applied when persistence must be ignored.
6907 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006908 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 while (srv) {
6912 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6913 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6914 if ((srv->state & SRV_RUNNING) ||
6915 (t->be->options & PR_O_PERSIST) ||
6916 (t->flags & SN_FORCE_PRST)) {
6917 /* we found the server and we can use it */
6918 txn->flags &= ~TX_CK_MASK;
6919 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6920 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006921 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 break;
6923 } else {
6924 /* we found a server, but it's down,
6925 * mark it as such and go on in case
6926 * another one is available.
6927 */
6928 txn->flags &= ~TX_CK_MASK;
6929 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006931 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006932 srv = srv->next;
6933 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006934
Willy Tarreauf64d1412010-10-07 20:06:11 +02006935 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006936 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006938 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6939 txn->flags |= TX_CK_UNUSED;
6940 else
6941 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 /* depending on the cookie mode, we may have to either :
6945 * - delete the complete cookie if we're in insert+indirect mode, so that
6946 * the server never sees it ;
6947 * - remove the server id from the cookie value, and tag the cookie as an
6948 * application cookie so that it does not get accidentely removed later,
6949 * if we're in cookie prefix mode
6950 */
Willy Tarreau67402132012-05-31 20:40:20 +02006951 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953
Willy Tarreau9b28e032012-10-12 23:49:43 +02006954 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006955 val_end += delta;
6956 next += delta;
6957 hdr_end += delta;
6958 hdr_next += delta;
6959 cur_hdr->len += delta;
6960 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 del_from = NULL;
6963 preserve_hdr = 1; /* we want to keep this cookie */
6964 }
6965 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006966 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 del_from = prev;
6968 }
6969 } else {
6970 /* This is not our cookie, so we must preserve it. But if we already
6971 * scheduled another cookie for removal, we cannot remove the
6972 * complete header, but we can remove the previous block itself.
6973 */
6974 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006977 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006978 if (att_beg >= del_from)
6979 att_beg += delta;
6980 if (att_end >= del_from)
6981 att_end += delta;
6982 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 val_end += delta;
6984 next += delta;
6985 hdr_end += delta;
6986 hdr_next += delta;
6987 cur_hdr->len += delta;
6988 http_msg_move_end(&txn->req, delta);
6989 prev = del_from;
6990 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 /* Look for the appsession cookie unless persistence must be ignored */
6995 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6996 int cmp_len, value_len;
6997 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 if (t->be->options2 & PR_O2_AS_PFX) {
7000 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7001 value_begin = att_beg + t->be->appsession_name_len;
7002 value_len = val_end - att_beg - t->be->appsession_name_len;
7003 } else {
7004 cmp_len = att_end - att_beg;
7005 value_begin = val_beg;
7006 value_len = val_end - val_beg;
7007 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007008
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007009 /* let's see if the cookie is our appcookie */
7010 if (cmp_len == t->be->appsession_name_len &&
7011 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7012 manage_client_side_appsession(t, value_begin, value_len);
7013 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007014 }
7015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 /* continue with next cookie on this header line */
7017 att_beg = next;
7018 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007020 /* There are no more cookies on this line.
7021 * We may still have one (or several) marked for deletion at the
7022 * end of the line. We must do this now in two ways :
7023 * - if some cookies must be preserved, we only delete from the
7024 * mark to the end of line ;
7025 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007026 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007028 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007029 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007030 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007031 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007032 cur_hdr->len += delta;
7033 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007034 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035
7036 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007037 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7038 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007040 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007041 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007043 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044 }
7045
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049}
7050
7051
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052/* Iterate the same filter through all response headers contained in <rtr>.
7053 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7054 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007055int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056{
7057 char term;
7058 char *cur_ptr, *cur_end, *cur_next;
7059 int cur_idx, old_idx, last_hdr;
7060 struct http_txn *txn = &t->txn;
7061 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007062 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063
7064 last_hdr = 0;
7065
Willy Tarreau9b28e032012-10-12 23:49:43 +02007066 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 old_idx = 0;
7068
7069 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007070 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007072 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 (exp->action == ACT_ALLOW ||
7074 exp->action == ACT_DENY))
7075 return 0;
7076
7077 cur_idx = txn->hdr_idx.v[old_idx].next;
7078 if (!cur_idx)
7079 break;
7080
7081 cur_hdr = &txn->hdr_idx.v[cur_idx];
7082 cur_ptr = cur_next;
7083 cur_end = cur_ptr + cur_hdr->len;
7084 cur_next = cur_end + cur_hdr->cr + 1;
7085
7086 /* Now we have one header between cur_ptr and cur_end,
7087 * and the next header starts at cur_next.
7088 */
7089
7090 /* The annoying part is that pattern matching needs
7091 * that we modify the contents to null-terminate all
7092 * strings before testing them.
7093 */
7094
7095 term = *cur_end;
7096 *cur_end = '\0';
7097
7098 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7099 switch (exp->action) {
7100 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 last_hdr = 1;
7103 break;
7104
7105 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007106 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107 last_hdr = 1;
7108 break;
7109
7110 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007111 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7112 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113 /* FIXME: if the user adds a newline in the replacement, the
7114 * index will not be recalculated for now, and the new line
7115 * will not be counted as a new header.
7116 */
7117
7118 cur_end += delta;
7119 cur_next += delta;
7120 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007121 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122 break;
7123
7124 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007125 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126 cur_next += delta;
7127
Willy Tarreaufa355d42009-11-29 18:12:29 +01007128 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7130 txn->hdr_idx.used--;
7131 cur_hdr->len = 0;
7132 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007133 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 break;
7135
7136 }
7137 }
7138 if (cur_end)
7139 *cur_end = term; /* restore the string terminator */
7140
7141 /* keep the link from this header to next one in case of later
7142 * removal of next header.
7143 */
7144 old_idx = cur_idx;
7145 }
7146 return 0;
7147}
7148
7149
7150/* Apply the filter to the status line in the response buffer <rtr>.
7151 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7152 * or -1 if a replacement resulted in an invalid status line.
7153 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007154int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155{
7156 char term;
7157 char *cur_ptr, *cur_end;
7158 int done;
7159 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007160 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161
7162
Willy Tarreau3d300592007-03-18 18:34:41 +01007163 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007165 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 (exp->action == ACT_ALLOW ||
7167 exp->action == ACT_DENY))
7168 return 0;
7169 else if (exp->action == ACT_REMOVE)
7170 return 0;
7171
7172 done = 0;
7173
Willy Tarreau9b28e032012-10-12 23:49:43 +02007174 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007175 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176
7177 /* Now we have the status line between cur_ptr and cur_end */
7178
7179 /* The annoying part is that pattern matching needs
7180 * that we modify the contents to null-terminate all
7181 * strings before testing them.
7182 */
7183
7184 term = *cur_end;
7185 *cur_end = '\0';
7186
7187 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7188 switch (exp->action) {
7189 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007190 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191 done = 1;
7192 break;
7193
7194 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007195 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 done = 1;
7197 break;
7198
7199 case ACT_REPLACE:
7200 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007201 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7202 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 /* FIXME: if the user adds a newline in the replacement, the
7204 * index will not be recalculated for now, and the new line
7205 * will not be counted as a new header.
7206 */
7207
Willy Tarreaufa355d42009-11-29 18:12:29 +01007208 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007210 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007211 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 cur_ptr, cur_end + 1,
7213 NULL, NULL);
7214 if (unlikely(!cur_end))
7215 return -1;
7216
7217 /* we have a full respnse and we know that we have either a CR
7218 * or an LF at <ptr>.
7219 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007220 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007221 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 /* there is no point trying this regex on headers */
7223 return 1;
7224 }
7225 }
7226 *cur_end = term; /* restore the string terminator */
7227 return done;
7228}
7229
7230
7231
7232/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007233 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7235 * unparsable response.
7236 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007237int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007239 struct http_txn *txn = &s->txn;
7240 struct hdr_exp *exp;
7241
7242 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 int ret;
7244
7245 /*
7246 * The interleaving of transformations and verdicts
7247 * makes it difficult to decide to continue or stop
7248 * the evaluation.
7249 */
7250
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007251 if (txn->flags & TX_SVDENY)
7252 break;
7253
Willy Tarreau3d300592007-03-18 18:34:41 +01007254 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7256 exp->action == ACT_PASS)) {
7257 exp = exp->next;
7258 continue;
7259 }
7260
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007261 /* if this filter had a condition, evaluate it now and skip to
7262 * next filter if the condition does not match.
7263 */
7264 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007265 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007266 ret = acl_pass(ret);
7267 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7268 ret = !ret;
7269 if (!ret)
7270 continue;
7271 }
7272
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007274 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 if (unlikely(ret < 0))
7276 return -1;
7277
7278 if (likely(ret == 0)) {
7279 /* The filter did not match the response, it can be
7280 * iterated through all headers.
7281 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007282 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 }
7285 return 0;
7286}
7287
7288
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007290 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007291 * desirable to call it only when needed. This function is also used when we
7292 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007294void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295{
7296 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007297 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007298 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007300 char *hdr_beg, *hdr_end, *hdr_next;
7301 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 /* Iterate through the headers.
7304 * we start with the start line.
7305 */
7306 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007307 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308
7309 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7310 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007311 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
7313 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007314 hdr_beg = hdr_next;
7315 hdr_end = hdr_beg + cur_hdr->len;
7316 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317
Willy Tarreau24581ba2010-08-31 22:39:35 +02007318 /* We have one full header between hdr_beg and hdr_end, and the
7319 * next header starts at hdr_next. We're only interested in
7320 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321 */
7322
Willy Tarreau24581ba2010-08-31 22:39:35 +02007323 is_cookie2 = 0;
7324 prev = hdr_beg + 10;
7325 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007326 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7328 if (!val) {
7329 old_idx = cur_idx;
7330 continue;
7331 }
7332 is_cookie2 = 1;
7333 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 }
7335
Willy Tarreau24581ba2010-08-31 22:39:35 +02007336 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7337 * <prev> points to the colon.
7338 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007339 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340
Willy Tarreau24581ba2010-08-31 22:39:35 +02007341 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7342 * check-cache is enabled) and we are not interested in checking
7343 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007344 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007345 if (t->be->cookie_name == NULL &&
7346 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007347 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 return;
7349
Willy Tarreau24581ba2010-08-31 22:39:35 +02007350 /* OK so now we know we have to process this response cookie.
7351 * The format of the Set-Cookie header is slightly different
7352 * from the format of the Cookie header in that it does not
7353 * support the comma as a cookie delimiter (thus the header
7354 * cannot be folded) because the Expires attribute described in
7355 * the original Netscape's spec may contain an unquoted date
7356 * with a comma inside. We have to live with this because
7357 * many browsers don't support Max-Age and some browsers don't
7358 * support quoted strings. However the Set-Cookie2 header is
7359 * clean.
7360 *
7361 * We have to keep multiple pointers in order to support cookie
7362 * removal at the beginning, middle or end of header without
7363 * corrupting the header (in case of set-cookie2). A special
7364 * pointer, <scav> points to the beginning of the set-cookie-av
7365 * fields after the first semi-colon. The <next> pointer points
7366 * either to the end of line (set-cookie) or next unquoted comma
7367 * (set-cookie2). All of these headers are valid :
7368 *
7369 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7370 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7371 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7372 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7373 * | | | | | | | | | |
7374 * | | | | | | | | +-> next hdr_end <--+
7375 * | | | | | | | +------------> scav
7376 * | | | | | | +--------------> val_end
7377 * | | | | | +--------------------> val_beg
7378 * | | | | +----------------------> equal
7379 * | | | +------------------------> att_end
7380 * | | +----------------------------> att_beg
7381 * | +------------------------------> prev
7382 * +-----------------------------------------> hdr_beg
7383 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384
Willy Tarreau24581ba2010-08-31 22:39:35 +02007385 for (; prev < hdr_end; prev = next) {
7386 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387
Willy Tarreau24581ba2010-08-31 22:39:35 +02007388 /* find att_beg */
7389 att_beg = prev + 1;
7390 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7391 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392
Willy Tarreau24581ba2010-08-31 22:39:35 +02007393 /* find att_end : this is the first character after the last non
7394 * space before the equal. It may be equal to hdr_end.
7395 */
7396 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397
Willy Tarreau24581ba2010-08-31 22:39:35 +02007398 while (equal < hdr_end) {
7399 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7400 break;
7401 if (http_is_spht[(unsigned char)*equal++])
7402 continue;
7403 att_end = equal;
7404 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405
Willy Tarreau24581ba2010-08-31 22:39:35 +02007406 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7407 * is between <att_beg> and <equal>, both may be identical.
7408 */
7409
7410 /* look for end of cookie if there is an equal sign */
7411 if (equal < hdr_end && *equal == '=') {
7412 /* look for the beginning of the value */
7413 val_beg = equal + 1;
7414 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7415 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416
Willy Tarreau24581ba2010-08-31 22:39:35 +02007417 /* find the end of the value, respecting quotes */
7418 next = find_cookie_value_end(val_beg, hdr_end);
7419
7420 /* make val_end point to the first white space or delimitor after the value */
7421 val_end = next;
7422 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7423 val_end--;
7424 } else {
7425 /* <equal> points to next comma, semi-colon or EOL */
7426 val_beg = val_end = next = equal;
7427 }
7428
7429 if (next < hdr_end) {
7430 /* Set-Cookie2 supports multiple cookies, and <next> points to
7431 * a colon or semi-colon before the end. So skip all attr-value
7432 * pairs and look for the next comma. For Set-Cookie, since
7433 * commas are permitted in values, skip to the end.
7434 */
7435 if (is_cookie2)
7436 next = find_hdr_value_end(next, hdr_end);
7437 else
7438 next = hdr_end;
7439 }
7440
7441 /* Now everything is as on the diagram above */
7442
7443 /* Ignore cookies with no equal sign */
7444 if (equal == val_end)
7445 continue;
7446
7447 /* If there are spaces around the equal sign, we need to
7448 * strip them otherwise we'll get trouble for cookie captures,
7449 * or even for rewrites. Since this happens extremely rarely,
7450 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007451 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7453 int stripped_before = 0;
7454 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007457 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007458 equal += stripped_before;
7459 val_beg += stripped_before;
7460 }
7461
7462 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007463 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007464 val_beg += stripped_after;
7465 stripped_before += stripped_after;
7466 }
7467
7468 val_end += stripped_before;
7469 next += stripped_before;
7470 hdr_end += stripped_before;
7471 hdr_next += stripped_before;
7472 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007473 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 }
7475
7476 /* First, let's see if we want to capture this cookie. We check
7477 * that we don't already have a server side cookie, because we
7478 * can only capture one. Also as an optimisation, we ignore
7479 * cookies shorter than the declared name.
7480 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007481 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007482 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 (val_end - att_beg >= t->fe->capture_namelen) &&
7484 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7485 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007486 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487 Alert("HTTP logging : out of memory.\n");
7488 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007489 else {
7490 if (log_len > t->fe->capture_len)
7491 log_len = t->fe->capture_len;
7492 memcpy(txn->srv_cookie, att_beg, log_len);
7493 txn->srv_cookie[log_len] = 0;
7494 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495 }
7496
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007497 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 if (!(t->flags & SN_IGNORE_PRST) &&
7500 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7501 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007502 /* assume passive cookie by default */
7503 txn->flags &= ~TX_SCK_MASK;
7504 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505
7506 /* If the cookie is in insert mode on a known server, we'll delete
7507 * this occurrence because we'll insert another one later.
7508 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007509 * a direct access.
7510 */
Willy Tarreau67402132012-05-31 20:40:20 +02007511 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007512 /* The "preserve" flag was set, we don't want to touch the
7513 * server's cookie.
7514 */
7515 }
Willy Tarreau67402132012-05-31 20:40:20 +02007516 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7517 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 /* this cookie must be deleted */
7519 if (*prev == ':' && next == hdr_end) {
7520 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007521 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007522 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7523 txn->hdr_idx.used--;
7524 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007525 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 hdr_next += delta;
7527 http_msg_move_end(&txn->rsp, delta);
7528 /* note: while both invalid now, <next> and <hdr_end>
7529 * are still equal, so the for() will stop as expected.
7530 */
7531 } else {
7532 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007533 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 next = prev;
7535 hdr_end += delta;
7536 hdr_next += delta;
7537 cur_hdr->len += delta;
7538 http_msg_move_end(&txn->rsp, delta);
7539 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007540 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007541 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007542 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007543 }
Willy Tarreau67402132012-05-31 20:40:20 +02007544 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007545 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007546 * with this server since we know it.
7547 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007548 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007549 next += delta;
7550 hdr_end += delta;
7551 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007553 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007554
Willy Tarreauf1348312010-10-07 15:54:11 +02007555 txn->flags &= ~TX_SCK_MASK;
7556 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007558 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007561 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007562 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 next += delta;
7564 hdr_end += delta;
7565 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007567 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568
Willy Tarreau827aee92011-03-10 16:55:02 +01007569 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007570 txn->flags &= ~TX_SCK_MASK;
7571 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007572 }
7573 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007574 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7575 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007576 int cmp_len, value_len;
7577 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007578
Cyril Bontéb21570a2009-11-29 20:04:48 +01007579 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007580 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7581 value_begin = att_beg + t->be->appsession_name_len;
7582 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007583 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007584 cmp_len = att_end - att_beg;
7585 value_begin = val_beg;
7586 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007587 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007588
Cyril Bonté17530c32010-04-06 21:11:10 +02007589 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7591 /* free a possibly previously allocated memory */
7592 pool_free2(apools.sessid, txn->sessid);
7593
Cyril Bontéb21570a2009-11-29 20:04:48 +01007594 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007595 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007596 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7597 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7598 return;
7599 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007600 memcpy(txn->sessid, value_begin, value_len);
7601 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007603 }
7604 /* that's done for this cookie, check the next one on the same
7605 * line when next != hdr_end (only if is_cookie2).
7606 */
7607 }
7608 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007610 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007611
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007612 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007613 appsess *asession = NULL;
7614 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007615 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007616 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007617 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007618 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7619 Alert("Not enough Memory process_srv():asession:calloc().\n");
7620 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7621 return;
7622 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007623 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7624
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007625 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7626 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7627 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007628 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007629 return;
7630 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007631 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007632 asession->sessid[t->be->appsession_len] = 0;
7633
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007634 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007635 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007636 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007637 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007638 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007639 return;
7640 }
7641 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007642 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007643
7644 asession->request_count = 0;
7645 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7646 }
7647
7648 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7649 asession->request_count++;
7650 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007651}
7652
7653
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654/*
7655 * Check if response is cacheable or not. Updates t->flags.
7656 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007657void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658{
7659 struct http_txn *txn = &t->txn;
7660 char *p1, *p2;
7661
7662 char *cur_ptr, *cur_end, *cur_next;
7663 int cur_idx;
7664
Willy Tarreau5df51872007-11-25 16:20:08 +01007665 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666 return;
7667
7668 /* Iterate through the headers.
7669 * we start with the start line.
7670 */
7671 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007672 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007673
7674 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7675 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007676 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677
7678 cur_hdr = &txn->hdr_idx.v[cur_idx];
7679 cur_ptr = cur_next;
7680 cur_end = cur_ptr + cur_hdr->len;
7681 cur_next = cur_end + cur_hdr->cr + 1;
7682
7683 /* We have one full header between cur_ptr and cur_end, and the
7684 * next header starts at cur_next. We're only interested in
7685 * "Cookie:" headers.
7686 */
7687
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007688 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7689 if (val) {
7690 if ((cur_end - (cur_ptr + val) >= 8) &&
7691 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7692 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7693 return;
7694 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 }
7696
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007697 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7698 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 continue;
7700
7701 /* OK, right now we know we have a cache-control header at cur_ptr */
7702
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007703 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007704
7705 if (p1 >= cur_end) /* no more info */
7706 continue;
7707
7708 /* p1 is at the beginning of the value */
7709 p2 = p1;
7710
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007711 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 p2++;
7713
7714 /* we have a complete value between p1 and p2 */
7715 if (p2 < cur_end && *p2 == '=') {
7716 /* we have something of the form no-cache="set-cookie" */
7717 if ((cur_end - p1 >= 21) &&
7718 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7719 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007720 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 continue;
7722 }
7723
7724 /* OK, so we know that either p2 points to the end of string or to a comma */
7725 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007726 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7728 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7729 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007730 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 return;
7732 }
7733
7734 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007735 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736 continue;
7737 }
7738 }
7739}
7740
7741
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742/*
7743 * Try to retrieve a known appsession in the URI, then the associated server.
7744 * If the server is found, it's assigned to the session.
7745 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007746void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007748 char *end_params, *first_param, *cur_param, *next_param;
7749 char separator;
7750 int value_len;
7751
7752 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007753
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007754 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007755 (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 +01007756 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Cyril Bontéb21570a2009-11-29 20:04:48 +01007759 first_param = NULL;
7760 switch (mode) {
7761 case PR_O2_AS_M_PP:
7762 first_param = memchr(begin, ';', len);
7763 break;
7764 case PR_O2_AS_M_QS:
7765 first_param = memchr(begin, '?', len);
7766 break;
7767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007768
Cyril Bontéb21570a2009-11-29 20:04:48 +01007769 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007770 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007772
Cyril Bontéb21570a2009-11-29 20:04:48 +01007773 switch (mode) {
7774 case PR_O2_AS_M_PP:
7775 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7776 end_params = (char *) begin + len;
7777 }
7778 separator = ';';
7779 break;
7780 case PR_O2_AS_M_QS:
7781 end_params = (char *) begin + len;
7782 separator = '&';
7783 break;
7784 default:
7785 /* unknown mode, shouldn't happen */
7786 return;
7787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
Cyril Bontéb21570a2009-11-29 20:04:48 +01007789 cur_param = next_param = end_params;
7790 while (cur_param > first_param) {
7791 cur_param--;
7792 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7793 /* let's see if this is the appsession parameter */
7794 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7795 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7796 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7797 /* Cool... it's the right one */
7798 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7799 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7800 if (value_len > 0) {
7801 manage_client_side_appsession(t, cur_param, value_len);
7802 }
7803 break;
7804 }
7805 next_param = cur_param;
7806 }
7807 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007809 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007810 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007812}
7813
Willy Tarreaub2513902006-12-17 14:52:38 +01007814/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007815 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007816 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007817 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007818 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007819 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007820 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007821 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007822 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007823int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007824{
7825 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007826 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007827 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007828
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007829 if (!uri_auth)
7830 return 0;
7831
Cyril Bonté70be45d2010-10-12 00:14:35 +02007832 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007833 return 0;
7834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007835 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007836 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007837 return 0;
7838
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007839 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007840 return 0;
7841
Willy Tarreaub2513902006-12-17 14:52:38 +01007842 return 1;
7843}
7844
Willy Tarreau4076a152009-04-02 15:18:36 +02007845/*
7846 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007847 * By default it tries to report the error position as msg->err_pos. However if
7848 * this one is not set, it will then report msg->next, which is the last known
7849 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007850 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007851 */
7852void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007853 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007854 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007855{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007856 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007857 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007858
Willy Tarreau9b28e032012-10-12 23:49:43 +02007859 es->len = MIN(chn->buf->i, sizeof(es->buf));
7860 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007861 len1 = MIN(len1, es->len);
7862 len2 = es->len - len1; /* remaining data if buffer wraps */
7863
Willy Tarreau9b28e032012-10-12 23:49:43 +02007864 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007865 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007866 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007867
Willy Tarreau4076a152009-04-02 15:18:36 +02007868 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007869 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007870 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007871 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007872
Willy Tarreau4076a152009-04-02 15:18:36 +02007873 es->when = date; // user-visible date
7874 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007875 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007876 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007877 if (objt_conn(s->req->prod->end))
7878 es->src = __objt_conn(s->req->prod->end)->addr.from;
7879 else
7880 memset(&es->src, 0, sizeof(es->src));
7881
Willy Tarreau078272e2010-12-12 12:46:33 +01007882 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007883 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007884 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007885 es->s_flags = s->flags;
7886 es->t_flags = s->txn.flags;
7887 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007888 es->b_out = chn->buf->o;
7889 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007890 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007891 es->m_clen = msg->chunk_len;
7892 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007893}
Willy Tarreaub2513902006-12-17 14:52:38 +01007894
Willy Tarreau294c4732011-12-16 21:35:50 +01007895/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7896 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7897 * performed over the whole headers. Otherwise it must contain a valid header
7898 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7899 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7900 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7901 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007902 * -1. The value fetch stops at commas, so this function is suited for use with
7903 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007904 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007905 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007906unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007907 struct hdr_idx *idx, int occ,
7908 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007909{
Willy Tarreau294c4732011-12-16 21:35:50 +01007910 struct hdr_ctx local_ctx;
7911 char *ptr_hist[MAX_HDR_HISTORY];
7912 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007913 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007914 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007915
Willy Tarreau294c4732011-12-16 21:35:50 +01007916 if (!ctx) {
7917 local_ctx.idx = 0;
7918 ctx = &local_ctx;
7919 }
7920
Willy Tarreaubce70882009-09-07 11:51:47 +02007921 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007922 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007923 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007924 occ--;
7925 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007926 *vptr = ctx->line + ctx->val;
7927 *vlen = ctx->vlen;
7928 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007929 }
7930 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007931 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007932 }
7933
7934 /* negative occurrence, we scan all the list then walk back */
7935 if (-occ > MAX_HDR_HISTORY)
7936 return 0;
7937
Willy Tarreau294c4732011-12-16 21:35:50 +01007938 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007939 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007940 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7941 len_hist[hist_ptr] = ctx->vlen;
7942 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007943 hist_ptr = 0;
7944 found++;
7945 }
7946 if (-occ > found)
7947 return 0;
7948 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007949 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7950 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7951 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007952 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007953 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007954 if (hist_ptr >= MAX_HDR_HISTORY)
7955 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007956 *vptr = ptr_hist[hist_ptr];
7957 *vlen = len_hist[hist_ptr];
7958 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007959}
7960
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007961/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7962 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7963 * performed over the whole headers. Otherwise it must contain a valid header
7964 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7965 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7966 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7967 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7968 * -1. This function differs from http_get_hdr() in that it only returns full
7969 * line header values and does not stop at commas.
7970 * The return value is 0 if nothing was found, or non-zero otherwise.
7971 */
7972unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7973 struct hdr_idx *idx, int occ,
7974 struct hdr_ctx *ctx, char **vptr, int *vlen)
7975{
7976 struct hdr_ctx local_ctx;
7977 char *ptr_hist[MAX_HDR_HISTORY];
7978 int len_hist[MAX_HDR_HISTORY];
7979 unsigned int hist_ptr;
7980 int found;
7981
7982 if (!ctx) {
7983 local_ctx.idx = 0;
7984 ctx = &local_ctx;
7985 }
7986
7987 if (occ >= 0) {
7988 /* search from the beginning */
7989 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7990 occ--;
7991 if (occ <= 0) {
7992 *vptr = ctx->line + ctx->val;
7993 *vlen = ctx->vlen;
7994 return 1;
7995 }
7996 }
7997 return 0;
7998 }
7999
8000 /* negative occurrence, we scan all the list then walk back */
8001 if (-occ > MAX_HDR_HISTORY)
8002 return 0;
8003
8004 found = hist_ptr = 0;
8005 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8006 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8007 len_hist[hist_ptr] = ctx->vlen;
8008 if (++hist_ptr >= MAX_HDR_HISTORY)
8009 hist_ptr = 0;
8010 found++;
8011 }
8012 if (-occ > found)
8013 return 0;
8014 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8015 * find occurrence -occ, so we have to check [hist_ptr+occ].
8016 */
8017 hist_ptr += occ;
8018 if (hist_ptr >= MAX_HDR_HISTORY)
8019 hist_ptr -= MAX_HDR_HISTORY;
8020 *vptr = ptr_hist[hist_ptr];
8021 *vlen = len_hist[hist_ptr];
8022 return 1;
8023}
8024
Willy Tarreaubaaee002006-06-26 02:48:02 +02008025/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008026 * Print a debug line with a header. Always stop at the first CR or LF char,
8027 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8028 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008029 */
8030void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8031{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008032 int max;
8033 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008034 dir,
8035 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8036 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 +02008037
8038 for (max = 0; start + max < end; max++)
8039 if (start[max] == '\r' || start[max] == '\n')
8040 break;
8041
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008042 UBOUND(max, trash.size - trash.len - 3);
8043 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8044 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008045 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008046}
8047
Willy Tarreau0937bc42009-12-22 15:03:09 +01008048/*
8049 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8050 * the required fields are properly allocated and that we only need to (re)init
8051 * them. This should be used before processing any new request.
8052 */
8053void http_init_txn(struct session *s)
8054{
8055 struct http_txn *txn = &s->txn;
8056 struct proxy *fe = s->fe;
8057
8058 txn->flags = 0;
8059 txn->status = -1;
8060
Willy Tarreauf64d1412010-10-07 20:06:11 +02008061 txn->cookie_first_date = 0;
8062 txn->cookie_last_date = 0;
8063
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008064 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008065 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008066 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008067 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008068 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008069 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008070 txn->req.chunk_len = 0LL;
8071 txn->req.body_len = 0LL;
8072 txn->rsp.chunk_len = 0LL;
8073 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008074 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8075 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008076 txn->req.chn = s->req;
8077 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008078
8079 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008080
8081 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8082 if (fe->options2 & PR_O2_REQBUG_OK)
8083 txn->req.err_pos = -1; /* let buggy requests pass */
8084
Willy Tarreau46023632010-01-07 22:51:47 +01008085 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008086 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8087
Willy Tarreau46023632010-01-07 22:51:47 +01008088 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008089 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8090
8091 if (txn->hdr_idx.v)
8092 hdr_idx_init(&txn->hdr_idx);
8093}
8094
8095/* to be used at the end of a transaction */
8096void http_end_txn(struct session *s)
8097{
8098 struct http_txn *txn = &s->txn;
8099
8100 /* these ones will have been dynamically allocated */
8101 pool_free2(pool2_requri, txn->uri);
8102 pool_free2(pool2_capture, txn->cli_cookie);
8103 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008104 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008105 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008106
William Lallemanda73203e2012-03-12 12:48:57 +01008107 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008108 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008109 txn->uri = NULL;
8110 txn->srv_cookie = NULL;
8111 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008112
8113 if (txn->req.cap) {
8114 struct cap_hdr *h;
8115 for (h = s->fe->req_cap; h; h = h->next)
8116 pool_free2(h->pool, txn->req.cap[h->index]);
8117 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8118 }
8119
8120 if (txn->rsp.cap) {
8121 struct cap_hdr *h;
8122 for (h = s->fe->rsp_cap; h; h = h->next)
8123 pool_free2(h->pool, txn->rsp.cap[h->index]);
8124 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8125 }
8126
Willy Tarreau0937bc42009-12-22 15:03:09 +01008127}
8128
8129/* to be used at the end of a transaction to prepare a new one */
8130void http_reset_txn(struct session *s)
8131{
8132 http_end_txn(s);
8133 http_init_txn(s);
8134
8135 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008136 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008137 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008138 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008139 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008140 /* re-init store persistence */
8141 s->store_count = 0;
8142
Willy Tarreau0937bc42009-12-22 15:03:09 +01008143 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008144
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008145 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008146
Willy Tarreau739cfba2010-01-25 23:11:14 +01008147 /* We must trim any excess data from the response buffer, because we
8148 * may have blocked an invalid response from a server that we don't
8149 * want to accidentely forward once we disable the analysers, nor do
8150 * we want those data to come along with next response. A typical
8151 * example of such data would be from a buggy server responding to
8152 * a HEAD with some data, or sending more than the advertised
8153 * content-length.
8154 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008155 if (unlikely(s->rep->buf->i))
8156 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008157
Willy Tarreau0937bc42009-12-22 15:03:09 +01008158 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008159 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008160
Willy Tarreaud04e8582010-05-31 12:31:35 +02008161 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008162 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008163
8164 s->req->rex = TICK_ETERNITY;
8165 s->req->wex = TICK_ETERNITY;
8166 s->req->analyse_exp = TICK_ETERNITY;
8167 s->rep->rex = TICK_ETERNITY;
8168 s->rep->wex = TICK_ETERNITY;
8169 s->rep->analyse_exp = TICK_ETERNITY;
8170}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008171
Willy Tarreauff011f22011-01-06 17:51:27 +01008172void free_http_req_rules(struct list *r) {
8173 struct http_req_rule *tr, *pr;
8174
8175 list_for_each_entry_safe(pr, tr, r, list) {
8176 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008177 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008178 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008179
8180 free(pr);
8181 }
8182}
8183
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008184/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008185struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8186{
8187 struct http_req_rule *rule;
8188 int cur_arg;
8189
8190 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8191 if (!rule) {
8192 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008193 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008194 }
8195
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008196 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008197 rule->action = HTTP_REQ_ACT_ALLOW;
8198 cur_arg = 1;
8199 } else if (!strcmp(args[0], "deny")) {
8200 rule->action = HTTP_REQ_ACT_DENY;
8201 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008202 } else if (!strcmp(args[0], "tarpit")) {
8203 rule->action = HTTP_REQ_ACT_TARPIT;
8204 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008205 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008206 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008207 cur_arg = 1;
8208
8209 while(*args[cur_arg]) {
8210 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008211 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008212 cur_arg+=2;
8213 continue;
8214 } else
8215 break;
8216 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008217 } else if (!strcmp(args[0], "set-nice")) {
8218 rule->action = HTTP_REQ_ACT_SET_NICE;
8219 cur_arg = 1;
8220
8221 if (!*args[cur_arg] ||
8222 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8223 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8224 file, linenum, args[0]);
8225 goto out_err;
8226 }
8227 rule->arg.nice = atoi(args[cur_arg]);
8228 if (rule->arg.nice < -1024)
8229 rule->arg.nice = -1024;
8230 else if (rule->arg.nice > 1024)
8231 rule->arg.nice = 1024;
8232 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008233 } else if (!strcmp(args[0], "set-tos")) {
8234#ifdef IP_TOS
8235 char *err;
8236 rule->action = HTTP_REQ_ACT_SET_TOS;
8237 cur_arg = 1;
8238
8239 if (!*args[cur_arg] ||
8240 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8241 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8242 file, linenum, args[0]);
8243 goto out_err;
8244 }
8245
8246 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8247 if (err && *err != '\0') {
8248 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8249 file, linenum, err, args[0]);
8250 goto out_err;
8251 }
8252 cur_arg++;
8253#else
8254 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8255 goto out_err;
8256#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008257 } else if (!strcmp(args[0], "set-mark")) {
8258#ifdef SO_MARK
8259 char *err;
8260 rule->action = HTTP_REQ_ACT_SET_MARK;
8261 cur_arg = 1;
8262
8263 if (!*args[cur_arg] ||
8264 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8265 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8266 file, linenum, args[0]);
8267 goto out_err;
8268 }
8269
8270 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8271 if (err && *err != '\0') {
8272 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8273 file, linenum, err, args[0]);
8274 goto out_err;
8275 }
8276 cur_arg++;
8277 global.last_checks |= LSTCHK_NETADM;
8278#else
8279 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8280 goto out_err;
8281#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008282 } else if (!strcmp(args[0], "set-log-level")) {
8283 rule->action = HTTP_REQ_ACT_SET_LOGL;
8284 cur_arg = 1;
8285
8286 if (!*args[cur_arg] ||
8287 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8288 bad_log_level:
8289 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8290 file, linenum, args[0]);
8291 goto out_err;
8292 }
8293 if (strcmp(args[cur_arg], "silent") == 0)
8294 rule->arg.loglevel = -1;
8295 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8296 goto bad_log_level;
8297 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008298 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8299 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8300 cur_arg = 1;
8301
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008302 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8303 (*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 +01008304 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8305 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008306 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008307 }
8308
8309 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8310 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8311 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008312
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008313 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008314 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8315 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008316 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008317 } else if (strcmp(args[0], "redirect") == 0) {
8318 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008319 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008320
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008321 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008322 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8323 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8324 goto out_err;
8325 }
8326
8327 /* this redirect rule might already contain a parsed condition which
8328 * we'll pass to the http-request rule.
8329 */
8330 rule->action = HTTP_REQ_ACT_REDIR;
8331 rule->arg.redir = redir;
8332 rule->cond = redir->cond;
8333 redir->cond = NULL;
8334 cur_arg = 2;
8335 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008336 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008337 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 +01008338 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008339 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008340 }
8341
8342 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8343 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008344 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008345
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008346 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8347 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8348 file, linenum, args[0], errmsg);
8349 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008350 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008351 }
8352 rule->cond = cond;
8353 }
8354 else if (*args[cur_arg]) {
8355 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8356 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8357 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008358 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008359 }
8360
8361 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008362 out_err:
8363 free(rule);
8364 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008365}
8366
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008367/* parse an "http-respose" rule */
8368struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8369{
8370 struct http_res_rule *rule;
8371 int cur_arg;
8372
8373 rule = calloc(1, sizeof(*rule));
8374 if (!rule) {
8375 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8376 goto out_err;
8377 }
8378
8379 if (!strcmp(args[0], "allow")) {
8380 rule->action = HTTP_RES_ACT_ALLOW;
8381 cur_arg = 1;
8382 } else if (!strcmp(args[0], "deny")) {
8383 rule->action = HTTP_RES_ACT_DENY;
8384 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008385 } else if (!strcmp(args[0], "set-nice")) {
8386 rule->action = HTTP_RES_ACT_SET_NICE;
8387 cur_arg = 1;
8388
8389 if (!*args[cur_arg] ||
8390 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8391 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8392 file, linenum, args[0]);
8393 goto out_err;
8394 }
8395 rule->arg.nice = atoi(args[cur_arg]);
8396 if (rule->arg.nice < -1024)
8397 rule->arg.nice = -1024;
8398 else if (rule->arg.nice > 1024)
8399 rule->arg.nice = 1024;
8400 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008401 } else if (!strcmp(args[0], "set-tos")) {
8402#ifdef IP_TOS
8403 char *err;
8404 rule->action = HTTP_RES_ACT_SET_TOS;
8405 cur_arg = 1;
8406
8407 if (!*args[cur_arg] ||
8408 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8409 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8410 file, linenum, args[0]);
8411 goto out_err;
8412 }
8413
8414 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8415 if (err && *err != '\0') {
8416 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8417 file, linenum, err, args[0]);
8418 goto out_err;
8419 }
8420 cur_arg++;
8421#else
8422 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8423 goto out_err;
8424#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008425 } else if (!strcmp(args[0], "set-mark")) {
8426#ifdef SO_MARK
8427 char *err;
8428 rule->action = HTTP_RES_ACT_SET_MARK;
8429 cur_arg = 1;
8430
8431 if (!*args[cur_arg] ||
8432 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8433 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8434 file, linenum, args[0]);
8435 goto out_err;
8436 }
8437
8438 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8439 if (err && *err != '\0') {
8440 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8441 file, linenum, err, args[0]);
8442 goto out_err;
8443 }
8444 cur_arg++;
8445 global.last_checks |= LSTCHK_NETADM;
8446#else
8447 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8448 goto out_err;
8449#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008450 } else if (!strcmp(args[0], "set-log-level")) {
8451 rule->action = HTTP_RES_ACT_SET_LOGL;
8452 cur_arg = 1;
8453
8454 if (!*args[cur_arg] ||
8455 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8456 bad_log_level:
8457 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8458 file, linenum, args[0]);
8459 goto out_err;
8460 }
8461 if (strcmp(args[cur_arg], "silent") == 0)
8462 rule->arg.loglevel = -1;
8463 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8464 goto bad_log_level;
8465 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008466 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8467 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8468 cur_arg = 1;
8469
8470 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8471 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8472 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8473 file, linenum, args[0]);
8474 goto out_err;
8475 }
8476
8477 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8478 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8479 LIST_INIT(&rule->arg.hdr_add.fmt);
8480
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008481 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008482 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8483 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8484 cur_arg += 2;
8485 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008486 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 +02008487 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8488 goto out_err;
8489 }
8490
8491 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8492 struct acl_cond *cond;
8493 char *errmsg = NULL;
8494
8495 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8496 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8497 file, linenum, args[0], errmsg);
8498 free(errmsg);
8499 goto out_err;
8500 }
8501 rule->cond = cond;
8502 }
8503 else if (*args[cur_arg]) {
8504 Alert("parsing [%s:%d]: 'http-response %s' expects"
8505 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8506 file, linenum, args[0], args[cur_arg]);
8507 goto out_err;
8508 }
8509
8510 return rule;
8511 out_err:
8512 free(rule);
8513 return NULL;
8514}
8515
Willy Tarreau4baae242012-12-27 12:00:31 +01008516/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008517 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8518 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008519 */
8520struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008521 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008522{
8523 struct redirect_rule *rule;
8524 int cur_arg;
8525 int type = REDIRECT_TYPE_NONE;
8526 int code = 302;
8527 const char *destination = NULL;
8528 const char *cookie = NULL;
8529 int cookie_set = 0;
8530 unsigned int flags = REDIRECT_FLAG_NONE;
8531 struct acl_cond *cond = NULL;
8532
8533 cur_arg = 0;
8534 while (*(args[cur_arg])) {
8535 if (strcmp(args[cur_arg], "location") == 0) {
8536 if (!*args[cur_arg + 1])
8537 goto missing_arg;
8538
8539 type = REDIRECT_TYPE_LOCATION;
8540 cur_arg++;
8541 destination = args[cur_arg];
8542 }
8543 else if (strcmp(args[cur_arg], "prefix") == 0) {
8544 if (!*args[cur_arg + 1])
8545 goto missing_arg;
8546
8547 type = REDIRECT_TYPE_PREFIX;
8548 cur_arg++;
8549 destination = args[cur_arg];
8550 }
8551 else if (strcmp(args[cur_arg], "scheme") == 0) {
8552 if (!*args[cur_arg + 1])
8553 goto missing_arg;
8554
8555 type = REDIRECT_TYPE_SCHEME;
8556 cur_arg++;
8557 destination = args[cur_arg];
8558 }
8559 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8560 if (!*args[cur_arg + 1])
8561 goto missing_arg;
8562
8563 cur_arg++;
8564 cookie = args[cur_arg];
8565 cookie_set = 1;
8566 }
8567 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8568 if (!*args[cur_arg + 1])
8569 goto missing_arg;
8570
8571 cur_arg++;
8572 cookie = args[cur_arg];
8573 cookie_set = 0;
8574 }
8575 else if (strcmp(args[cur_arg], "code") == 0) {
8576 if (!*args[cur_arg + 1])
8577 goto missing_arg;
8578
8579 cur_arg++;
8580 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008581 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008582 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008583 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008584 args[cur_arg - 1], args[cur_arg]);
8585 return NULL;
8586 }
8587 }
8588 else if (!strcmp(args[cur_arg],"drop-query")) {
8589 flags |= REDIRECT_FLAG_DROP_QS;
8590 }
8591 else if (!strcmp(args[cur_arg],"append-slash")) {
8592 flags |= REDIRECT_FLAG_APPEND_SLASH;
8593 }
8594 else if (strcmp(args[cur_arg], "if") == 0 ||
8595 strcmp(args[cur_arg], "unless") == 0) {
8596 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8597 if (!cond) {
8598 memprintf(errmsg, "error in condition: %s", *errmsg);
8599 return NULL;
8600 }
8601 break;
8602 }
8603 else {
8604 memprintf(errmsg,
8605 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8606 args[cur_arg]);
8607 return NULL;
8608 }
8609 cur_arg++;
8610 }
8611
8612 if (type == REDIRECT_TYPE_NONE) {
8613 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8614 return NULL;
8615 }
8616
8617 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8618 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008619 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008620
8621 if (!use_fmt) {
8622 /* old-style static redirect rule */
8623 rule->rdr_str = strdup(destination);
8624 rule->rdr_len = strlen(destination);
8625 }
8626 else {
8627 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008628
8629 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8630 * if prefix == "/", we don't want to add anything, otherwise it
8631 * makes it hard for the user to configure a self-redirection.
8632 */
8633 proxy->conf.args.ctx = ARGC_RDR;
8634 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8635 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8636 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8637 }
8638 }
8639
Willy Tarreau4baae242012-12-27 12:00:31 +01008640 if (cookie) {
8641 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8642 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8643 */
8644 rule->cookie_len = strlen(cookie);
8645 if (cookie_set) {
8646 rule->cookie_str = malloc(rule->cookie_len + 10);
8647 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8648 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8649 rule->cookie_len += 9;
8650 } else {
8651 rule->cookie_str = malloc(rule->cookie_len + 21);
8652 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8653 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8654 rule->cookie_len += 20;
8655 }
8656 }
8657 rule->type = type;
8658 rule->code = code;
8659 rule->flags = flags;
8660 LIST_INIT(&rule->list);
8661 return rule;
8662
8663 missing_arg:
8664 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8665 return NULL;
8666}
8667
Willy Tarreau8797c062007-05-07 00:55:35 +02008668/************************************************************************/
8669/* The code below is dedicated to ACL parsing and matching */
8670/************************************************************************/
8671
8672
Willy Tarreau14174bc2012-04-16 14:34:04 +02008673/* This function ensures that the prerequisites for an L7 fetch are ready,
8674 * which means that a request or response is ready. If some data is missing,
8675 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008676 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8677 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008678 *
8679 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008680 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8681 * decide whether or not an HTTP message is present ;
8682 * 0 if the requested data cannot be fetched or if it is certain that
8683 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008684 * 1 if an HTTP message is ready
8685 */
8686static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008687smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008688 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008689{
8690 struct http_txn *txn = l7;
8691 struct http_msg *msg = &txn->req;
8692
8693 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8694 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8695 */
8696
8697 if (unlikely(!s || !txn))
8698 return 0;
8699
8700 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008701 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008702
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008703 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008704 if (unlikely(!s->req))
8705 return 0;
8706
Willy Tarreauaae75e32013-03-29 12:31:49 +01008707 /* If the buffer does not leave enough free space at the end,
8708 * we must first realign it.
8709 */
8710 if (s->req->buf->p > s->req->buf->data &&
8711 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8712 buffer_slow_realign(s->req->buf);
8713
Willy Tarreau14174bc2012-04-16 14:34:04 +02008714 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008715 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008716 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008717
8718 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008719 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008720 http_msg_analyzer(msg, &txn->hdr_idx);
8721
8722 /* Still no valid request ? */
8723 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008724 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008725 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008726 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008727 }
8728 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008729 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008730 return 0;
8731 }
8732
8733 /* OK we just got a valid HTTP request. We have some minor
8734 * preparation to perform so that further checks can rely
8735 * on HTTP tests.
8736 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008737
8738 /* If the request was parsed but was too large, we must absolutely
8739 * return an error so that it is not processed. At the moment this
8740 * cannot happen, but if the parsers are to change in the future,
8741 * we want this check to be maintained.
8742 */
8743 if (unlikely(s->req->buf->i + s->req->buf->p >
8744 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8745 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008746 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008747 return 1;
8748 }
8749
Willy Tarreau9b28e032012-10-12 23:49:43 +02008750 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008751 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8752 s->flags |= SN_REDIRECTABLE;
8753
Willy Tarreau506d0502013-07-06 13:29:24 +02008754 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8755 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008756 }
8757
Willy Tarreau506d0502013-07-06 13:29:24 +02008758 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008759 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008760 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008761
8762 /* otherwise everything's ready for the request */
8763 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008764 else {
8765 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008766 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8767 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008768 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008769 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008770 }
8771
8772 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008773 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008774 return 1;
8775}
Willy Tarreau8797c062007-05-07 00:55:35 +02008776
Willy Tarreauc0239e02012-04-16 14:42:55 +02008777#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008778 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 +02008779
Willy Tarreau24e32d82012-04-23 23:55:44 +02008780#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008781 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 +02008782
Willy Tarreau8797c062007-05-07 00:55:35 +02008783
8784/* 1. Check on METHOD
8785 * We use the pre-parsed method if it is known, and store its number as an
8786 * integer. If it is unknown, we use the pointer and the length.
8787 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008788static 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 +02008789{
8790 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008791 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008792
Willy Tarreauae8b7962007-06-09 23:10:04 +02008793 len = strlen(*text);
8794 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008795
8796 pattern->val.i = meth;
8797 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008798 if (usage == PAT_U_COMPILE) {
8799 pattern->ptr.str = strdup(*text);
8800 if (!pattern->ptr.str) {
8801 memprintf(err, "out of memory while loading pattern");
8802 return 0;
8803 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008804 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008805 else {
8806 trash = get_trash_chunk();
8807 if (trash->size < len) {
8808 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8809 len, trash->size);
8810 return 0;
8811 }
8812 pattern->ptr.str = trash->str;
8813 }
8814 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008815 pattern->len = len;
8816 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008817 else
8818 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008819 return 1;
8820}
8821
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008822/* This function fetches the method of current HTTP request and stores
8823 * it in the global pattern struct as a chunk. There are two possibilities :
8824 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8825 * in <len> and <ptr> is NULL ;
8826 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8827 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008828 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008829 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008830static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008831smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008832 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008833{
8834 int meth;
8835 struct http_txn *txn = l7;
8836
Willy Tarreau24e32d82012-04-23 23:55:44 +02008837 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008838
Willy Tarreau8797c062007-05-07 00:55:35 +02008839 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008840 smp->type = SMP_T_UINT;
8841 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008842 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008843 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8844 /* ensure the indexes are not affected */
8845 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008846 smp->type = SMP_T_CSTR;
8847 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008848 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008849 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008850 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008851 return 1;
8852}
8853
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008854/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008855static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008856{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008857 int icase;
8858
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008859
Willy Tarreauf853c462012-04-23 18:53:56 +02008860 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008861 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008862 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008863 return PAT_MATCH;
8864 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008865 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008866
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008867 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8868 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008869 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008870
8871 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008872 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008873 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008874
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008875 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008876 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8877 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008878 return PAT_NOMATCH;
8879 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008880}
8881
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008882static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008883smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008884 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008885{
8886 struct http_txn *txn = l7;
8887 char *ptr;
8888 int len;
8889
Willy Tarreauc0239e02012-04-16 14:42:55 +02008890 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008891
Willy Tarreau8797c062007-05-07 00:55:35 +02008892 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008893 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008894
8895 while ((len-- > 0) && (*ptr++ != '/'));
8896 if (len <= 0)
8897 return 0;
8898
Willy Tarreauf853c462012-04-23 18:53:56 +02008899 smp->type = SMP_T_CSTR;
8900 smp->data.str.str = ptr;
8901 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008902
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008903 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008904 return 1;
8905}
8906
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008907static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008908smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008909 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008910{
8911 struct http_txn *txn = l7;
8912 char *ptr;
8913 int len;
8914
Willy Tarreauc0239e02012-04-16 14:42:55 +02008915 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008916
Willy Tarreauf26b2522012-12-14 08:33:14 +01008917 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8918 return 0;
8919
Willy Tarreau8797c062007-05-07 00:55:35 +02008920 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008921 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008922
8923 while ((len-- > 0) && (*ptr++ != '/'));
8924 if (len <= 0)
8925 return 0;
8926
Willy Tarreauf853c462012-04-23 18:53:56 +02008927 smp->type = SMP_T_CSTR;
8928 smp->data.str.str = ptr;
8929 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008930
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008931 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008932 return 1;
8933}
8934
8935/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008936static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008937smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008938 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008939{
8940 struct http_txn *txn = l7;
8941 char *ptr;
8942 int len;
8943
Willy Tarreauc0239e02012-04-16 14:42:55 +02008944 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008945
Willy Tarreauf26b2522012-12-14 08:33:14 +01008946 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8947 return 0;
8948
Willy Tarreau8797c062007-05-07 00:55:35 +02008949 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008950 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008951
Willy Tarreauf853c462012-04-23 18:53:56 +02008952 smp->type = SMP_T_UINT;
8953 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008954 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008955 return 1;
8956}
8957
8958/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008959static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008960smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008961 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008962{
8963 struct http_txn *txn = l7;
8964
Willy Tarreauc0239e02012-04-16 14:42:55 +02008965 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008966
Willy Tarreauf853c462012-04-23 18:53:56 +02008967 smp->type = SMP_T_CSTR;
8968 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008969 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008970 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008971 return 1;
8972}
8973
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008974static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008975smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008976 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008977{
8978 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008979 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008980
Willy Tarreauc0239e02012-04-16 14:42:55 +02008981 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008982
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008983 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8984 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008985 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008986
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008987 smp->type = SMP_T_IPV4;
8988 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008989 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008990 return 1;
8991}
8992
8993static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008994smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008995 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008996{
8997 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008998 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008999
Willy Tarreauc0239e02012-04-16 14:42:55 +02009000 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009001
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009002 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9003 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9004 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009005
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009006 smp->type = SMP_T_UINT;
9007 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009008 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009009 return 1;
9010}
9011
Willy Tarreau185b5c42012-04-26 15:11:51 +02009012/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9013 * Accepts an optional argument of type string containing the header field name,
9014 * and an optional argument of type signed or unsigned integer to request an
9015 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009016 * headers are considered from the first one. It does not stop on commas and
9017 * returns full lines instead (useful for User-Agent or Date for example).
9018 */
9019static int
9020smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009021 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009022{
9023 struct http_txn *txn = l7;
9024 struct hdr_idx *idx = &txn->hdr_idx;
9025 struct hdr_ctx *ctx = smp->ctx.a[0];
9026 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9027 int occ = 0;
9028 const char *name_str = NULL;
9029 int name_len = 0;
9030
9031 if (!ctx) {
9032 /* first call */
9033 ctx = &static_hdr_ctx;
9034 ctx->idx = 0;
9035 smp->ctx.a[0] = ctx;
9036 }
9037
9038 if (args) {
9039 if (args[0].type != ARGT_STR)
9040 return 0;
9041 name_str = args[0].data.str.str;
9042 name_len = args[0].data.str.len;
9043
9044 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9045 occ = args[1].data.uint;
9046 }
9047
9048 CHECK_HTTP_MESSAGE_FIRST();
9049
9050 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9051 /* search for header from the beginning */
9052 ctx->idx = 0;
9053
9054 if (!occ && !(opt & SMP_OPT_ITERATE))
9055 /* no explicit occurrence and single fetch => last header by default */
9056 occ = -1;
9057
9058 if (!occ)
9059 /* prepare to report multiple occurrences for ACL fetches */
9060 smp->flags |= SMP_F_NOT_LAST;
9061
9062 smp->type = SMP_T_CSTR;
9063 smp->flags |= SMP_F_VOL_HDR;
9064 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9065 return 1;
9066
9067 smp->flags &= ~SMP_F_NOT_LAST;
9068 return 0;
9069}
9070
9071/* 6. Check on HTTP header count. The number of occurrences is returned.
9072 * Accepts exactly 1 argument of type string. It does not stop on commas and
9073 * returns full lines instead (useful for User-Agent or Date for example).
9074 */
9075static int
9076smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009077 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009078{
9079 struct http_txn *txn = l7;
9080 struct hdr_idx *idx = &txn->hdr_idx;
9081 struct hdr_ctx ctx;
9082 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9083 int cnt;
9084
9085 if (!args || args->type != ARGT_STR)
9086 return 0;
9087
9088 CHECK_HTTP_MESSAGE_FIRST();
9089
9090 ctx.idx = 0;
9091 cnt = 0;
9092 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9093 cnt++;
9094
9095 smp->type = SMP_T_UINT;
9096 smp->data.uint = cnt;
9097 smp->flags = SMP_F_VOL_HDR;
9098 return 1;
9099}
9100
9101/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9102 * Accepts an optional argument of type string containing the header field name,
9103 * and an optional argument of type signed or unsigned integer to request an
9104 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009105 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009106 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009107static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009108smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009109 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009110{
9111 struct http_txn *txn = l7;
9112 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009113 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009114 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009115 int occ = 0;
9116 const char *name_str = NULL;
9117 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009118
Willy Tarreaua890d072013-04-02 12:01:06 +02009119 if (!ctx) {
9120 /* first call */
9121 ctx = &static_hdr_ctx;
9122 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009123 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009124 }
9125
Willy Tarreau185b5c42012-04-26 15:11:51 +02009126 if (args) {
9127 if (args[0].type != ARGT_STR)
9128 return 0;
9129 name_str = args[0].data.str.str;
9130 name_len = args[0].data.str.len;
9131
9132 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9133 occ = args[1].data.uint;
9134 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009135
Willy Tarreaue333ec92012-04-16 16:26:40 +02009136 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009137
Willy Tarreau185b5c42012-04-26 15:11:51 +02009138 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009139 /* search for header from the beginning */
9140 ctx->idx = 0;
9141
Willy Tarreau185b5c42012-04-26 15:11:51 +02009142 if (!occ && !(opt & SMP_OPT_ITERATE))
9143 /* no explicit occurrence and single fetch => last header by default */
9144 occ = -1;
9145
9146 if (!occ)
9147 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009148 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009149
Willy Tarreau185b5c42012-04-26 15:11:51 +02009150 smp->type = SMP_T_CSTR;
9151 smp->flags |= SMP_F_VOL_HDR;
9152 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 +02009153 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009154
Willy Tarreau37406352012-04-23 16:16:37 +02009155 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009156 return 0;
9157}
9158
Willy Tarreauc11416f2007-06-17 16:58:38 +02009159/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009160 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009161 */
9162static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009163smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009164 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009165{
9166 struct http_txn *txn = l7;
9167 struct hdr_idx *idx = &txn->hdr_idx;
9168 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009169 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009170 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009171
Willy Tarreau24e32d82012-04-23 23:55:44 +02009172 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009173 return 0;
9174
Willy Tarreaue333ec92012-04-16 16:26:40 +02009175 CHECK_HTTP_MESSAGE_FIRST();
9176
Willy Tarreau33a7e692007-06-10 19:45:56 +02009177 ctx.idx = 0;
9178 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009179 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 +02009180 cnt++;
9181
Willy Tarreauf853c462012-04-23 18:53:56 +02009182 smp->type = SMP_T_UINT;
9183 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009184 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009185 return 1;
9186}
9187
Willy Tarreau185b5c42012-04-26 15:11:51 +02009188/* Fetch an HTTP header's integer value. The integer value is returned. It
9189 * takes a mandatory argument of type string and an optional one of type int
9190 * to designate a specific occurrence. It returns an unsigned integer, which
9191 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009192 */
9193static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009194smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009195 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009196{
Willy Tarreauef38c392013-07-22 16:29:32 +02009197 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009198
Willy Tarreauf853c462012-04-23 18:53:56 +02009199 if (ret > 0) {
9200 smp->type = SMP_T_UINT;
9201 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9202 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009203
Willy Tarreaud53e2422012-04-16 17:21:11 +02009204 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009205}
9206
Cyril Bonté69fa9922012-10-25 00:01:06 +02009207/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9208 * and an optional one of type int to designate a specific occurrence.
9209 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009210 */
9211static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009212smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009213 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009214{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009215 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009216
Willy Tarreauef38c392013-07-22 16:29:32 +02009217 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009218 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9219 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009220 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009221 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009222 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009223 if (smp->data.str.len < temp->size - 1) {
9224 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9225 temp->str[smp->data.str.len] = '\0';
9226 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9227 smp->type = SMP_T_IPV6;
9228 break;
9229 }
9230 }
9231 }
9232
Willy Tarreaud53e2422012-04-16 17:21:11 +02009233 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009234 if (!(smp->flags & SMP_F_NOT_LAST))
9235 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009236 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009237 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009238}
9239
Willy Tarreau737b0c12007-06-10 21:28:46 +02009240/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9241 * the first '/' after the possible hostname, and ends before the possible '?'.
9242 */
9243static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009244smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009245 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009246{
9247 struct http_txn *txn = l7;
9248 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009249
Willy Tarreauc0239e02012-04-16 14:42:55 +02009250 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009251
Willy Tarreau9b28e032012-10-12 23:49:43 +02009252 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009253 ptr = http_get_path(txn);
9254 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009255 return 0;
9256
9257 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009258 smp->type = SMP_T_CSTR;
9259 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009260
9261 while (ptr < end && *ptr != '?')
9262 ptr++;
9263
Willy Tarreauf853c462012-04-23 18:53:56 +02009264 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009265 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009266 return 1;
9267}
9268
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009269/* This produces a concatenation of the first occurrence of the Host header
9270 * followed by the path component if it begins with a slash ('/'). This means
9271 * that '*' will not be added, resulting in exactly the first Host entry.
9272 * If no Host header is found, then the path is returned as-is. The returned
9273 * value is stored in the trash so it does not need to be marked constant.
9274 */
9275static int
9276smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009277 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009278{
9279 struct http_txn *txn = l7;
9280 char *ptr, *end, *beg;
9281 struct hdr_ctx ctx;
9282
9283 CHECK_HTTP_MESSAGE_FIRST();
9284
9285 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009286 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 +02009287 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009288 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009289
9290 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009291 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009292 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009293 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009294 smp->data.str.len = ctx.vlen;
9295
9296 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009297 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 +02009298 beg = http_get_path(txn);
9299 if (!beg)
9300 beg = end;
9301
9302 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9303
9304 if (beg < ptr && *beg == '/') {
9305 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9306 smp->data.str.len += ptr - beg;
9307 }
9308
9309 smp->flags = SMP_F_VOL_1ST;
9310 return 1;
9311}
9312
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009313/* This produces a 32-bit hash of the concatenation of the first occurrence of
9314 * the Host header followed by the path component if it begins with a slash ('/').
9315 * This means that '*' will not be added, resulting in exactly the first Host
9316 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009317 * is hashed using the path hash followed by a full avalanche hash and provides a
9318 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009319 * high-traffic sites without having to store whole paths.
9320 */
9321static int
9322smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009323 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009324{
9325 struct http_txn *txn = l7;
9326 struct hdr_ctx ctx;
9327 unsigned int hash = 0;
9328 char *ptr, *beg, *end;
9329 int len;
9330
9331 CHECK_HTTP_MESSAGE_FIRST();
9332
9333 ctx.idx = 0;
9334 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9335 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9336 ptr = ctx.line + ctx.val;
9337 len = ctx.vlen;
9338 while (len--)
9339 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9340 }
9341
9342 /* now retrieve the path */
9343 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9344 beg = http_get_path(txn);
9345 if (!beg)
9346 beg = end;
9347
9348 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9349
9350 if (beg < ptr && *beg == '/') {
9351 while (beg < ptr)
9352 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9353 }
9354 hash = full_hash(hash);
9355
9356 smp->type = SMP_T_UINT;
9357 smp->data.uint = hash;
9358 smp->flags = SMP_F_VOL_1ST;
9359 return 1;
9360}
9361
Willy Tarreau4a550602012-12-09 14:53:32 +01009362/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009363 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9364 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9365 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009366 * that in environments where IPv6 is insignificant, truncating the output to
9367 * 8 bytes would still work.
9368 */
9369static int
9370smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009371 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009372{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009373 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009374 struct connection *cli_conn = objt_conn(l4->si[0].end);
9375
9376 if (!cli_conn)
9377 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009378
Willy Tarreauef38c392013-07-22 16:29:32 +02009379 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009380 return 0;
9381
Willy Tarreau47ca5452012-12-23 20:22:19 +01009382 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009383 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9384 temp->len += sizeof(smp->data.uint);
9385
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009386 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009387 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009388 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009389 temp->len += 4;
9390 break;
9391 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009392 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009393 temp->len += 16;
9394 break;
9395 default:
9396 return 0;
9397 }
9398
9399 smp->data.str = *temp;
9400 smp->type = SMP_T_BIN;
9401 return 1;
9402}
9403
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009404static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009405smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009406 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009407{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009408 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9409 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9410 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009411
Willy Tarreau24e32d82012-04-23 23:55:44 +02009412 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009413
Willy Tarreauf853c462012-04-23 18:53:56 +02009414 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009415 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009416 return 1;
9417}
9418
Willy Tarreau7f18e522010-10-22 20:04:13 +02009419/* return a valid test if the current request is the first one on the connection */
9420static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009421smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009422 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009423{
9424 if (!s)
9425 return 0;
9426
Willy Tarreauf853c462012-04-23 18:53:56 +02009427 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009428 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009429 return 1;
9430}
9431
Willy Tarreau34db1082012-04-19 17:16:54 +02009432/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009433static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009434smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009435 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009436{
9437
Willy Tarreau24e32d82012-04-23 23:55:44 +02009438 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009439 return 0;
9440
Willy Tarreauc0239e02012-04-16 14:42:55 +02009441 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009442
Willy Tarreauc0239e02012-04-16 14:42:55 +02009443 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009444 return 0;
9445
Willy Tarreauf853c462012-04-23 18:53:56 +02009446 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009447 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 +01009448 return 1;
9449}
Willy Tarreau8797c062007-05-07 00:55:35 +02009450
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009451/* Accepts exactly 1 argument of type userlist */
9452static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009453smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009454 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009455{
9456
9457 if (!args || args->type != ARGT_USR)
9458 return 0;
9459
9460 CHECK_HTTP_MESSAGE_FIRST();
9461
9462 if (!get_http_auth(l4))
9463 return 0;
9464
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009465 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009466 smp->ctx.a[0] = args->data.usr; /* user list */
9467 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9468 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9469
9470 /* if the user does not belong to the userlist or has a wrong password,
9471 * report that it unconditionally does not match. Otherwise we return
9472 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009473 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009474 */
9475 smp->type = SMP_T_BOOL;
9476 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9477 if (smp->data.uint)
9478 smp->type = SMP_T_UINT;
9479
9480 return 1;
9481}
9482
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009483/* Try to find the next occurrence of a cookie name in a cookie header value.
9484 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9485 * the cookie value is returned into *value and *value_l, and the function
9486 * returns a pointer to the next pointer to search from if the value was found.
9487 * Otherwise if the cookie was not found, NULL is returned and neither value
9488 * nor value_l are touched. The input <hdr> string should first point to the
9489 * header's value, and the <hdr_end> pointer must point to the first character
9490 * not part of the value. <list> must be non-zero if value may represent a list
9491 * of values (cookie headers). This makes it faster to abort parsing when no
9492 * list is expected.
9493 */
9494static char *
9495extract_cookie_value(char *hdr, const char *hdr_end,
9496 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009497 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009498{
9499 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9500 char *next;
9501
9502 /* we search at least a cookie name followed by an equal, and more
9503 * generally something like this :
9504 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9505 */
9506 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9507 /* Iterate through all cookies on this line */
9508
9509 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9510 att_beg++;
9511
9512 /* find att_end : this is the first character after the last non
9513 * space before the equal. It may be equal to hdr_end.
9514 */
9515 equal = att_end = att_beg;
9516
9517 while (equal < hdr_end) {
9518 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9519 break;
9520 if (http_is_spht[(unsigned char)*equal++])
9521 continue;
9522 att_end = equal;
9523 }
9524
9525 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9526 * is between <att_beg> and <equal>, both may be identical.
9527 */
9528
9529 /* look for end of cookie if there is an equal sign */
9530 if (equal < hdr_end && *equal == '=') {
9531 /* look for the beginning of the value */
9532 val_beg = equal + 1;
9533 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9534 val_beg++;
9535
9536 /* find the end of the value, respecting quotes */
9537 next = find_cookie_value_end(val_beg, hdr_end);
9538
9539 /* make val_end point to the first white space or delimitor after the value */
9540 val_end = next;
9541 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9542 val_end--;
9543 } else {
9544 val_beg = val_end = next = equal;
9545 }
9546
9547 /* We have nothing to do with attributes beginning with '$'. However,
9548 * they will automatically be removed if a header before them is removed,
9549 * since they're supposed to be linked together.
9550 */
9551 if (*att_beg == '$')
9552 continue;
9553
9554 /* Ignore cookies with no equal sign */
9555 if (equal == next)
9556 continue;
9557
9558 /* Now we have the cookie name between att_beg and att_end, and
9559 * its value between val_beg and val_end.
9560 */
9561
9562 if (att_end - att_beg == cookie_name_l &&
9563 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9564 /* let's return this value and indicate where to go on from */
9565 *value = val_beg;
9566 *value_l = val_end - val_beg;
9567 return next + 1;
9568 }
9569
9570 /* Set-Cookie headers only have the name in the first attr=value part */
9571 if (!list)
9572 break;
9573 }
9574
9575 return NULL;
9576}
9577
Willy Tarreaue333ec92012-04-16 16:26:40 +02009578/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009579 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009580 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009581 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009582 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009583 * Accepts exactly 1 argument of type string. If the input options indicate
9584 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009585 */
9586static int
Willy Tarreau51539362012-05-08 12:46:28 +02009587smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009588 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009589{
9590 struct http_txn *txn = l7;
9591 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009592 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009593 const struct http_msg *msg;
9594 const char *hdr_name;
9595 int hdr_name_len;
9596 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009597 int occ = 0;
9598 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009599
Willy Tarreau24e32d82012-04-23 23:55:44 +02009600 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009601 return 0;
9602
Willy Tarreaua890d072013-04-02 12:01:06 +02009603 if (!ctx) {
9604 /* first call */
9605 ctx = &static_hdr_ctx;
9606 ctx->idx = 0;
9607 smp->ctx.a[2] = ctx;
9608 }
9609
Willy Tarreaue333ec92012-04-16 16:26:40 +02009610 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009611
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009612 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009613 msg = &txn->req;
9614 hdr_name = "Cookie";
9615 hdr_name_len = 6;
9616 } else {
9617 msg = &txn->rsp;
9618 hdr_name = "Set-Cookie";
9619 hdr_name_len = 10;
9620 }
9621
Willy Tarreau28376d62012-04-26 21:26:10 +02009622 if (!occ && !(opt & SMP_OPT_ITERATE))
9623 /* no explicit occurrence and single fetch => last cookie by default */
9624 occ = -1;
9625
9626 /* OK so basically here, either we want only one value and it's the
9627 * last one, or we want to iterate over all of them and we fetch the
9628 * next one.
9629 */
9630
Willy Tarreau9b28e032012-10-12 23:49:43 +02009631 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009632 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009633 /* search for the header from the beginning, we must first initialize
9634 * the search parameters.
9635 */
Willy Tarreau37406352012-04-23 16:16:37 +02009636 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009637 ctx->idx = 0;
9638 }
9639
Willy Tarreau28376d62012-04-26 21:26:10 +02009640 smp->flags |= SMP_F_VOL_HDR;
9641
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009642 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009643 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9644 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009645 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9646 goto out;
9647
Willy Tarreau24e32d82012-04-23 23:55:44 +02009648 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009649 continue;
9650
Willy Tarreau37406352012-04-23 16:16:37 +02009651 smp->ctx.a[0] = ctx->line + ctx->val;
9652 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009653 }
9654
Willy Tarreauf853c462012-04-23 18:53:56 +02009655 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009656 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009657 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009658 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009659 &smp->data.str.str,
9660 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009661 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009662 found = 1;
9663 if (occ >= 0) {
9664 /* one value was returned into smp->data.str.{str,len} */
9665 smp->flags |= SMP_F_NOT_LAST;
9666 return 1;
9667 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009668 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009669 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009670 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009671 /* all cookie headers and values were scanned. If we're looking for the
9672 * last occurrence, we may return it now.
9673 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009674 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009675 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009676 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009677}
9678
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009679/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009680 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009681 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009682 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009683 */
9684static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009685smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009686 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009687{
9688 struct http_txn *txn = l7;
9689 struct hdr_idx *idx = &txn->hdr_idx;
9690 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009691 const struct http_msg *msg;
9692 const char *hdr_name;
9693 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009694 int cnt;
9695 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009696 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009697
Willy Tarreau24e32d82012-04-23 23:55:44 +02009698 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009699 return 0;
9700
Willy Tarreaue333ec92012-04-16 16:26:40 +02009701 CHECK_HTTP_MESSAGE_FIRST();
9702
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009703 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009704 msg = &txn->req;
9705 hdr_name = "Cookie";
9706 hdr_name_len = 6;
9707 } else {
9708 msg = &txn->rsp;
9709 hdr_name = "Set-Cookie";
9710 hdr_name_len = 10;
9711 }
9712
Willy Tarreau9b28e032012-10-12 23:49:43 +02009713 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009714 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009715 ctx.idx = 0;
9716 cnt = 0;
9717
9718 while (1) {
9719 /* Note: val_beg == NULL every time we need to fetch a new header */
9720 if (!val_beg) {
9721 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9722 break;
9723
Willy Tarreau24e32d82012-04-23 23:55:44 +02009724 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009725 continue;
9726
9727 val_beg = ctx.line + ctx.val;
9728 val_end = val_beg + ctx.vlen;
9729 }
9730
Willy Tarreauf853c462012-04-23 18:53:56 +02009731 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009732 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009733 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009734 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009735 &smp->data.str.str,
9736 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009737 cnt++;
9738 }
9739 }
9740
Willy Tarreauf853c462012-04-23 18:53:56 +02009741 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009742 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009743 return 1;
9744}
9745
Willy Tarreau51539362012-05-08 12:46:28 +02009746/* Fetch an cookie's integer value. The integer value is returned. It
9747 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9748 */
9749static int
9750smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009751 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009752{
Willy Tarreauef38c392013-07-22 16:29:32 +02009753 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009754
9755 if (ret > 0) {
9756 smp->type = SMP_T_UINT;
9757 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9758 }
9759
9760 return ret;
9761}
9762
Willy Tarreau8797c062007-05-07 00:55:35 +02009763/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009764/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009765/************************************************************************/
9766
David Cournapeau16023ee2010-12-23 20:55:41 +09009767/*
9768 * Given a path string and its length, find the position of beginning of the
9769 * query string. Returns NULL if no query string is found in the path.
9770 *
9771 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9772 *
9773 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9774 */
bedis4c75cca2012-10-05 08:38:24 +02009775static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009776{
9777 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009778
bedis4c75cca2012-10-05 08:38:24 +02009779 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009780 return p ? p + 1 : NULL;
9781}
9782
bedis4c75cca2012-10-05 08:38:24 +02009783static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009784{
bedis4c75cca2012-10-05 08:38:24 +02009785 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009786}
9787
9788/*
9789 * Given a url parameter, find the starting position of the first occurence,
9790 * or NULL if the parameter is not found.
9791 *
9792 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9793 * the function will return query_string+8.
9794 */
9795static char*
9796find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009797 char* url_param_name, size_t url_param_name_l,
9798 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009799{
9800 char *pos, *last;
9801
9802 pos = query_string;
9803 last = query_string + query_string_l - url_param_name_l - 1;
9804
9805 while (pos <= last) {
9806 if (pos[url_param_name_l] == '=') {
9807 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9808 return pos;
9809 pos += url_param_name_l + 1;
9810 }
bedis4c75cca2012-10-05 08:38:24 +02009811 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009812 pos++;
9813 pos++;
9814 }
9815 return NULL;
9816}
9817
9818/*
9819 * Given a url parameter name, returns its value and size into *value and
9820 * *value_l respectively, and returns non-zero. If the parameter is not found,
9821 * zero is returned and value/value_l are not touched.
9822 */
9823static int
9824find_url_param_value(char* path, size_t path_l,
9825 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009826 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009827{
9828 char *query_string, *qs_end;
9829 char *arg_start;
9830 char *value_start, *value_end;
9831
bedis4c75cca2012-10-05 08:38:24 +02009832 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009833 if (!query_string)
9834 return 0;
9835
9836 qs_end = path + path_l;
9837 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009838 url_param_name, url_param_name_l,
9839 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009840 if (!arg_start)
9841 return 0;
9842
9843 value_start = arg_start + url_param_name_l + 1;
9844 value_end = value_start;
9845
bedis4c75cca2012-10-05 08:38:24 +02009846 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009847 value_end++;
9848
9849 *value = value_start;
9850 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009851 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009852}
9853
9854static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009855smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009856 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009857{
bedis4c75cca2012-10-05 08:38:24 +02009858 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009859 struct http_txn *txn = l7;
9860 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009861
bedis4c75cca2012-10-05 08:38:24 +02009862 if (!args || args[0].type != ARGT_STR ||
9863 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009864 return 0;
9865
9866 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009867
bedis4c75cca2012-10-05 08:38:24 +02009868 if (args[1].type)
9869 delim = *args[1].data.str.str;
9870
Willy Tarreau9b28e032012-10-12 23:49:43 +02009871 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009872 args->data.str.str, args->data.str.len,
9873 &smp->data.str.str, &smp->data.str.len,
9874 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009875 return 0;
9876
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009877 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009878 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009879 return 1;
9880}
9881
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009882/* Return the signed integer value for the specified url parameter (see url_param
9883 * above).
9884 */
9885static int
9886smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009887 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009888{
Willy Tarreauef38c392013-07-22 16:29:32 +02009889 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009890
9891 if (ret > 0) {
9892 smp->type = SMP_T_UINT;
9893 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9894 }
9895
9896 return ret;
9897}
9898
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009899/* This produces a 32-bit hash of the concatenation of the first occurrence of
9900 * the Host header followed by the path component if it begins with a slash ('/').
9901 * This means that '*' will not be added, resulting in exactly the first Host
9902 * entry. If no Host header is found, then the path is used. The resulting value
9903 * is hashed using the url hash followed by a full avalanche hash and provides a
9904 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9905 * high-traffic sites without having to store whole paths.
9906 * this differs from the base32 functions in that it includes the url parameters
9907 * as well as the path
9908 */
9909static int
9910smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009911 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009912{
9913 struct http_txn *txn = l7;
9914 struct hdr_ctx ctx;
9915 unsigned int hash = 0;
9916 char *ptr, *beg, *end;
9917 int len;
9918
9919 CHECK_HTTP_MESSAGE_FIRST();
9920
9921 ctx.idx = 0;
9922 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9923 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9924 ptr = ctx.line + ctx.val;
9925 len = ctx.vlen;
9926 while (len--)
9927 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9928 }
9929
9930 /* now retrieve the path */
9931 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9932 beg = http_get_path(txn);
9933 if (!beg)
9934 beg = end;
9935
9936 for (ptr = beg; ptr < end ; ptr++);
9937
9938 if (beg < ptr && *beg == '/') {
9939 while (beg < ptr)
9940 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9941 }
9942 hash = full_hash(hash);
9943
9944 smp->type = SMP_T_UINT;
9945 smp->data.uint = hash;
9946 smp->flags = SMP_F_VOL_1ST;
9947 return 1;
9948}
9949
9950/* This concatenates the source address with the 32-bit hash of the Host and
9951 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9952 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9953 * on the source address length. The URL hash is stored before the address so
9954 * that in environments where IPv6 is insignificant, truncating the output to
9955 * 8 bytes would still work.
9956 */
9957static int
9958smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009959 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009960{
9961 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009962 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009963
Willy Tarreaue155ec22013-11-18 18:33:22 +01009964 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009965 return 0;
9966
9967 temp = get_trash_chunk();
9968 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9969 temp->len += sizeof(smp->data.uint);
9970
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009971 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009972 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009973 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009974 temp->len += 4;
9975 break;
9976 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009977 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009978 temp->len += 16;
9979 break;
9980 default:
9981 return 0;
9982 }
9983
9984 smp->data.str = *temp;
9985 smp->type = SMP_T_BIN;
9986 return 1;
9987}
9988
Willy Tarreau185b5c42012-04-26 15:11:51 +02009989/* This function is used to validate the arguments passed to any "hdr" fetch
9990 * keyword. These keywords support an optional positive or negative occurrence
9991 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9992 * is assumed that the types are already the correct ones. Returns 0 on error,
9993 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9994 * error message in case of error, that the caller is responsible for freeing.
9995 * The initial location must either be freeable or NULL.
9996 */
9997static int val_hdr(struct arg *arg, char **err_msg)
9998{
9999 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010000 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010001 return 0;
10002 }
10003 return 1;
10004}
10005
Willy Tarreau276fae92013-07-25 14:36:01 +020010006/* takes an UINT value on input supposed to represent the time since EPOCH,
10007 * adds an optional offset found in args[0] and emits a string representing
10008 * the date in RFC-1123/5322 format.
10009 */
10010static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10011{
10012 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10013 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10014 struct chunk *temp;
10015 struct tm *tm;
10016 time_t curr_date = smp->data.uint;
10017
10018 /* add offset */
10019 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10020 curr_date += args[0].data.sint;
10021
10022 tm = gmtime(&curr_date);
10023
10024 temp = get_trash_chunk();
10025 temp->len = snprintf(temp->str, temp->size - temp->len,
10026 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10027 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10028 tm->tm_hour, tm->tm_min, tm->tm_sec);
10029
10030 smp->data.str = *temp;
10031 smp->type = SMP_T_STR;
10032 return 1;
10033}
10034
Willy Tarreau4a568972010-05-12 08:08:50 +020010035/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010036/* All supported ACL keywords must be declared here. */
10037/************************************************************************/
10038
10039/* Note: must not be declared <const> as its list will be overwritten.
10040 * Please take care of keeping this list alphabetically sorted.
10041 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010042static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010043 { "base", "base", pat_parse_str, pat_match_str },
10044 { "base_beg", "base", pat_parse_str, pat_match_beg },
10045 { "base_dir", "base", pat_parse_str, pat_match_dir },
10046 { "base_dom", "base", pat_parse_str, pat_match_dom },
10047 { "base_end", "base", pat_parse_str, pat_match_end },
10048 { "base_len", "base", pat_parse_int, pat_match_len },
10049 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10050 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010051
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010052 { "cook", "req.cook", pat_parse_str, pat_match_str },
10053 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10054 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10055 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10056 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10057 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10058 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10059 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010060
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010061 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10062 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10063 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10064 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10065 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10066 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10067 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10068 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010069
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010070 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010071
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010072 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010073
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010074 { "path", "path", pat_parse_str, pat_match_str },
10075 { "path_beg", "path", pat_parse_str, pat_match_beg },
10076 { "path_dir", "path", pat_parse_str, pat_match_dir },
10077 { "path_dom", "path", pat_parse_str, pat_match_dom },
10078 { "path_end", "path", pat_parse_str, pat_match_end },
10079 { "path_len", "path", pat_parse_int, pat_match_len },
10080 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10081 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010082
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010083 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10084 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010085
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010086 { "scook", "res.cook", pat_parse_str, pat_match_str },
10087 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10088 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10089 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10090 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10091 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10092 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10093 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010094
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010095 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10096 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10097 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10098 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10099 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10100 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10101 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10102 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010103
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010104 { "url", "url", pat_parse_str, pat_match_str },
10105 { "url_beg", "url", pat_parse_str, pat_match_beg },
10106 { "url_dir", "url", pat_parse_str, pat_match_dir },
10107 { "url_dom", "url", pat_parse_str, pat_match_dom },
10108 { "url_end", "url", pat_parse_str, pat_match_end },
10109 { "url_len", "url", pat_parse_int, pat_match_len },
10110 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10111 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010112
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010113 { "urlp", "urlp", pat_parse_str, pat_match_str },
10114 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10115 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10116 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10117 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10118 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10119 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10120 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010121
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010122 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010123}};
10124
10125/************************************************************************/
10126/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010127/************************************************************************/
10128/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010129static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010130 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10131 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10132 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10133
10134 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10135 * are only here to match the ACL's name, are request-only and are used
10136 * for ACL compatibility only.
10137 */
10138 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10139 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10140 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10141 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10142
10143 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10144 * only here to match the ACL's name, are request-only and are used for
10145 * ACL compatibility only.
10146 */
10147 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10148 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10149 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10150 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10151
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010152 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10153 { "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 +010010154 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10155 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10156 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010157
10158 /* HTTP protocol on the request path */
10159 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010160 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010161
10162 /* HTTP version on the request path */
10163 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010164 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010165
10166 /* HTTP version on the response path */
10167 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010168 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10169
Willy Tarreau18ed2562013-01-14 15:56:36 +010010170 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10171 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10172 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10173 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10174
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010175 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10176 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010177 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10178 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10179 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10180 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10181
10182 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10183 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10184 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10185 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10186
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010187 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10188 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010189 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10190 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10191 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10192 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10193
Willy Tarreau409bcde2013-01-08 00:31:00 +010010194 /* scook is valid only on the response and is used for ACL compatibility */
10195 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10196 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10197 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10198 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10199
10200 /* shdr is valid only on the response and is used for ACL compatibility */
10201 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10202 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10203 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10204 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10205
10206 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10207 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010208 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10209 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010210 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10211 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10212 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10213 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10214 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10215 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010216}};
10217
Willy Tarreau8797c062007-05-07 00:55:35 +020010218
Willy Tarreau276fae92013-07-25 14:36:01 +020010219/* Note: must not be declared <const> as its list will be overwritten */
10220static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10221 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10222 { NULL, NULL, 0, 0, 0 },
10223}};
10224
Willy Tarreau8797c062007-05-07 00:55:35 +020010225__attribute__((constructor))
10226static void __http_protocol_init(void)
10227{
10228 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010229 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010230 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010231}
10232
10233
Willy Tarreau58f10d72006-12-04 02:26:12 +010010234/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010235 * Local variables:
10236 * c-indent-level: 8
10237 * c-basic-offset: 8
10238 * End:
10239 */