blob: e92dc6a6ab9374a56d6bb9b67da57ee3e6fc9058 [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
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001290 * searching again for something we are unable to find anyway. However, if
1291 * the result if valid, the cache is not reused because we would risk to
1292 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001293 */
1294
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001295char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001296
1297int
1298get_http_auth(struct session *s)
1299{
1300
1301 struct http_txn *txn = &s->txn;
1302 struct chunk auth_method;
1303 struct hdr_ctx ctx;
1304 char *h, *p;
1305 int len;
1306
1307#ifdef DEBUG_AUTH
1308 printf("Auth for session %p: %d\n", s, txn->auth.method);
1309#endif
1310
1311 if (txn->auth.method == HTTP_AUTH_WRONG)
1312 return 0;
1313
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001314 txn->auth.method = HTTP_AUTH_WRONG;
1315
1316 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001317
1318 if (txn->flags & TX_USE_PX_CONN) {
1319 h = "Proxy-Authorization";
1320 len = strlen(h);
1321 } else {
1322 h = "Authorization";
1323 len = strlen(h);
1324 }
1325
Willy Tarreau9b28e032012-10-12 23:49:43 +02001326 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001327 return 0;
1328
1329 h = ctx.line + ctx.val;
1330
1331 p = memchr(h, ' ', ctx.vlen);
1332 if (!p || p == h)
1333 return 0;
1334
1335 chunk_initlen(&auth_method, h, 0, p-h);
1336 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1337
1338 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1339
1340 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001341 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001342
1343 if (len < 0)
1344 return 0;
1345
1346
1347 get_http_auth_buff[len] = '\0';
1348
1349 p = strchr(get_http_auth_buff, ':');
1350
1351 if (!p)
1352 return 0;
1353
1354 txn->auth.user = get_http_auth_buff;
1355 *p = '\0';
1356 txn->auth.pass = p+1;
1357
1358 txn->auth.method = HTTP_AUTH_BASIC;
1359 return 1;
1360 }
1361
1362 return 0;
1363}
1364
Willy Tarreau58f10d72006-12-04 02:26:12 +01001365
Willy Tarreau8973c702007-01-21 23:58:29 +01001366/*
1367 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001368 * depending on the initial msg->msg_state. The caller is responsible for
1369 * ensuring that the message does not wrap. The function can be preempted
1370 * everywhere when data are missing and recalled at the exact same location
1371 * with no information loss. The message may even be realigned between two
1372 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001373 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001374 * fields. Note that msg->sol will be initialized after completing the first
1375 * state, so that none of the msg pointers has to be initialized prior to the
1376 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001378void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379{
Willy Tarreau3770f232013-12-07 00:01:53 +01001380 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001382 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001384 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001385 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001386 ptr = buf->p + msg->next;
1387 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(ptr >= end))
1390 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 /*
1394 * First, states that are specific to the response only.
1395 * We check them first so that request and headers are
1396 * closer to each other (accessed more often).
1397 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001399 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001401 /* we have a start of message, but we have to check
1402 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001403 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001404 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001405 if (unlikely(ptr != buf->p)) {
1406 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001407 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001408 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001409 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 }
Willy Tarreau26927362012-05-18 23:22:52 +02001411 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001412 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 hdr_idx_init(idx);
1414 state = HTTP_MSG_RPVER;
1415 goto http_msg_rpver;
1416 }
1417
1418 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1419 goto http_msg_invalid;
1420
1421 if (unlikely(*ptr == '\n'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1424 /* stop here */
1425
Willy Tarreau8973c702007-01-21 23:58:29 +01001426 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001427 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 EXPECT_LF_HERE(ptr, http_msg_invalid);
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1430 /* stop here */
1431
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001433 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 case HTTP_MSG_RPVER_SP:
1435 case HTTP_MSG_RPCODE:
1436 case HTTP_MSG_RPCODE_SP:
1437 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001438 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001439 state, ptr, end,
1440 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 if (unlikely(!ptr))
1442 return;
1443
1444 /* we have a full response and we know that we have either a CR
1445 * or an LF at <ptr>.
1446 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1448
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001449 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1452 goto http_msg_rpline_end;
1453
Willy Tarreau8973c702007-01-21 23:58:29 +01001454 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001456 /* msg->sol must point to the first of CR or LF. */
1457 EXPECT_LF_HERE(ptr, http_msg_invalid);
1458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1459 /* stop here */
1460
1461 /*
1462 * Second, states that are specific to the request only
1463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001465 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001467 /* we have a start of message, but we have to check
1468 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001469 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001470 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001471 if (likely(ptr != buf->p)) {
1472 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001473 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001474 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001475 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 }
Willy Tarreau26927362012-05-18 23:22:52 +02001477 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001478 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 state = HTTP_MSG_RQMETH;
1480 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1484 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 if (unlikely(*ptr == '\n'))
1487 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1488 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001489 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EXPECT_LF_HERE(ptr, http_msg_invalid);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001498 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 case HTTP_MSG_RQMETH_SP:
1500 case HTTP_MSG_RQURI:
1501 case HTTP_MSG_RQURI_SP:
1502 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001503 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001504 state, ptr, end,
1505 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 if (unlikely(!ptr))
1507 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 /* we have a full request and we know that we have either a CR
1510 * or an LF at <ptr>.
1511 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001514 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 if (likely(*ptr == '\r'))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001520 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 /* check for HTTP/0.9 request : no version information available.
1522 * msg->sol must point to the first of CR or LF.
1523 */
1524 if (unlikely(msg->sl.rq.v_l == 0))
1525 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 /*
1532 * Common states below
1533 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001535 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001536 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 if (likely(!HTTP_IS_CRLF(*ptr))) {
1538 goto http_msg_hdr_name;
1539 }
1540
1541 if (likely(*ptr == '\r'))
1542 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1543 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001546 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 /* assumes msg->sol points to the first char */
1548 if (likely(HTTP_IS_TOKEN(*ptr)))
1549 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001551 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001553
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001554 if (likely(msg->err_pos < -1) || *ptr == '\n')
1555 goto http_msg_invalid;
1556
1557 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001558 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001559
1560 /* and we still accept this non-token character */
1561 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001565 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (likely(HTTP_IS_SPHT(*ptr)))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001570 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 if (likely(!HTTP_IS_CRLF(*ptr))) {
1573 goto http_msg_hdr_val;
1574 }
1575
1576 if (likely(*ptr == '\r'))
1577 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1578 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001581 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 EXPECT_LF_HERE(ptr, http_msg_invalid);
1583 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001586 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 if (likely(HTTP_IS_SPHT(*ptr))) {
1588 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001589 for (; buf->p + msg->sov < ptr; msg->sov++)
1590 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 goto http_msg_hdr_l1_sp;
1592 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001593 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001594 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 goto http_msg_complete_header;
1596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001598 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001599 /* assumes msg->sol points to the first char, and msg->sov
1600 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 */
1602 if (likely(!HTTP_IS_CRLF(*ptr)))
1603 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001605 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 /* Note: we could also copy eol into ->eoh so that we have the
1607 * real header end in case it ends with lots of LWS, but is this
1608 * really needed ?
1609 */
1610 if (likely(*ptr == '\r'))
1611 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1612 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 EXPECT_LF_HERE(ptr, http_msg_invalid);
1617 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001620 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1622 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001623 for (; buf->p + msg->eol < ptr; msg->eol++)
1624 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 goto http_msg_hdr_val;
1626 }
1627 http_msg_complete_header:
1628 /*
1629 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001630 * Assumes msg->sol points to the first char, msg->sov points
1631 * to the first character of the value and msg->eol to the
1632 * first CR or LF so we know how the line ends. We insert last
1633 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001635 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 idx, idx->tail) < 0))
1637 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001638
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001639 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (likely(!HTTP_IS_CRLF(*ptr))) {
1641 goto http_msg_hdr_name;
1642 }
1643
1644 if (likely(*ptr == '\r'))
1645 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1646 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001649 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 /* Assumes msg->sol points to the first of either CR or LF */
1651 EXPECT_LF_HERE(ptr, http_msg_invalid);
1652 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001653 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001654 msg->eoh = msg->sol;
1655 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001658
1659 case HTTP_MSG_ERROR:
1660 /* this may only happen if we call http_msg_analyser() twice with an error */
1661 break;
1662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001664#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1666 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001667#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001668 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 }
1670 http_msg_ood:
1671 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001672 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 http_msg_invalid:
1677 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001678 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001679 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 return;
1681}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001682
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001683/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1684 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1685 * nothing is done and 1 is returned.
1686 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001687static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001688{
1689 int delta;
1690 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001691 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692
1693 if (msg->sl.rq.v_l != 0)
1694 return 1;
1695
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 delta = 0;
1698
1699 if (msg->sl.rq.u_l == 0) {
1700 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001702 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001703 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001704 }
1705 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001706 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001707 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001708 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001709 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001710 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001711 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001712 NULL, NULL);
1713 if (unlikely(!cur_end))
1714 return 0;
1715
1716 /* we have a full HTTP/1.0 request now and we know that
1717 * we have either a CR or an LF at <ptr>.
1718 */
1719 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1720 return 1;
1721}
1722
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001723/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001724 * and "keep-alive" values. If we already know that some headers may safely
1725 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1727 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001728 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1730 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1731 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001732 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001733 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001734void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001735{
Willy Tarreau5b154472009-12-21 20:11:07 +01001736 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001737 const char *hdr_val = "Connection";
1738 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001740 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001741 return;
1742
Willy Tarreau88d349d2010-01-25 12:15:43 +01001743 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1744 hdr_val = "Proxy-Connection";
1745 hdr_len = 16;
1746 }
1747
Willy Tarreau5b154472009-12-21 20:11:07 +01001748 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001749 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001750 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001751 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1752 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001753 if (to_del & 2)
1754 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001755 else
1756 txn->flags |= TX_CON_KAL_SET;
1757 }
1758 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1759 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001760 if (to_del & 1)
1761 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001762 else
1763 txn->flags |= TX_CON_CLO_SET;
1764 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001765 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1766 txn->flags |= TX_HDR_CONN_UPG;
1767 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001768 }
1769
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001770 txn->flags |= TX_HDR_CONN_PRS;
1771 return;
1772}
Willy Tarreau5b154472009-12-21 20:11:07 +01001773
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001774/* Apply desired changes on the Connection: header. Values may be removed and/or
1775 * added depending on the <wanted> flags, which are exclusively composed of
1776 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1777 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1778 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001779void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001780{
1781 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001782 const char *hdr_val = "Connection";
1783 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001784
1785 ctx.idx = 0;
1786
Willy Tarreau88d349d2010-01-25 12:15:43 +01001787
1788 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1789 hdr_val = "Proxy-Connection";
1790 hdr_len = 16;
1791 }
1792
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001793 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001794 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1796 if (wanted & TX_CON_KAL_SET)
1797 txn->flags |= TX_CON_KAL_SET;
1798 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001799 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001800 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001801 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1802 if (wanted & TX_CON_CLO_SET)
1803 txn->flags |= TX_CON_CLO_SET;
1804 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001805 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001806 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001807 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808
1809 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1810 return;
1811
1812 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1813 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 hdr_val = "Connection: close";
1815 hdr_len = 17;
1816 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1817 hdr_val = "Proxy-Connection: close";
1818 hdr_len = 23;
1819 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 }
1822
1823 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1824 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001825 hdr_val = "Connection: keep-alive";
1826 hdr_len = 22;
1827 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1828 hdr_val = "Proxy-Connection: keep-alive";
1829 hdr_len = 28;
1830 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001831 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001832 }
1833 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001834}
1835
Willy Tarreaua458b672012-03-05 11:17:50 +01001836/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001838 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001841 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001842static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001843{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001844 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001846 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 const char *end = buf->data + buf->size;
1848 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 unsigned int chunk = 0;
1850
1851 /* The chunk size is in the following form, though we are only
1852 * interested in the size and CRLF :
1853 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1854 */
1855 while (1) {
1856 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001857 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001858 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001860 if (c < 0) /* not a hex digit anymore */
1861 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001862 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001863 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001864 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001865 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 chunk = (chunk << 4) + c;
1867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001870 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001871 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872
1873 while (http_is_spht[(unsigned char)*ptr]) {
1874 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001875 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001876 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001877 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001878 }
1879
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 /* Up to there, we know that at least one byte is present at *ptr. Check
1881 * for the end of chunk size.
1882 */
1883 while (1) {
1884 if (likely(HTTP_IS_CRLF(*ptr))) {
1885 /* we now have a CR or an LF at ptr */
1886 if (likely(*ptr == '\r')) {
1887 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001888 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001889 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 return 0;
1891 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001892
Willy Tarreaud98cf932009-12-27 22:54:55 +01001893 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001894 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001896 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001897 /* done */
1898 break;
1899 }
1900 else if (*ptr == ';') {
1901 /* chunk extension, ends at next CRLF */
1902 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001903 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001904 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001905 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906
1907 while (!HTTP_IS_CRLF(*ptr)) {
1908 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001909 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001910 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 return 0;
1912 }
1913 /* we have a CRLF now, loop above */
1914 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001915 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001917 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 }
1919
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001921 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001924 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001926 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001928 msg->chunk_len = chunk;
1929 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001932 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001933 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001934 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001935}
1936
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001937/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001938 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001939 * the trailers is found, it is automatically scheduled to be forwarded,
1940 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1941 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001942 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001943 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001944 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001945 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1946 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001947 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001948 * matches the length of trailers already parsed and not forwarded. It is also
1949 * important to note that this function is designed to be able to parse wrapped
1950 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001952static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001954 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001955
Willy Tarreaua458b672012-03-05 11:17:50 +01001956 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001958 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001959 const char *ptr = b_ptr(buf, msg->next);
1960 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001961 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962
1963 /* scan current line and stop at LF or CRLF */
1964 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001965 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 return 0;
1967
1968 if (*ptr == '\n') {
1969 if (!p1)
1970 p1 = ptr;
1971 p2 = ptr;
1972 break;
1973 }
1974
1975 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001976 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001979 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001980 p1 = ptr;
1981 }
1982
1983 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 if (ptr >= buf->data + buf->size)
1985 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001986 }
1987
1988 /* after LF; point to beginning of next line */
1989 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 if (p2 >= buf->data + buf->size)
1991 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001993 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001994 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001995 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001996
1997 /* schedule this line for forwarding */
1998 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001999 if (msg->sov >= buf->size)
2000 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002003 /* LF/CRLF at beginning of line => end of trailers at p2.
2004 * Everything was scheduled for forwarding, there's nothing
2005 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002006 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002007 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 msg->msg_state = HTTP_MSG_DONE;
2009 return 1;
2010 }
2011 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002013 }
2014}
2015
Willy Tarreau54d23df2012-10-25 19:04:45 +02002016/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002018 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2021 * not enough data are available, the function does not change anything and
2022 * returns zero. If a parse error is encountered, the function returns < 0 and
2023 * does not change anything. Note: this function is designed to parse wrapped
2024 * CRLF at the end of the buffer.
2025 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002026static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002028 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002029 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002030 int bytes;
2031
2032 /* NB: we'll check data availabilty at the end. It's not a
2033 * problem because whatever we match first will be checked
2034 * against the correct length.
2035 */
2036 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002037 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr == '\r') {
2039 bytes++;
2040 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 if (ptr >= buf->data + buf->size)
2042 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043 }
2044
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002045 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002048 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002049 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002050 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002051 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002052
2053 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002054 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002055 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002056 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2057 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002058 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2060 return 1;
2061}
Willy Tarreau5b154472009-12-21 20:11:07 +01002062
William Lallemand82fe75c2012-10-23 10:25:10 +02002063
2064/*
2065 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002066 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002067int select_compression_request_header(struct session *s, struct buffer *req)
2068{
2069 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002070 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002071 struct hdr_ctx ctx;
2072 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002073 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002074
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002075 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2076 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002077 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2078 */
2079 ctx.idx = 0;
2080 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2081 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002082 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2083 (ctx.vlen < 31 ||
2084 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2085 ctx.line[ctx.val + 30] < '6' ||
2086 (ctx.line[ctx.val + 30] == '6' &&
2087 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2088 s->comp_algo = NULL;
2089 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002090 }
2091
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002093 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 +02002094 ctx.idx = 0;
2095 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002096 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002097 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2098 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002099
2100 /* remove all occurrences of the header when "compression offload" is set */
2101
2102 if ((s->be->comp && s->be->comp->offload) ||
2103 (s->fe->comp && s->fe->comp->offload)) {
2104 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2105 ctx.idx = 0;
2106 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2107 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2108 }
2109 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002110 return 1;
2111 }
2112 }
2113 }
2114 }
2115
2116 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002117 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2118 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002119 if (comp_algo->add_data == identity_add_data) {
2120 s->comp_algo = comp_algo;
2121 return 1;
2122 }
2123 }
2124 }
2125
2126 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002127 return 0;
2128}
2129
2130/*
2131 * Selects a comression algorithm depending of the server response.
2132 */
2133int select_compression_response_header(struct session *s, struct buffer *res)
2134{
2135 struct http_txn *txn = &s->txn;
2136 struct http_msg *msg = &txn->rsp;
2137 struct hdr_ctx ctx;
2138 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002139
2140 /* no common compression algorithm was found in request header */
2141 if (s->comp_algo == NULL)
2142 goto fail;
2143
2144 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002145 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002146 goto fail;
2147
William Lallemandd3002612012-11-26 14:34:47 +01002148 /* 200 only */
2149 if (txn->status != 200)
2150 goto fail;
2151
William Lallemand82fe75c2012-10-23 10:25:10 +02002152 /* Content-Length is null */
2153 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2154 goto fail;
2155
Willy Tarreau667c2a32013-04-09 08:13:58 +02002156 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2157 if (msg->flags & HTTP_MSGF_TE_CHNK)
2158 goto fail;
2159
William Lallemand82fe75c2012-10-23 10:25:10 +02002160 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002161 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002162 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2163 goto fail;
2164
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002165 /* no compression when Cache-Control: no-transform is present in the message */
2166 ctx.idx = 0;
2167 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2168 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2169 goto fail;
2170 }
2171
William Lallemand82fe75c2012-10-23 10:25:10 +02002172 comp_type = NULL;
2173
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002174 /* we don't want to compress multipart content-types, nor content-types that are
2175 * not listed in the "compression type" directive if any. If no content-type was
2176 * found but configuration requires one, we don't compress either. Backend has
2177 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002178 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002179 ctx.idx = 0;
2180 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2181 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2182 goto fail;
2183
2184 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2185 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002186 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 if (ctx.vlen >= comp_type->name_len &&
2188 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002189 /* this Content-Type should be compressed */
2190 break;
2191 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002192 /* this Content-Type should not be compressed */
2193 if (comp_type == NULL)
2194 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002195 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002196 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002197 else { /* no content-type header */
2198 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2199 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002200 }
2201
William Lallemandd85f9172012-11-09 17:05:39 +01002202 /* limit compression rate */
2203 if (global.comp_rate_lim > 0)
2204 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2205 goto fail;
2206
William Lallemand072a2bf2012-11-20 17:01:01 +01002207 /* limit cpu usage */
2208 if (idle_pct < compress_min_idle)
2209 goto fail;
2210
William Lallemand4c49fae2012-11-07 15:00:23 +01002211 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002212 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002213 goto fail;
2214
William Lallemandec3e3892012-11-12 17:02:18 +01002215 s->flags |= SN_COMP_READY;
2216
William Lallemand82fe75c2012-10-23 10:25:10 +02002217 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002218 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002219 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2220 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2221
2222 /* add Transfer-Encoding header */
2223 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2224 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2225
2226 /*
2227 * Add Content-Encoding header when it's not identity encoding.
2228 * RFC 2616 : Identity encoding: This content-coding is used only in the
2229 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2230 * header.
2231 */
2232 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002233 trash.len = 18;
2234 memcpy(trash.str, "Content-Encoding: ", trash.len);
2235 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2236 trash.len += s->comp_algo->name_len;
2237 trash.str[trash.len] = '\0';
2238 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002239 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002240 return 1;
2241
2242fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002243 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002244 return 0;
2245}
2246
2247
Willy Tarreaud787e662009-07-07 10:14:51 +02002248/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2249 * processing can continue on next analysers, or zero if it either needs more
2250 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2251 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2252 * when it has nothing left to do, and may remove any analyser when it wants to
2253 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002254 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002255int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002256{
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 /*
2258 * We will parse the partial (or complete) lines.
2259 * We will check the request syntax, and also join multi-line
2260 * headers. An index of all the lines will be elaborated while
2261 * parsing.
2262 *
2263 * For the parsing, we use a 28 states FSM.
2264 *
2265 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002266 * req->buf->p = beginning of request
2267 * req->buf->p + msg->eoh = end of processed headers / start of current one
2268 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002269 * msg->eol = end of current header or line (LF or CRLF)
2270 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002271 *
2272 * At end of parsing, we may perform a capture of the error (if any), and
2273 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2274 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2275 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002279 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002280 struct http_txn *txn = &s->txn;
2281 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002282 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002283
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002284 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 +01002285 now_ms, __FUNCTION__,
2286 s,
2287 req,
2288 req->rex, req->wex,
2289 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002290 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002291 req->analysers);
2292
Willy Tarreau52a0c602009-08-16 22:45:38 +02002293 /* we're speaking HTTP here, so let's speak HTTP to the client */
2294 s->srv_error = http_return_srv_error;
2295
Willy Tarreau83e3af02009-12-28 17:39:57 +01002296 /* There's a protected area at the end of the buffer for rewriting
2297 * purposes. We don't want to start to parse the request if the
2298 * protected area is affected, because we may have to move processed
2299 * data later, which is much more complicated.
2300 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002301 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002302 if (txn->flags & TX_NOT_FIRST) {
2303 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002304 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002305 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002306 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002307 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002308 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002309 req->flags |= CF_WAKE_WRITE;
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 Tarreaud7ad9f52013-12-31 17:26:25 +01002334 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002335 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002336 return 0;
2337 }
2338 }
2339
Willy Tarreau9b28e032012-10-12 23:49:43 +02002340 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002341 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002342 }
2343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* 1: we might have to print this header in debug mode */
2345 if (unlikely((global.mode & MODE_DEBUG) &&
2346 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002347 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002349
Willy Tarreau9b28e032012-10-12 23:49:43 +02002350 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002351 /* this is a bit complex : in case of error on the request line,
2352 * we know that rq.l is still zero, so we display only the part
2353 * up to the end of the line (truncated by debug_hdr).
2354 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002355 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 sol += hdr_idx_first_pos(&txn->hdr_idx);
2359 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 while (cur_idx) {
2362 eol = sol + txn->hdr_idx.v[cur_idx].len;
2363 debug_hdr("clihdr", s, sol, eol);
2364 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2365 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002366 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 }
2368
Willy Tarreau58f10d72006-12-04 02:26:12 +01002369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 /*
2371 * Now we quickly check if we have found a full valid request.
2372 * If not so, we check the FD and buffer states before leaving.
2373 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002374 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002375 * requests are checked first. When waiting for a second request
2376 * on a keep-alive session, if we encounter and error, close, t/o,
2377 * we note the error in the session flags but don't set any state.
2378 * Since the error will be noted there, it will not be counted by
2379 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002380 * Last, we may increase some tracked counters' http request errors on
2381 * the cases that are deliberately the client's fault. For instance,
2382 * a timeout or connection reset is not counted as an error. However
2383 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002385
Willy Tarreau655dce92009-11-08 13:10:58 +01002386 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002387 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002390 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002391 session_inc_http_req_ctr(s);
2392 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002393 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 /* 1: Since we are in header mode, if there's no space
2398 * left for headers, we won't be able to free more
2399 * later, so the session will never terminate. We
2400 * must terminate it now.
2401 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002402 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002403 /* FIXME: check if URI is set and return Status
2404 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002405 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002406 session_inc_http_req_ctr(s);
2407 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002408 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002409 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002410 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 goto return_bad_req;
2412 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002415 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002416 if (!(s->flags & SN_ERR_MASK))
2417 s->flags |= SN_ERR_CLICL;
2418
Willy Tarreaufcffa692010-01-10 14:21:19 +01002419 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002420 goto failed_keep_alive;
2421
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002423 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002424 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002425 session_inc_http_err_ctr(s);
2426 }
2427
Willy Tarreaudc979f22012-12-04 10:39:01 +01002428 txn->status = 400;
2429 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 msg->msg_state = HTTP_MSG_ERROR;
2431 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002432
Willy Tarreauda7ff642010-06-23 11:44:09 +02002433 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002434 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002435 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002436 if (s->listener->counters)
2437 s->listener->counters->failed_req++;
2438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 if (!(s->flags & SN_FINST_MASK))
2440 s->flags |= SN_FINST_R;
2441 return 0;
2442 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002443
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002445 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002446 if (!(s->flags & SN_ERR_MASK))
2447 s->flags |= SN_ERR_CLITO;
2448
Willy Tarreaufcffa692010-01-10 14:21:19 +01002449 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002450 goto failed_keep_alive;
2451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002453 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002454 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_err_ctr(s);
2456 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002457 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002458 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002459 msg->msg_state = HTTP_MSG_ERROR;
2460 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002461
Willy Tarreauda7ff642010-06-23 11:44:09 +02002462 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002463 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002464 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002465 if (s->listener->counters)
2466 s->listener->counters->failed_req++;
2467
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 if (!(s->flags & SN_FINST_MASK))
2469 s->flags |= SN_FINST_R;
2470 return 0;
2471 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002474 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002475 if (!(s->flags & SN_ERR_MASK))
2476 s->flags |= SN_ERR_CLICL;
2477
Willy Tarreaufcffa692010-01-10 14:21:19 +01002478 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002479 goto failed_keep_alive;
2480
Willy Tarreau4076a152009-04-02 15:18:36 +02002481 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002482 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002484 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 msg->msg_state = HTTP_MSG_ERROR;
2486 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002487
Willy Tarreauda7ff642010-06-23 11:44:09 +02002488 session_inc_http_err_ctr(s);
2489 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002490 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002491 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002492 if (s->listener->counters)
2493 s->listener->counters->failed_req++;
2494
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 if (!(s->flags & SN_FINST_MASK))
2496 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002497 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498 }
2499
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002500 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002501 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2502 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002503#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002504 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002505 /* We need more data, we have to re-enable quick-ack in case we
2506 * previously disabled it, otherwise we might cause the client
2507 * to delay next data.
2508 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002509 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002510 }
2511#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002512
Willy Tarreaufcffa692010-01-10 14:21:19 +01002513 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2514 /* If the client starts to talk, let's fall back to
2515 * request timeout processing.
2516 */
2517 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002518 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002519 }
2520
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002522 if (!tick_isset(req->analyse_exp)) {
2523 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2524 (txn->flags & TX_WAIT_NEXT_RQ) &&
2525 tick_isset(s->be->timeout.httpka))
2526 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2527 else
2528 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2529 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002530
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 /* we're not ready yet */
2532 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002533
2534 failed_keep_alive:
2535 /* Here we process low-level errors for keep-alive requests. In
2536 * short, if the request is not the first one and it experiences
2537 * a timeout, read error or shutdown, we just silently close so
2538 * that the client can try again.
2539 */
2540 txn->status = 0;
2541 msg->msg_state = HTTP_MSG_RQBEFORE;
2542 req->analysers = 0;
2543 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002544 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002545 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002546 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002547 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002549
Willy Tarreaud787e662009-07-07 10:14:51 +02002550 /* OK now we have a complete HTTP request with indexed headers. Let's
2551 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002553 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002554 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002555 * byte after the last LF. msg->sov points to the first byte of data.
2556 * msg->eol cannot be trusted because it may have been left uninitialized
2557 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002559
Willy Tarreauda7ff642010-06-23 11:44:09 +02002560 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002561 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2562
Willy Tarreaub16a5742010-01-10 14:46:16 +01002563 if (txn->flags & TX_WAIT_NEXT_RQ) {
2564 /* kill the pending keep-alive timeout */
2565 txn->flags &= ~TX_WAIT_NEXT_RQ;
2566 req->analyse_exp = TICK_ETERNITY;
2567 }
2568
2569
Willy Tarreaud787e662009-07-07 10:14:51 +02002570 /* Maybe we found in invalid header name while we were configured not
2571 * to block on that, so we have to capture it now.
2572 */
2573 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002574 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002575
Willy Tarreau59234e92008-11-30 23:51:27 +01002576 /*
2577 * 1: identify the method
2578 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002579 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002580
2581 /* we can make use of server redirect on GET and HEAD */
2582 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2583 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002584
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 /*
2586 * 2: check if the URI matches the monitor_uri.
2587 * We have to do this for every request which gets in, because
2588 * the monitor-uri is defined by the frontend.
2589 */
2590 if (unlikely((s->fe->monitor_uri_len != 0) &&
2591 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 s->fe->monitor_uri,
2594 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002595 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002597 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002601 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002602
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002604 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002605 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002606
Willy Tarreau59234e92008-11-30 23:51:27 +01002607 ret = acl_pass(ret);
2608 if (cond->pol == ACL_COND_UNLESS)
2609 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 if (ret) {
2612 /* we fail this request, let's return 503 service unavail */
2613 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002614 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002615 if (!(s->flags & SN_ERR_MASK))
2616 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002618 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002619 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002620
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 /* nothing to fail, let's reply normaly */
2622 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002623 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002624 if (!(s->flags & SN_ERR_MASK))
2625 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 goto return_prx_cond;
2627 }
2628
2629 /*
2630 * 3: Maybe we have to copy the original REQURI for the logs ?
2631 * Note: we cannot log anymore if the request has been
2632 * classified as invalid.
2633 */
2634 if (unlikely(s->logs.logwait & LW_REQ)) {
2635 /* we have a complete HTTP request that we must log */
2636 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2637 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 if (urilen >= REQURI_LEN)
2640 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002643
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002644 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 s->do_log(s);
2646 } else {
2647 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002648 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 }
Willy Tarreau06619262006-12-17 08:37:22 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002652 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002653 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002654
Willy Tarreau5b154472009-12-21 20:11:07 +01002655 /* ... and check if the request is HTTP/1.1 or above */
2656 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002657 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2658 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2659 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002660 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002661
2662 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002663 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 +01002664
Willy Tarreau88d349d2010-01-25 12:15:43 +01002665 /* if the frontend has "option http-use-proxy-header", we'll check if
2666 * we have what looks like a proxied connection instead of a connection,
2667 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2668 * Note that this is *not* RFC-compliant, however browsers and proxies
2669 * happen to do that despite being non-standard :-(
2670 * We consider that a request not beginning with either '/' or '*' is
2671 * a proxied connection, which covers both "scheme://location" and
2672 * CONNECT ip:port.
2673 */
2674 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002675 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002676 txn->flags |= TX_USE_PX_CONN;
2677
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002678 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002679 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002682 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002683 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002685
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002686 /* 6: determine the transfer-length.
2687 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2688 * the presence of a message-body in a REQUEST and its transfer length
2689 * must be determined that way (in order of precedence) :
2690 * 1. The presence of a message-body in a request is signaled by the
2691 * inclusion of a Content-Length or Transfer-Encoding header field
2692 * in the request's header fields. When a request message contains
2693 * both a message-body of non-zero length and a method that does
2694 * not define any semantics for that request message-body, then an
2695 * origin server SHOULD either ignore the message-body or respond
2696 * with an appropriate error message (e.g., 413). A proxy or
2697 * gateway, when presented the same request, SHOULD either forward
2698 * the request inbound with the message- body or ignore the
2699 * message-body when determining a response.
2700 *
2701 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2702 * and the "chunked" transfer-coding (Section 6.2) is used, the
2703 * transfer-length is defined by the use of this transfer-coding.
2704 * If a Transfer-Encoding header field is present and the "chunked"
2705 * transfer-coding is not present, the transfer-length is defined
2706 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002707 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002708 * 3. If a Content-Length header field is present, its decimal value in
2709 * OCTETs represents both the entity-length and the transfer-length.
2710 * If a message is received with both a Transfer-Encoding header
2711 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002712 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002713 * 4. By the server closing the connection. (Closing the connection
2714 * cannot be used to indicate the end of a request body, since that
2715 * would leave no possibility for the server to send back a response.)
2716 *
2717 * Whenever a transfer-coding is applied to a message-body, the set of
2718 * transfer-codings MUST include "chunked", unless the message indicates
2719 * it is terminated by closing the connection. When the "chunked"
2720 * transfer-coding is used, it MUST be the last transfer-coding applied
2721 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002722 */
2723
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002724 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002725 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002726 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002729 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002730 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2731 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002732 /* bad transfer-encoding (chunked followed by something else) */
2733 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002734 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002735 break;
2736 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002737 }
2738
Willy Tarreau32b47f42009-10-18 20:55:02 +02002739 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002740 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002742 signed long long cl;
2743
Willy Tarreauad14f752011-09-02 20:33:27 +02002744 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002745 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002747 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748
Willy Tarreauad14f752011-09-02 20:33:27 +02002749 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002750 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002752 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002753
Willy Tarreauad14f752011-09-02 20:33:27 +02002754 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002755 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002757 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002758
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002759 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002760 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002761 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002762 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002763
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002764 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002765 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002766 }
2767
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002768 /* bodyless requests have a known length */
2769 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002770 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002771
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002773 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002774 req->analyse_exp = TICK_ETERNITY;
2775 return 1;
2776
2777 return_bad_req:
2778 /* We centralize bad requests processing here */
2779 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2780 /* we detected a parsing error. We want to archive this request
2781 * in the dedicated proxy area for later troubleshooting.
2782 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002783 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002784 }
2785
2786 txn->req.msg_state = HTTP_MSG_ERROR;
2787 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002788 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002789
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002790 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002791 if (s->listener->counters)
2792 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002793
2794 return_prx_cond:
2795 if (!(s->flags & SN_ERR_MASK))
2796 s->flags |= SN_ERR_PRXCOND;
2797 if (!(s->flags & SN_FINST_MASK))
2798 s->flags |= SN_FINST_R;
2799
2800 req->analysers = 0;
2801 req->analyse_exp = TICK_ETERNITY;
2802 return 0;
2803}
2804
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002805
Willy Tarreau347a35d2013-11-22 17:51:09 +01002806/* This function prepares an applet to handle the stats. It can deal with the
2807 * "100-continue" expectation, check that admin rules are met for POST requests,
2808 * and program a response message if something was unexpected. It cannot fail
2809 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002810 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002811 * s->target which is supposed to already point to the stats applet. The caller
2812 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002813 */
2814int http_handle_stats(struct session *s, struct channel *req)
2815{
2816 struct stats_admin_rule *stats_admin_rule;
2817 struct stream_interface *si = s->rep->prod;
2818 struct http_txn *txn = &s->txn;
2819 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002820 struct uri_auth *uri_auth = s->be->uri_auth;
2821 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002822 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002824 appctx = si_appctx(si);
2825 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2826 appctx->st1 = appctx->st2 = 0;
2827 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2828 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002829
2830 uri = msg->chn->buf->p + msg->sl.rq.u;
2831 lookup = uri + uri_auth->uri_len;
2832
2833 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2834 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002835 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002836 break;
2837 }
2838 }
2839
2840 if (uri_auth->refresh) {
2841 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2842 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002843 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002844 break;
2845 }
2846 }
2847 }
2848
2849 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2850 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002851 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002852 break;
2853 }
2854 }
2855
2856 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2857 if (memcmp(h, ";st=", 4) == 0) {
2858 int i;
2859 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002860 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002861 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2862 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002863 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002864 break;
2865 }
2866 }
2867 break;
2868 }
2869 }
2870
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002871 appctx->ctx.stats.scope_str = 0;
2872 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002873 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2874 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2875 int itx = 0;
2876 const char *h2;
2877 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2878 const char *err;
2879
2880 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2881 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002882 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002883 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2884 itx++;
2885 h++;
2886 }
2887
2888 if (itx > STAT_SCOPE_TXT_MAXLEN)
2889 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002890 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002892 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002893 memcpy(scope_txt, h2, itx);
2894 scope_txt[itx] = '\0';
2895 err = invalid_char(scope_txt);
2896 if (err) {
2897 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002898 appctx->ctx.stats.scope_str = 0;
2899 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002900 }
2901 break;
2902 }
2903 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002904
2905 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002906 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002907 int ret = 1;
2908
2909 if (stats_admin_rule->cond) {
2910 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2911 ret = acl_pass(ret);
2912 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2913 ret = !ret;
2914 }
2915
2916 if (ret) {
2917 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002918 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002919 break;
2920 }
2921 }
2922
2923 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002924 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002925 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002926 if (msg->msg_state < HTTP_MSG_100_SENT) {
2927 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2928 * send an HTTP/1.1 100 Continue intermediate response.
2929 */
2930 if (msg->flags & HTTP_MSGF_VER_11) {
2931 struct hdr_ctx ctx;
2932 ctx.idx = 0;
2933 /* Expect is allowed in 1.1, look for it */
2934 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2935 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2936 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2937 }
2938 }
2939 msg->msg_state = HTTP_MSG_100_SENT;
2940 s->logs.tv_request = now; /* update the request timer to reflect full request */
2941 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002942 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002943 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002944 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002945 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2946 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002947 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002948 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002949 else {
2950 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002951 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002952 }
2953
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002954 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002955 return 1;
2956}
2957
Lukas Tribus67db8df2013-06-23 17:37:13 +02002958/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2959 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2960 */
2961static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2962{
2963#ifdef IP_TOS
2964 if (from.ss_family == AF_INET)
2965 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2966#endif
2967#ifdef IPV6_TCLASS
2968 if (from.ss_family == AF_INET6) {
2969 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2970 /* v4-mapped addresses need IP_TOS */
2971 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2972 else
2973 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2974 }
2975#endif
2976}
2977
Willy Tarreau20b0de52012-12-24 15:45:22 +01002978/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002979 * transaction <txn>. Returns the first rule that prevents further processing
2980 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2981 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2982 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002984static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002985http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002987 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002988 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002989 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990
Willy Tarreauff011f22011-01-06 17:51:27 +01002991 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002992 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 continue;
2994
Willy Tarreau96257ec2012-12-27 10:46:37 +01002995 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002996 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002997 int ret;
2998
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002999 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 ret = acl_pass(ret);
3001
Willy Tarreauff011f22011-01-06 17:51:27 +01003002 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003003 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003004
3005 if (!ret) /* condition not matched */
3006 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003007 }
3008
Willy Tarreau20b0de52012-12-24 15:45:22 +01003009
Willy Tarreau96257ec2012-12-27 10:46:37 +01003010 switch (rule->action) {
3011 case HTTP_REQ_ACT_ALLOW:
3012 return NULL; /* "allow" rules are OK */
3013
3014 case HTTP_REQ_ACT_DENY:
3015 txn->flags |= TX_CLDENY;
3016 return rule;
3017
Willy Tarreauccbcc372012-12-27 12:37:57 +01003018 case HTTP_REQ_ACT_TARPIT:
3019 txn->flags |= TX_CLTARPIT;
3020 return rule;
3021
Willy Tarreau96257ec2012-12-27 10:46:37 +01003022 case HTTP_REQ_ACT_AUTH:
3023 return rule;
3024
Willy Tarreau81499eb2012-12-27 12:19:02 +01003025 case HTTP_REQ_ACT_REDIR:
3026 return rule;
3027
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003028 case HTTP_REQ_ACT_SET_NICE:
3029 s->task->nice = rule->arg.nice;
3030 break;
3031
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003032 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003033 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003034 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003035 break;
3036
Willy Tarreau51347ed2013-06-11 19:34:13 +02003037 case HTTP_REQ_ACT_SET_MARK:
3038#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003039 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003040 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003041#endif
3042 break;
3043
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003044 case HTTP_REQ_ACT_SET_LOGL:
3045 s->logs.level = rule->arg.loglevel;
3046 break;
3047
Willy Tarreau96257ec2012-12-27 10:46:37 +01003048 case HTTP_REQ_ACT_SET_HDR:
3049 ctx.idx = 0;
3050 /* remove all occurrences of the header */
3051 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3052 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3053 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003054 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003055 /* now fall through to header addition */
3056
3057 case HTTP_REQ_ACT_ADD_HDR:
3058 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3059 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3060 trash.len = rule->arg.hdr_add.name_len;
3061 trash.str[trash.len++] = ':';
3062 trash.str[trash.len++] = ' ';
3063 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3064 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3065 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003066 }
3067 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003068
3069 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003070 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003071}
3072
Willy Tarreau71241ab2012-12-27 11:30:54 +01003073
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003074/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3075 * transaction <txn>. Returns the first rule that prevents further processing
3076 * of the response (deny, ...) or NULL if it executed all rules or stopped
3077 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3078 * rule.
3079 */
3080static struct http_res_rule *
3081http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3082{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003083 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003084 struct http_res_rule *rule;
3085 struct hdr_ctx ctx;
3086
3087 list_for_each_entry(rule, rules, list) {
3088 if (rule->action >= HTTP_RES_ACT_MAX)
3089 continue;
3090
3091 /* check optional condition */
3092 if (rule->cond) {
3093 int ret;
3094
3095 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3096 ret = acl_pass(ret);
3097
3098 if (rule->cond->pol == ACL_COND_UNLESS)
3099 ret = !ret;
3100
3101 if (!ret) /* condition not matched */
3102 continue;
3103 }
3104
3105
3106 switch (rule->action) {
3107 case HTTP_RES_ACT_ALLOW:
3108 return NULL; /* "allow" rules are OK */
3109
3110 case HTTP_RES_ACT_DENY:
3111 txn->flags |= TX_SVDENY;
3112 return rule;
3113
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003114 case HTTP_RES_ACT_SET_NICE:
3115 s->task->nice = rule->arg.nice;
3116 break;
3117
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003118 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003119 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003120 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003121 break;
3122
Willy Tarreau51347ed2013-06-11 19:34:13 +02003123 case HTTP_RES_ACT_SET_MARK:
3124#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003125 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003126 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003127#endif
3128 break;
3129
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003130 case HTTP_RES_ACT_SET_LOGL:
3131 s->logs.level = rule->arg.loglevel;
3132 break;
3133
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003134 case HTTP_RES_ACT_SET_HDR:
3135 ctx.idx = 0;
3136 /* remove all occurrences of the header */
3137 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3138 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3139 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3140 }
3141 /* now fall through to header addition */
3142
3143 case HTTP_RES_ACT_ADD_HDR:
3144 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3145 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3146 trash.len = rule->arg.hdr_add.name_len;
3147 trash.str[trash.len++] = ':';
3148 trash.str[trash.len++] = ' ';
3149 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3150 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3151 break;
3152 }
3153 }
3154
3155 /* we reached the end of the rules, nothing to report */
3156 return NULL;
3157}
3158
3159
Willy Tarreau71241ab2012-12-27 11:30:54 +01003160/* Perform an HTTP redirect based on the information in <rule>. The function
3161 * returns non-zero on success, or zero in case of a, irrecoverable error such
3162 * as too large a request to build a valid response.
3163 */
3164static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3165{
3166 struct http_msg *msg = &txn->req;
3167 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003168 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003169
3170 /* build redirect message */
3171 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003172 case 308:
3173 msg_fmt = HTTP_308;
3174 break;
3175 case 307:
3176 msg_fmt = HTTP_307;
3177 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003178 case 303:
3179 msg_fmt = HTTP_303;
3180 break;
3181 case 301:
3182 msg_fmt = HTTP_301;
3183 break;
3184 case 302:
3185 default:
3186 msg_fmt = HTTP_302;
3187 break;
3188 }
3189
3190 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3191 return 0;
3192
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003193 location = trash.str + trash.len;
3194
Willy Tarreau71241ab2012-12-27 11:30:54 +01003195 switch(rule->type) {
3196 case REDIRECT_TYPE_SCHEME: {
3197 const char *path;
3198 const char *host;
3199 struct hdr_ctx ctx;
3200 int pathlen;
3201 int hostlen;
3202
3203 host = "";
3204 hostlen = 0;
3205 ctx.idx = 0;
3206 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3207 host = ctx.line + ctx.val;
3208 hostlen = ctx.vlen;
3209 }
3210
3211 path = http_get_path(txn);
3212 /* build message using path */
3213 if (path) {
3214 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3215 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3216 int qs = 0;
3217 while (qs < pathlen) {
3218 if (path[qs] == '?') {
3219 pathlen = qs;
3220 break;
3221 }
3222 qs++;
3223 }
3224 }
3225 } else {
3226 path = "/";
3227 pathlen = 1;
3228 }
3229
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003230 if (rule->rdr_str) { /* this is an old "redirect" rule */
3231 /* check if we can add scheme + "://" + host + path */
3232 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3233 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003234
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003235 /* add scheme */
3236 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3237 trash.len += rule->rdr_len;
3238 }
3239 else {
3240 /* add scheme with executing log format */
3241 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003242
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003243 /* check if we can add scheme + "://" + host + path */
3244 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3245 return 0;
3246 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003247 /* add "://" */
3248 memcpy(trash.str + trash.len, "://", 3);
3249 trash.len += 3;
3250
3251 /* add host */
3252 memcpy(trash.str + trash.len, host, hostlen);
3253 trash.len += hostlen;
3254
3255 /* add path */
3256 memcpy(trash.str + trash.len, path, pathlen);
3257 trash.len += pathlen;
3258
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003259 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003260 if (trash.len && trash.str[trash.len - 1] != '/' &&
3261 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3262 if (trash.len > trash.size - 5)
3263 return 0;
3264 trash.str[trash.len] = '/';
3265 trash.len++;
3266 }
3267
3268 break;
3269 }
3270 case REDIRECT_TYPE_PREFIX: {
3271 const char *path;
3272 int pathlen;
3273
3274 path = http_get_path(txn);
3275 /* build message using path */
3276 if (path) {
3277 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3278 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3279 int qs = 0;
3280 while (qs < pathlen) {
3281 if (path[qs] == '?') {
3282 pathlen = qs;
3283 break;
3284 }
3285 qs++;
3286 }
3287 }
3288 } else {
3289 path = "/";
3290 pathlen = 1;
3291 }
3292
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003293 if (rule->rdr_str) { /* this is an old "redirect" rule */
3294 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3295 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003296
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003297 /* add prefix. Note that if prefix == "/", we don't want to
3298 * add anything, otherwise it makes it hard for the user to
3299 * configure a self-redirection.
3300 */
3301 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3302 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3303 trash.len += rule->rdr_len;
3304 }
3305 }
3306 else {
3307 /* add prefix with executing log format */
3308 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3309
3310 /* Check length */
3311 if (trash.len + pathlen > trash.size - 4)
3312 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313 }
3314
3315 /* add path */
3316 memcpy(trash.str + trash.len, path, pathlen);
3317 trash.len += pathlen;
3318
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003319 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 if (trash.len && trash.str[trash.len - 1] != '/' &&
3321 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3322 if (trash.len > trash.size - 5)
3323 return 0;
3324 trash.str[trash.len] = '/';
3325 trash.len++;
3326 }
3327
3328 break;
3329 }
3330 case REDIRECT_TYPE_LOCATION:
3331 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003332 if (rule->rdr_str) { /* this is an old "redirect" rule */
3333 if (trash.len + rule->rdr_len > trash.size - 4)
3334 return 0;
3335
3336 /* add location */
3337 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3338 trash.len += rule->rdr_len;
3339 }
3340 else {
3341 /* add location with executing log format */
3342 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003344 /* Check left length */
3345 if (trash.len > trash.size - 4)
3346 return 0;
3347 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003348 break;
3349 }
3350
3351 if (rule->cookie_len) {
3352 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3353 trash.len += 14;
3354 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3355 trash.len += rule->cookie_len;
3356 memcpy(trash.str + trash.len, "\r\n", 2);
3357 trash.len += 2;
3358 }
3359
3360 /* add end of headers and the keep-alive/close status.
3361 * We may choose to set keep-alive if the Location begins
3362 * with a slash, because the client will come back to the
3363 * same server.
3364 */
3365 txn->status = rule->code;
3366 /* let's log the request time */
3367 s->logs.tv_request = now;
3368
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003369 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3371 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3372 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3373 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3374 /* keep-alive possible */
3375 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3376 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3377 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3378 trash.len += 30;
3379 } else {
3380 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3381 trash.len += 24;
3382 }
3383 }
3384 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3385 trash.len += 4;
3386 bo_inject(txn->rsp.chn, trash.str, trash.len);
3387 /* "eat" the request */
3388 bi_fast_delete(txn->req.chn->buf, msg->sov);
3389 msg->sov = 0;
3390 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3391 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3392 txn->req.msg_state = HTTP_MSG_CLOSED;
3393 txn->rsp.msg_state = HTTP_MSG_DONE;
3394 } else {
3395 /* keep-alive not possible */
3396 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3397 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3398 trash.len += 29;
3399 } else {
3400 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3401 trash.len += 23;
3402 }
3403 stream_int_retnclose(txn->req.chn->prod, &trash);
3404 txn->req.chn->analysers = 0;
3405 }
3406
3407 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003408 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003409 if (!(s->flags & SN_FINST_MASK))
3410 s->flags |= SN_FINST_R;
3411
3412 return 1;
3413}
3414
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003415/* This stream analyser runs all HTTP request processing which is common to
3416 * frontends and backends, which means blocking ACLs, filters, connection-close,
3417 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003418 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 * either needs more data or wants to immediately abort the request (eg: deny,
3420 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003421 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003422int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003423{
Willy Tarreaud787e662009-07-07 10:14:51 +02003424 struct http_txn *txn = &s->txn;
3425 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003426 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003427 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003428 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003429 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003430
Willy Tarreau655dce92009-11-08 13:10:58 +01003431 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003432 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003433 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003434 return 0;
3435 }
3436
Willy Tarreau3a816292009-07-07 10:55:49 +02003437 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003438 req->analyse_exp = TICK_ETERNITY;
3439
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003440 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 +02003441 now_ms, __FUNCTION__,
3442 s,
3443 req,
3444 req->rex, req->wex,
3445 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003446 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003447 req->analysers);
3448
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003449 /* first check whether we have some ACLs set to block this request */
3450 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003451 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003452
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003453 ret = acl_pass(ret);
3454 if (cond->pol == ACL_COND_UNLESS)
3455 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003456
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457 if (ret) {
3458 txn->status = 403;
3459 /* let's log the request time */
3460 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003461 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003462 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003464 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003465 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003466
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003467 /* just in case we have some per-backend tracking */
3468 session_inc_be_http_req_ctr(s);
3469
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003470 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003471 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003472
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003473 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003474 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003475 if (stats_check_uri(s->rep->prod, txn, px)) {
3476 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003477 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3478 txn->status = 500;
3479 s->logs.tv_request = now;
3480 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003481
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003482 if (!(s->flags & SN_ERR_MASK))
3483 s->flags |= SN_ERR_RESOURCE;
3484 goto return_prx_cond;
3485 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003486 /* parse the whole stats request and extract the relevant information */
3487 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003488 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 +01003489 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003490 }
3491
Willy Tarreau3b44e722013-11-16 10:28:23 +01003492 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3493 * blocked by an http-request rule.
3494 */
3495 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003496 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003498 }
Willy Tarreau06619262006-12-17 08:37:22 +01003499
Willy Tarreau3b44e722013-11-16 10:28:23 +01003500 /* return a 403 if either rule has blocked */
3501 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003502 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003503 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003504 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003505 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003506 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003507 s->fe->fe_counters.denied_req++;
3508 if (s->fe != s->be)
3509 s->be->be_counters.denied_req++;
3510 if (s->listener->counters)
3511 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003512 goto return_prx_cond;
3513 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003514
3515 /* When a connection is tarpitted, we use the tarpit timeout,
3516 * which may be the same as the connect timeout if unspecified.
3517 * If unset, then set it to zero because we really want it to
3518 * eventually expire. We build the tarpit as an analyser.
3519 */
3520 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003521 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003522 /* wipe the request out so that we can drop the connection early
3523 * if the client closes first.
3524 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003525 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003526 req->analysers = 0; /* remove switching rules etc... */
3527 req->analysers |= AN_REQ_HTTP_TARPIT;
3528 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3529 if (!req->analyse_exp)
3530 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003531 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003532 s->fe->fe_counters.denied_req++;
3533 if (s->fe != s->be)
3534 s->be->be_counters.denied_req++;
3535 if (s->listener->counters)
3536 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003537 return 1;
3538 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003539 }
Willy Tarreau06619262006-12-17 08:37:22 +01003540
Willy Tarreau70dffda2014-01-30 03:07:23 +01003541 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003542 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003543 * Option httpclose by itself sets tunnel mode where headers are mangled.
3544 * However, if another mode is set, it will affect it (eg: server-close/
3545 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3546 * if FE and BE have the same settings (common). The method consists in
3547 * checking if options changed between the two calls (implying that either
3548 * one is non-null, or one of them is non-null and we are there for the first
3549 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003550 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003551
Willy Tarreau416ce612014-01-31 15:45:34 +01003552 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003553 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003554 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003555
Willy Tarreau70dffda2014-01-30 03:07:23 +01003556 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3557 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3558 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3559 tmp = TX_CON_WANT_TUN;
3560
3561 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3562 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3563 tmp = TX_CON_WANT_TUN;
3564 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003565
3566 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003567 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3568 /* option httpclose + server_close => forceclose */
3569 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3570 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3571 tmp = TX_CON_WANT_CLO;
3572 else
3573 tmp = TX_CON_WANT_SCL;
3574 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003575
3576 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3577 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003578 tmp = TX_CON_WANT_CLO;
3579
Willy Tarreau5b154472009-12-21 20:11:07 +01003580 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3581 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003582
Willy Tarreau416ce612014-01-31 15:45:34 +01003583 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3584 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003585 /* parse the Connection header and possibly clean it */
3586 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003587 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003588 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3589 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003590 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003591 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003592 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003593 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003594 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003595
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003596 /* check if client or config asks for explicit close in KAL/SCL */
3597 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3598 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3599 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003600 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003601 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003602 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003603 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3604 }
Willy Tarreau78599912009-10-17 20:12:21 +02003605
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003606 /* we can be blocked here because the request needs to be authenticated,
3607 * either to pass or to access stats.
3608 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003609 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003610 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003611
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003612 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003613 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003616 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003617 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003618 /* on 401 we still count one error, because normal browsing
3619 * won't significantly increase the counter but brute force
3620 * attempts will.
3621 */
3622 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003623 goto return_prx_cond;
3624 }
3625
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003626 /* add request headers from the rule sets in the same order */
3627 list_for_each_entry(wl, &px->req_add, list) {
3628 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003629 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003630 ret = acl_pass(ret);
3631 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3632 ret = !ret;
3633 if (!ret)
3634 continue;
3635 }
3636
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003637 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003638 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003639 }
3640
3641 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3642 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3643 goto return_bad_req;
3644 req->analyse_exp = TICK_ETERNITY;
3645 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646 }
3647
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003648 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003649 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003650 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3651 s->fe->fe_counters.intercepted_req++;
3652
3653 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3654 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3655 if (!(s->flags & SN_FINST_MASK))
3656 s->flags |= SN_FINST_R;
3657
3658 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003659 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003660 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003661 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003662
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003663 /* check whether we have some ACLs set to redirect this request */
3664 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003665 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003666 int ret;
3667
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003668 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003669 ret = acl_pass(ret);
3670 if (rule->cond->pol == ACL_COND_UNLESS)
3671 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003672 if (!ret)
3673 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003674 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003675 if (!http_apply_redirect_rule(rule, s, txn))
3676 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003677
Willy Tarreau71241ab2012-12-27 11:30:54 +01003678 req->analyse_exp = TICK_ETERNITY;
3679 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003681
Willy Tarreau2be39392010-01-03 17:24:51 +01003682 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3683 * If this happens, then the data will not come immediately, so we must
3684 * send all what we have without waiting. Note that due to the small gain
3685 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003686 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003687 * itself once used.
3688 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003689 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003690
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003691 /* that's OK for us now, let's move on to next analysers */
3692 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003693
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003694 return_bad_req:
3695 /* We centralize bad requests processing here */
3696 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3697 /* we detected a parsing error. We want to archive this request
3698 * in the dedicated proxy area for later troubleshooting.
3699 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003700 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003703 txn->req.msg_state = HTTP_MSG_ERROR;
3704 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003705 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003706
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003707 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003708 if (s->listener->counters)
3709 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003710
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003711 return_prx_cond:
3712 if (!(s->flags & SN_ERR_MASK))
3713 s->flags |= SN_ERR_PRXCOND;
3714 if (!(s->flags & SN_FINST_MASK))
3715 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003716
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003717 req->analysers = 0;
3718 req->analyse_exp = TICK_ETERNITY;
3719 return 0;
3720}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003722/* This function performs all the processing enabled for the current request.
3723 * It returns 1 if the processing can continue on next analysers, or zero if it
3724 * needs more data, encounters an error, or wants to immediately abort the
3725 * request. It relies on buffers flags, and updates s->req->analysers.
3726 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003727int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003728{
3729 struct http_txn *txn = &s->txn;
3730 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003731 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003732
Willy Tarreau655dce92009-11-08 13:10:58 +01003733 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003734 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003735 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003736 return 0;
3737 }
3738
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003739 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 +02003740 now_ms, __FUNCTION__,
3741 s,
3742 req,
3743 req->rex, req->wex,
3744 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003745 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003746 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003747
William Lallemand82fe75c2012-10-23 10:25:10 +02003748 if (s->fe->comp || s->be->comp)
3749 select_compression_request_header(s, req->buf);
3750
Willy Tarreau59234e92008-11-30 23:51:27 +01003751 /*
3752 * Right now, we know that we have processed the entire headers
3753 * and that unwanted requests have been filtered out. We can do
3754 * whatever we want with the remaining request. Also, now we
3755 * may have separate values for ->fe, ->be.
3756 */
Willy Tarreau06619262006-12-17 08:37:22 +01003757
Willy Tarreau59234e92008-11-30 23:51:27 +01003758 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003759 * If HTTP PROXY is set we simply get remote server address parsing
3760 * incoming request. Note that this requires that a connection is
3761 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003762 */
3763 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003764 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003765 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003766
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003767 /* Note that for now we don't reuse existing proxy connections */
3768 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003769 txn->req.msg_state = HTTP_MSG_ERROR;
3770 txn->status = 500;
3771 req->analysers = 0;
3772 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3773
3774 if (!(s->flags & SN_ERR_MASK))
3775 s->flags |= SN_ERR_RESOURCE;
3776 if (!(s->flags & SN_FINST_MASK))
3777 s->flags |= SN_FINST_R;
3778
3779 return 0;
3780 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003781
3782 path = http_get_path(txn);
3783 url2sa(req->buf->p + msg->sl.rq.u,
3784 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3785 &conn->addr.to);
3786 /* if the path was found, we have to remove everything between
3787 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3788 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3789 * u_l characters by a single "/".
3790 */
3791 if (path) {
3792 char *cur_ptr = req->buf->p;
3793 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3794 int delta;
3795
3796 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3797 http_msg_move_end(&txn->req, delta);
3798 cur_end += delta;
3799 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3800 goto return_bad_req;
3801 }
3802 else {
3803 char *cur_ptr = req->buf->p;
3804 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3805 int delta;
3806
3807 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3808 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3809 http_msg_move_end(&txn->req, delta);
3810 cur_end += delta;
3811 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3812 goto return_bad_req;
3813 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003815
Willy Tarreau59234e92008-11-30 23:51:27 +01003816 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003817 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 * Note that doing so might move headers in the request, but
3819 * the fields will stay coherent and the URI will not move.
3820 * This should only be performed in the backend.
3821 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003822 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3824 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003825
Willy Tarreau59234e92008-11-30 23:51:27 +01003826 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003827 * 8: the appsession cookie was looked up very early in 1.2,
3828 * so let's do the same now.
3829 */
3830
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003831 /* It needs to look into the URI unless persistence must be ignored */
3832 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003833 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 +01003834 }
3835
William Lallemanda73203e2012-03-12 12:48:57 +01003836 /* add unique-id if "header-unique-id" is specified */
3837
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003838 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3839 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3840 goto return_bad_req;
3841 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003842 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003843 }
William Lallemanda73203e2012-03-12 12:48:57 +01003844
3845 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003846 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3847 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003848 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003849 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003850 goto return_bad_req;
3851 }
3852
Cyril Bontéb21570a2009-11-29 20:04:48 +01003853 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 * 9: add X-Forwarded-For if either the frontend or the backend
3855 * asks for it.
3856 */
3857 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003858 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003859 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003860 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3861 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003862 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003863 /* The header is set to be added only if none is present
3864 * and we found it, so don't do anything.
3865 */
3866 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003867 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003868 /* Add an X-Forwarded-For header unless the source IP is
3869 * in the 'except' network range.
3870 */
3871 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003872 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003873 != s->fe->except_net.s_addr) &&
3874 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003875 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003876 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003877 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003878 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003879 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003880
3881 /* Note: we rely on the backend to get the header name to be used for
3882 * x-forwarded-for, because the header is really meant for the backends.
3883 * However, if the backend did not specify any option, we have to rely
3884 * on the frontend's header name.
3885 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003886 if (s->be->fwdfor_hdr_len) {
3887 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003888 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003889 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003890 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003891 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003892 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003893 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003894
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003895 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003896 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003897 }
3898 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003899 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 /* FIXME: for the sake of completeness, we should also support
3901 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003902 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003903 int len;
3904 char pn[INET6_ADDRSTRLEN];
3905 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003906 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003907 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003908
Willy Tarreau59234e92008-11-30 23:51:27 +01003909 /* Note: we rely on the backend to get the header name to be used for
3910 * x-forwarded-for, because the header is really meant for the backends.
3911 * However, if the backend did not specify any option, we have to rely
3912 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003913 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003914 if (s->be->fwdfor_hdr_len) {
3915 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003916 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003917 } else {
3918 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003919 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003920 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003921 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003922
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003923 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 goto return_bad_req;
3925 }
3926 }
3927
3928 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003929 * 10: add X-Original-To if either the frontend or the backend
3930 * asks for it.
3931 */
3932 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3933
3934 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003935 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003936 /* Add an X-Original-To header unless the destination IP is
3937 * in the 'except' network range.
3938 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003939 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003940
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003941 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003942 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003943 (((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 +02003944 != s->fe->except_to.s_addr) &&
3945 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003946 (((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 +02003947 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003948 int len;
3949 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003950 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003951
3952 /* Note: we rely on the backend to get the header name to be used for
3953 * x-original-to, because the header is really meant for the backends.
3954 * However, if the backend did not specify any option, we have to rely
3955 * on the frontend's header name.
3956 */
3957 if (s->be->orgto_hdr_len) {
3958 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003959 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003960 } else {
3961 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003963 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003964 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003965
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003966 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003967 goto return_bad_req;
3968 }
3969 }
3970 }
3971
Willy Tarreau50fc7772012-11-11 22:19:57 +01003972 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3973 * If an "Upgrade" token is found, the header is left untouched in order not to have
3974 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3975 * "Upgrade" is present in the Connection header.
3976 */
3977 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3978 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003979 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3980 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003981 unsigned int want_flags = 0;
3982
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003983 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003984 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003985 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3986 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02003987 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003988 want_flags |= TX_CON_CLO_SET;
3989 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003990 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003991 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
3992 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02003993 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003994 want_flags |= TX_CON_KAL_SET;
3995 }
3996
3997 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003998 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003999 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004000
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004001
Willy Tarreau522d6c02009-12-06 18:49:18 +01004002 /* If we have no server assigned yet and we're balancing on url_param
4003 * with a POST request, we may be interested in checking the body for
4004 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004005 */
4006 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4007 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004008 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004009 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004010 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004011 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004012 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004013
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004014 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004015 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004016#ifdef TCP_QUICKACK
4017 /* We expect some data from the client. Unless we know for sure
4018 * we already have a full request, we have to re-enable quick-ack
4019 * in case we previously disabled it, otherwise we might cause
4020 * the client to delay further data.
4021 */
4022 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004023 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004024 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004025 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004026 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004027#endif
4028 }
Willy Tarreau03945942009-12-22 16:50:27 +01004029
Willy Tarreau59234e92008-11-30 23:51:27 +01004030 /*************************************************************
4031 * OK, that's finished for the headers. We have done what we *
4032 * could. Let's switch to the DATA state. *
4033 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004034 req->analyse_exp = TICK_ETERNITY;
4035 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004036
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004037 /* if the server closes the connection, we want to immediately react
4038 * and close the socket to save packets and syscalls.
4039 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004040 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4041 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004042
Willy Tarreau59234e92008-11-30 23:51:27 +01004043 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004044 /* OK let's go on with the BODY now */
4045 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004046
Willy Tarreau59234e92008-11-30 23:51:27 +01004047 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004048 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004049 /* we detected a parsing error. We want to archive this request
4050 * in the dedicated proxy area for later troubleshooting.
4051 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004052 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004053 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004054
Willy Tarreau59234e92008-11-30 23:51:27 +01004055 txn->req.msg_state = HTTP_MSG_ERROR;
4056 txn->status = 400;
4057 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004058 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004060 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004061 if (s->listener->counters)
4062 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004063
Willy Tarreau59234e92008-11-30 23:51:27 +01004064 if (!(s->flags & SN_ERR_MASK))
4065 s->flags |= SN_ERR_PRXCOND;
4066 if (!(s->flags & SN_FINST_MASK))
4067 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004068 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004069}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004070
Willy Tarreau60b85b02008-11-30 23:28:40 +01004071/* This function is an analyser which processes the HTTP tarpit. It always
4072 * returns zero, at the beginning because it prevents any other processing
4073 * from occurring, and at the end because it terminates the request.
4074 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004075int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004076{
4077 struct http_txn *txn = &s->txn;
4078
4079 /* This connection is being tarpitted. The CLIENT side has
4080 * already set the connect expiration date to the right
4081 * timeout. We just have to check that the client is still
4082 * there and that the timeout has not expired.
4083 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004084 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004085 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004086 !tick_is_expired(req->analyse_exp, now_ms))
4087 return 0;
4088
4089 /* We will set the queue timer to the time spent, just for
4090 * logging purposes. We fake a 500 server error, so that the
4091 * attacker will not suspect his connection has been tarpitted.
4092 * It will not cause trouble to the logs because we can exclude
4093 * the tarpitted connections by filtering on the 'PT' status flags.
4094 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004095 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4096
4097 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004098 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004099 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004100
4101 req->analysers = 0;
4102 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004103
Willy Tarreau60b85b02008-11-30 23:28:40 +01004104 if (!(s->flags & SN_ERR_MASK))
4105 s->flags |= SN_ERR_PRXCOND;
4106 if (!(s->flags & SN_FINST_MASK))
4107 s->flags |= SN_FINST_T;
4108 return 0;
4109}
4110
Willy Tarreaud34af782008-11-30 23:36:37 +01004111/* This function is an analyser which processes the HTTP request body. It looks
4112 * for parameters to be used for the load balancing algorithm (url_param). It
4113 * must only be called after the standard HTTP request processing has occurred,
4114 * because it expects the request to be parsed. It returns zero if it needs to
4115 * read more data, or 1 once it has completed its analysis.
4116 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004117int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004118{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004119 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004120 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004121 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004122
4123 /* We have to parse the HTTP request body to find any required data.
4124 * "balance url_param check_post" should have been the only way to get
4125 * into this. We were brought here after HTTP header analysis, so all
4126 * related structures are ready.
4127 */
4128
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4130 goto missing_data;
4131
4132 if (msg->msg_state < HTTP_MSG_100_SENT) {
4133 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4134 * send an HTTP/1.1 100 Continue intermediate response.
4135 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004136 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004137 struct hdr_ctx ctx;
4138 ctx.idx = 0;
4139 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004140 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004142 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004143 }
4144 }
4145 msg->msg_state = HTTP_MSG_100_SENT;
4146 }
4147
4148 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004149 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004150 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004151 * is still null. We must save the body in msg->next because it
4152 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004153 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004154 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004155
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004156 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004157 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4158 else
4159 msg->msg_state = HTTP_MSG_DATA;
4160 }
4161
4162 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004163 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004164 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004165 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004166 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004167 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004168
Willy Tarreau115acb92009-12-26 13:56:06 +01004169 if (!ret)
4170 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004171 else if (ret < 0) {
4172 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004173 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004174 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004175 }
4176
Willy Tarreaud98cf932009-12-27 22:54:55 +01004177 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004178 * We have the first data byte is in msg->sov. We're waiting for at
4179 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004180 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004181
Willy Tarreau124d9912011-03-01 20:30:48 +01004182 if (msg->body_len < limit)
4183 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184
Willy Tarreau9b28e032012-10-12 23:49:43 +02004185 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004186 goto http_end;
4187
4188 missing_data:
4189 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004190 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004191 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004192 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004193 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004194
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004195 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004196 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004197 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004198
4199 if (!(s->flags & SN_ERR_MASK))
4200 s->flags |= SN_ERR_CLITO;
4201 if (!(s->flags & SN_FINST_MASK))
4202 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004203 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004204 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004205
4206 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004207 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004208 /* Not enough data. We'll re-use the http-request
4209 * timeout here. Ideally, we should set the timeout
4210 * relative to the accept() date. We just set the
4211 * request timeout once at the beginning of the
4212 * request.
4213 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004214 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004215 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004216 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004217 return 0;
4218 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004219
4220 http_end:
4221 /* The situation will not evolve, so let's give up on the analysis. */
4222 s->logs.tv_request = now; /* update the request timer to reflect full request */
4223 req->analysers &= ~an_bit;
4224 req->analyse_exp = TICK_ETERNITY;
4225 return 1;
4226
4227 return_bad_req: /* let's centralize all bad requests */
4228 txn->req.msg_state = HTTP_MSG_ERROR;
4229 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004230 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004231
Willy Tarreau79ebac62010-06-07 13:47:49 +02004232 if (!(s->flags & SN_ERR_MASK))
4233 s->flags |= SN_ERR_PRXCOND;
4234 if (!(s->flags & SN_FINST_MASK))
4235 s->flags |= SN_FINST_R;
4236
Willy Tarreau522d6c02009-12-06 18:49:18 +01004237 return_err_msg:
4238 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004239 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004240 if (s->listener->counters)
4241 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004242 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004243}
4244
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004245/* send a server's name with an outgoing request over an established connection.
4246 * Note: this function is designed to be called once the request has been scheduled
4247 * for being forwarded. This is the reason why it rewinds the buffer before
4248 * proceeding.
4249 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004250int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004251
4252 struct hdr_ctx ctx;
4253
Mark Lamourinec2247f02012-01-04 13:02:01 -05004254 char *hdr_name = be->server_id_hdr_name;
4255 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004256 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004257 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004258 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004259
William Lallemandd9e90662012-01-30 17:27:17 +01004260 ctx.idx = 0;
4261
Willy Tarreau9b28e032012-10-12 23:49:43 +02004262 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004263 if (old_o) {
4264 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004265 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004266 }
4267
Willy Tarreau9b28e032012-10-12 23:49:43 +02004268 old_i = chn->buf->i;
4269 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 -05004270 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004271 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004272 }
4273
4274 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004275 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004276 memcpy(hdr_val, hdr_name, hdr_name_len);
4277 hdr_val += hdr_name_len;
4278 *hdr_val++ = ':';
4279 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004280 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4281 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004282
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004283 if (old_o) {
4284 /* If this was a forwarded request, we must readjust the amount of
4285 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004286 * variations. Note that if the request was already scheduled for
4287 * forwarding, it had its req->sol pointing to the body, which
4288 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004289 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004290 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004291 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004292 }
4293
Mark Lamourinec2247f02012-01-04 13:02:01 -05004294 return 0;
4295}
4296
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297/* Terminate current transaction and prepare a new one. This is very tricky
4298 * right now but it works.
4299 */
4300void http_end_txn_clean_session(struct session *s)
4301{
Willy Tarreau068621e2013-12-23 15:11:25 +01004302 int prev_status = s->txn.status;
4303
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 /* FIXME: We need a more portable way of releasing a backend's and a
4305 * server's connections. We need a safer way to reinitialize buffer
4306 * flags. We also need a more accurate method for computing per-request
4307 * data.
4308 */
4309 http_silent_debug(__LINE__, s);
4310
Willy Tarreau4213a112013-12-15 10:25:42 +01004311 /* unless we're doing keep-alive, we want to quickly close the connection
4312 * to the server.
4313 */
4314 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4315 !si_conn_ready(s->req->cons)) {
4316 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4317 si_shutr(s->req->cons);
4318 si_shutw(s->req->cons);
4319 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320
4321 http_silent_debug(__LINE__, s);
4322
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004323 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004324 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004325 if (unlikely(s->srv_conn))
4326 sess_change_server(s, NULL);
4327 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004328
4329 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4330 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004331 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332
4333 if (s->txn.status) {
4334 int n;
4335
4336 n = s->txn.status / 100;
4337 if (n < 1 || n > 5)
4338 n = 0;
4339
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004340 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004341 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004342 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004343 s->fe->fe_counters.p.http.comp_rsp++;
4344 }
Willy Tarreau24657792010-02-26 10:30:28 +01004345 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004346 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004347 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004348 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004349 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004350 s->be->be_counters.p.http.comp_rsp++;
4351 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352 }
4353
4354 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004355 s->logs.bytes_in -= s->req->buf->i;
4356 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004357
4358 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004359 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 !(s->flags & SN_MONITOR) &&
4361 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4362 s->do_log(s);
4363 }
4364
4365 s->logs.accept_date = date; /* user-visible date for logging */
4366 s->logs.tv_accept = now; /* corrected date for internal use */
4367 tv_zero(&s->logs.tv_request);
4368 s->logs.t_queue = -1;
4369 s->logs.t_connect = -1;
4370 s->logs.t_data = -1;
4371 s->logs.t_close = 0;
4372 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4373 s->logs.srv_queue_size = 0; /* we will get this number soon */
4374
Willy Tarreau9b28e032012-10-12 23:49:43 +02004375 s->logs.bytes_in = s->req->total = s->req->buf->i;
4376 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377
4378 if (s->pend_pos)
4379 pendconn_free(s->pend_pos);
4380
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004381 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004382 if (s->flags & SN_CURR_SESS) {
4383 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004384 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004386 if (may_dequeue_tasks(objt_server(s->target), s->be))
4387 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388 }
4389
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004390 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004391
Willy Tarreau4213a112013-12-15 10:25:42 +01004392 /* only release our endpoint if we don't intend to reuse the
4393 * connection.
4394 */
4395 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4396 !si_conn_ready(s->req->cons)) {
4397 si_release_endpoint(s->req->cons);
4398 }
4399
Willy Tarreau610ecce2010-01-04 21:15:02 +01004400 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004402 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004403 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004404 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004405 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4406 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 +02004407 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 +01004408 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004409
4410 if (s->flags & SN_COMP_READY)
4411 s->comp_algo->end(&s->comp_ctx);
4412 s->comp_algo = NULL;
4413 s->flags &= ~SN_COMP_READY;
4414
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 s->txn.meth = 0;
4416 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004417 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004418
4419 if (prev_status == 401 || prev_status == 407) {
4420 /* In HTTP keep-alive mode, if we receive a 401, we still have
4421 * a chance of being able to send the visitor again to the same
4422 * server over the same connection. This is required by some
4423 * broken protocols such as NTLM, and anyway whenever there is
4424 * an opportunity for sending the challenge to the proper place,
4425 * it's better to do it (at least it helps with debugging).
4426 */
4427 s->txn.flags |= TX_PREFER_LAST;
4428 }
4429
Willy Tarreauee55dc02010-06-01 10:56:34 +02004430 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431 s->req->cons->flags |= SI_FL_INDEP_STR;
4432
Willy Tarreau96e31212011-05-30 18:10:30 +02004433 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004434 s->req->flags |= CF_NEVER_WAIT;
4435 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004436 }
4437
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 /* if the request buffer is not empty, it means we're
4439 * about to process another request, so send pending
4440 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004441 * Just don't do this if the buffer is close to be full,
4442 * because the request will wait for it to flush a little
4443 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004445 if (s->req->buf->i) {
4446 if (s->rep->buf->o &&
4447 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4448 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004449 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004450 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004451
4452 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004453 channel_auto_read(s->req);
4454 channel_auto_close(s->req);
4455 channel_auto_read(s->rep);
4456 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457
Willy Tarreau27375622013-12-17 00:00:28 +01004458 /* we're in keep-alive with an idle connection, monitor it */
4459 si_idle_conn(s->req->cons);
4460
Willy Tarreau342b11c2010-11-24 16:22:09 +01004461 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462 s->rep->analysers = 0;
4463
4464 http_silent_debug(__LINE__, s);
4465}
4466
4467
4468/* This function updates the request state machine according to the response
4469 * state machine and buffer flags. It returns 1 if it changes anything (flag
4470 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4471 * it is only used to find when a request/response couple is complete. Both
4472 * this function and its equivalent should loop until both return zero. It
4473 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4474 */
4475int http_sync_req_state(struct session *s)
4476{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004477 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004479 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 unsigned int old_state = txn->req.msg_state;
4481
4482 http_silent_debug(__LINE__, s);
4483 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4484 return 0;
4485
4486 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004487 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004488 * We can shut the read side unless we want to abort_on_close,
4489 * or we have a POST request. The issue with POST requests is
4490 * that some browsers still send a CRLF after the request, and
4491 * this CRLF must be read so that it does not remain in the kernel
4492 * buffers, otherwise a close could cause an RST on some systems
4493 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004494 * Note that if we're using keep-alive on the client side, we'd
4495 * rather poll now and keep the polling enabled for the whole
4496 * session's life than enabling/disabling it between each
4497 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004498 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004499 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4500 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4501 !(s->be->options & PR_O_ABRT_CLOSE) &&
4502 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004503 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004504
Willy Tarreau40f151a2012-12-20 12:10:09 +01004505 /* if the server closes the connection, we want to immediately react
4506 * and close the socket to save packets and syscalls.
4507 */
4508 chn->cons->flags |= SI_FL_NOHALF;
4509
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4511 goto wait_other_side;
4512
4513 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4514 /* The server has not finished to respond, so we
4515 * don't want to move in order not to upset it.
4516 */
4517 goto wait_other_side;
4518 }
4519
4520 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4521 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004522 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004524 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004525 goto wait_other_side;
4526 }
4527
4528 /* When we get here, it means that both the request and the
4529 * response have finished receiving. Depending on the connection
4530 * mode, we'll have to wait for the last bytes to leave in either
4531 * direction, and sometimes for a close to be effective.
4532 */
4533
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004534 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4535 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004536 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4537 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004538 }
4539 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4540 /* Option forceclose is set, or either side wants to close,
4541 * let's enforce it now that we're not expecting any new
4542 * data to come. The caller knows the session is complete
4543 * once both states are CLOSED.
4544 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004545 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4546 channel_shutr_now(chn);
4547 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004549 }
4550 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004551 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4552 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004553 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004554 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4555 channel_auto_read(chn);
4556 txn->req.msg_state = HTTP_MSG_TUNNEL;
4557 chn->flags |= CF_NEVER_WAIT;
4558 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 }
4560
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004561 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004564
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004565 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 txn->req.msg_state = HTTP_MSG_CLOSING;
4567 goto http_msg_closing;
4568 }
4569 else {
4570 txn->req.msg_state = HTTP_MSG_CLOSED;
4571 goto http_msg_closed;
4572 }
4573 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004574 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 }
4576
4577 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4578 http_msg_closing:
4579 /* nothing else to forward, just waiting for the output buffer
4580 * to be empty and for the shutw_now to take effect.
4581 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 txn->req.msg_state = HTTP_MSG_CLOSED;
4584 goto http_msg_closed;
4585 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004586 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 txn->req.msg_state = HTTP_MSG_ERROR;
4588 goto wait_other_side;
4589 }
4590 }
4591
4592 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4593 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004594 /* see above in MSG_DONE why we only do this in these states */
4595 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4596 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4597 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004598 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599 goto wait_other_side;
4600 }
4601
4602 wait_other_side:
4603 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004604 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605}
4606
4607
4608/* This function updates the response state machine according to the request
4609 * state machine and buffer flags. It returns 1 if it changes anything (flag
4610 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4611 * it is only used to find when a request/response couple is complete. Both
4612 * this function and its equivalent should loop until both return zero. It
4613 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4614 */
4615int http_sync_res_state(struct session *s)
4616{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004617 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 unsigned int old_state = txn->rsp.msg_state;
4621
4622 http_silent_debug(__LINE__, s);
4623 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4624 return 0;
4625
4626 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4627 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004628 * still monitor the server connection for a possible close
4629 * while the request is being uploaded, so we don't disable
4630 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004632 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633
4634 if (txn->req.msg_state == HTTP_MSG_ERROR)
4635 goto wait_other_side;
4636
4637 if (txn->req.msg_state < HTTP_MSG_DONE) {
4638 /* The client seems to still be sending data, probably
4639 * because we got an error response during an upload.
4640 * We have the choice of either breaking the connection
4641 * or letting it pass through. Let's do the later.
4642 */
4643 goto wait_other_side;
4644 }
4645
4646 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4647 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004648 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004650 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 goto wait_other_side;
4652 }
4653
4654 /* When we get here, it means that both the request and the
4655 * response have finished receiving. Depending on the connection
4656 * mode, we'll have to wait for the last bytes to leave in either
4657 * direction, and sometimes for a close to be effective.
4658 */
4659
4660 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4661 /* Server-close mode : shut read and wait for the request
4662 * side to close its output buffer. The caller will detect
4663 * when we're in DONE and the other is in CLOSED and will
4664 * catch that for the final cleanup.
4665 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004666 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4667 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004669 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4670 /* Option forceclose is set, or either side wants to close,
4671 * let's enforce it now that we're not expecting any new
4672 * data to come. The caller knows the session is complete
4673 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004675 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4676 channel_shutr_now(chn);
4677 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004678 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004679 }
4680 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004681 /* The last possible modes are keep-alive and tunnel. Tunnel will
4682 * need to forward remaining data. Keep-alive will need to monitor
4683 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004685 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004686 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004687 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4688 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004689 }
4690
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004691 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004692 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004693 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004694 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4695 goto http_msg_closing;
4696 }
4697 else {
4698 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4699 goto http_msg_closed;
4700 }
4701 }
4702 goto wait_other_side;
4703 }
4704
4705 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4706 http_msg_closing:
4707 /* nothing else to forward, just waiting for the output buffer
4708 * to be empty and for the shutw_now to take effect.
4709 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004710 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4712 goto http_msg_closed;
4713 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004714 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004715 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004716 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004717 if (objt_server(s->target))
4718 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 goto wait_other_side;
4720 }
4721 }
4722
4723 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4724 http_msg_closed:
4725 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004726 bi_erase(chn);
4727 channel_auto_close(chn);
4728 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 goto wait_other_side;
4730 }
4731
4732 wait_other_side:
4733 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004734 /* We force the response to leave immediately if we're waiting for the
4735 * other side, since there is no pending shutdown to push it out.
4736 */
4737 if (!channel_is_empty(chn))
4738 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004739 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740}
4741
4742
4743/* Resync the request and response state machines. Return 1 if either state
4744 * changes.
4745 */
4746int http_resync_states(struct session *s)
4747{
4748 struct http_txn *txn = &s->txn;
4749 int old_req_state = txn->req.msg_state;
4750 int old_res_state = txn->rsp.msg_state;
4751
4752 http_silent_debug(__LINE__, s);
4753 http_sync_req_state(s);
4754 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004755 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 if (!http_sync_res_state(s))
4757 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004758 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759 if (!http_sync_req_state(s))
4760 break;
4761 }
4762 http_silent_debug(__LINE__, s);
4763 /* OK, both state machines agree on a compatible state.
4764 * There are a few cases we're interested in :
4765 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4766 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4767 * directions, so let's simply disable both analysers.
4768 * - HTTP_MSG_CLOSED on the response only means we must abort the
4769 * request.
4770 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4771 * with server-close mode means we've completed one request and we
4772 * must re-initialize the server connection.
4773 */
4774
4775 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4776 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4777 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4778 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4779 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004780 channel_auto_close(s->req);
4781 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004783 channel_auto_close(s->rep);
4784 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004786 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4787 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004788 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004789 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004790 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004791 channel_auto_close(s->rep);
4792 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004793 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004794 channel_abort(s->req);
4795 channel_auto_close(s->req);
4796 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004797 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004799 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4800 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004802 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4803 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4804 /* server-close/keep-alive: terminate this transaction,
4805 * possibly killing the server connection and reinitialize
4806 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 */
4808 http_end_txn_clean_session(s);
4809 }
4810
4811 http_silent_debug(__LINE__, s);
4812 return txn->req.msg_state != old_req_state ||
4813 txn->rsp.msg_state != old_res_state;
4814}
4815
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816/* This function is an analyser which forwards request body (including chunk
4817 * sizes if any). It is called as soon as we must forward, even if we forward
4818 * zero byte. The only situation where it must not be called is when we're in
4819 * tunnel mode and we want to forward till the close. It's used both to forward
4820 * remaining data and to resync after end of body. It expects the msg_state to
4821 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4822 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004823 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004824 * bytes of pending data + the headers if not already done (between sol and sov).
4825 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004826 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004827int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004828{
4829 struct http_txn *txn = &s->txn;
4830 struct http_msg *msg = &s->txn.req;
4831
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004832 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4833 return 0;
4834
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004835 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004836 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004837 /* Output closed while we were sending data. We must abort and
4838 * wake the other side up.
4839 */
4840 msg->msg_state = HTTP_MSG_ERROR;
4841 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004842 return 1;
4843 }
4844
Willy Tarreau4fe41902010-06-07 22:27:41 +02004845 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004846 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847
4848 /* Note that we don't have to send 100-continue back because we don't
4849 * need the data to complete our job, and it's up to the server to
4850 * decide whether to return 100, 417 or anything else in return of
4851 * an "Expect: 100-continue" header.
4852 */
4853
4854 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004855 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004856 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004857 * is still null. We must save the body in msg->next because it
4858 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004860 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004861
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004862 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004863 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004864 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004865 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 }
4867
Willy Tarreaud98cf932009-12-27 22:54:55 +01004868 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004869 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004870
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004872 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004873 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004874 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004875 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004876 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004877 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004878 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004879 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004880
Willy Tarreaucaabe412010-01-03 23:08:28 +01004881 if (msg->msg_state == HTTP_MSG_DATA) {
4882 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004883 if (req->to_forward) {
4884 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004885 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004886 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004887
4888 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004889 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004890 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004892 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004893 }
4894 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004895 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004896 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004897 * TRAILERS state.
4898 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004899 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004900
Willy Tarreau54d23df2012-10-25 19:04:45 +02004901 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004902 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004903 else if (ret < 0) {
4904 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004905 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004906 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004907 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004908 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004909 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004910 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004911 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004913 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914
4915 if (ret == 0)
4916 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004917 else if (ret < 0) {
4918 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004919 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004920 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004921 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004922 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923 /* we're in MSG_CHUNK_SIZE now */
4924 }
4925 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004926 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004927
4928 if (ret == 0)
4929 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004930 else if (ret < 0) {
4931 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004932 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004933 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004934 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004935 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004936 /* we're in HTTP_MSG_DONE now */
4937 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004939 int old_state = msg->msg_state;
4940
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004942 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004943 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4944 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004945 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946 if (http_resync_states(s)) {
4947 /* some state changes occurred, maybe the analyser
4948 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004949 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004950 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004951 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004952 /* request errors are most likely due to
4953 * the server aborting the transfer.
4954 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004955 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004956 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004957 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004958 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004960 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004962 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004963
4964 /* If "option abortonclose" is set on the backend, we
4965 * want to monitor the client's connection and forward
4966 * any shutdown notification to the server, which will
4967 * decide whether to close or to go on processing the
4968 * request.
4969 */
4970 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004971 channel_auto_read(req);
4972 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004973 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004974 else if (s->txn.meth == HTTP_METH_POST) {
4975 /* POST requests may require to read extra CRLF
4976 * sent by broken browsers and which could cause
4977 * an RST to be sent upon close on some systems
4978 * (eg: Linux).
4979 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004980 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004981 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004982
Willy Tarreau610ecce2010-01-04 21:15:02 +01004983 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 }
4985 }
4986
Willy Tarreaud98cf932009-12-27 22:54:55 +01004987 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004988 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004989 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004990 if (!(s->flags & SN_ERR_MASK))
4991 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004992 if (!(s->flags & SN_FINST_MASK)) {
4993 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4994 s->flags |= SN_FINST_H;
4995 else
4996 s->flags |= SN_FINST_D;
4997 }
4998
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004999 s->fe->fe_counters.cli_aborts++;
5000 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005001 if (objt_server(s->target))
5002 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005003
5004 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005005 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005006
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005007 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005008 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005009 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005010
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005011 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005012 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005013 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005014 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005015 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005016
Willy Tarreau5c620922011-05-11 19:56:11 +02005017 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005018 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005019 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005020 * modes are already handled by the stream sock layer. We must not do
5021 * this in content-length mode because it could present the MSG_MORE
5022 * flag with the last block of forwarded data, which would cause an
5023 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005024 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005025 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005026 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005027
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005029 return 0;
5030
Willy Tarreaud98cf932009-12-27 22:54:55 +01005031 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005032 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005033 if (s->listener->counters)
5034 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005035 return_bad_req_stats_ok:
5036 txn->req.msg_state = HTTP_MSG_ERROR;
5037 if (txn->status) {
5038 /* Note: we don't send any error if some data were already sent */
5039 stream_int_retnclose(req->prod, NULL);
5040 } else {
5041 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005042 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005043 }
5044 req->analysers = 0;
5045 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005046
5047 if (!(s->flags & SN_ERR_MASK))
5048 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049 if (!(s->flags & SN_FINST_MASK)) {
5050 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5051 s->flags |= SN_FINST_H;
5052 else
5053 s->flags |= SN_FINST_D;
5054 }
5055 return 0;
5056
5057 aborted_xfer:
5058 txn->req.msg_state = HTTP_MSG_ERROR;
5059 if (txn->status) {
5060 /* Note: we don't send any error if some data were already sent */
5061 stream_int_retnclose(req->prod, NULL);
5062 } else {
5063 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005064 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005065 }
5066 req->analysers = 0;
5067 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5068
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005069 s->fe->fe_counters.srv_aborts++;
5070 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005071 if (objt_server(s->target))
5072 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005073
5074 if (!(s->flags & SN_ERR_MASK))
5075 s->flags |= SN_ERR_SRVCL;
5076 if (!(s->flags & SN_FINST_MASK)) {
5077 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5078 s->flags |= SN_FINST_H;
5079 else
5080 s->flags |= SN_FINST_D;
5081 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005082 return 0;
5083}
5084
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005085/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5086 * processing can continue on next analysers, or zero if it either needs more
5087 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5088 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5089 * when it has nothing left to do, and may remove any analyser when it wants to
5090 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005091 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005092int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005093{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005094 struct http_txn *txn = &s->txn;
5095 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005096 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005097 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005099 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005100
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005101 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 +02005102 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005103 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005104 rep,
5105 rep->rex, rep->wex,
5106 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005107 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005108 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005109
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005110 /*
5111 * Now parse the partial (or complete) lines.
5112 * We will check the response syntax, and also join multi-line
5113 * headers. An index of all the lines will be elaborated while
5114 * parsing.
5115 *
5116 * For the parsing, we use a 28 states FSM.
5117 *
5118 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005119 * rep->buf->p = beginning of response
5120 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5121 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005122 * msg->eol = end of current header or line (LF or CRLF)
5123 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005124 */
5125
Willy Tarreau83e3af02009-12-28 17:39:57 +01005126 /* There's a protected area at the end of the buffer for rewriting
5127 * purposes. We don't want to start to parse the request if the
5128 * protected area is affected, because we may have to move processed
5129 * data later, which is much more complicated.
5130 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005131 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005132 if (unlikely(!channel_reserved(rep))) {
5133 /* some data has still not left the buffer, wake us once that's done */
5134 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5135 goto abort_response;
5136 channel_dont_close(rep);
5137 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005138 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005139 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005140 }
5141
Willy Tarreau379357a2013-06-08 12:55:46 +02005142 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5143 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5144 buffer_slow_realign(rep->buf);
5145
Willy Tarreau9b28e032012-10-12 23:49:43 +02005146 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005147 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005148 }
5149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150 /* 1: we might have to print this header in debug mode */
5151 if (unlikely((global.mode & MODE_DEBUG) &&
5152 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005153 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005155
Willy Tarreau9b28e032012-10-12 23:49:43 +02005156 sol = rep->buf->p;
5157 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160 sol += hdr_idx_first_pos(&txn->hdr_idx);
5161 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005162
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005163 while (cur_idx) {
5164 eol = sol + txn->hdr_idx.v[cur_idx].len;
5165 debug_hdr("srvhdr", s, sol, eol);
5166 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5167 cur_idx = txn->hdr_idx.v[cur_idx].next;
5168 }
5169 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171 /*
5172 * Now we quickly check if we have found a full valid response.
5173 * If not so, we check the FD and buffer states before leaving.
5174 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005175 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005176 * responses are checked first.
5177 *
5178 * Depending on whether the client is still there or not, we
5179 * may send an error response back or not. Note that normally
5180 * we should only check for HTTP status there, and check I/O
5181 * errors somewhere else.
5182 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183
Willy Tarreau655dce92009-11-08 13:10:58 +01005184 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 /* Invalid response */
5186 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5187 /* we detected a parsing error. We want to archive this response
5188 * in the dedicated proxy area for later troubleshooting.
5189 */
5190 hdr_response_bad:
5191 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005192 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005194 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 if (objt_server(s->target)) {
5196 objt_server(s->target)->counters.failed_resp++;
5197 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005198 }
Willy Tarreau64648412010-03-05 10:41:54 +01005199 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005200 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201 rep->analysers = 0;
5202 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005203 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005204 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005205 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206
5207 if (!(s->flags & SN_ERR_MASK))
5208 s->flags |= SN_ERR_PRXCOND;
5209 if (!(s->flags & SN_FINST_MASK))
5210 s->flags |= SN_FINST_H;
5211
5212 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005213 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005214
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005216 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005217 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005218 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 goto hdr_response_bad;
5220 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005221
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005222 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005223 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005224 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005225 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005226 else if (txn->flags & TX_NOT_FIRST)
5227 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005228
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005229 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005230 if (objt_server(s->target)) {
5231 objt_server(s->target)->counters.failed_resp++;
5232 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005233 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005234
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005235 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005236 rep->analysers = 0;
5237 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005238 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005239 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005240 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005241
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242 if (!(s->flags & SN_ERR_MASK))
5243 s->flags |= SN_ERR_SRVCL;
5244 if (!(s->flags & SN_FINST_MASK))
5245 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005246 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005248
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005250 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005252 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005253 else if (txn->flags & TX_NOT_FIRST)
5254 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005255
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005256 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005257 if (objt_server(s->target)) {
5258 objt_server(s->target)->counters.failed_resp++;
5259 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005260 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005261
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005262 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 rep->analysers = 0;
5264 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005265 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005266 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005267 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005268
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269 if (!(s->flags & SN_ERR_MASK))
5270 s->flags |= SN_ERR_SRVTO;
5271 if (!(s->flags & SN_FINST_MASK))
5272 s->flags |= SN_FINST_H;
5273 return 0;
5274 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005275
Willy Tarreauf003d372012-11-26 13:35:37 +01005276 /* client abort with an abortonclose */
5277 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5278 s->fe->fe_counters.cli_aborts++;
5279 s->be->be_counters.cli_aborts++;
5280 if (objt_server(s->target))
5281 objt_server(s->target)->counters.cli_aborts++;
5282
5283 rep->analysers = 0;
5284 channel_auto_close(rep);
5285
5286 txn->status = 400;
5287 bi_erase(rep);
5288 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5289
5290 if (!(s->flags & SN_ERR_MASK))
5291 s->flags |= SN_ERR_CLICL;
5292 if (!(s->flags & SN_FINST_MASK))
5293 s->flags |= SN_FINST_H;
5294
5295 /* process_session() will take care of the error */
5296 return 0;
5297 }
5298
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005299 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005300 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005301 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005302 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005303 else if (txn->flags & TX_NOT_FIRST)
5304 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005305
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005306 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005307 if (objt_server(s->target)) {
5308 objt_server(s->target)->counters.failed_resp++;
5309 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005310 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005311
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005312 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 rep->analysers = 0;
5314 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005315 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005316 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005317 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005318
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005319 if (!(s->flags & SN_ERR_MASK))
5320 s->flags |= SN_ERR_SRVCL;
5321 if (!(s->flags & SN_FINST_MASK))
5322 s->flags |= SN_FINST_H;
5323 return 0;
5324 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005326 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005327 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005329 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005330 else if (txn->flags & TX_NOT_FIRST)
5331 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005332
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005333 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005334 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005335 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336
5337 if (!(s->flags & SN_ERR_MASK))
5338 s->flags |= SN_ERR_CLICL;
5339 if (!(s->flags & SN_FINST_MASK))
5340 s->flags |= SN_FINST_H;
5341
5342 /* process_session() will take care of the error */
5343 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005345
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005346 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005347 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005348 return 0;
5349 }
5350
5351 /* More interesting part now : we know that we have a complete
5352 * response which at least looks like HTTP. We have an indicator
5353 * of each header's length, so we can parse them quickly.
5354 */
5355
5356 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005357 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005358
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005359 /*
5360 * 1: get the status code
5361 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005362 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005363 if (n < 1 || n > 5)
5364 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005365 /* when the client triggers a 4xx from the server, it's most often due
5366 * to a missing object or permission. These events should be tracked
5367 * because if they happen often, it may indicate a brute force or a
5368 * vulnerability scan.
5369 */
5370 if (n == 4)
5371 session_inc_http_err_ctr(s);
5372
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005373 if (objt_server(s->target))
5374 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005375
Willy Tarreau5b154472009-12-21 20:11:07 +01005376 /* check if the response is HTTP/1.1 or above */
5377 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005378 ((rep->buf->p[5] > '1') ||
5379 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005380 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005381
5382 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005383 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 +01005384
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005385 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005387
Willy Tarreau9b28e032012-10-12 23:49:43 +02005388 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389
Willy Tarreau39650402010-03-15 19:44:39 +01005390 /* Adjust server's health based on status code. Note: status codes 501
5391 * and 505 are triggered on demand by client request, so we must not
5392 * count them as server failures.
5393 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005394 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005395 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005396 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005397 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005398 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005399 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005400
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401 /*
5402 * 2: check for cacheability.
5403 */
5404
5405 switch (txn->status) {
5406 case 200:
5407 case 203:
5408 case 206:
5409 case 300:
5410 case 301:
5411 case 410:
5412 /* RFC2616 @13.4:
5413 * "A response received with a status code of
5414 * 200, 203, 206, 300, 301 or 410 MAY be stored
5415 * by a cache (...) unless a cache-control
5416 * directive prohibits caching."
5417 *
5418 * RFC2616 @9.5: POST method :
5419 * "Responses to this method are not cacheable,
5420 * unless the response includes appropriate
5421 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005422 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005423 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005424 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005425 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5426 break;
5427 default:
5428 break;
5429 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005430
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005431 /*
5432 * 3: we may need to capture headers
5433 */
5434 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005435 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005436 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437 txn->rsp.cap, s->fe->rsp_cap);
5438
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005439 /* 4: determine the transfer-length.
5440 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5441 * the presence of a message-body in a RESPONSE and its transfer length
5442 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005443 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005444 * All responses to the HEAD request method MUST NOT include a
5445 * message-body, even though the presence of entity-header fields
5446 * might lead one to believe they do. All 1xx (informational), 204
5447 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5448 * message-body. All other responses do include a message-body,
5449 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005450 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005451 * 1. Any response which "MUST NOT" include a message-body (such as the
5452 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5453 * always terminated by the first empty line after the header fields,
5454 * regardless of the entity-header fields present in the message.
5455 *
5456 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5457 * the "chunked" transfer-coding (Section 6.2) is used, the
5458 * transfer-length is defined by the use of this transfer-coding.
5459 * If a Transfer-Encoding header field is present and the "chunked"
5460 * transfer-coding is not present, the transfer-length is defined by
5461 * the sender closing the connection.
5462 *
5463 * 3. If a Content-Length header field is present, its decimal value in
5464 * OCTETs represents both the entity-length and the transfer-length.
5465 * If a message is received with both a Transfer-Encoding header
5466 * field and a Content-Length header field, the latter MUST be ignored.
5467 *
5468 * 4. If the message uses the media type "multipart/byteranges", and
5469 * the transfer-length is not otherwise specified, then this self-
5470 * delimiting media type defines the transfer-length. This media
5471 * type MUST NOT be used unless the sender knows that the recipient
5472 * can parse it; the presence in a request of a Range header with
5473 * multiple byte-range specifiers from a 1.1 client implies that the
5474 * client can parse multipart/byteranges responses.
5475 *
5476 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005477 */
5478
5479 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005480 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005481 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005482 * FIXME: should we parse anyway and return an error on chunked encoding ?
5483 */
5484 if (txn->meth == HTTP_METH_HEAD ||
5485 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005486 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005487 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005488 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005489 goto skip_content_length;
5490 }
5491
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005492 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005493 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005494 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005495 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005496 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005497 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5498 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005499 /* bad transfer-encoding (chunked followed by something else) */
5500 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005501 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005502 break;
5503 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005504 }
5505
5506 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5507 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005508 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005509 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005510 signed long long cl;
5511
Willy Tarreauad14f752011-09-02 20:33:27 +02005512 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005513 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005514 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005515 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005516
Willy Tarreauad14f752011-09-02 20:33:27 +02005517 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005518 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005519 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005520 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005521
Willy Tarreauad14f752011-09-02 20:33:27 +02005522 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005523 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005524 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005525 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005527 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005528 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005529 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005530 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005531
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005532 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005533 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005534 }
5535
William Lallemand82fe75c2012-10-23 10:25:10 +02005536 if (s->fe->comp || s->be->comp)
5537 select_compression_response_header(s, rep->buf);
5538
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005539 /* FIXME: we should also implement the multipart/byterange method.
5540 * For now on, we resort to close mode in this case (unknown length).
5541 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005544 /* end of job, return OK */
5545 rep->analysers &= ~an_bit;
5546 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005547 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005548 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005549
5550 abort_keep_alive:
5551 /* A keep-alive request to the server failed on a network error.
5552 * The client is required to retry. We need to close without returning
5553 * any other information so that the client retries.
5554 */
5555 txn->status = 0;
5556 rep->analysers = 0;
5557 s->req->analysers = 0;
5558 channel_auto_close(rep);
5559 s->logs.logwait = 0;
5560 s->logs.level = 0;
5561 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5562 bi_erase(rep);
5563 stream_int_retnclose(rep->cons, NULL);
5564 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005565}
5566
5567/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005568 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5569 * and updates t->rep->analysers. It might make sense to explode it into several
5570 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005571 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005572int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005573{
5574 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005575 struct http_msg *msg = &txn->rsp;
5576 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005577 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005578 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005579
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005580 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 +02005581 now_ms, __FUNCTION__,
5582 t,
5583 rep,
5584 rep->rex, rep->wex,
5585 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005586 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587 rep->analysers);
5588
Willy Tarreau655dce92009-11-08 13:10:58 +01005589 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005590 return 0;
5591
5592 rep->analysers &= ~an_bit;
5593 rep->analyse_exp = TICK_ETERNITY;
5594
Willy Tarreau5b154472009-12-21 20:11:07 +01005595 /* Now we have to check if we need to modify the Connection header.
5596 * This is more difficult on the response than it is on the request,
5597 * because we can have two different HTTP versions and we don't know
5598 * how the client will interprete a response. For instance, let's say
5599 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5600 * HTTP/1.1 response without any header. Maybe it will bound itself to
5601 * HTTP/1.0 because it only knows about it, and will consider the lack
5602 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5603 * the lack of header as a keep-alive. Thus we will use two flags
5604 * indicating how a request MAY be understood by the client. In case
5605 * of multiple possibilities, we'll fix the header to be explicit. If
5606 * ambiguous cases such as both close and keepalive are seen, then we
5607 * will fall back to explicit close. Note that we won't take risks with
5608 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005609 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005610 */
5611
Willy Tarreaudc008c52010-02-01 16:20:08 +01005612 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5613 txn->status == 101)) {
5614 /* Either we've established an explicit tunnel, or we're
5615 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005616 * to understand the next protocols. We have to switch to tunnel
5617 * mode, so that we transfer the request and responses then let
5618 * this protocol pass unmodified. When we later implement specific
5619 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005620 * header which contains information about that protocol for
5621 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005622 */
5623 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5624 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005625 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5626 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005627 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5628 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005629 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005630
Willy Tarreau70dffda2014-01-30 03:07:23 +01005631 /* this situation happens when combining pretend-keepalive with httpclose. */
5632 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5633 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5634 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5635 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5636
Willy Tarreau60466522010-01-18 19:08:45 +01005637 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005638 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005639 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5640 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005641
Willy Tarreau60466522010-01-18 19:08:45 +01005642 /* now adjust header transformations depending on current state */
5643 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5644 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5645 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005646 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005647 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005648 }
Willy Tarreau60466522010-01-18 19:08:45 +01005649 else { /* SCL / KAL */
5650 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005651 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005652 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005653 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005654
Willy Tarreau60466522010-01-18 19:08:45 +01005655 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005656 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005657
Willy Tarreau60466522010-01-18 19:08:45 +01005658 /* Some keep-alive responses are converted to Server-close if
5659 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005660 */
Willy Tarreau60466522010-01-18 19:08:45 +01005661 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5662 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005663 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005664 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005665 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005666 }
5667
Willy Tarreau7959a552013-09-23 16:44:27 +02005668 /* we want to have the response time before we start processing it */
5669 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5670
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005671 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005672 /*
5673 * 3: we will have to evaluate the filters.
5674 * As opposed to version 1.2, now they will be evaluated in the
5675 * filters order and not in the header order. This means that
5676 * each filter has to be validated among all headers.
5677 *
5678 * Filters are tried with ->be first, then with ->fe if it is
5679 * different from ->be.
5680 */
5681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005682 cur_proxy = t->be;
5683 while (1) {
5684 struct proxy *rule_set = cur_proxy;
5685
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005686 /* evaluate http-response rules */
5687 if (!http_res_last_rule)
5688 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5689
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005690 /* try headers filters */
5691 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005692 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005693 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005694 if (objt_server(t->target)) {
5695 objt_server(t->target)->counters.failed_resp++;
5696 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005697 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005698 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005700 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005701 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005702 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005703 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005704 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005705 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005706 if (!(t->flags & SN_ERR_MASK))
5707 t->flags |= SN_ERR_PRXCOND;
5708 if (!(t->flags & SN_FINST_MASK))
5709 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005710 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005711 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005712 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714 /* has the response been denied ? */
5715 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005716 if (objt_server(t->target))
5717 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005718
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005719 t->be->be_counters.denied_resp++;
5720 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005721 if (t->listener->counters)
5722 t->listener->counters->denied_resp++;
5723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005724 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005725 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005726
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005728 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005729 if (txn->status < 200)
5730 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005731 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005732 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005733 ret = acl_pass(ret);
5734 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5735 ret = !ret;
5736 if (!ret)
5737 continue;
5738 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005739 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005741 }
5742
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005743 /* check whether we're already working on the frontend */
5744 if (cur_proxy == t->fe)
5745 break;
5746 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005747 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005748
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005749 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005750 * We may be facing a 100-continue response, in which case this
5751 * is not the right response, and we're waiting for the next one.
5752 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005753 * next one.
5754 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005755 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005756 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005757 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005758 msg->msg_state = HTTP_MSG_RPBEFORE;
5759 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005760 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005761 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5762 return 1;
5763 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005764 else if (unlikely(txn->status < 200))
5765 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005766
5767 /* we don't have any 1xx status code now */
5768
5769 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005770 * 4: check for server cookie.
5771 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005772 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5773 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005774 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005775
Willy Tarreaubaaee002006-06-26 02:48:02 +02005776
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005778 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005779 */
Willy Tarreau67402132012-05-31 20:40:20 +02005780 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005781 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005782
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005783 /*
5784 * 6: add server cookie in the response if needed
5785 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005786 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005787 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005788 (!(t->flags & SN_DIRECT) ||
5789 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5790 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5791 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5792 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005793 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005794 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005795 /* the server is known, it's not the one the client requested, or the
5796 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797 * insert a set-cookie here, except if we want to insert only on POST
5798 * requests and this one isn't. Note that servers which don't have cookies
5799 * (eg: some backup servers) will return a full cookie removal request.
5800 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005801 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005802 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005803 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5804 t->be->cookie_name);
5805 }
5806 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005807 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005808
5809 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5810 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005811 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5812 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5813 trash.len += 5;
5814
Willy Tarreauef4f3912010-10-07 21:00:29 +02005815 if (t->be->cookie_maxlife) {
5816 /* emit first_date, which is either the original one or
5817 * the current date.
5818 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005819 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005820 s30tob64(txn->cookie_first_date ?
5821 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005822 (date.tv_sec+3) >> 2, trash.str + trash.len);
5823 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005824 }
5825 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005826 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005827 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005828
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005829 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005830 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005831
Willy Tarreau4992dd22012-05-31 21:02:17 +02005832 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005833 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005834
5835 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005837
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005838 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005839 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005840
Willy Tarreauf1348312010-10-07 15:54:11 +02005841 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005842 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005843 /* the server did not change, only the date was updated */
5844 txn->flags |= TX_SCK_UPDATED;
5845 else
5846 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005847
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005848 /* Here, we will tell an eventual cache on the client side that we don't
5849 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5850 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5851 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5852 */
Willy Tarreau67402132012-05-31 20:40:20 +02005853 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5856
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005857 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005858 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005859 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005860 }
5861 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863 /*
5864 * 7: check if result will be cacheable with a cookie.
5865 * We'll block the response if security checks have caught
5866 * nasty things such as a cacheable cookie.
5867 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005868 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5869 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005870 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871
5872 /* we're in presence of a cacheable response containing
5873 * a set-cookie header. We'll block it as requested by
5874 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005876 if (objt_server(t->target))
5877 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005878
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005879 t->be->be_counters.denied_resp++;
5880 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005881 if (t->listener->counters)
5882 t->listener->counters->denied_resp++;
5883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005884 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005885 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005886 send_log(t->be, LOG_ALERT,
5887 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005888 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005889 goto return_srv_prx_502;
5890 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
5892 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005893 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005894 * If an "Upgrade" token is found, the header is left untouched in order
5895 * not to have to deal with some client bugs : some of them fail an upgrade
5896 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005898 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5899 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005900 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5901 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005902 unsigned int want_flags = 0;
5903
5904 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5905 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5906 /* we want a keep-alive response here. Keep-alive header
5907 * required if either side is not 1.1.
5908 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005909 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005910 want_flags |= TX_CON_KAL_SET;
5911 }
5912 else {
5913 /* we want a close response here. Close header required if
5914 * the server is 1.1, regardless of the client.
5915 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005916 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005917 want_flags |= TX_CON_CLO_SET;
5918 }
5919
5920 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005921 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005922 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005924 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005925 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005927 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005928
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005929 /*************************************************************
5930 * OK, that's finished for the headers. We have done what we *
5931 * could. Let's switch to the DATA state. *
5932 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005933
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005934 /* if the user wants to log as soon as possible, without counting
5935 * bytes from the server, then this is the right moment. We have
5936 * to temporarily assign bytes_out to log what we currently have.
5937 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005938 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5940 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005941 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005942 t->logs.bytes_out = 0;
5943 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005945 /* Note: we must not try to cheat by jumping directly to DATA,
5946 * otherwise we would not let the client side wake up.
5947 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005949 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005950 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005951 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005952}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005953
Willy Tarreaud98cf932009-12-27 22:54:55 +01005954/* This function is an analyser which forwards response body (including chunk
5955 * sizes if any). It is called as soon as we must forward, even if we forward
5956 * zero byte. The only situation where it must not be called is when we're in
5957 * tunnel mode and we want to forward till the close. It's used both to forward
5958 * remaining data and to resync after end of body. It expects the msg_state to
5959 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5960 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005961 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005962 * bytes of pending data + the headers if not already done (between sol and sov).
5963 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005964 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005965int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005966{
5967 struct http_txn *txn = &s->txn;
5968 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005969 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005970 static struct buffer *tmpbuf = NULL;
5971 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005972 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005973 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005974
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005975 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5976 return 0;
5977
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005978 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005979 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005980 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005981 /* Output closed while we were sending data. We must abort and
5982 * wake the other side up.
5983 */
5984 msg->msg_state = HTTP_MSG_ERROR;
5985 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005986 return 1;
5987 }
5988
Willy Tarreau4fe41902010-06-07 22:27:41 +02005989 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005990 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005991
William Lallemand82fe75c2012-10-23 10:25:10 +02005992 /* this is the first time we need the compression buffer */
5993 if (s->comp_algo != NULL && tmpbuf == NULL) {
5994 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5995 goto aborted_xfer; /* no memory */
5996 }
5997
Willy Tarreaud98cf932009-12-27 22:54:55 +01005998 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005999 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006000 * rep->buf.p still points to the beginning of the message and msg->sol
6001 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006002 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006003 channel_forward(res, msg->sov);
6004 msg->next = 0;
6005 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006006
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006007 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006008 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006009 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006010 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006011 }
6012
William Lallemand82fe75c2012-10-23 10:25:10 +02006013 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006014 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006015 if (ret < 0) {
6016 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006017 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006018 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006019 compressing = 1;
6020 }
6021
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006023 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006024 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006025 if (s->comp_algo == NULL) {
6026 bytes = msg->sov - msg->sol;
6027 if (msg->chunk_len || bytes) {
6028 msg->sol = msg->sov;
6029 msg->next -= bytes; /* will be forwarded */
6030 msg->chunk_len += bytes;
6031 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6032 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006033 }
6034
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006035 switch (msg->msg_state - HTTP_MSG_DATA) {
6036 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006037 if (compressing) {
6038 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6039 if (ret < 0)
6040 goto aborted_xfer;
6041 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006042
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006043 if (res->to_forward || msg->chunk_len) {
6044 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006045 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006046 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006047
6048 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006049 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006050 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006051 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006052 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006053 if (compressing && consumed_data) {
6054 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6055 compressing = 0;
6056 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006057 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006058 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006059 /* fall through for HTTP_MSG_CHUNK_CRLF */
6060
6061 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6062 /* we want the CRLF after the data */
6063
6064 ret = http_skip_chunk_crlf(msg);
6065 if (ret == 0)
6066 goto missing_data;
6067 else if (ret < 0) {
6068 if (msg->err_pos >= 0)
6069 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6070 goto return_bad_res;
6071 }
6072 /* skipping data in buffer for compression */
6073 if (compressing) {
6074 b_adv(res->buf, msg->next);
6075 msg->next = 0;
6076 msg->sov = 0;
6077 msg->sol = 0;
6078 }
6079 /* we're in MSG_CHUNK_SIZE now, fall through */
6080
6081 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006082 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006083 * set ->sov and ->next to point to the body and switch to DATA or
6084 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006085 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006086
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006087 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006088 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006089 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006090 else if (ret < 0) {
6091 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006092 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006093 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006094 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006095 if (compressing) {
6096 if (likely(msg->chunk_len > 0)) {
6097 /* skipping data if we are in compression mode */
6098 b_adv(res->buf, msg->next);
6099 msg->next = 0;
6100 msg->sov = 0;
6101 msg->sol = 0;
6102 } else {
6103 if (consumed_data) {
6104 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6105 compressing = 0;
6106 }
6107 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006108 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006109 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006110 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006111
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006112 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6113 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006114 if (ret == 0)
6115 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006116 else if (ret < 0) {
6117 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006118 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006119 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006120 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006121 if (s->comp_algo != NULL) {
6122 /* forwarding trailers */
6123 channel_forward(res, msg->next);
6124 msg->next = 0;
6125 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006126 /* we're in HTTP_MSG_DONE now, but we might still have
6127 * some data pending, so let's loop over once.
6128 */
6129 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006130
6131 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006132 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006133
6134 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006135 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6137 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006138 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006139 if (http_resync_states(s)) {
6140 http_silent_debug(__LINE__, s);
6141 /* some state changes occurred, maybe the analyser
6142 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006143 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006144 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006145 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006146 /* response errors are most likely due to
6147 * the client aborting the transfer.
6148 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006149 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006150 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006151 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006152 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006153 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006154 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006155 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006156 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006157 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006158 }
6159 }
6160
Willy Tarreaud98cf932009-12-27 22:54:55 +01006161 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006162 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006163 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6164 compressing = 0;
6165 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006166
6167 if (res->flags & CF_SHUTW)
6168 goto aborted_xfer;
6169
6170 /* stop waiting for data if the input is closed before the end. If the
6171 * client side was already closed, it means that the client has aborted,
6172 * so we don't want to count this as a server abort. Otherwise it's a
6173 * server abort.
6174 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006175 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006176 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6177 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006178 if (!(s->flags & SN_ERR_MASK))
6179 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006180 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006181 if (objt_server(s->target))
6182 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006183 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006184 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006185
Willy Tarreau40dba092010-03-04 18:14:51 +01006186 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006187 if (!s->req->analysers)
6188 goto return_bad_res;
6189
Willy Tarreauea953162012-05-18 23:41:28 +02006190 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006191 if (s->comp_algo == NULL) {
6192 bytes = msg->sov - msg->sol;
6193 if (msg->chunk_len || bytes) {
6194 msg->sol = msg->sov;
6195 msg->next -= bytes; /* will be forwarded */
6196 msg->chunk_len += bytes;
6197 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6198 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006199 }
6200
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006201 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006202 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006203 * Similarly, with keep-alive on the client side, we don't want to forward a
6204 * close.
6205 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006206 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006207 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6208 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006209 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006210
Willy Tarreau5c620922011-05-11 19:56:11 +02006211 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006212 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006213 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006214 * modes are already handled by the stream sock layer. We must not do
6215 * this in content-length mode because it could present the MSG_MORE
6216 * flag with the last block of forwarded data, which would cause an
6217 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006218 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006219 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006220 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006221
Willy Tarreaud98cf932009-12-27 22:54:55 +01006222 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006223 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006224 return 0;
6225
Willy Tarreau40dba092010-03-04 18:14:51 +01006226 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006227 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006228 if (objt_server(s->target))
6229 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006230
6231 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006232 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006233 /* don't send any error message as we're in the body */
6234 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006235 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006236 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006237 if (objt_server(s->target))
6238 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006239
6240 if (!(s->flags & SN_ERR_MASK))
6241 s->flags |= SN_ERR_PRXCOND;
6242 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006243 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006244 return 0;
6245
6246 aborted_xfer:
6247 txn->rsp.msg_state = HTTP_MSG_ERROR;
6248 /* don't send any error message as we're in the body */
6249 stream_int_retnclose(res->cons, NULL);
6250 res->analysers = 0;
6251 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6252
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006253 s->fe->fe_counters.cli_aborts++;
6254 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006255 if (objt_server(s->target))
6256 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006257
6258 if (!(s->flags & SN_ERR_MASK))
6259 s->flags |= SN_ERR_CLICL;
6260 if (!(s->flags & SN_FINST_MASK))
6261 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006262 return 0;
6263}
6264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006265/* Iterate the same filter through all request headers.
6266 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006267 * Since it can manage the switch to another backend, it updates the per-proxy
6268 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006270int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006272 char term;
6273 char *cur_ptr, *cur_end, *cur_next;
6274 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006275 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006277 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006278
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006279 last_hdr = 0;
6280
Willy Tarreau9b28e032012-10-12 23:49:43 +02006281 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 old_idx = 0;
6283
6284 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006285 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006286 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006287 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 (exp->action == ACT_ALLOW ||
6289 exp->action == ACT_DENY ||
6290 exp->action == ACT_TARPIT))
6291 return 0;
6292
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006293 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006294 if (!cur_idx)
6295 break;
6296
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006297 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 cur_ptr = cur_next;
6299 cur_end = cur_ptr + cur_hdr->len;
6300 cur_next = cur_end + cur_hdr->cr + 1;
6301
6302 /* Now we have one header between cur_ptr and cur_end,
6303 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006304 */
6305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 /* The annoying part is that pattern matching needs
6307 * that we modify the contents to null-terminate all
6308 * strings before testing them.
6309 */
6310
6311 term = *cur_end;
6312 *cur_end = '\0';
6313
6314 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6315 switch (exp->action) {
6316 case ACT_SETBE:
6317 /* It is not possible to jump a second time.
6318 * FIXME: should we return an HTTP/500 here so that
6319 * the admin knows there's a problem ?
6320 */
6321 if (t->be != t->fe)
6322 break;
6323
6324 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006325 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 last_hdr = 1;
6327 break;
6328
6329 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006330 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006331 last_hdr = 1;
6332 break;
6333
6334 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006335 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 break;
6338
6339 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006340 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 break;
6343
6344 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006345 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6346 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 /* FIXME: if the user adds a newline in the replacement, the
6348 * index will not be recalculated for now, and the new line
6349 * will not be counted as a new header.
6350 */
6351
6352 cur_end += delta;
6353 cur_next += delta;
6354 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006355 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006356 break;
6357
6358 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006359 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 cur_next += delta;
6361
Willy Tarreaufa355d42009-11-29 18:12:29 +01006362 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006363 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6364 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 cur_hdr->len = 0;
6366 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006367 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 break;
6369
6370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006371 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006372 if (cur_end)
6373 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 /* keep the link from this header to next one in case of later
6376 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006377 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006378 old_idx = cur_idx;
6379 }
6380 return 0;
6381}
6382
6383
6384/* Apply the filter to the request line.
6385 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6386 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006387 * Since it can manage the switch to another backend, it updates the per-proxy
6388 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006390int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006391{
6392 char term;
6393 char *cur_ptr, *cur_end;
6394 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006395 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006396 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397
Willy Tarreau3d300592007-03-18 18:34:41 +01006398 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006399 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006400 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401 (exp->action == ACT_ALLOW ||
6402 exp->action == ACT_DENY ||
6403 exp->action == ACT_TARPIT))
6404 return 0;
6405 else if (exp->action == ACT_REMOVE)
6406 return 0;
6407
6408 done = 0;
6409
Willy Tarreau9b28e032012-10-12 23:49:43 +02006410 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006411 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006412
6413 /* Now we have the request line between cur_ptr and cur_end */
6414
6415 /* The annoying part is that pattern matching needs
6416 * that we modify the contents to null-terminate all
6417 * strings before testing them.
6418 */
6419
6420 term = *cur_end;
6421 *cur_end = '\0';
6422
6423 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6424 switch (exp->action) {
6425 case ACT_SETBE:
6426 /* It is not possible to jump a second time.
6427 * FIXME: should we return an HTTP/500 here so that
6428 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006430 if (t->be != t->fe)
6431 break;
6432
6433 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006434 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 done = 1;
6436 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006438 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006439 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 done = 1;
6441 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006444 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 done = 1;
6446 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006448 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006449 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 done = 1;
6451 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453 case ACT_REPLACE:
6454 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006455 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6456 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457 /* FIXME: if the user adds a newline in the replacement, the
6458 * index will not be recalculated for now, and the new line
6459 * will not be counted as a new header.
6460 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006461
Willy Tarreaufa355d42009-11-29 18:12:29 +01006462 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006464 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 HTTP_MSG_RQMETH,
6466 cur_ptr, cur_end + 1,
6467 NULL, NULL);
6468 if (unlikely(!cur_end))
6469 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 /* we have a full request and we know that we have either a CR
6472 * or an LF at <ptr>.
6473 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006474 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6475 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 /* there is no point trying this regex on headers */
6477 return 1;
6478 }
6479 }
6480 *cur_end = term; /* restore the string terminator */
6481 return done;
6482}
Willy Tarreau97de6242006-12-27 17:18:38 +01006483
Willy Tarreau58f10d72006-12-04 02:26:12 +01006484
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006487 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006489 * unparsable request. Since it can manage the switch to another backend, it
6490 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006492int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006494 struct http_txn *txn = &s->txn;
6495 struct hdr_exp *exp;
6496
6497 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006500 /*
6501 * The interleaving of transformations and verdicts
6502 * makes it difficult to decide to continue or stop
6503 * the evaluation.
6504 */
6505
Willy Tarreau6c123b12010-01-28 20:22:06 +01006506 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6507 break;
6508
Willy Tarreau3d300592007-03-18 18:34:41 +01006509 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006511 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006513
6514 /* if this filter had a condition, evaluate it now and skip to
6515 * next filter if the condition does not match.
6516 */
6517 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006518 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006519 ret = acl_pass(ret);
6520 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6521 ret = !ret;
6522
6523 if (!ret)
6524 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 }
6526
6527 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006528 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 if (unlikely(ret < 0))
6530 return -1;
6531
6532 if (likely(ret == 0)) {
6533 /* The filter did not match the request, it can be
6534 * iterated through all headers.
6535 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006536 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006537 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540}
6541
6542
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006545 * Try to retrieve the server associated to the appsession.
6546 * If the server is found, it's assigned to the session.
6547 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006548void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006549 struct http_txn *txn = &t->txn;
6550 appsess *asession = NULL;
6551 char *sessid_temp = NULL;
6552
Cyril Bontéb21570a2009-11-29 20:04:48 +01006553 if (len > t->be->appsession_len) {
6554 len = t->be->appsession_len;
6555 }
6556
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006557 if (t->be->options2 & PR_O2_AS_REQL) {
6558 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006559 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006560 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006561 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006562 }
6563
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006564 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006565 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6566 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6567 return;
6568 }
6569
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006570 memcpy(txn->sessid, buf, len);
6571 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006572 }
6573
6574 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6575 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6576 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6577 return;
6578 }
6579
Cyril Bontéb21570a2009-11-29 20:04:48 +01006580 memcpy(sessid_temp, buf, len);
6581 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006582
6583 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6584 /* free previously allocated memory */
6585 pool_free2(apools.sessid, sessid_temp);
6586
6587 if (asession != NULL) {
6588 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6589 if (!(t->be->options2 & PR_O2_AS_REQL))
6590 asession->request_count++;
6591
6592 if (asession->serverid != NULL) {
6593 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006594
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006595 while (srv) {
6596 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006597 if ((srv->state & SRV_RUNNING) ||
6598 (t->be->options & PR_O_PERSIST) ||
6599 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006600 /* we found the server and it's usable */
6601 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006602 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006603 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006604 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006605
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006606 break;
6607 } else {
6608 txn->flags &= ~TX_CK_MASK;
6609 txn->flags |= TX_CK_DOWN;
6610 }
6611 }
6612 srv = srv->next;
6613 }
6614 }
6615 }
6616}
6617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006618/* Find the end of a cookie value contained between <s> and <e>. It works the
6619 * same way as with headers above except that the semi-colon also ends a token.
6620 * See RFC2965 for more information. Note that it requires a valid header to
6621 * return a valid result.
6622 */
6623char *find_cookie_value_end(char *s, const char *e)
6624{
6625 int quoted, qdpair;
6626
6627 quoted = qdpair = 0;
6628 for (; s < e; s++) {
6629 if (qdpair) qdpair = 0;
6630 else if (quoted) {
6631 if (*s == '\\') qdpair = 1;
6632 else if (*s == '"') quoted = 0;
6633 }
6634 else if (*s == '"') quoted = 1;
6635 else if (*s == ',' || *s == ';') return s;
6636 }
6637 return s;
6638}
6639
6640/* Delete a value in a header between delimiters <from> and <next> in buffer
6641 * <buf>. The number of characters displaced is returned, and the pointer to
6642 * the first delimiter is updated if required. The function tries as much as
6643 * possible to respect the following principles :
6644 * - replace <from> delimiter by the <next> one unless <from> points to a
6645 * colon, in which case <next> is simply removed
6646 * - set exactly one space character after the new first delimiter, unless
6647 * there are not enough characters in the block being moved to do so.
6648 * - remove unneeded spaces before the previous delimiter and after the new
6649 * one.
6650 *
6651 * It is the caller's responsibility to ensure that :
6652 * - <from> points to a valid delimiter or the colon ;
6653 * - <next> points to a valid delimiter or the final CR/LF ;
6654 * - there are non-space chars before <from> ;
6655 * - there is a CR/LF at or after <next>.
6656 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006657int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658{
6659 char *prev = *from;
6660
6661 if (*prev == ':') {
6662 /* We're removing the first value, preserve the colon and add a
6663 * space if possible.
6664 */
6665 if (!http_is_crlf[(unsigned char)*next])
6666 next++;
6667 prev++;
6668 if (prev < next)
6669 *prev++ = ' ';
6670
6671 while (http_is_spht[(unsigned char)*next])
6672 next++;
6673 } else {
6674 /* Remove useless spaces before the old delimiter. */
6675 while (http_is_spht[(unsigned char)*(prev-1)])
6676 prev--;
6677 *from = prev;
6678
6679 /* copy the delimiter and if possible a space if we're
6680 * not at the end of the line.
6681 */
6682 if (!http_is_crlf[(unsigned char)*next]) {
6683 *prev++ = *next++;
6684 if (prev + 1 < next)
6685 *prev++ = ' ';
6686 while (http_is_spht[(unsigned char)*next])
6687 next++;
6688 }
6689 }
6690 return buffer_replace2(buf, prev, next, NULL, 0);
6691}
6692
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006693/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006694 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 * desirable to call it only when needed. This code is quite complex because
6696 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6697 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006699void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006700{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006701 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006703 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006704 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6705 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006708 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006709 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006711 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006713 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006715 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006716 hdr_beg = hdr_next;
6717 hdr_end = hdr_beg + cur_hdr->len;
6718 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006719
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006720 /* We have one full header between hdr_beg and hdr_end, and the
6721 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722 * "Cookie:" headers.
6723 */
6724
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006725 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006726 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006727 old_idx = cur_idx;
6728 continue;
6729 }
6730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006731 del_from = NULL; /* nothing to be deleted */
6732 preserve_hdr = 0; /* assume we may kill the whole header */
6733
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734 /* Now look for cookies. Conforming to RFC2109, we have to support
6735 * attributes whose name begin with a '$', and associate them with
6736 * the right cookie, if we want to delete this cookie.
6737 * So there are 3 cases for each cookie read :
6738 * 1) it's a special attribute, beginning with a '$' : ignore it.
6739 * 2) it's a server id cookie that we *MAY* want to delete : save
6740 * some pointers on it (last semi-colon, beginning of cookie...)
6741 * 3) it's an application cookie : we *MAY* have to delete a previous
6742 * "special" cookie.
6743 * At the end of loop, if a "special" cookie remains, we may have to
6744 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006745 * *MUST* delete it.
6746 *
6747 * Note: RFC2965 is unclear about the processing of spaces around
6748 * the equal sign in the ATTR=VALUE form. A careful inspection of
6749 * the RFC explicitly allows spaces before it, and not within the
6750 * tokens (attrs or values). An inspection of RFC2109 allows that
6751 * too but section 10.1.3 lets one think that spaces may be allowed
6752 * after the equal sign too, resulting in some (rare) buggy
6753 * implementations trying to do that. So let's do what servers do.
6754 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6755 * allowed quoted strings in values, with any possible character
6756 * after a backslash, including control chars and delimitors, which
6757 * causes parsing to become ambiguous. Browsers also allow spaces
6758 * within values even without quotes.
6759 *
6760 * We have to keep multiple pointers in order to support cookie
6761 * removal at the beginning, middle or end of header without
6762 * corrupting the header. All of these headers are valid :
6763 *
6764 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6765 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6766 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6767 * | | | | | | | | |
6768 * | | | | | | | | hdr_end <--+
6769 * | | | | | | | +--> next
6770 * | | | | | | +----> val_end
6771 * | | | | | +-----------> val_beg
6772 * | | | | +--------------> equal
6773 * | | | +----------------> att_end
6774 * | | +---------------------> att_beg
6775 * | +--------------------------> prev
6776 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006777 */
6778
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006779 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6780 /* Iterate through all cookies on this line */
6781
6782 /* find att_beg */
6783 att_beg = prev + 1;
6784 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6785 att_beg++;
6786
6787 /* find att_end : this is the first character after the last non
6788 * space before the equal. It may be equal to hdr_end.
6789 */
6790 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006791
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006792 while (equal < hdr_end) {
6793 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006794 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 if (http_is_spht[(unsigned char)*equal++])
6796 continue;
6797 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006798 }
6799
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006800 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6801 * is between <att_beg> and <equal>, both may be identical.
6802 */
6803
6804 /* look for end of cookie if there is an equal sign */
6805 if (equal < hdr_end && *equal == '=') {
6806 /* look for the beginning of the value */
6807 val_beg = equal + 1;
6808 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6809 val_beg++;
6810
6811 /* find the end of the value, respecting quotes */
6812 next = find_cookie_value_end(val_beg, hdr_end);
6813
6814 /* make val_end point to the first white space or delimitor after the value */
6815 val_end = next;
6816 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6817 val_end--;
6818 } else {
6819 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006821
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006822 /* We have nothing to do with attributes beginning with '$'. However,
6823 * they will automatically be removed if a header before them is removed,
6824 * since they're supposed to be linked together.
6825 */
6826 if (*att_beg == '$')
6827 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006828
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006829 /* Ignore cookies with no equal sign */
6830 if (equal == next) {
6831 /* This is not our cookie, so we must preserve it. But if we already
6832 * scheduled another cookie for removal, we cannot remove the
6833 * complete header, but we can remove the previous block itself.
6834 */
6835 preserve_hdr = 1;
6836 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006837 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 val_end += delta;
6839 next += delta;
6840 hdr_end += delta;
6841 hdr_next += delta;
6842 cur_hdr->len += delta;
6843 http_msg_move_end(&txn->req, delta);
6844 prev = del_from;
6845 del_from = NULL;
6846 }
6847 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006849
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 /* if there are spaces around the equal sign, we need to
6851 * strip them otherwise we'll get trouble for cookie captures,
6852 * or even for rewrites. Since this happens extremely rarely,
6853 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006854 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006855 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6856 int stripped_before = 0;
6857 int stripped_after = 0;
6858
6859 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006860 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 equal += stripped_before;
6862 val_beg += stripped_before;
6863 }
6864
6865 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006866 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006867 val_beg += stripped_after;
6868 stripped_before += stripped_after;
6869 }
6870
6871 val_end += stripped_before;
6872 next += stripped_before;
6873 hdr_end += stripped_before;
6874 hdr_next += stripped_before;
6875 cur_hdr->len += stripped_before;
6876 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006879
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006880 /* First, let's see if we want to capture this cookie. We check
6881 * that we don't already have a client side cookie, because we
6882 * can only capture one. Also as an optimisation, we ignore
6883 * cookies shorter than the declared name.
6884 */
6885 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6886 (val_end - att_beg >= t->fe->capture_namelen) &&
6887 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6888 int log_len = val_end - att_beg;
6889
6890 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6891 Alert("HTTP logging : out of memory.\n");
6892 } else {
6893 if (log_len > t->fe->capture_len)
6894 log_len = t->fe->capture_len;
6895 memcpy(txn->cli_cookie, att_beg, log_len);
6896 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006899
Willy Tarreaubca99692010-10-06 19:25:55 +02006900 /* Persistence cookies in passive, rewrite or insert mode have the
6901 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006903 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006905 * For cookies in prefix mode, the form is :
6906 *
6907 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 */
6909 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6910 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6911 struct server *srv = t->be->srv;
6912 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6915 * have the server ID between val_beg and delim, and the original cookie between
6916 * delim+1 and val_end. Otherwise, delim==val_end :
6917 *
6918 * Cookie: NAME=SRV; # in all but prefix modes
6919 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6920 * | || || | |+-> next
6921 * | || || | +--> val_end
6922 * | || || +---------> delim
6923 * | || |+------------> val_beg
6924 * | || +-------------> att_end = equal
6925 * | |+-----------------> att_beg
6926 * | +------------------> prev
6927 * +-------------------------> hdr_beg
6928 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006929
Willy Tarreau67402132012-05-31 20:40:20 +02006930 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006931 for (delim = val_beg; delim < val_end; delim++)
6932 if (*delim == COOKIE_DELIM)
6933 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006934 } else {
6935 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006937 /* Now check if the cookie contains a date field, which would
6938 * appear after a vertical bar ('|') just after the server name
6939 * and before the delimiter.
6940 */
6941 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6942 if (vbar1) {
6943 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006944 * right is the last seen date. It is a base64 encoded
6945 * 30-bit value representing the UNIX date since the
6946 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006947 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006948 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006949 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006950 if (val_end - vbar1 >= 5) {
6951 val = b64tos30(vbar1);
6952 if (val > 0)
6953 txn->cookie_last_date = val << 2;
6954 }
6955 /* look for a second vertical bar */
6956 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6957 if (vbar1 && (val_end - vbar1 > 5)) {
6958 val = b64tos30(vbar1 + 1);
6959 if (val > 0)
6960 txn->cookie_first_date = val << 2;
6961 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006962 }
6963 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964
Willy Tarreauf64d1412010-10-07 20:06:11 +02006965 /* if the cookie has an expiration date and the proxy wants to check
6966 * it, then we do that now. We first check if the cookie is too old,
6967 * then only if it has expired. We detect strict overflow because the
6968 * time resolution here is not great (4 seconds). Cookies with dates
6969 * in the future are ignored if their offset is beyond one day. This
6970 * allows an admin to fix timezone issues without expiring everyone
6971 * and at the same time avoids keeping unwanted side effects for too
6972 * long.
6973 */
6974 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006975 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6976 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006977 txn->flags &= ~TX_CK_MASK;
6978 txn->flags |= TX_CK_OLD;
6979 delim = val_beg; // let's pretend we have not found the cookie
6980 txn->cookie_first_date = 0;
6981 txn->cookie_last_date = 0;
6982 }
6983 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006984 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6985 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006986 txn->flags &= ~TX_CK_MASK;
6987 txn->flags |= TX_CK_EXPIRED;
6988 delim = val_beg; // let's pretend we have not found the cookie
6989 txn->cookie_first_date = 0;
6990 txn->cookie_last_date = 0;
6991 }
6992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 /* Here, we'll look for the first running server which supports the cookie.
6994 * This allows to share a same cookie between several servers, for example
6995 * to dedicate backup servers to specific servers only.
6996 * However, to prevent clients from sticking to cookie-less backup server
6997 * when they have incidentely learned an empty cookie, we simply ignore
6998 * empty cookies and mark them as invalid.
6999 * The same behaviour is applied when persistence must be ignored.
7000 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007001 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007002 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007004 while (srv) {
7005 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7006 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7007 if ((srv->state & SRV_RUNNING) ||
7008 (t->be->options & PR_O_PERSIST) ||
7009 (t->flags & SN_FORCE_PRST)) {
7010 /* we found the server and we can use it */
7011 txn->flags &= ~TX_CK_MASK;
7012 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7013 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007014 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007015 break;
7016 } else {
7017 /* we found a server, but it's down,
7018 * mark it as such and go on in case
7019 * another one is available.
7020 */
7021 txn->flags &= ~TX_CK_MASK;
7022 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007023 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007024 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007025 srv = srv->next;
7026 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007027
Willy Tarreauf64d1412010-10-07 20:06:11 +02007028 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007029 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007030 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007031 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7032 txn->flags |= TX_CK_UNUSED;
7033 else
7034 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007036
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 /* depending on the cookie mode, we may have to either :
7038 * - delete the complete cookie if we're in insert+indirect mode, so that
7039 * the server never sees it ;
7040 * - remove the server id from the cookie value, and tag the cookie as an
7041 * application cookie so that it does not get accidentely removed later,
7042 * if we're in cookie prefix mode
7043 */
Willy Tarreau67402132012-05-31 20:40:20 +02007044 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007046
Willy Tarreau9b28e032012-10-12 23:49:43 +02007047 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 val_end += delta;
7049 next += delta;
7050 hdr_end += delta;
7051 hdr_next += delta;
7052 cur_hdr->len += delta;
7053 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 del_from = NULL;
7056 preserve_hdr = 1; /* we want to keep this cookie */
7057 }
7058 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007059 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 del_from = prev;
7061 }
7062 } else {
7063 /* This is not our cookie, so we must preserve it. But if we already
7064 * scheduled another cookie for removal, we cannot remove the
7065 * complete header, but we can remove the previous block itself.
7066 */
7067 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007070 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007071 if (att_beg >= del_from)
7072 att_beg += delta;
7073 if (att_end >= del_from)
7074 att_end += delta;
7075 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007076 val_end += delta;
7077 next += delta;
7078 hdr_end += delta;
7079 hdr_next += delta;
7080 cur_hdr->len += delta;
7081 http_msg_move_end(&txn->req, delta);
7082 prev = del_from;
7083 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 /* Look for the appsession cookie unless persistence must be ignored */
7088 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7089 int cmp_len, value_len;
7090 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 if (t->be->options2 & PR_O2_AS_PFX) {
7093 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7094 value_begin = att_beg + t->be->appsession_name_len;
7095 value_len = val_end - att_beg - t->be->appsession_name_len;
7096 } else {
7097 cmp_len = att_end - att_beg;
7098 value_begin = val_beg;
7099 value_len = val_end - val_beg;
7100 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007101
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 /* let's see if the cookie is our appcookie */
7103 if (cmp_len == t->be->appsession_name_len &&
7104 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7105 manage_client_side_appsession(t, value_begin, value_len);
7106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107 }
7108
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007109 /* continue with next cookie on this header line */
7110 att_beg = next;
7111 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 /* There are no more cookies on this line.
7114 * We may still have one (or several) marked for deletion at the
7115 * end of the line. We must do this now in two ways :
7116 * - if some cookies must be preserved, we only delete from the
7117 * mark to the end of line ;
7118 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007122 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007123 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125 cur_hdr->len += delta;
7126 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007127 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128
7129 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007130 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7131 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007133 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007136 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137 }
7138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007139 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007140 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007141 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007142}
7143
7144
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145/* Iterate the same filter through all response headers contained in <rtr>.
7146 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7147 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007148int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149{
7150 char term;
7151 char *cur_ptr, *cur_end, *cur_next;
7152 int cur_idx, old_idx, last_hdr;
7153 struct http_txn *txn = &t->txn;
7154 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007155 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156
7157 last_hdr = 0;
7158
Willy Tarreau9b28e032012-10-12 23:49:43 +02007159 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160 old_idx = 0;
7161
7162 while (!last_hdr) {
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
7170 cur_idx = txn->hdr_idx.v[old_idx].next;
7171 if (!cur_idx)
7172 break;
7173
7174 cur_hdr = &txn->hdr_idx.v[cur_idx];
7175 cur_ptr = cur_next;
7176 cur_end = cur_ptr + cur_hdr->len;
7177 cur_next = cur_end + cur_hdr->cr + 1;
7178
7179 /* Now we have one header between cur_ptr and cur_end,
7180 * and the next header starts at cur_next.
7181 */
7182
7183 /* The annoying part is that pattern matching needs
7184 * that we modify the contents to null-terminate all
7185 * strings before testing them.
7186 */
7187
7188 term = *cur_end;
7189 *cur_end = '\0';
7190
7191 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7192 switch (exp->action) {
7193 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007194 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 last_hdr = 1;
7196 break;
7197
7198 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007199 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 last_hdr = 1;
7201 break;
7202
7203 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007204 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7205 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 /* FIXME: if the user adds a newline in the replacement, the
7207 * index will not be recalculated for now, and the new line
7208 * will not be counted as a new header.
7209 */
7210
7211 cur_end += delta;
7212 cur_next += delta;
7213 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007214 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215 break;
7216
7217 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007218 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 cur_next += delta;
7220
Willy Tarreaufa355d42009-11-29 18:12:29 +01007221 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7223 txn->hdr_idx.used--;
7224 cur_hdr->len = 0;
7225 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007226 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 break;
7228
7229 }
7230 }
7231 if (cur_end)
7232 *cur_end = term; /* restore the string terminator */
7233
7234 /* keep the link from this header to next one in case of later
7235 * removal of next header.
7236 */
7237 old_idx = cur_idx;
7238 }
7239 return 0;
7240}
7241
7242
7243/* Apply the filter to the status line in the response buffer <rtr>.
7244 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7245 * or -1 if a replacement resulted in an invalid status line.
7246 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007247int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248{
7249 char term;
7250 char *cur_ptr, *cur_end;
7251 int done;
7252 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007253 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254
7255
Willy Tarreau3d300592007-03-18 18:34:41 +01007256 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007258 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259 (exp->action == ACT_ALLOW ||
7260 exp->action == ACT_DENY))
7261 return 0;
7262 else if (exp->action == ACT_REMOVE)
7263 return 0;
7264
7265 done = 0;
7266
Willy Tarreau9b28e032012-10-12 23:49:43 +02007267 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007268 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269
7270 /* Now we have the status line between cur_ptr and cur_end */
7271
7272 /* The annoying part is that pattern matching needs
7273 * that we modify the contents to null-terminate all
7274 * strings before testing them.
7275 */
7276
7277 term = *cur_end;
7278 *cur_end = '\0';
7279
7280 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7281 switch (exp->action) {
7282 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 done = 1;
7285 break;
7286
7287 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007288 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 done = 1;
7290 break;
7291
7292 case ACT_REPLACE:
7293 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007294 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7295 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296 /* FIXME: if the user adds a newline in the replacement, the
7297 * index will not be recalculated for now, and the new line
7298 * will not be counted as a new header.
7299 */
7300
Willy Tarreaufa355d42009-11-29 18:12:29 +01007301 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007303 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007304 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 cur_ptr, cur_end + 1,
7306 NULL, NULL);
7307 if (unlikely(!cur_end))
7308 return -1;
7309
7310 /* we have a full respnse and we know that we have either a CR
7311 * or an LF at <ptr>.
7312 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007313 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007314 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 /* there is no point trying this regex on headers */
7316 return 1;
7317 }
7318 }
7319 *cur_end = term; /* restore the string terminator */
7320 return done;
7321}
7322
7323
7324
7325/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007326 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7328 * unparsable response.
7329 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007330int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007332 struct http_txn *txn = &s->txn;
7333 struct hdr_exp *exp;
7334
7335 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 int ret;
7337
7338 /*
7339 * The interleaving of transformations and verdicts
7340 * makes it difficult to decide to continue or stop
7341 * the evaluation.
7342 */
7343
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007344 if (txn->flags & TX_SVDENY)
7345 break;
7346
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7349 exp->action == ACT_PASS)) {
7350 exp = exp->next;
7351 continue;
7352 }
7353
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007354 /* if this filter had a condition, evaluate it now and skip to
7355 * next filter if the condition does not match.
7356 */
7357 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007358 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007359 ret = acl_pass(ret);
7360 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7361 ret = !ret;
7362 if (!ret)
7363 continue;
7364 }
7365
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007367 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 if (unlikely(ret < 0))
7369 return -1;
7370
7371 if (likely(ret == 0)) {
7372 /* The filter did not match the response, it can be
7373 * iterated through all headers.
7374 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007375 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377 }
7378 return 0;
7379}
7380
7381
Willy Tarreaua15645d2007-03-18 16:22:39 +01007382/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007383 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007384 * desirable to call it only when needed. This function is also used when we
7385 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007387void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388{
7389 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007390 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007391 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007393 char *hdr_beg, *hdr_end, *hdr_next;
7394 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007395
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 /* Iterate through the headers.
7397 * we start with the start line.
7398 */
7399 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007400 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401
7402 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7403 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007404 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405
7406 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007407 hdr_beg = hdr_next;
7408 hdr_end = hdr_beg + cur_hdr->len;
7409 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007410
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 /* We have one full header between hdr_beg and hdr_end, and the
7412 * next header starts at hdr_next. We're only interested in
7413 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 */
7415
Willy Tarreau24581ba2010-08-31 22:39:35 +02007416 is_cookie2 = 0;
7417 prev = hdr_beg + 10;
7418 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007419 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007420 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7421 if (!val) {
7422 old_idx = cur_idx;
7423 continue;
7424 }
7425 is_cookie2 = 1;
7426 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 }
7428
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7430 * <prev> points to the colon.
7431 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007432 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007433
Willy Tarreau24581ba2010-08-31 22:39:35 +02007434 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7435 * check-cache is enabled) and we are not interested in checking
7436 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007437 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007438 if (t->be->cookie_name == NULL &&
7439 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007440 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441 return;
7442
Willy Tarreau24581ba2010-08-31 22:39:35 +02007443 /* OK so now we know we have to process this response cookie.
7444 * The format of the Set-Cookie header is slightly different
7445 * from the format of the Cookie header in that it does not
7446 * support the comma as a cookie delimiter (thus the header
7447 * cannot be folded) because the Expires attribute described in
7448 * the original Netscape's spec may contain an unquoted date
7449 * with a comma inside. We have to live with this because
7450 * many browsers don't support Max-Age and some browsers don't
7451 * support quoted strings. However the Set-Cookie2 header is
7452 * clean.
7453 *
7454 * We have to keep multiple pointers in order to support cookie
7455 * removal at the beginning, middle or end of header without
7456 * corrupting the header (in case of set-cookie2). A special
7457 * pointer, <scav> points to the beginning of the set-cookie-av
7458 * fields after the first semi-colon. The <next> pointer points
7459 * either to the end of line (set-cookie) or next unquoted comma
7460 * (set-cookie2). All of these headers are valid :
7461 *
7462 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7463 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7464 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7465 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7466 * | | | | | | | | | |
7467 * | | | | | | | | +-> next hdr_end <--+
7468 * | | | | | | | +------------> scav
7469 * | | | | | | +--------------> val_end
7470 * | | | | | +--------------------> val_beg
7471 * | | | | +----------------------> equal
7472 * | | | +------------------------> att_end
7473 * | | +----------------------------> att_beg
7474 * | +------------------------------> prev
7475 * +-----------------------------------------> hdr_beg
7476 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477
Willy Tarreau24581ba2010-08-31 22:39:35 +02007478 for (; prev < hdr_end; prev = next) {
7479 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480
Willy Tarreau24581ba2010-08-31 22:39:35 +02007481 /* find att_beg */
7482 att_beg = prev + 1;
7483 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7484 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485
Willy Tarreau24581ba2010-08-31 22:39:35 +02007486 /* find att_end : this is the first character after the last non
7487 * space before the equal. It may be equal to hdr_end.
7488 */
7489 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 while (equal < hdr_end) {
7492 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7493 break;
7494 if (http_is_spht[(unsigned char)*equal++])
7495 continue;
7496 att_end = equal;
7497 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7500 * is between <att_beg> and <equal>, both may be identical.
7501 */
7502
7503 /* look for end of cookie if there is an equal sign */
7504 if (equal < hdr_end && *equal == '=') {
7505 /* look for the beginning of the value */
7506 val_beg = equal + 1;
7507 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7508 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 /* find the end of the value, respecting quotes */
7511 next = find_cookie_value_end(val_beg, hdr_end);
7512
7513 /* make val_end point to the first white space or delimitor after the value */
7514 val_end = next;
7515 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7516 val_end--;
7517 } else {
7518 /* <equal> points to next comma, semi-colon or EOL */
7519 val_beg = val_end = next = equal;
7520 }
7521
7522 if (next < hdr_end) {
7523 /* Set-Cookie2 supports multiple cookies, and <next> points to
7524 * a colon or semi-colon before the end. So skip all attr-value
7525 * pairs and look for the next comma. For Set-Cookie, since
7526 * commas are permitted in values, skip to the end.
7527 */
7528 if (is_cookie2)
7529 next = find_hdr_value_end(next, hdr_end);
7530 else
7531 next = hdr_end;
7532 }
7533
7534 /* Now everything is as on the diagram above */
7535
7536 /* Ignore cookies with no equal sign */
7537 if (equal == val_end)
7538 continue;
7539
7540 /* If there are spaces around the equal sign, we need to
7541 * strip them otherwise we'll get trouble for cookie captures,
7542 * or even for rewrites. Since this happens extremely rarely,
7543 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007545 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7546 int stripped_before = 0;
7547 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007548
Willy Tarreau24581ba2010-08-31 22:39:35 +02007549 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007550 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007551 equal += stripped_before;
7552 val_beg += stripped_before;
7553 }
7554
7555 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007556 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 val_beg += stripped_after;
7558 stripped_before += stripped_after;
7559 }
7560
7561 val_end += stripped_before;
7562 next += stripped_before;
7563 hdr_end += stripped_before;
7564 hdr_next += stripped_before;
7565 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007566 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007567 }
7568
7569 /* First, let's see if we want to capture this cookie. We check
7570 * that we don't already have a server side cookie, because we
7571 * can only capture one. Also as an optimisation, we ignore
7572 * cookies shorter than the declared name.
7573 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007574 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007575 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007576 (val_end - att_beg >= t->fe->capture_namelen) &&
7577 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7578 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007579 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 Alert("HTTP logging : out of memory.\n");
7581 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007582 else {
7583 if (log_len > t->fe->capture_len)
7584 log_len = t->fe->capture_len;
7585 memcpy(txn->srv_cookie, att_beg, log_len);
7586 txn->srv_cookie[log_len] = 0;
7587 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588 }
7589
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007590 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007592 if (!(t->flags & SN_IGNORE_PRST) &&
7593 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7594 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007595 /* assume passive cookie by default */
7596 txn->flags &= ~TX_SCK_MASK;
7597 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598
7599 /* If the cookie is in insert mode on a known server, we'll delete
7600 * this occurrence because we'll insert another one later.
7601 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 * a direct access.
7603 */
Willy Tarreau67402132012-05-31 20:40:20 +02007604 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007605 /* The "preserve" flag was set, we don't want to touch the
7606 * server's cookie.
7607 */
7608 }
Willy Tarreau67402132012-05-31 20:40:20 +02007609 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7610 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007611 /* this cookie must be deleted */
7612 if (*prev == ':' && next == hdr_end) {
7613 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007614 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007615 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7616 txn->hdr_idx.used--;
7617 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007618 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 hdr_next += delta;
7620 http_msg_move_end(&txn->rsp, delta);
7621 /* note: while both invalid now, <next> and <hdr_end>
7622 * are still equal, so the for() will stop as expected.
7623 */
7624 } else {
7625 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007626 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007627 next = prev;
7628 hdr_end += delta;
7629 hdr_next += delta;
7630 cur_hdr->len += delta;
7631 http_msg_move_end(&txn->rsp, delta);
7632 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007633 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007634 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007635 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007636 }
Willy Tarreau67402132012-05-31 20:40:20 +02007637 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007638 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639 * with this server since we know it.
7640 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007641 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 next += delta;
7643 hdr_end += delta;
7644 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007646 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647
Willy Tarreauf1348312010-10-07 15:54:11 +02007648 txn->flags &= ~TX_SCK_MASK;
7649 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007651 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007653 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007655 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 next += delta;
7657 hdr_end += delta;
7658 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007660 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661
Willy Tarreau827aee92011-03-10 16:55:02 +01007662 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007663 txn->flags &= ~TX_SCK_MASK;
7664 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 }
7666 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007667 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7668 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007669 int cmp_len, value_len;
7670 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671
Cyril Bontéb21570a2009-11-29 20:04:48 +01007672 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007673 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7674 value_begin = att_beg + t->be->appsession_name_len;
7675 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007676 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007677 cmp_len = att_end - att_beg;
7678 value_begin = val_beg;
7679 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007680 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007681
Cyril Bonté17530c32010-04-06 21:11:10 +02007682 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007683 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7684 /* free a possibly previously allocated memory */
7685 pool_free2(apools.sessid, txn->sessid);
7686
Cyril Bontéb21570a2009-11-29 20:04:48 +01007687 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007688 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007689 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7690 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7691 return;
7692 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007693 memcpy(txn->sessid, value_begin, value_len);
7694 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007696 }
7697 /* that's done for this cookie, check the next one on the same
7698 * line when next != hdr_end (only if is_cookie2).
7699 */
7700 }
7701 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007703 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007704
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007705 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007706 appsess *asession = NULL;
7707 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007708 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007709 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007710 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007711 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7712 Alert("Not enough Memory process_srv():asession:calloc().\n");
7713 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7714 return;
7715 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007716 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7717
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007718 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7719 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7720 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007721 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007722 return;
7723 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007724 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007725 asession->sessid[t->be->appsession_len] = 0;
7726
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007727 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007728 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007729 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007730 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007731 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007732 return;
7733 }
7734 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007735 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007736
7737 asession->request_count = 0;
7738 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7739 }
7740
7741 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7742 asession->request_count++;
7743 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007744}
7745
7746
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747/*
7748 * Check if response is cacheable or not. Updates t->flags.
7749 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007750void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751{
7752 struct http_txn *txn = &t->txn;
7753 char *p1, *p2;
7754
7755 char *cur_ptr, *cur_end, *cur_next;
7756 int cur_idx;
7757
Willy Tarreau5df51872007-11-25 16:20:08 +01007758 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 return;
7760
7761 /* Iterate through the headers.
7762 * we start with the start line.
7763 */
7764 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007765 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007766
7767 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7768 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007769 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770
7771 cur_hdr = &txn->hdr_idx.v[cur_idx];
7772 cur_ptr = cur_next;
7773 cur_end = cur_ptr + cur_hdr->len;
7774 cur_next = cur_end + cur_hdr->cr + 1;
7775
7776 /* We have one full header between cur_ptr and cur_end, and the
7777 * next header starts at cur_next. We're only interested in
7778 * "Cookie:" headers.
7779 */
7780
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007781 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7782 if (val) {
7783 if ((cur_end - (cur_ptr + val) >= 8) &&
7784 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7785 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7786 return;
7787 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788 }
7789
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007790 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7791 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792 continue;
7793
7794 /* OK, right now we know we have a cache-control header at cur_ptr */
7795
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007796 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797
7798 if (p1 >= cur_end) /* no more info */
7799 continue;
7800
7801 /* p1 is at the beginning of the value */
7802 p2 = p1;
7803
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007804 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 p2++;
7806
7807 /* we have a complete value between p1 and p2 */
7808 if (p2 < cur_end && *p2 == '=') {
7809 /* we have something of the form no-cache="set-cookie" */
7810 if ((cur_end - p1 >= 21) &&
7811 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7812 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007813 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814 continue;
7815 }
7816
7817 /* OK, so we know that either p2 points to the end of string or to a comma */
7818 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007819 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7821 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7822 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007823 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 return;
7825 }
7826
7827 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007828 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 continue;
7830 }
7831 }
7832}
7833
7834
Willy Tarreau58f10d72006-12-04 02:26:12 +01007835/*
7836 * Try to retrieve a known appsession in the URI, then the associated server.
7837 * If the server is found, it's assigned to the session.
7838 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007839void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007840{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007841 char *end_params, *first_param, *cur_param, *next_param;
7842 char separator;
7843 int value_len;
7844
7845 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007847 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007848 (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 +01007849 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007851
Cyril Bontéb21570a2009-11-29 20:04:48 +01007852 first_param = NULL;
7853 switch (mode) {
7854 case PR_O2_AS_M_PP:
7855 first_param = memchr(begin, ';', len);
7856 break;
7857 case PR_O2_AS_M_QS:
7858 first_param = memchr(begin, '?', len);
7859 break;
7860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007861
Cyril Bontéb21570a2009-11-29 20:04:48 +01007862 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007863 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007865
Cyril Bontéb21570a2009-11-29 20:04:48 +01007866 switch (mode) {
7867 case PR_O2_AS_M_PP:
7868 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7869 end_params = (char *) begin + len;
7870 }
7871 separator = ';';
7872 break;
7873 case PR_O2_AS_M_QS:
7874 end_params = (char *) begin + len;
7875 separator = '&';
7876 break;
7877 default:
7878 /* unknown mode, shouldn't happen */
7879 return;
7880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007881
Cyril Bontéb21570a2009-11-29 20:04:48 +01007882 cur_param = next_param = end_params;
7883 while (cur_param > first_param) {
7884 cur_param--;
7885 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7886 /* let's see if this is the appsession parameter */
7887 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7888 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7889 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7890 /* Cool... it's the right one */
7891 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7892 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7893 if (value_len > 0) {
7894 manage_client_side_appsession(t, cur_param, value_len);
7895 }
7896 break;
7897 }
7898 next_param = cur_param;
7899 }
7900 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007901#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007902 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007903 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007904#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905}
7906
Willy Tarreaub2513902006-12-17 14:52:38 +01007907/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007908 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007909 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007910 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007911 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007912 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007913 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007914 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007915 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007916int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007917{
7918 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007919 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007920 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007921
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007922 if (!uri_auth)
7923 return 0;
7924
Cyril Bonté70be45d2010-10-12 00:14:35 +02007925 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007926 return 0;
7927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007928 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007929 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007930 return 0;
7931
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007932 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007933 return 0;
7934
Willy Tarreaub2513902006-12-17 14:52:38 +01007935 return 1;
7936}
7937
Willy Tarreau4076a152009-04-02 15:18:36 +02007938/*
7939 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007940 * By default it tries to report the error position as msg->err_pos. However if
7941 * this one is not set, it will then report msg->next, which is the last known
7942 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007943 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007944 */
7945void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007946 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007947 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007948{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007949 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007950 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007951
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 es->len = MIN(chn->buf->i, sizeof(es->buf));
7953 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007954 len1 = MIN(len1, es->len);
7955 len2 = es->len - len1; /* remaining data if buffer wraps */
7956
Willy Tarreau9b28e032012-10-12 23:49:43 +02007957 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007958 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007959 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007960
Willy Tarreau4076a152009-04-02 15:18:36 +02007961 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007962 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007963 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007964 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007965
Willy Tarreau4076a152009-04-02 15:18:36 +02007966 es->when = date; // user-visible date
7967 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007968 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007969 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007970 if (objt_conn(s->req->prod->end))
7971 es->src = __objt_conn(s->req->prod->end)->addr.from;
7972 else
7973 memset(&es->src, 0, sizeof(es->src));
7974
Willy Tarreau078272e2010-12-12 12:46:33 +01007975 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007976 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007977 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007978 es->s_flags = s->flags;
7979 es->t_flags = s->txn.flags;
7980 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007981 es->b_out = chn->buf->o;
7982 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007983 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007984 es->m_clen = msg->chunk_len;
7985 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007986}
Willy Tarreaub2513902006-12-17 14:52:38 +01007987
Willy Tarreau294c4732011-12-16 21:35:50 +01007988/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7989 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7990 * performed over the whole headers. Otherwise it must contain a valid header
7991 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7992 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7993 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7994 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007995 * -1. The value fetch stops at commas, so this function is suited for use with
7996 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007997 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007998 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007999unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008000 struct hdr_idx *idx, int occ,
8001 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008002{
Willy Tarreau294c4732011-12-16 21:35:50 +01008003 struct hdr_ctx local_ctx;
8004 char *ptr_hist[MAX_HDR_HISTORY];
8005 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008006 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008007 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008008
Willy Tarreau294c4732011-12-16 21:35:50 +01008009 if (!ctx) {
8010 local_ctx.idx = 0;
8011 ctx = &local_ctx;
8012 }
8013
Willy Tarreaubce70882009-09-07 11:51:47 +02008014 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008015 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008016 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008017 occ--;
8018 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008019 *vptr = ctx->line + ctx->val;
8020 *vlen = ctx->vlen;
8021 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008022 }
8023 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008024 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008025 }
8026
8027 /* negative occurrence, we scan all the list then walk back */
8028 if (-occ > MAX_HDR_HISTORY)
8029 return 0;
8030
Willy Tarreau294c4732011-12-16 21:35:50 +01008031 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008032 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008033 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8034 len_hist[hist_ptr] = ctx->vlen;
8035 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008036 hist_ptr = 0;
8037 found++;
8038 }
8039 if (-occ > found)
8040 return 0;
8041 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008042 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8043 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8044 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008045 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008046 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008047 if (hist_ptr >= MAX_HDR_HISTORY)
8048 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008049 *vptr = ptr_hist[hist_ptr];
8050 *vlen = len_hist[hist_ptr];
8051 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008052}
8053
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008054/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8055 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8056 * performed over the whole headers. Otherwise it must contain a valid header
8057 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8058 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8059 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8060 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8061 * -1. This function differs from http_get_hdr() in that it only returns full
8062 * line header values and does not stop at commas.
8063 * The return value is 0 if nothing was found, or non-zero otherwise.
8064 */
8065unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8066 struct hdr_idx *idx, int occ,
8067 struct hdr_ctx *ctx, char **vptr, int *vlen)
8068{
8069 struct hdr_ctx local_ctx;
8070 char *ptr_hist[MAX_HDR_HISTORY];
8071 int len_hist[MAX_HDR_HISTORY];
8072 unsigned int hist_ptr;
8073 int found;
8074
8075 if (!ctx) {
8076 local_ctx.idx = 0;
8077 ctx = &local_ctx;
8078 }
8079
8080 if (occ >= 0) {
8081 /* search from the beginning */
8082 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8083 occ--;
8084 if (occ <= 0) {
8085 *vptr = ctx->line + ctx->val;
8086 *vlen = ctx->vlen;
8087 return 1;
8088 }
8089 }
8090 return 0;
8091 }
8092
8093 /* negative occurrence, we scan all the list then walk back */
8094 if (-occ > MAX_HDR_HISTORY)
8095 return 0;
8096
8097 found = hist_ptr = 0;
8098 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8099 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8100 len_hist[hist_ptr] = ctx->vlen;
8101 if (++hist_ptr >= MAX_HDR_HISTORY)
8102 hist_ptr = 0;
8103 found++;
8104 }
8105 if (-occ > found)
8106 return 0;
8107 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8108 * find occurrence -occ, so we have to check [hist_ptr+occ].
8109 */
8110 hist_ptr += occ;
8111 if (hist_ptr >= MAX_HDR_HISTORY)
8112 hist_ptr -= MAX_HDR_HISTORY;
8113 *vptr = ptr_hist[hist_ptr];
8114 *vlen = len_hist[hist_ptr];
8115 return 1;
8116}
8117
Willy Tarreaubaaee002006-06-26 02:48:02 +02008118/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008119 * Print a debug line with a header. Always stop at the first CR or LF char,
8120 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8121 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008122 */
8123void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8124{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008125 int max;
8126 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008127 dir,
8128 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8129 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 +02008130
8131 for (max = 0; start + max < end; max++)
8132 if (start[max] == '\r' || start[max] == '\n')
8133 break;
8134
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008135 UBOUND(max, trash.size - trash.len - 3);
8136 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8137 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008138 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008139}
8140
Willy Tarreau0937bc42009-12-22 15:03:09 +01008141/*
8142 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8143 * the required fields are properly allocated and that we only need to (re)init
8144 * them. This should be used before processing any new request.
8145 */
8146void http_init_txn(struct session *s)
8147{
8148 struct http_txn *txn = &s->txn;
8149 struct proxy *fe = s->fe;
8150
8151 txn->flags = 0;
8152 txn->status = -1;
8153
Willy Tarreauf64d1412010-10-07 20:06:11 +02008154 txn->cookie_first_date = 0;
8155 txn->cookie_last_date = 0;
8156
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008157 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008158 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008159 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008160 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008161 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008162 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008163 txn->req.chunk_len = 0LL;
8164 txn->req.body_len = 0LL;
8165 txn->rsp.chunk_len = 0LL;
8166 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008167 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8168 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008169 txn->req.chn = s->req;
8170 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008171
8172 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008173
8174 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8175 if (fe->options2 & PR_O2_REQBUG_OK)
8176 txn->req.err_pos = -1; /* let buggy requests pass */
8177
Willy Tarreau46023632010-01-07 22:51:47 +01008178 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008179 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8180
Willy Tarreau46023632010-01-07 22:51:47 +01008181 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008182 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8183
8184 if (txn->hdr_idx.v)
8185 hdr_idx_init(&txn->hdr_idx);
8186}
8187
8188/* to be used at the end of a transaction */
8189void http_end_txn(struct session *s)
8190{
8191 struct http_txn *txn = &s->txn;
8192
8193 /* these ones will have been dynamically allocated */
8194 pool_free2(pool2_requri, txn->uri);
8195 pool_free2(pool2_capture, txn->cli_cookie);
8196 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008197 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008198 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008199
William Lallemanda73203e2012-03-12 12:48:57 +01008200 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008201 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008202 txn->uri = NULL;
8203 txn->srv_cookie = NULL;
8204 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008205
8206 if (txn->req.cap) {
8207 struct cap_hdr *h;
8208 for (h = s->fe->req_cap; h; h = h->next)
8209 pool_free2(h->pool, txn->req.cap[h->index]);
8210 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8211 }
8212
8213 if (txn->rsp.cap) {
8214 struct cap_hdr *h;
8215 for (h = s->fe->rsp_cap; h; h = h->next)
8216 pool_free2(h->pool, txn->rsp.cap[h->index]);
8217 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8218 }
8219
Willy Tarreau0937bc42009-12-22 15:03:09 +01008220}
8221
8222/* to be used at the end of a transaction to prepare a new one */
8223void http_reset_txn(struct session *s)
8224{
8225 http_end_txn(s);
8226 http_init_txn(s);
8227
8228 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008229 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008230 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008231 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008232 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008233 /* re-init store persistence */
8234 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008235 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008236
Willy Tarreau0937bc42009-12-22 15:03:09 +01008237 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008238
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008239 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008240
Willy Tarreau739cfba2010-01-25 23:11:14 +01008241 /* We must trim any excess data from the response buffer, because we
8242 * may have blocked an invalid response from a server that we don't
8243 * want to accidentely forward once we disable the analysers, nor do
8244 * we want those data to come along with next response. A typical
8245 * example of such data would be from a buggy server responding to
8246 * a HEAD with some data, or sending more than the advertised
8247 * content-length.
8248 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008249 if (unlikely(s->rep->buf->i))
8250 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008251
Willy Tarreau0937bc42009-12-22 15:03:09 +01008252 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008253 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008254
Willy Tarreaud04e8582010-05-31 12:31:35 +02008255 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008256 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008257
8258 s->req->rex = TICK_ETERNITY;
8259 s->req->wex = TICK_ETERNITY;
8260 s->req->analyse_exp = TICK_ETERNITY;
8261 s->rep->rex = TICK_ETERNITY;
8262 s->rep->wex = TICK_ETERNITY;
8263 s->rep->analyse_exp = TICK_ETERNITY;
8264}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008265
Willy Tarreauff011f22011-01-06 17:51:27 +01008266void free_http_req_rules(struct list *r) {
8267 struct http_req_rule *tr, *pr;
8268
8269 list_for_each_entry_safe(pr, tr, r, list) {
8270 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008271 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008272 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008273
8274 free(pr);
8275 }
8276}
8277
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008278/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008279struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8280{
8281 struct http_req_rule *rule;
8282 int cur_arg;
8283
8284 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8285 if (!rule) {
8286 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008287 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008288 }
8289
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008290 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008291 rule->action = HTTP_REQ_ACT_ALLOW;
8292 cur_arg = 1;
8293 } else if (!strcmp(args[0], "deny")) {
8294 rule->action = HTTP_REQ_ACT_DENY;
8295 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008296 } else if (!strcmp(args[0], "tarpit")) {
8297 rule->action = HTTP_REQ_ACT_TARPIT;
8298 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008299 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008300 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008301 cur_arg = 1;
8302
8303 while(*args[cur_arg]) {
8304 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008305 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008306 cur_arg+=2;
8307 continue;
8308 } else
8309 break;
8310 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008311 } else if (!strcmp(args[0], "set-nice")) {
8312 rule->action = HTTP_REQ_ACT_SET_NICE;
8313 cur_arg = 1;
8314
8315 if (!*args[cur_arg] ||
8316 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8317 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8318 file, linenum, args[0]);
8319 goto out_err;
8320 }
8321 rule->arg.nice = atoi(args[cur_arg]);
8322 if (rule->arg.nice < -1024)
8323 rule->arg.nice = -1024;
8324 else if (rule->arg.nice > 1024)
8325 rule->arg.nice = 1024;
8326 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008327 } else if (!strcmp(args[0], "set-tos")) {
8328#ifdef IP_TOS
8329 char *err;
8330 rule->action = HTTP_REQ_ACT_SET_TOS;
8331 cur_arg = 1;
8332
8333 if (!*args[cur_arg] ||
8334 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8335 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8336 file, linenum, args[0]);
8337 goto out_err;
8338 }
8339
8340 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8341 if (err && *err != '\0') {
8342 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8343 file, linenum, err, args[0]);
8344 goto out_err;
8345 }
8346 cur_arg++;
8347#else
8348 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8349 goto out_err;
8350#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008351 } else if (!strcmp(args[0], "set-mark")) {
8352#ifdef SO_MARK
8353 char *err;
8354 rule->action = HTTP_REQ_ACT_SET_MARK;
8355 cur_arg = 1;
8356
8357 if (!*args[cur_arg] ||
8358 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8359 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8360 file, linenum, args[0]);
8361 goto out_err;
8362 }
8363
8364 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8365 if (err && *err != '\0') {
8366 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8367 file, linenum, err, args[0]);
8368 goto out_err;
8369 }
8370 cur_arg++;
8371 global.last_checks |= LSTCHK_NETADM;
8372#else
8373 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8374 goto out_err;
8375#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008376 } else if (!strcmp(args[0], "set-log-level")) {
8377 rule->action = HTTP_REQ_ACT_SET_LOGL;
8378 cur_arg = 1;
8379
8380 if (!*args[cur_arg] ||
8381 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8382 bad_log_level:
8383 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8384 file, linenum, args[0]);
8385 goto out_err;
8386 }
8387 if (strcmp(args[cur_arg], "silent") == 0)
8388 rule->arg.loglevel = -1;
8389 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8390 goto bad_log_level;
8391 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008392 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8393 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8394 cur_arg = 1;
8395
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008396 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8397 (*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 +01008398 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8399 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008400 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008401 }
8402
8403 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8404 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8405 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008406
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008407 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008408 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8409 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008410 free(proxy->conf.lfs_file);
8411 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8412 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008413 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008414 } else if (strcmp(args[0], "redirect") == 0) {
8415 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008416 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008417
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008418 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008419 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8420 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8421 goto out_err;
8422 }
8423
8424 /* this redirect rule might already contain a parsed condition which
8425 * we'll pass to the http-request rule.
8426 */
8427 rule->action = HTTP_REQ_ACT_REDIR;
8428 rule->arg.redir = redir;
8429 rule->cond = redir->cond;
8430 redir->cond = NULL;
8431 cur_arg = 2;
8432 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008433 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008434 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 +01008435 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008436 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008437 }
8438
8439 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8440 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008441 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008442
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008443 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8444 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8445 file, linenum, args[0], errmsg);
8446 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008447 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008448 }
8449 rule->cond = cond;
8450 }
8451 else if (*args[cur_arg]) {
8452 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8453 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8454 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008455 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008456 }
8457
8458 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008459 out_err:
8460 free(rule);
8461 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008462}
8463
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008464/* parse an "http-respose" rule */
8465struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8466{
8467 struct http_res_rule *rule;
8468 int cur_arg;
8469
8470 rule = calloc(1, sizeof(*rule));
8471 if (!rule) {
8472 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8473 goto out_err;
8474 }
8475
8476 if (!strcmp(args[0], "allow")) {
8477 rule->action = HTTP_RES_ACT_ALLOW;
8478 cur_arg = 1;
8479 } else if (!strcmp(args[0], "deny")) {
8480 rule->action = HTTP_RES_ACT_DENY;
8481 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008482 } else if (!strcmp(args[0], "set-nice")) {
8483 rule->action = HTTP_RES_ACT_SET_NICE;
8484 cur_arg = 1;
8485
8486 if (!*args[cur_arg] ||
8487 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8488 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8489 file, linenum, args[0]);
8490 goto out_err;
8491 }
8492 rule->arg.nice = atoi(args[cur_arg]);
8493 if (rule->arg.nice < -1024)
8494 rule->arg.nice = -1024;
8495 else if (rule->arg.nice > 1024)
8496 rule->arg.nice = 1024;
8497 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008498 } else if (!strcmp(args[0], "set-tos")) {
8499#ifdef IP_TOS
8500 char *err;
8501 rule->action = HTTP_RES_ACT_SET_TOS;
8502 cur_arg = 1;
8503
8504 if (!*args[cur_arg] ||
8505 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8506 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8507 file, linenum, args[0]);
8508 goto out_err;
8509 }
8510
8511 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8512 if (err && *err != '\0') {
8513 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8514 file, linenum, err, args[0]);
8515 goto out_err;
8516 }
8517 cur_arg++;
8518#else
8519 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8520 goto out_err;
8521#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008522 } else if (!strcmp(args[0], "set-mark")) {
8523#ifdef SO_MARK
8524 char *err;
8525 rule->action = HTTP_RES_ACT_SET_MARK;
8526 cur_arg = 1;
8527
8528 if (!*args[cur_arg] ||
8529 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8530 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8531 file, linenum, args[0]);
8532 goto out_err;
8533 }
8534
8535 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8536 if (err && *err != '\0') {
8537 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8538 file, linenum, err, args[0]);
8539 goto out_err;
8540 }
8541 cur_arg++;
8542 global.last_checks |= LSTCHK_NETADM;
8543#else
8544 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8545 goto out_err;
8546#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008547 } else if (!strcmp(args[0], "set-log-level")) {
8548 rule->action = HTTP_RES_ACT_SET_LOGL;
8549 cur_arg = 1;
8550
8551 if (!*args[cur_arg] ||
8552 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8553 bad_log_level:
8554 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8555 file, linenum, args[0]);
8556 goto out_err;
8557 }
8558 if (strcmp(args[cur_arg], "silent") == 0)
8559 rule->arg.loglevel = -1;
8560 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8561 goto bad_log_level;
8562 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008563 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8564 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8565 cur_arg = 1;
8566
8567 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8568 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8569 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8570 file, linenum, args[0]);
8571 goto out_err;
8572 }
8573
8574 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8575 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8576 LIST_INIT(&rule->arg.hdr_add.fmt);
8577
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008578 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008579 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8580 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008581 free(proxy->conf.lfs_file);
8582 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8583 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008584 cur_arg += 2;
8585 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008586 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 +02008587 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8588 goto out_err;
8589 }
8590
8591 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8592 struct acl_cond *cond;
8593 char *errmsg = NULL;
8594
8595 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8596 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8597 file, linenum, args[0], errmsg);
8598 free(errmsg);
8599 goto out_err;
8600 }
8601 rule->cond = cond;
8602 }
8603 else if (*args[cur_arg]) {
8604 Alert("parsing [%s:%d]: 'http-response %s' expects"
8605 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8606 file, linenum, args[0], args[cur_arg]);
8607 goto out_err;
8608 }
8609
8610 return rule;
8611 out_err:
8612 free(rule);
8613 return NULL;
8614}
8615
Willy Tarreau4baae242012-12-27 12:00:31 +01008616/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008617 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8618 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008619 */
8620struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008621 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008622{
8623 struct redirect_rule *rule;
8624 int cur_arg;
8625 int type = REDIRECT_TYPE_NONE;
8626 int code = 302;
8627 const char *destination = NULL;
8628 const char *cookie = NULL;
8629 int cookie_set = 0;
8630 unsigned int flags = REDIRECT_FLAG_NONE;
8631 struct acl_cond *cond = NULL;
8632
8633 cur_arg = 0;
8634 while (*(args[cur_arg])) {
8635 if (strcmp(args[cur_arg], "location") == 0) {
8636 if (!*args[cur_arg + 1])
8637 goto missing_arg;
8638
8639 type = REDIRECT_TYPE_LOCATION;
8640 cur_arg++;
8641 destination = args[cur_arg];
8642 }
8643 else if (strcmp(args[cur_arg], "prefix") == 0) {
8644 if (!*args[cur_arg + 1])
8645 goto missing_arg;
8646
8647 type = REDIRECT_TYPE_PREFIX;
8648 cur_arg++;
8649 destination = args[cur_arg];
8650 }
8651 else if (strcmp(args[cur_arg], "scheme") == 0) {
8652 if (!*args[cur_arg + 1])
8653 goto missing_arg;
8654
8655 type = REDIRECT_TYPE_SCHEME;
8656 cur_arg++;
8657 destination = args[cur_arg];
8658 }
8659 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8660 if (!*args[cur_arg + 1])
8661 goto missing_arg;
8662
8663 cur_arg++;
8664 cookie = args[cur_arg];
8665 cookie_set = 1;
8666 }
8667 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8668 if (!*args[cur_arg + 1])
8669 goto missing_arg;
8670
8671 cur_arg++;
8672 cookie = args[cur_arg];
8673 cookie_set = 0;
8674 }
8675 else if (strcmp(args[cur_arg], "code") == 0) {
8676 if (!*args[cur_arg + 1])
8677 goto missing_arg;
8678
8679 cur_arg++;
8680 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008681 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008682 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008683 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008684 args[cur_arg - 1], args[cur_arg]);
8685 return NULL;
8686 }
8687 }
8688 else if (!strcmp(args[cur_arg],"drop-query")) {
8689 flags |= REDIRECT_FLAG_DROP_QS;
8690 }
8691 else if (!strcmp(args[cur_arg],"append-slash")) {
8692 flags |= REDIRECT_FLAG_APPEND_SLASH;
8693 }
8694 else if (strcmp(args[cur_arg], "if") == 0 ||
8695 strcmp(args[cur_arg], "unless") == 0) {
8696 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8697 if (!cond) {
8698 memprintf(errmsg, "error in condition: %s", *errmsg);
8699 return NULL;
8700 }
8701 break;
8702 }
8703 else {
8704 memprintf(errmsg,
8705 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8706 args[cur_arg]);
8707 return NULL;
8708 }
8709 cur_arg++;
8710 }
8711
8712 if (type == REDIRECT_TYPE_NONE) {
8713 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8714 return NULL;
8715 }
8716
8717 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8718 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008719 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008720
8721 if (!use_fmt) {
8722 /* old-style static redirect rule */
8723 rule->rdr_str = strdup(destination);
8724 rule->rdr_len = strlen(destination);
8725 }
8726 else {
8727 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008728
8729 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8730 * if prefix == "/", we don't want to add anything, otherwise it
8731 * makes it hard for the user to configure a self-redirection.
8732 */
8733 proxy->conf.args.ctx = ARGC_RDR;
8734 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8735 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8736 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008737 free(curproxy->conf.lfs_file);
8738 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8739 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008740 }
8741 }
8742
Willy Tarreau4baae242012-12-27 12:00:31 +01008743 if (cookie) {
8744 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8745 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8746 */
8747 rule->cookie_len = strlen(cookie);
8748 if (cookie_set) {
8749 rule->cookie_str = malloc(rule->cookie_len + 10);
8750 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8751 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8752 rule->cookie_len += 9;
8753 } else {
8754 rule->cookie_str = malloc(rule->cookie_len + 21);
8755 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8756 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8757 rule->cookie_len += 20;
8758 }
8759 }
8760 rule->type = type;
8761 rule->code = code;
8762 rule->flags = flags;
8763 LIST_INIT(&rule->list);
8764 return rule;
8765
8766 missing_arg:
8767 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8768 return NULL;
8769}
8770
Willy Tarreau8797c062007-05-07 00:55:35 +02008771/************************************************************************/
8772/* The code below is dedicated to ACL parsing and matching */
8773/************************************************************************/
8774
8775
Willy Tarreau14174bc2012-04-16 14:34:04 +02008776/* This function ensures that the prerequisites for an L7 fetch are ready,
8777 * which means that a request or response is ready. If some data is missing,
8778 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008779 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8780 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008781 *
8782 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008783 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8784 * decide whether or not an HTTP message is present ;
8785 * 0 if the requested data cannot be fetched or if it is certain that
8786 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008787 * 1 if an HTTP message is ready
8788 */
8789static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008790smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008791 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008792{
8793 struct http_txn *txn = l7;
8794 struct http_msg *msg = &txn->req;
8795
8796 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8797 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8798 */
8799
8800 if (unlikely(!s || !txn))
8801 return 0;
8802
8803 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008804 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008805
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008806 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008807 if (unlikely(!s->req))
8808 return 0;
8809
Willy Tarreauaae75e32013-03-29 12:31:49 +01008810 /* If the buffer does not leave enough free space at the end,
8811 * we must first realign it.
8812 */
8813 if (s->req->buf->p > s->req->buf->data &&
8814 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8815 buffer_slow_realign(s->req->buf);
8816
Willy Tarreau14174bc2012-04-16 14:34:04 +02008817 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008818 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008819 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008820
8821 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008822 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008823 http_msg_analyzer(msg, &txn->hdr_idx);
8824
8825 /* Still no valid request ? */
8826 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008827 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008828 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008829 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008830 }
8831 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008832 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008833 return 0;
8834 }
8835
8836 /* OK we just got a valid HTTP request. We have some minor
8837 * preparation to perform so that further checks can rely
8838 * on HTTP tests.
8839 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008840
8841 /* If the request was parsed but was too large, we must absolutely
8842 * return an error so that it is not processed. At the moment this
8843 * cannot happen, but if the parsers are to change in the future,
8844 * we want this check to be maintained.
8845 */
8846 if (unlikely(s->req->buf->i + s->req->buf->p >
8847 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8848 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008849 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008850 return 1;
8851 }
8852
Willy Tarreau9b28e032012-10-12 23:49:43 +02008853 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008854 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8855 s->flags |= SN_REDIRECTABLE;
8856
Willy Tarreau506d0502013-07-06 13:29:24 +02008857 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8858 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008859 }
8860
Willy Tarreau506d0502013-07-06 13:29:24 +02008861 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008862 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008863 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008864
8865 /* otherwise everything's ready for the request */
8866 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008867 else {
8868 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008869 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8870 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008871 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008872 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008873 }
8874
8875 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008876 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008877 return 1;
8878}
Willy Tarreau8797c062007-05-07 00:55:35 +02008879
Willy Tarreauc0239e02012-04-16 14:42:55 +02008880#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008881 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 +02008882
Willy Tarreau24e32d82012-04-23 23:55:44 +02008883#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008884 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 +02008885
Willy Tarreau8797c062007-05-07 00:55:35 +02008886
8887/* 1. Check on METHOD
8888 * We use the pre-parsed method if it is known, and store its number as an
8889 * integer. If it is unknown, we use the pointer and the length.
8890 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008891static 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 +02008892{
8893 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008894 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008895
Willy Tarreauae8b7962007-06-09 23:10:04 +02008896 len = strlen(*text);
8897 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008898
8899 pattern->val.i = meth;
8900 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008901 if (usage == PAT_U_COMPILE) {
8902 pattern->ptr.str = strdup(*text);
8903 if (!pattern->ptr.str) {
8904 memprintf(err, "out of memory while loading pattern");
8905 return 0;
8906 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008907 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008908 else {
8909 trash = get_trash_chunk();
8910 if (trash->size < len) {
8911 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8912 len, trash->size);
8913 return 0;
8914 }
8915 pattern->ptr.str = trash->str;
8916 }
8917 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008918 pattern->len = len;
8919 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008920 else
8921 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008922 return 1;
8923}
8924
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008925/* This function fetches the method of current HTTP request and stores
8926 * it in the global pattern struct as a chunk. There are two possibilities :
8927 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8928 * in <len> and <ptr> is NULL ;
8929 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8930 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008931 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008932 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008933static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008934smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008935 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008936{
8937 int meth;
8938 struct http_txn *txn = l7;
8939
Willy Tarreau24e32d82012-04-23 23:55:44 +02008940 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008941
Willy Tarreau8797c062007-05-07 00:55:35 +02008942 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008943 smp->type = SMP_T_UINT;
8944 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008945 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008946 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8947 /* ensure the indexes are not affected */
8948 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008949 smp->type = SMP_T_CSTR;
8950 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008951 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008952 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008953 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008954 return 1;
8955}
8956
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008957/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008958static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008959{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008960 int icase;
8961
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008962
Willy Tarreauf853c462012-04-23 18:53:56 +02008963 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008964 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008965 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008966 return PAT_MATCH;
8967 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008968 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008969
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008970 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8971 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008972 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008973
8974 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008975 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008976 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008977
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008978 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008979 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8980 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008981 return PAT_NOMATCH;
8982 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008983}
8984
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008985static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008986smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008987 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008988{
8989 struct http_txn *txn = l7;
8990 char *ptr;
8991 int len;
8992
Willy Tarreauc0239e02012-04-16 14:42:55 +02008993 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008994
Willy Tarreau8797c062007-05-07 00:55:35 +02008995 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008996 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008997
8998 while ((len-- > 0) && (*ptr++ != '/'));
8999 if (len <= 0)
9000 return 0;
9001
Willy Tarreauf853c462012-04-23 18:53:56 +02009002 smp->type = SMP_T_CSTR;
9003 smp->data.str.str = ptr;
9004 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009005
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009006 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009007 return 1;
9008}
9009
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009010static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009011smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009012 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009013{
9014 struct http_txn *txn = l7;
9015 char *ptr;
9016 int len;
9017
Willy Tarreauc0239e02012-04-16 14:42:55 +02009018 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009019
Willy Tarreauf26b2522012-12-14 08:33:14 +01009020 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9021 return 0;
9022
Willy Tarreau8797c062007-05-07 00:55:35 +02009023 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009024 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009025
9026 while ((len-- > 0) && (*ptr++ != '/'));
9027 if (len <= 0)
9028 return 0;
9029
Willy Tarreauf853c462012-04-23 18:53:56 +02009030 smp->type = SMP_T_CSTR;
9031 smp->data.str.str = ptr;
9032 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009033
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009034 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009035 return 1;
9036}
9037
9038/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009039static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009040smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009041 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009042{
9043 struct http_txn *txn = l7;
9044 char *ptr;
9045 int len;
9046
Willy Tarreauc0239e02012-04-16 14:42:55 +02009047 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009048
Willy Tarreauf26b2522012-12-14 08:33:14 +01009049 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9050 return 0;
9051
Willy Tarreau8797c062007-05-07 00:55:35 +02009052 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009053 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009054
Willy Tarreauf853c462012-04-23 18:53:56 +02009055 smp->type = SMP_T_UINT;
9056 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009057 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009058 return 1;
9059}
9060
9061/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009062static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009063smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009064 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009065{
9066 struct http_txn *txn = l7;
9067
Willy Tarreauc0239e02012-04-16 14:42:55 +02009068 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009069
Willy Tarreauf853c462012-04-23 18:53:56 +02009070 smp->type = SMP_T_CSTR;
9071 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009072 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009073 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009074 return 1;
9075}
9076
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009077static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009078smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009079 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009080{
9081 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009082 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009083
Willy Tarreauc0239e02012-04-16 14:42:55 +02009084 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009085
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009086 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9087 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009088 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009089
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009090 smp->type = SMP_T_IPV4;
9091 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009092 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009093 return 1;
9094}
9095
9096static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009097smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009098 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009099{
9100 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009101 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009102
Willy Tarreauc0239e02012-04-16 14:42:55 +02009103 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009104
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009105 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9106 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9107 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009108
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009109 smp->type = SMP_T_UINT;
9110 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009111 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009112 return 1;
9113}
9114
Willy Tarreau185b5c42012-04-26 15:11:51 +02009115/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9116 * Accepts an optional argument of type string containing the header field name,
9117 * and an optional argument of type signed or unsigned integer to request an
9118 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009119 * headers are considered from the first one. It does not stop on commas and
9120 * returns full lines instead (useful for User-Agent or Date for example).
9121 */
9122static int
9123smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009124 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009125{
9126 struct http_txn *txn = l7;
9127 struct hdr_idx *idx = &txn->hdr_idx;
9128 struct hdr_ctx *ctx = smp->ctx.a[0];
9129 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9130 int occ = 0;
9131 const char *name_str = NULL;
9132 int name_len = 0;
9133
9134 if (!ctx) {
9135 /* first call */
9136 ctx = &static_hdr_ctx;
9137 ctx->idx = 0;
9138 smp->ctx.a[0] = ctx;
9139 }
9140
9141 if (args) {
9142 if (args[0].type != ARGT_STR)
9143 return 0;
9144 name_str = args[0].data.str.str;
9145 name_len = args[0].data.str.len;
9146
9147 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9148 occ = args[1].data.uint;
9149 }
9150
9151 CHECK_HTTP_MESSAGE_FIRST();
9152
9153 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9154 /* search for header from the beginning */
9155 ctx->idx = 0;
9156
9157 if (!occ && !(opt & SMP_OPT_ITERATE))
9158 /* no explicit occurrence and single fetch => last header by default */
9159 occ = -1;
9160
9161 if (!occ)
9162 /* prepare to report multiple occurrences for ACL fetches */
9163 smp->flags |= SMP_F_NOT_LAST;
9164
9165 smp->type = SMP_T_CSTR;
9166 smp->flags |= SMP_F_VOL_HDR;
9167 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9168 return 1;
9169
9170 smp->flags &= ~SMP_F_NOT_LAST;
9171 return 0;
9172}
9173
9174/* 6. Check on HTTP header count. The number of occurrences is returned.
9175 * Accepts exactly 1 argument of type string. It does not stop on commas and
9176 * returns full lines instead (useful for User-Agent or Date for example).
9177 */
9178static int
9179smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009180 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009181{
9182 struct http_txn *txn = l7;
9183 struct hdr_idx *idx = &txn->hdr_idx;
9184 struct hdr_ctx ctx;
9185 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9186 int cnt;
9187
9188 if (!args || args->type != ARGT_STR)
9189 return 0;
9190
9191 CHECK_HTTP_MESSAGE_FIRST();
9192
9193 ctx.idx = 0;
9194 cnt = 0;
9195 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9196 cnt++;
9197
9198 smp->type = SMP_T_UINT;
9199 smp->data.uint = cnt;
9200 smp->flags = SMP_F_VOL_HDR;
9201 return 1;
9202}
9203
9204/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9205 * Accepts an optional argument of type string containing the header field name,
9206 * and an optional argument of type signed or unsigned integer to request an
9207 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009208 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009209 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009210static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009211smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009212 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009213{
9214 struct http_txn *txn = l7;
9215 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009216 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009217 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009218 int occ = 0;
9219 const char *name_str = NULL;
9220 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009221
Willy Tarreaua890d072013-04-02 12:01:06 +02009222 if (!ctx) {
9223 /* first call */
9224 ctx = &static_hdr_ctx;
9225 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009226 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009227 }
9228
Willy Tarreau185b5c42012-04-26 15:11:51 +02009229 if (args) {
9230 if (args[0].type != ARGT_STR)
9231 return 0;
9232 name_str = args[0].data.str.str;
9233 name_len = args[0].data.str.len;
9234
9235 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9236 occ = args[1].data.uint;
9237 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009238
Willy Tarreaue333ec92012-04-16 16:26:40 +02009239 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009240
Willy Tarreau185b5c42012-04-26 15:11:51 +02009241 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009242 /* search for header from the beginning */
9243 ctx->idx = 0;
9244
Willy Tarreau185b5c42012-04-26 15:11:51 +02009245 if (!occ && !(opt & SMP_OPT_ITERATE))
9246 /* no explicit occurrence and single fetch => last header by default */
9247 occ = -1;
9248
9249 if (!occ)
9250 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009251 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009252
Willy Tarreau185b5c42012-04-26 15:11:51 +02009253 smp->type = SMP_T_CSTR;
9254 smp->flags |= SMP_F_VOL_HDR;
9255 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 +02009256 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009257
Willy Tarreau37406352012-04-23 16:16:37 +02009258 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009259 return 0;
9260}
9261
Willy Tarreauc11416f2007-06-17 16:58:38 +02009262/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009263 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009264 */
9265static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009266smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009267 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009268{
9269 struct http_txn *txn = l7;
9270 struct hdr_idx *idx = &txn->hdr_idx;
9271 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009272 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009273 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009274
Willy Tarreau24e32d82012-04-23 23:55:44 +02009275 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009276 return 0;
9277
Willy Tarreaue333ec92012-04-16 16:26:40 +02009278 CHECK_HTTP_MESSAGE_FIRST();
9279
Willy Tarreau33a7e692007-06-10 19:45:56 +02009280 ctx.idx = 0;
9281 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009282 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 +02009283 cnt++;
9284
Willy Tarreauf853c462012-04-23 18:53:56 +02009285 smp->type = SMP_T_UINT;
9286 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009287 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009288 return 1;
9289}
9290
Willy Tarreau185b5c42012-04-26 15:11:51 +02009291/* Fetch an HTTP header's integer value. The integer value is returned. It
9292 * takes a mandatory argument of type string and an optional one of type int
9293 * to designate a specific occurrence. It returns an unsigned integer, which
9294 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009295 */
9296static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009297smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009298 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009299{
Willy Tarreauef38c392013-07-22 16:29:32 +02009300 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009301
Willy Tarreauf853c462012-04-23 18:53:56 +02009302 if (ret > 0) {
9303 smp->type = SMP_T_UINT;
9304 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9305 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009306
Willy Tarreaud53e2422012-04-16 17:21:11 +02009307 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009308}
9309
Cyril Bonté69fa9922012-10-25 00:01:06 +02009310/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9311 * and an optional one of type int to designate a specific occurrence.
9312 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009313 */
9314static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009315smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009316 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009317{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009318 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009319
Willy Tarreauef38c392013-07-22 16:29:32 +02009320 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009321 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9322 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009323 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009324 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009325 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009326 if (smp->data.str.len < temp->size - 1) {
9327 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9328 temp->str[smp->data.str.len] = '\0';
9329 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9330 smp->type = SMP_T_IPV6;
9331 break;
9332 }
9333 }
9334 }
9335
Willy Tarreaud53e2422012-04-16 17:21:11 +02009336 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009337 if (!(smp->flags & SMP_F_NOT_LAST))
9338 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009339 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009340 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009341}
9342
Willy Tarreau737b0c12007-06-10 21:28:46 +02009343/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9344 * the first '/' after the possible hostname, and ends before the possible '?'.
9345 */
9346static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009347smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009348 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009349{
9350 struct http_txn *txn = l7;
9351 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009352
Willy Tarreauc0239e02012-04-16 14:42:55 +02009353 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009354
Willy Tarreau9b28e032012-10-12 23:49:43 +02009355 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009356 ptr = http_get_path(txn);
9357 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009358 return 0;
9359
9360 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009361 smp->type = SMP_T_CSTR;
9362 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009363
9364 while (ptr < end && *ptr != '?')
9365 ptr++;
9366
Willy Tarreauf853c462012-04-23 18:53:56 +02009367 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009368 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009369 return 1;
9370}
9371
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009372/* This produces a concatenation of the first occurrence of the Host header
9373 * followed by the path component if it begins with a slash ('/'). This means
9374 * that '*' will not be added, resulting in exactly the first Host entry.
9375 * If no Host header is found, then the path is returned as-is. The returned
9376 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009377 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009378 */
9379static int
9380smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009381 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009382{
9383 struct http_txn *txn = l7;
9384 char *ptr, *end, *beg;
9385 struct hdr_ctx ctx;
9386
9387 CHECK_HTTP_MESSAGE_FIRST();
9388
9389 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009390 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 +02009391 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009392 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009393
9394 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009395 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009396 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009397 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009398 smp->data.str.len = ctx.vlen;
9399
9400 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009401 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 +02009402 beg = http_get_path(txn);
9403 if (!beg)
9404 beg = end;
9405
9406 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9407
9408 if (beg < ptr && *beg == '/') {
9409 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9410 smp->data.str.len += ptr - beg;
9411 }
9412
9413 smp->flags = SMP_F_VOL_1ST;
9414 return 1;
9415}
9416
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009417/* This produces a 32-bit hash of the concatenation of the first occurrence of
9418 * the Host header followed by the path component if it begins with a slash ('/').
9419 * This means that '*' will not be added, resulting in exactly the first Host
9420 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009421 * is hashed using the path hash followed by a full avalanche hash and provides a
9422 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009423 * high-traffic sites without having to store whole paths.
9424 */
9425static int
9426smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009427 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009428{
9429 struct http_txn *txn = l7;
9430 struct hdr_ctx ctx;
9431 unsigned int hash = 0;
9432 char *ptr, *beg, *end;
9433 int len;
9434
9435 CHECK_HTTP_MESSAGE_FIRST();
9436
9437 ctx.idx = 0;
9438 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9439 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9440 ptr = ctx.line + ctx.val;
9441 len = ctx.vlen;
9442 while (len--)
9443 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9444 }
9445
9446 /* now retrieve the path */
9447 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9448 beg = http_get_path(txn);
9449 if (!beg)
9450 beg = end;
9451
9452 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9453
9454 if (beg < ptr && *beg == '/') {
9455 while (beg < ptr)
9456 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9457 }
9458 hash = full_hash(hash);
9459
9460 smp->type = SMP_T_UINT;
9461 smp->data.uint = hash;
9462 smp->flags = SMP_F_VOL_1ST;
9463 return 1;
9464}
9465
Willy Tarreau4a550602012-12-09 14:53:32 +01009466/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009467 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9468 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9469 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009470 * that in environments where IPv6 is insignificant, truncating the output to
9471 * 8 bytes would still work.
9472 */
9473static int
9474smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009475 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009476{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009477 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009478 struct connection *cli_conn = objt_conn(l4->si[0].end);
9479
9480 if (!cli_conn)
9481 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009482
Willy Tarreauef38c392013-07-22 16:29:32 +02009483 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009484 return 0;
9485
Willy Tarreau47ca5452012-12-23 20:22:19 +01009486 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009487 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9488 temp->len += sizeof(smp->data.uint);
9489
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009490 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009491 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009492 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009493 temp->len += 4;
9494 break;
9495 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009496 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009497 temp->len += 16;
9498 break;
9499 default:
9500 return 0;
9501 }
9502
9503 smp->data.str = *temp;
9504 smp->type = SMP_T_BIN;
9505 return 1;
9506}
9507
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009508static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009509smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009510 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009511{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009512 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9513 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9514 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009515
Willy Tarreau24e32d82012-04-23 23:55:44 +02009516 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009517
Willy Tarreauf853c462012-04-23 18:53:56 +02009518 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009519 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009520 return 1;
9521}
9522
Willy Tarreau7f18e522010-10-22 20:04:13 +02009523/* return a valid test if the current request is the first one on the connection */
9524static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009525smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009526 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009527{
9528 if (!s)
9529 return 0;
9530
Willy Tarreauf853c462012-04-23 18:53:56 +02009531 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009532 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009533 return 1;
9534}
9535
Willy Tarreau34db1082012-04-19 17:16:54 +02009536/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009537static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009538smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009539 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009540{
9541
Willy Tarreau24e32d82012-04-23 23:55:44 +02009542 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009543 return 0;
9544
Willy Tarreauc0239e02012-04-16 14:42:55 +02009545 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009546
Willy Tarreauc0239e02012-04-16 14:42:55 +02009547 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009548 return 0;
9549
Willy Tarreauf853c462012-04-23 18:53:56 +02009550 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009551 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 +01009552 return 1;
9553}
Willy Tarreau8797c062007-05-07 00:55:35 +02009554
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009555/* Accepts exactly 1 argument of type userlist */
9556static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009557smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009558 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009559{
9560
9561 if (!args || args->type != ARGT_USR)
9562 return 0;
9563
9564 CHECK_HTTP_MESSAGE_FIRST();
9565
9566 if (!get_http_auth(l4))
9567 return 0;
9568
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009569 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009570 smp->ctx.a[0] = args->data.usr; /* user list */
9571 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9572 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9573
9574 /* if the user does not belong to the userlist or has a wrong password,
9575 * report that it unconditionally does not match. Otherwise we return
9576 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009577 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009578 */
9579 smp->type = SMP_T_BOOL;
9580 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9581 if (smp->data.uint)
9582 smp->type = SMP_T_UINT;
9583
9584 return 1;
9585}
9586
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009587/* Try to find the next occurrence of a cookie name in a cookie header value.
9588 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9589 * the cookie value is returned into *value and *value_l, and the function
9590 * returns a pointer to the next pointer to search from if the value was found.
9591 * Otherwise if the cookie was not found, NULL is returned and neither value
9592 * nor value_l are touched. The input <hdr> string should first point to the
9593 * header's value, and the <hdr_end> pointer must point to the first character
9594 * not part of the value. <list> must be non-zero if value may represent a list
9595 * of values (cookie headers). This makes it faster to abort parsing when no
9596 * list is expected.
9597 */
9598static char *
9599extract_cookie_value(char *hdr, const char *hdr_end,
9600 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009601 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009602{
9603 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9604 char *next;
9605
9606 /* we search at least a cookie name followed by an equal, and more
9607 * generally something like this :
9608 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9609 */
9610 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9611 /* Iterate through all cookies on this line */
9612
9613 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9614 att_beg++;
9615
9616 /* find att_end : this is the first character after the last non
9617 * space before the equal. It may be equal to hdr_end.
9618 */
9619 equal = att_end = att_beg;
9620
9621 while (equal < hdr_end) {
9622 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9623 break;
9624 if (http_is_spht[(unsigned char)*equal++])
9625 continue;
9626 att_end = equal;
9627 }
9628
9629 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9630 * is between <att_beg> and <equal>, both may be identical.
9631 */
9632
9633 /* look for end of cookie if there is an equal sign */
9634 if (equal < hdr_end && *equal == '=') {
9635 /* look for the beginning of the value */
9636 val_beg = equal + 1;
9637 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9638 val_beg++;
9639
9640 /* find the end of the value, respecting quotes */
9641 next = find_cookie_value_end(val_beg, hdr_end);
9642
9643 /* make val_end point to the first white space or delimitor after the value */
9644 val_end = next;
9645 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9646 val_end--;
9647 } else {
9648 val_beg = val_end = next = equal;
9649 }
9650
9651 /* We have nothing to do with attributes beginning with '$'. However,
9652 * they will automatically be removed if a header before them is removed,
9653 * since they're supposed to be linked together.
9654 */
9655 if (*att_beg == '$')
9656 continue;
9657
9658 /* Ignore cookies with no equal sign */
9659 if (equal == next)
9660 continue;
9661
9662 /* Now we have the cookie name between att_beg and att_end, and
9663 * its value between val_beg and val_end.
9664 */
9665
9666 if (att_end - att_beg == cookie_name_l &&
9667 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9668 /* let's return this value and indicate where to go on from */
9669 *value = val_beg;
9670 *value_l = val_end - val_beg;
9671 return next + 1;
9672 }
9673
9674 /* Set-Cookie headers only have the name in the first attr=value part */
9675 if (!list)
9676 break;
9677 }
9678
9679 return NULL;
9680}
9681
William Lallemanda43ba4e2014-01-28 18:14:25 +01009682/* Fetch a captured HTTP request header. The index is the position of
9683 * the "capture" option in the configuration file
9684 */
9685static int
9686smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9687 const struct arg *args, struct sample *smp, const char *kw)
9688{
9689 struct proxy *fe = l4->fe;
9690 struct http_txn *txn = l7;
9691 int idx;
9692
9693 if (!args || args->type != ARGT_UINT)
9694 return 0;
9695
9696 idx = args->data.uint;
9697
9698 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9699 return 0;
9700
9701 smp->type = SMP_T_CSTR;
9702 smp->data.str.str = txn->req.cap[idx];
9703 smp->data.str.len = strlen(txn->req.cap[idx]);
9704
9705 return 1;
9706}
9707
9708/* Fetch a captured HTTP response header. The index is the position of
9709 * the "capture" option in the configuration file
9710 */
9711static int
9712smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9713 const struct arg *args, struct sample *smp, const char *kw)
9714{
9715 struct proxy *fe = l4->fe;
9716 struct http_txn *txn = l7;
9717 int idx;
9718
9719 if (!args || args->type != ARGT_UINT)
9720 return 0;
9721
9722 idx = args->data.uint;
9723
9724 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9725 return 0;
9726
9727 smp->type = SMP_T_CSTR;
9728 smp->data.str.str = txn->rsp.cap[idx];
9729 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9730
9731 return 1;
9732}
9733
Willy Tarreaue333ec92012-04-16 16:26:40 +02009734/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009735 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009736 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009737 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009738 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009739 * Accepts exactly 1 argument of type string. If the input options indicate
9740 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009741 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009742 */
9743static int
Willy Tarreau51539362012-05-08 12:46:28 +02009744smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009745 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009746{
9747 struct http_txn *txn = l7;
9748 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009749 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009750 const struct http_msg *msg;
9751 const char *hdr_name;
9752 int hdr_name_len;
9753 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009754 int occ = 0;
9755 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009756
Willy Tarreau24e32d82012-04-23 23:55:44 +02009757 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009758 return 0;
9759
Willy Tarreaua890d072013-04-02 12:01:06 +02009760 if (!ctx) {
9761 /* first call */
9762 ctx = &static_hdr_ctx;
9763 ctx->idx = 0;
9764 smp->ctx.a[2] = ctx;
9765 }
9766
Willy Tarreaue333ec92012-04-16 16:26:40 +02009767 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009768
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009769 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009770 msg = &txn->req;
9771 hdr_name = "Cookie";
9772 hdr_name_len = 6;
9773 } else {
9774 msg = &txn->rsp;
9775 hdr_name = "Set-Cookie";
9776 hdr_name_len = 10;
9777 }
9778
Willy Tarreau28376d62012-04-26 21:26:10 +02009779 if (!occ && !(opt & SMP_OPT_ITERATE))
9780 /* no explicit occurrence and single fetch => last cookie by default */
9781 occ = -1;
9782
9783 /* OK so basically here, either we want only one value and it's the
9784 * last one, or we want to iterate over all of them and we fetch the
9785 * next one.
9786 */
9787
Willy Tarreau9b28e032012-10-12 23:49:43 +02009788 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009789 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009790 /* search for the header from the beginning, we must first initialize
9791 * the search parameters.
9792 */
Willy Tarreau37406352012-04-23 16:16:37 +02009793 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009794 ctx->idx = 0;
9795 }
9796
Willy Tarreau28376d62012-04-26 21:26:10 +02009797 smp->flags |= SMP_F_VOL_HDR;
9798
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009799 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009800 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9801 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009802 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9803 goto out;
9804
Willy Tarreau24e32d82012-04-23 23:55:44 +02009805 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009806 continue;
9807
Willy Tarreau37406352012-04-23 16:16:37 +02009808 smp->ctx.a[0] = ctx->line + ctx->val;
9809 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009810 }
9811
Willy Tarreauf853c462012-04-23 18:53:56 +02009812 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009813 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009814 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009815 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009816 &smp->data.str.str,
9817 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009818 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009819 found = 1;
9820 if (occ >= 0) {
9821 /* one value was returned into smp->data.str.{str,len} */
9822 smp->flags |= SMP_F_NOT_LAST;
9823 return 1;
9824 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009825 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009826 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009827 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009828 /* all cookie headers and values were scanned. If we're looking for the
9829 * last occurrence, we may return it now.
9830 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009831 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009832 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009833 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009834}
9835
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009836/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009837 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009838 * multiple cookies may be parsed on the same line. The returned sample is of
9839 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009840 */
9841static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009842smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009843 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009844{
9845 struct http_txn *txn = l7;
9846 struct hdr_idx *idx = &txn->hdr_idx;
9847 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009848 const struct http_msg *msg;
9849 const char *hdr_name;
9850 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009851 int cnt;
9852 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009853 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009854
Willy Tarreau24e32d82012-04-23 23:55:44 +02009855 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009856 return 0;
9857
Willy Tarreaue333ec92012-04-16 16:26:40 +02009858 CHECK_HTTP_MESSAGE_FIRST();
9859
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009860 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009861 msg = &txn->req;
9862 hdr_name = "Cookie";
9863 hdr_name_len = 6;
9864 } else {
9865 msg = &txn->rsp;
9866 hdr_name = "Set-Cookie";
9867 hdr_name_len = 10;
9868 }
9869
Willy Tarreau9b28e032012-10-12 23:49:43 +02009870 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009871 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009872 ctx.idx = 0;
9873 cnt = 0;
9874
9875 while (1) {
9876 /* Note: val_beg == NULL every time we need to fetch a new header */
9877 if (!val_beg) {
9878 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9879 break;
9880
Willy Tarreau24e32d82012-04-23 23:55:44 +02009881 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009882 continue;
9883
9884 val_beg = ctx.line + ctx.val;
9885 val_end = val_beg + ctx.vlen;
9886 }
9887
Willy Tarreauf853c462012-04-23 18:53:56 +02009888 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009889 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009890 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009891 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009892 &smp->data.str.str,
9893 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009894 cnt++;
9895 }
9896 }
9897
Willy Tarreaub169eba2013-12-16 15:14:43 +01009898 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +02009899 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009900 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009901 return 1;
9902}
9903
Willy Tarreau51539362012-05-08 12:46:28 +02009904/* Fetch an cookie's integer value. The integer value is returned. It
9905 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9906 */
9907static int
9908smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009909 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009910{
Willy Tarreauef38c392013-07-22 16:29:32 +02009911 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009912
9913 if (ret > 0) {
9914 smp->type = SMP_T_UINT;
9915 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9916 }
9917
9918 return ret;
9919}
9920
Willy Tarreau8797c062007-05-07 00:55:35 +02009921/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009922/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009923/************************************************************************/
9924
David Cournapeau16023ee2010-12-23 20:55:41 +09009925/*
9926 * Given a path string and its length, find the position of beginning of the
9927 * query string. Returns NULL if no query string is found in the path.
9928 *
9929 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9930 *
9931 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9932 */
bedis4c75cca2012-10-05 08:38:24 +02009933static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009934{
9935 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009936
bedis4c75cca2012-10-05 08:38:24 +02009937 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009938 return p ? p + 1 : NULL;
9939}
9940
bedis4c75cca2012-10-05 08:38:24 +02009941static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009942{
bedis4c75cca2012-10-05 08:38:24 +02009943 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009944}
9945
9946/*
9947 * Given a url parameter, find the starting position of the first occurence,
9948 * or NULL if the parameter is not found.
9949 *
9950 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9951 * the function will return query_string+8.
9952 */
9953static char*
9954find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009955 char* url_param_name, size_t url_param_name_l,
9956 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009957{
9958 char *pos, *last;
9959
9960 pos = query_string;
9961 last = query_string + query_string_l - url_param_name_l - 1;
9962
9963 while (pos <= last) {
9964 if (pos[url_param_name_l] == '=') {
9965 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9966 return pos;
9967 pos += url_param_name_l + 1;
9968 }
bedis4c75cca2012-10-05 08:38:24 +02009969 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009970 pos++;
9971 pos++;
9972 }
9973 return NULL;
9974}
9975
9976/*
9977 * Given a url parameter name, returns its value and size into *value and
9978 * *value_l respectively, and returns non-zero. If the parameter is not found,
9979 * zero is returned and value/value_l are not touched.
9980 */
9981static int
9982find_url_param_value(char* path, size_t path_l,
9983 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009984 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009985{
9986 char *query_string, *qs_end;
9987 char *arg_start;
9988 char *value_start, *value_end;
9989
bedis4c75cca2012-10-05 08:38:24 +02009990 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009991 if (!query_string)
9992 return 0;
9993
9994 qs_end = path + path_l;
9995 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009996 url_param_name, url_param_name_l,
9997 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009998 if (!arg_start)
9999 return 0;
10000
10001 value_start = arg_start + url_param_name_l + 1;
10002 value_end = value_start;
10003
bedis4c75cca2012-10-05 08:38:24 +020010004 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010005 value_end++;
10006
10007 *value = value_start;
10008 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010009 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010010}
10011
10012static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010013smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010014 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010015{
bedis4c75cca2012-10-05 08:38:24 +020010016 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010017 struct http_txn *txn = l7;
10018 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010019
bedis4c75cca2012-10-05 08:38:24 +020010020 if (!args || args[0].type != ARGT_STR ||
10021 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010022 return 0;
10023
10024 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010025
bedis4c75cca2012-10-05 08:38:24 +020010026 if (args[1].type)
10027 delim = *args[1].data.str.str;
10028
Willy Tarreau9b28e032012-10-12 23:49:43 +020010029 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010030 args->data.str.str, args->data.str.len,
10031 &smp->data.str.str, &smp->data.str.len,
10032 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010033 return 0;
10034
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010035 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010036 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010037 return 1;
10038}
10039
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010040/* Return the signed integer value for the specified url parameter (see url_param
10041 * above).
10042 */
10043static int
10044smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010045 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010046{
Willy Tarreauef38c392013-07-22 16:29:32 +020010047 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010048
10049 if (ret > 0) {
10050 smp->type = SMP_T_UINT;
10051 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10052 }
10053
10054 return ret;
10055}
10056
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010057/* This produces a 32-bit hash of the concatenation of the first occurrence of
10058 * the Host header followed by the path component if it begins with a slash ('/').
10059 * This means that '*' will not be added, resulting in exactly the first Host
10060 * entry. If no Host header is found, then the path is used. The resulting value
10061 * is hashed using the url hash followed by a full avalanche hash and provides a
10062 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10063 * high-traffic sites without having to store whole paths.
10064 * this differs from the base32 functions in that it includes the url parameters
10065 * as well as the path
10066 */
10067static int
10068smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010069 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010070{
10071 struct http_txn *txn = l7;
10072 struct hdr_ctx ctx;
10073 unsigned int hash = 0;
10074 char *ptr, *beg, *end;
10075 int len;
10076
10077 CHECK_HTTP_MESSAGE_FIRST();
10078
10079 ctx.idx = 0;
10080 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10081 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10082 ptr = ctx.line + ctx.val;
10083 len = ctx.vlen;
10084 while (len--)
10085 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10086 }
10087
10088 /* now retrieve the path */
10089 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10090 beg = http_get_path(txn);
10091 if (!beg)
10092 beg = end;
10093
10094 for (ptr = beg; ptr < end ; ptr++);
10095
10096 if (beg < ptr && *beg == '/') {
10097 while (beg < ptr)
10098 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10099 }
10100 hash = full_hash(hash);
10101
10102 smp->type = SMP_T_UINT;
10103 smp->data.uint = hash;
10104 smp->flags = SMP_F_VOL_1ST;
10105 return 1;
10106}
10107
10108/* This concatenates the source address with the 32-bit hash of the Host and
10109 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10110 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10111 * on the source address length. The URL hash is stored before the address so
10112 * that in environments where IPv6 is insignificant, truncating the output to
10113 * 8 bytes would still work.
10114 */
10115static int
10116smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010117 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010118{
10119 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010120 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010121
Willy Tarreaue155ec22013-11-18 18:33:22 +010010122 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010123 return 0;
10124
10125 temp = get_trash_chunk();
10126 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10127 temp->len += sizeof(smp->data.uint);
10128
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010129 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010130 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010131 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010132 temp->len += 4;
10133 break;
10134 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010135 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010136 temp->len += 16;
10137 break;
10138 default:
10139 return 0;
10140 }
10141
10142 smp->data.str = *temp;
10143 smp->type = SMP_T_BIN;
10144 return 1;
10145}
10146
Willy Tarreau185b5c42012-04-26 15:11:51 +020010147/* This function is used to validate the arguments passed to any "hdr" fetch
10148 * keyword. These keywords support an optional positive or negative occurrence
10149 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10150 * is assumed that the types are already the correct ones. Returns 0 on error,
10151 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10152 * error message in case of error, that the caller is responsible for freeing.
10153 * The initial location must either be freeable or NULL.
10154 */
10155static int val_hdr(struct arg *arg, char **err_msg)
10156{
10157 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010158 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010159 return 0;
10160 }
10161 return 1;
10162}
10163
Willy Tarreau276fae92013-07-25 14:36:01 +020010164/* takes an UINT value on input supposed to represent the time since EPOCH,
10165 * adds an optional offset found in args[0] and emits a string representing
10166 * the date in RFC-1123/5322 format.
10167 */
10168static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10169{
10170 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10171 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10172 struct chunk *temp;
10173 struct tm *tm;
10174 time_t curr_date = smp->data.uint;
10175
10176 /* add offset */
10177 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10178 curr_date += args[0].data.sint;
10179
10180 tm = gmtime(&curr_date);
10181
10182 temp = get_trash_chunk();
10183 temp->len = snprintf(temp->str, temp->size - temp->len,
10184 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10185 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10186 tm->tm_hour, tm->tm_min, tm->tm_sec);
10187
10188 smp->data.str = *temp;
10189 smp->type = SMP_T_STR;
10190 return 1;
10191}
10192
Willy Tarreau4a568972010-05-12 08:08:50 +020010193/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010194/* All supported ACL keywords must be declared here. */
10195/************************************************************************/
10196
10197/* Note: must not be declared <const> as its list will be overwritten.
10198 * Please take care of keeping this list alphabetically sorted.
10199 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010200static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010201 { "base", "base", pat_parse_str, pat_match_str },
10202 { "base_beg", "base", pat_parse_str, pat_match_beg },
10203 { "base_dir", "base", pat_parse_str, pat_match_dir },
10204 { "base_dom", "base", pat_parse_str, pat_match_dom },
10205 { "base_end", "base", pat_parse_str, pat_match_end },
10206 { "base_len", "base", pat_parse_int, pat_match_len },
10207 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10208 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010209
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010210 { "cook", "req.cook", pat_parse_str, pat_match_str },
10211 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10212 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10213 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10214 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10215 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10216 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10217 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010218
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010219 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10220 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10221 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10222 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10223 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10224 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10225 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10226 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010227
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010228 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010229
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010230 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010231
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010232 { "path", "path", pat_parse_str, pat_match_str },
10233 { "path_beg", "path", pat_parse_str, pat_match_beg },
10234 { "path_dir", "path", pat_parse_str, pat_match_dir },
10235 { "path_dom", "path", pat_parse_str, pat_match_dom },
10236 { "path_end", "path", pat_parse_str, pat_match_end },
10237 { "path_len", "path", pat_parse_int, pat_match_len },
10238 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10239 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010240
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010241 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10242 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010243
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010244 { "scook", "res.cook", pat_parse_str, pat_match_str },
10245 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10246 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10247 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10248 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10249 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10250 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10251 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010252
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010253 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10254 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10255 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10256 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10257 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10258 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10259 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10260 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010261
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010262 { "url", "url", pat_parse_str, pat_match_str },
10263 { "url_beg", "url", pat_parse_str, pat_match_beg },
10264 { "url_dir", "url", pat_parse_str, pat_match_dir },
10265 { "url_dom", "url", pat_parse_str, pat_match_dom },
10266 { "url_end", "url", pat_parse_str, pat_match_end },
10267 { "url_len", "url", pat_parse_int, pat_match_len },
10268 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10269 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010270
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010271 { "urlp", "urlp", pat_parse_str, pat_match_str },
10272 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10273 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10274 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10275 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10276 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10277 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10278 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010279
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010280 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010281}};
10282
10283/************************************************************************/
10284/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010285/************************************************************************/
10286/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010287static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010288 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10289 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10290 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10291
William Lallemanda43ba4e2014-01-28 18:14:25 +010010292 /* capture are allocated and are permanent in the session */
10293 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10294 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10295
Willy Tarreau409bcde2013-01-08 00:31:00 +010010296 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10297 * are only here to match the ACL's name, are request-only and are used
10298 * for ACL compatibility only.
10299 */
10300 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10301 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10302 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10303 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10304
10305 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10306 * only here to match the ACL's name, are request-only and are used for
10307 * ACL compatibility only.
10308 */
10309 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10310 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10311 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10312 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10313
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010314 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10315 { "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 +010010316 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10317 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10318 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010319
10320 /* HTTP protocol on the request path */
10321 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010322 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010323
10324 /* HTTP version on the request path */
10325 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010326 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010327
10328 /* HTTP version on the response path */
10329 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010330 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10331
Willy Tarreau18ed2562013-01-14 15:56:36 +010010332 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10333 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10334 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10335 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10336
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010337 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10338 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010339 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10340 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10341 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10342 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10343
10344 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10345 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10346 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10347 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10348
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010349 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10350 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010351 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10352 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10353 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10354 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10355
Willy Tarreau409bcde2013-01-08 00:31:00 +010010356 /* scook is valid only on the response and is used for ACL compatibility */
10357 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10358 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10359 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10360 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10361
10362 /* shdr is valid only on the response and is used for ACL compatibility */
10363 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10364 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10365 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10366 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10367
10368 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10369 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010370 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10371 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010372 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10373 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10374 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10375 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10376 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10377 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010378}};
10379
Willy Tarreau8797c062007-05-07 00:55:35 +020010380
Willy Tarreau276fae92013-07-25 14:36:01 +020010381/* Note: must not be declared <const> as its list will be overwritten */
10382static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10383 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10384 { NULL, NULL, 0, 0, 0 },
10385}};
10386
Willy Tarreau8797c062007-05-07 00:55:35 +020010387__attribute__((constructor))
10388static void __http_protocol_init(void)
10389{
10390 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010391 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010392 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010393}
10394
10395
Willy Tarreau58f10d72006-12-04 02:26:12 +010010396/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010397 * Local variables:
10398 * c-indent-level: 8
10399 * c-basic-offset: 8
10400 * End:
10401 */