blob: 45860164ad5156b4e6ad4990e197abb1aa4bf56b [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 Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020045#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010046#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010049#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020050#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020052#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010054#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/session.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020062#include <proto/sock_raw.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010063#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/task.h>
65
Willy Tarreau522d6c02009-12-06 18:49:18 +010066const char HTTP_100[] =
67 "HTTP/1.1 100 Continue\r\n\r\n";
68
69const struct chunk http_100_chunk = {
70 .str = (char *)&HTTP_100,
71 .len = sizeof(HTTP_100)-1
72};
73
Willy Tarreaua9679ac2010-01-03 17:32:57 +010074/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020075const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010076 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
95const char *HTTP_401_fmt =
96 "HTTP/1.0 401 Unauthorized\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020099 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
101 "\r\n"
102 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
103
Willy Tarreau844a7e72010-01-31 21:46:18 +0100104const char *HTTP_407_fmt =
105 "HTTP/1.0 407 Unauthorized\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Connection: close\r\n"
108 "Content-Type: text/html\r\n"
109 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
110 "\r\n"
111 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
112
Willy Tarreau0f772532006-12-23 20:51:41 +0100113
114const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200115 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200126 [HTTP_ERR_200] =
127 "HTTP/1.0 200 OK\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
133
Willy Tarreau0f772532006-12-23 20:51:41 +0100134 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100135 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
141
142 [HTTP_ERR_403] =
143 "HTTP/1.0 403 Forbidden\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
149
150 [HTTP_ERR_408] =
151 "HTTP/1.0 408 Request Time-out\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
157
158 [HTTP_ERR_500] =
159 "HTTP/1.0 500 Server Error\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
165
166 [HTTP_ERR_502] =
167 "HTTP/1.0 502 Bad Gateway\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
173
174 [HTTP_ERR_503] =
175 "HTTP/1.0 503 Service Unavailable\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
181
182 [HTTP_ERR_504] =
183 "HTTP/1.0 504 Gateway Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
189
190};
191
Cyril Bonté19979e12012-04-04 12:57:21 +0200192/* status codes available for the stats admin page (strictly 4 chars length) */
193const char *stat_status_codes[STAT_STATUS_SIZE] = {
194 [STAT_STATUS_DENY] = "DENY",
195 [STAT_STATUS_DONE] = "DONE",
196 [STAT_STATUS_ERRP] = "ERRP",
197 [STAT_STATUS_EXCD] = "EXCD",
198 [STAT_STATUS_NONE] = "NONE",
199 [STAT_STATUS_PART] = "PART",
200 [STAT_STATUS_UNKN] = "UNKN",
201};
202
203
Willy Tarreau80587432006-12-24 17:47:20 +0100204/* We must put the messages here since GCC cannot initialize consts depending
205 * on strlen().
206 */
207struct chunk http_err_chunks[HTTP_ERR_SIZE];
208
Willy Tarreau42250582007-04-01 01:30:43 +0200209#define FD_SETS_ARE_BITFIELDS
210#ifdef FD_SETS_ARE_BITFIELDS
211/*
212 * This map is used with all the FD_* macros to check whether a particular bit
213 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
214 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
215 * byte should be encoded. Be careful to always pass bytes from 0 to 255
216 * exclusively to the macros.
217 */
218fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220
221#else
222#error "Check if your OS uses bitfields for fd_sets"
223#endif
224
Willy Tarreau80587432006-12-24 17:47:20 +0100225void init_proto_http()
226{
Willy Tarreau42250582007-04-01 01:30:43 +0200227 int i;
228 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100229 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200230
Willy Tarreau80587432006-12-24 17:47:20 +0100231 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
232 if (!http_err_msgs[msg]) {
233 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
234 abort();
235 }
236
237 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
238 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
239 }
Willy Tarreau42250582007-04-01 01:30:43 +0200240
241 /* initialize the log header encoding map : '{|}"#' should be encoded with
242 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
243 * URL encoding only requires '"', '#' to be encoded as well as non-
244 * printable characters above.
245 */
246 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
247 memset(url_encode_map, 0, sizeof(url_encode_map));
248 for (i = 0; i < 32; i++) {
249 FD_SET(i, hdr_encode_map);
250 FD_SET(i, url_encode_map);
251 }
252 for (i = 127; i < 256; i++) {
253 FD_SET(i, hdr_encode_map);
254 FD_SET(i, url_encode_map);
255 }
256
257 tmp = "\"#{|}";
258 while (*tmp) {
259 FD_SET(*tmp, hdr_encode_map);
260 tmp++;
261 }
262
263 tmp = "\"#";
264 while (*tmp) {
265 FD_SET(*tmp, url_encode_map);
266 tmp++;
267 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200268
269 /* memory allocations */
270 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200271 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
392 int size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200393 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100394 "[%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",
Willy Tarreaue988a792010-01-04 21:13:14 +0100395 line,
396 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100397 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398 write(-1, trash, size);
399 size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200400 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100401 " %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",
Willy Tarreaue988a792010-01-04 21:13:14 +0100402 line,
403 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100404 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405
406 write(-1, trash, size);
407}
408#else
409#define http_silent_debug(l,s) do { } while (0)
410#endif
411
412/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100413 * Adds a header and its CRLF at the tail of the message's buffer, just before
414 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100415 * The header is also automatically added to the index <hdr_idx>, and the end
416 * of headers is automatically adjusted. The number of bytes added is returned
417 * on success, otherwise <0 is returned indicating an error.
418 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100419int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100420{
421 int bytes, len;
422
423 len = strlen(text);
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100424 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100425 if (!bytes)
426 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100427 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100428 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
429}
430
431/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100432 * Adds a header and its CRLF at the tail of the message's buffer, just before
433 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434 * the buffer is only opened and the space reserved, but nothing is copied.
435 * The header is also automatically added to the index <hdr_idx>, and the end
436 * of headers is automatically adjusted. The number of bytes added is returned
437 * on success, otherwise <0 is returned indicating an error.
438 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100439int http_header_add_tail2(struct http_msg *msg,
440 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100441{
442 int bytes;
443
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100444 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100445 if (!bytes)
446 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100447 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
449}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
451/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100452 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
453 * If so, returns the position of the first non-space character relative to
454 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
455 * to return a pointer to the place after the first space. Returns 0 if the
456 * header name does not match. Checks are case-insensitive.
457 */
458int http_header_match2(const char *hdr, const char *end,
459 const char *name, int len)
460{
461 const char *val;
462
463 if (hdr + len >= end)
464 return 0;
465 if (hdr[len] != ':')
466 return 0;
467 if (strncasecmp(hdr, name, len) != 0)
468 return 0;
469 val = hdr + len + 1;
470 while (val < end && HTTP_IS_SPHT(*val))
471 val++;
472 if ((val >= end) && (len + 2 <= end - hdr))
473 return len + 2; /* we may replace starting from second space */
474 return val - hdr;
475}
476
Willy Tarreau68085d82010-01-18 14:54:04 +0100477/* Find the end of the header value contained between <s> and <e>. See RFC2616,
478 * par 2.2 for more information. Note that it requires a valid header to return
479 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100481char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482{
483 int quoted, qdpair;
484
485 quoted = qdpair = 0;
486 for (; s < e; s++) {
487 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200488 else if (quoted) {
489 if (*s == '\\') qdpair = 1;
490 else if (*s == '"') quoted = 0;
491 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 else if (*s == '"') quoted = 1;
493 else if (*s == ',') return s;
494 }
495 return s;
496}
497
498/* Find the first or next occurrence of header <name> in message buffer <sol>
499 * using headers index <idx>, and return it in the <ctx> structure. This
500 * structure holds everything necessary to use the header and find next
501 * occurrence. If its <idx> member is 0, the header is searched from the
502 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 * 1 when it finds a value, and 0 when there is no more. It is designed to work
504 * with headers defined as comma-separated lists. As a special case, if ctx->val
505 * is NULL when searching for a new values of a header, the current header is
506 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 */
508int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
Willy Tarreau68085d82010-01-18 14:54:04 +0100512 char *eol, *sov;
513 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515 cur_idx = ctx->idx;
516 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 /* We have previously returned a value, let's search
518 * another one on the same line.
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200521 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100522 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200523 eol = sol + idx->v[cur_idx].len;
524
525 if (sov >= eol)
526 /* no more values in this header */
527 goto next_hdr;
528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 /* values remaining for this header, skip the comma but save it
530 * for later use (eg: for header deletion).
531 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 sov++;
533 while (sov < eol && http_is_lws[(unsigned char)*sov])
534 sov++;
535
536 goto return_hdr;
537 }
538
539 /* first request for this header */
540 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100541 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 while (cur_idx) {
544 eol = sol + idx->v[cur_idx].len;
545
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200546 if (len == 0) {
547 /* No argument was passed, we want any header.
548 * To achieve this, we simply build a fake request. */
549 while (sol + len < eol && sol[len] != ':')
550 len++;
551 name = sol;
552 }
553
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 if ((len < eol - sol) &&
555 (sol[len] == ':') &&
556 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 sov = sol + len + 1;
559 while (sov < eol && http_is_lws[(unsigned char)*sov])
560 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 ctx->prev = old_idx;
564 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 ctx->idx = cur_idx;
566 ctx->val = sov - sol;
567
568 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200570 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200571 eol--;
572 ctx->tws++;
573 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 ctx->vlen = eol - sov;
575 return 1;
576 }
577 next_hdr:
578 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100579 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 cur_idx = idx->v[cur_idx].next;
581 }
582 return 0;
583}
584
585int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200587 struct hdr_ctx *ctx)
588{
589 return http_find_header2(name, strlen(name), sol, idx, ctx);
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Remove one value of a header. This only works on a <ctx> returned by one of
593 * the http_find_header functions. The value is removed, as well as surrounding
594 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100595 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 * message <msg>. The new index is returned. If it is zero, it means there is
597 * no more header, so any processing may stop. The ctx is always left in a form
598 * that can be handled by http_find_header2() to find next occurrence.
599 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100600int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100601{
602 int cur_idx = ctx->idx;
603 char *sol = ctx->line;
604 struct hdr_idx_elem *hdr;
605 int delta, skip_comma;
606
607 if (!cur_idx)
608 return 0;
609
610 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100613 delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100614 http_msg_move_end(msg, delta);
615 idx->used--;
616 hdr->len = 0; /* unused entry */
617 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100618 if (idx->tail == ctx->idx)
619 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 ctx->idx = ctx->prev; /* walk back to the end of previous header */
621 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
622 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200623 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 return ctx->idx;
625 }
626
627 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200628 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
629 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 */
631
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100633 delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 NULL, 0);
636 hdr->len += delta;
637 http_msg_move_end(msg, delta);
638 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200639 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 return ctx->idx;
641}
642
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100643/* This function handles a server error at the stream interface level. The
644 * stream interface is assumed to be already in a closed state. An optional
645 * message is copied into the input buffer, and an HTTP status code stored.
646 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100647 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100649static void http_server_error(struct session *t, struct stream_interface *si,
650 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100652 buffer_auto_read(si->ob);
653 buffer_abort(si->ob);
654 buffer_auto_close(si->ob);
655 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200656 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100657 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100658 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100659 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200660 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661 }
662 if (!(t->flags & SN_ERR_MASK))
663 t->flags |= err;
664 if (!(t->flags & SN_FINST_MASK))
665 t->flags |= finst;
666}
667
Willy Tarreau80587432006-12-24 17:47:20 +0100668/* This function returns the appropriate error location for the given session
669 * and message.
670 */
671
672struct chunk *error_message(struct session *s, int msgnum)
673{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200674 if (s->be->errmsg[msgnum].str)
675 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100676 else if (s->fe->errmsg[msgnum].str)
677 return &s->fe->errmsg[msgnum];
678 else
679 return &http_err_chunks[msgnum];
680}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682/*
683 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
684 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
685 */
686static http_meth_t find_http_meth(const char *str, const int len)
687{
688 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100689 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100690
691 m = ((unsigned)*str - 'A');
692
693 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100694 for (h = http_methods[m]; h->len > 0; h++) {
695 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 };
700 return HTTP_METH_OTHER;
701 }
702 return HTTP_METH_NONE;
703
704}
705
Willy Tarreau21d2af32008-02-14 20:25:24 +0100706/* Parse the URI from the given transaction (which is assumed to be in request
707 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
708 * It is returned otherwise.
709 */
710static char *
711http_get_path(struct http_txn *txn)
712{
713 char *ptr, *end;
714
Willy Tarreau09d1e252012-05-18 22:36:34 +0200715 ptr = txn->req.buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100716 end = ptr + txn->req.sl.rq.u_l;
717
718 if (ptr >= end)
719 return NULL;
720
721 /* RFC2616, par. 5.1.2 :
722 * Request-URI = "*" | absuri | abspath | authority
723 */
724
725 if (*ptr == '*')
726 return NULL;
727
728 if (isalpha((unsigned char)*ptr)) {
729 /* this is a scheme as described by RFC3986, par. 3.1 */
730 ptr++;
731 while (ptr < end &&
732 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
733 ptr++;
734 /* skip '://' */
735 if (ptr == end || *ptr++ != ':')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 if (ptr == end || *ptr++ != '/')
740 return NULL;
741 }
742 /* skip [user[:passwd]@]host[:[port]] */
743
744 while (ptr < end && *ptr != '/')
745 ptr++;
746
747 if (ptr == end)
748 return NULL;
749
750 /* OK, we got the '/' ! */
751 return ptr;
752}
753
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754/* Returns a 302 for a redirectable request. This may only be called just after
755 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
756 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200757 * NOTE: this function is designed to support being called once data are scheduled
758 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100761{
762 struct http_txn *txn;
763 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100765 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200766 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100767
768 /* 1: create the response header */
769 rdr.len = strlen(HTTP_302);
770 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200771 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772 memcpy(rdr.str, HTTP_302, rdr.len);
773
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 srv = target_srv(&s->target);
775
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100780 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100781 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
782 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
783 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786 /* 3: add the request URI. Since it was already forwarded, we need
787 * to temporarily rewind the buffer.
788 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 txn = &s->txn;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200790 b_rew(s->req, rewind = s->req->o);
791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 path = http_get_path(txn);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200793 len = buffer_count(s->req, path, b_ptr(s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
794
795 b_adv(s->req, rewind);
796
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 if (!path)
798 return;
799
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200800 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801 return;
802
803 memcpy(rdr.str + rdr.len, path, len);
804 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100805
806 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
807 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
808 rdr.len += 29;
809 } else {
810 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
811 rdr.len += 23;
812 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200815 si_shutr(si);
816 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 si->err_type = SI_ET_NONE;
818 si->err_loc = NULL;
819 si->state = SI_ST_CLO;
820
821 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823
824 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 if (srv)
826 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827}
828
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 * that the server side is closed. Note that err_type is actually a
831 * bitmask, where almost only aborts may be cumulated with other
832 * values. We consider that aborted operations are more important
833 * than timeouts or errors due to the fact that nobody else in the
834 * logs might explain incomplete retries. All others should avoid
835 * being cumulated. It should normally not be possible to have multiple
836 * aborts at once, but just in case, the first one in sequence is reported.
837 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100838void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841
842 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
844 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
847 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
850 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
853 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100855 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
856 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100858 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
859 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100860 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100861 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
862 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100863}
864
Willy Tarreau42250582007-04-01 01:30:43 +0200865extern const char sess_term_cond[8];
866extern const char sess_fin_state[8];
867extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200868struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200869struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100870struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871
Willy Tarreau117f59e2007-03-04 18:17:17 +0100872/*
873 * Capture headers from message starting at <som> according to header list
874 * <cap_hdr>, and fill the <idx> structure appropriately.
875 */
876void capture_headers(char *som, struct hdr_idx *idx,
877 char **cap, struct cap_hdr *cap_hdr)
878{
879 char *eol, *sol, *col, *sov;
880 int cur_idx;
881 struct cap_hdr *h;
882 int len;
883
884 sol = som + hdr_idx_first_pos(idx);
885 cur_idx = hdr_idx_first_idx(idx);
886
887 while (cur_idx) {
888 eol = sol + idx->v[cur_idx].len;
889
890 col = sol;
891 while (col < eol && *col != ':')
892 col++;
893
894 sov = col + 1;
895 while (sov < eol && http_is_lws[(unsigned char)*sov])
896 sov++;
897
898 for (h = cap_hdr; h; h = h->next) {
899 if ((h->namelen == col - sol) &&
900 (strncasecmp(sol, h->name, h->namelen) == 0)) {
901 if (cap[h->index] == NULL)
902 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200903 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100904
905 if (cap[h->index] == NULL) {
906 Alert("HTTP capture : out of memory.\n");
907 continue;
908 }
909
910 len = eol - sov;
911 if (len > h->len)
912 len = h->len;
913
914 memcpy(cap[h->index], sov, len);
915 cap[h->index][len]=0;
916 }
917 }
918 sol = eol + idx->v[cur_idx].cr + 1;
919 cur_idx = idx->v[cur_idx].next;
920 }
921}
922
923
Willy Tarreau42250582007-04-01 01:30:43 +0200924/* either we find an LF at <ptr> or we jump to <bad>.
925 */
926#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
927
928/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
929 * otherwise to <http_msg_ood> with <state> set to <st>.
930 */
931#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
932 ptr++; \
933 if (likely(ptr < end)) \
934 goto good; \
935 else { \
936 state = (st); \
937 goto http_msg_ood; \
938 } \
939 } while (0)
940
941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100943 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
945 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
946 * will give undefined results.
947 * Note that it is upon the caller's responsibility to ensure that ptr < end,
948 * and that msg->sol points to the beginning of the response.
949 * If a complete line is found (which implies that at least one CR or LF is
950 * found before <end>, the updated <ptr> is returned, otherwise NULL is
951 * returned indicating an incomplete line (which does not mean that parts have
952 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
953 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
954 * upon next call.
955 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200956 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
958 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200959 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200961const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100962 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100963 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100964{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100965 const char *msg_start = msg->buf->p;
966
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100970 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
976 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100977 state = HTTP_MSG_ERROR;
978 break;
979
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200981 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100983 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 goto http_msg_rpcode;
985 }
986 if (likely(HTTP_IS_SPHT(*ptr)))
987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
988 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100989 state = HTTP_MSG_ERROR;
990 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200993 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 if (likely(!HTTP_IS_LWS(*ptr)))
995 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
996
997 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100998 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1000 }
1001
1002 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 http_msg_rsp_reason:
1005 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001006 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 msg->sl.st.r_l = 0;
1008 goto http_msg_rpline_eol;
1009
Willy Tarreau8973c702007-01-21 23:58:29 +01001010 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001011 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 goto http_msg_rpreason;
1015 }
1016 if (likely(HTTP_IS_SPHT(*ptr)))
1017 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1018 /* so it's a CR/LF, so there is no reason phrase */
1019 goto http_msg_rsp_reason;
1020
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001022 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 if (likely(!HTTP_IS_CRLF(*ptr)))
1024 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001025 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 http_msg_rpline_eol:
1027 /* We have seen the end of line. Note that we do not
1028 * necessarily have the \n yet, but at least we know that we
1029 * have EITHER \r OR \n, otherwise the response would not be
1030 * complete. We can then record the response length and return
1031 * to the caller which will be able to register it.
1032 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001033 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001034 return ptr;
1035
1036#ifdef DEBUG_FULL
1037 default:
1038 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1039 exit(1);
1040#endif
1041 }
1042
1043 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001044 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 if (ret_state)
1046 *ret_state = state;
1047 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001048 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001050}
1051
Willy Tarreau8973c702007-01-21 23:58:29 +01001052/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001053 * This function parses a request line between <ptr> and <end>, starting with
1054 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1055 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1056 * will give undefined results.
1057 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1058 * and that msg->sol points to the beginning of the request.
1059 * If a complete line is found (which implies that at least one CR or LF is
1060 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1061 * returned indicating an incomplete line (which does not mean that parts have
1062 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1063 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1064 * upon next call.
1065 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001066 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1068 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001069 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001071const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001072 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001073 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001075 const char *msg_start = msg->buf->p;
1076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001079 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 if (likely(HTTP_IS_TOKEN(*ptr)))
1081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 if (likely(HTTP_IS_CRLF(*ptr))) {
1089 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 msg->sl.rq.v_l = 0;
1098 goto http_msg_rqline_eol;
1099 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001100 state = HTTP_MSG_ERROR;
1101 break;
1102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 goto http_msg_rquri;
1108 }
1109 if (likely(HTTP_IS_SPHT(*ptr)))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1111 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1112 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001115 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001116 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001119 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001120 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001125 /* non-ASCII chars are forbidden unless option
1126 * accept-invalid-http-request is enabled in the frontend.
1127 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001129 if (msg->err_pos < -1)
1130 goto invalid_char;
1131 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001132 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1134 }
1135
1136 if (likely(HTTP_IS_CRLF(*ptr))) {
1137 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1138 goto http_msg_req09_uri_e;
1139 }
1140
1141 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001142 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001143 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001144 state = HTTP_MSG_ERROR;
1145 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001148 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001150 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 goto http_msg_rqver;
1152 }
1153 if (likely(HTTP_IS_SPHT(*ptr)))
1154 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1155 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1156 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001159 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001162
1163 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001165 http_msg_rqline_eol:
1166 /* We have seen the end of line. Note that we do not
1167 * necessarily have the \n yet, but at least we know that we
1168 * have EITHER \r OR \n, otherwise the request would not be
1169 * complete. We can then record the request length and return
1170 * to the caller which will be able to register it.
1171 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001172 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001173 return ptr;
1174 }
1175
1176 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001177 state = HTTP_MSG_ERROR;
1178 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180#ifdef DEBUG_FULL
1181 default:
1182 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1183 exit(1);
1184#endif
1185 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001188 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189 if (ret_state)
1190 *ret_state = state;
1191 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001192 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001195
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001196/*
1197 * Returns the data from Authorization header. Function may be called more
1198 * than once so data is stored in txn->auth_data. When no header is found
1199 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1200 * searching again for something we are unable to find anyway.
1201 */
1202
1203char get_http_auth_buff[BUFSIZE];
1204
1205int
1206get_http_auth(struct session *s)
1207{
1208
1209 struct http_txn *txn = &s->txn;
1210 struct chunk auth_method;
1211 struct hdr_ctx ctx;
1212 char *h, *p;
1213 int len;
1214
1215#ifdef DEBUG_AUTH
1216 printf("Auth for session %p: %d\n", s, txn->auth.method);
1217#endif
1218
1219 if (txn->auth.method == HTTP_AUTH_WRONG)
1220 return 0;
1221
1222 if (txn->auth.method)
1223 return 1;
1224
1225 txn->auth.method = HTTP_AUTH_WRONG;
1226
1227 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001228
1229 if (txn->flags & TX_USE_PX_CONN) {
1230 h = "Proxy-Authorization";
1231 len = strlen(h);
1232 } else {
1233 h = "Authorization";
1234 len = strlen(h);
1235 }
1236
Willy Tarreau09d1e252012-05-18 22:36:34 +02001237 if (!http_find_header2(h, len, s->req->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001238 return 0;
1239
1240 h = ctx.line + ctx.val;
1241
1242 p = memchr(h, ' ', ctx.vlen);
1243 if (!p || p == h)
1244 return 0;
1245
1246 chunk_initlen(&auth_method, h, 0, p-h);
1247 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1248
1249 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1250
1251 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1252 get_http_auth_buff, BUFSIZE - 1);
1253
1254 if (len < 0)
1255 return 0;
1256
1257
1258 get_http_auth_buff[len] = '\0';
1259
1260 p = strchr(get_http_auth_buff, ':');
1261
1262 if (!p)
1263 return 0;
1264
1265 txn->auth.user = get_http_auth_buff;
1266 *p = '\0';
1267 txn->auth.pass = p+1;
1268
1269 txn->auth.method = HTTP_AUTH_BASIC;
1270 return 1;
1271 }
1272
1273 return 0;
1274}
1275
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8973c702007-01-21 23:58:29 +01001277/*
1278 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001279 * depending on the initial msg->msg_state. The caller is responsible for
1280 * ensuring that the message does not wrap. The function can be preempted
1281 * everywhere when data are missing and recalled at the exact same location
1282 * with no information loss. The message may even be realigned between two
1283 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001285 * fields. Note that msg->sol will be initialized after completing the first
1286 * state, so that none of the msg pointers has to be initialized prior to the
1287 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001289void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001291 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001293 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 state = msg->msg_state;
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001296 ptr = buf->p + msg->next;
1297 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(ptr >= end))
1300 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 /*
1304 * First, states that are specific to the response only.
1305 * We check them first so that request and headers are
1306 * closer to each other (accessed more often).
1307 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001309 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001311 /* we have a start of message, but we have to check
1312 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001313 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001314 */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001315 if (unlikely(ptr != buf->p)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001316 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001317 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001318 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001319 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 }
Willy Tarreau26927362012-05-18 23:22:52 +02001321 msg->sol = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 hdr_idx_init(idx);
1323 state = HTTP_MSG_RPVER;
1324 goto http_msg_rpver;
1325 }
1326
1327 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1328 goto http_msg_invalid;
1329
1330 if (unlikely(*ptr == '\n'))
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1333 /* stop here */
1334
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001336 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 EXPECT_LF_HERE(ptr, http_msg_invalid);
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1339 /* stop here */
1340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001342 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 case HTTP_MSG_RPVER_SP:
1344 case HTTP_MSG_RPCODE:
1345 case HTTP_MSG_RPCODE_SP:
1346 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001347 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001348 state, ptr, end,
1349 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (unlikely(!ptr))
1351 return;
1352
1353 /* we have a full response and we know that we have either a CR
1354 * or an LF at <ptr>.
1355 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1357
Willy Tarreau3a215be2012-03-09 21:39:51 +01001358 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 if (likely(*ptr == '\r'))
1360 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1361 goto http_msg_rpline_end;
1362
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 /* msg->sol must point to the first of CR or LF. */
1366 EXPECT_LF_HERE(ptr, http_msg_invalid);
1367 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1368 /* stop here */
1369
1370 /*
1371 * Second, states that are specific to the request only
1372 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001374 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 /* we have a start of message, but we have to check
1377 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001378 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001380 if (likely(ptr != buf->p)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001381 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001382 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001383 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001384 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 }
Willy Tarreau26927362012-05-18 23:22:52 +02001386 msg->sol = 0;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001387 /* we will need this when keep-alive will be supported
1388 hdr_idx_init(idx);
1389 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 state = HTTP_MSG_RQMETH;
1391 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1395 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (unlikely(*ptr == '\n'))
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EXPECT_LF_HERE(ptr, http_msg_invalid);
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001406 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001409 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 case HTTP_MSG_RQMETH_SP:
1411 case HTTP_MSG_RQURI:
1412 case HTTP_MSG_RQURI_SP:
1413 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001414 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001415 state, ptr, end,
1416 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (unlikely(!ptr))
1418 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 /* we have a full request and we know that we have either a CR
1421 * or an LF at <ptr>.
1422 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau3a215be2012-03-09 21:39:51 +01001425 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (likely(*ptr == '\r'))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 /* check for HTTP/0.9 request : no version information available.
1433 * msg->sol must point to the first of CR or LF.
1434 */
1435 if (unlikely(msg->sl.rq.v_l == 0))
1436 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 EXPECT_LF_HERE(ptr, http_msg_invalid);
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 /*
1443 * Common states below
1444 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001447 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(!HTTP_IS_CRLF(*ptr))) {
1449 goto http_msg_hdr_name;
1450 }
1451
1452 if (likely(*ptr == '\r'))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1454 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 /* assumes msg->sol points to the first char */
1459 if (likely(HTTP_IS_TOKEN(*ptr)))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001462 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465 if (likely(msg->err_pos < -1) || *ptr == '\n')
1466 goto http_msg_invalid;
1467
1468 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001469 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001470
1471 /* and we still accept this non-token character */
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001475 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001476 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(HTTP_IS_SPHT(*ptr)))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001481 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (likely(!HTTP_IS_CRLF(*ptr))) {
1484 goto http_msg_hdr_val;
1485 }
1486
1487 if (likely(*ptr == '\r'))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1489 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EXPECT_LF_HERE(ptr, http_msg_invalid);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001497 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (likely(HTTP_IS_SPHT(*ptr))) {
1499 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001500 for (; buf->p + msg->sov < ptr; msg->sov++)
1501 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 goto http_msg_hdr_l1_sp;
1503 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001504 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001505 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 goto http_msg_complete_header;
1507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001509 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001510 /* assumes msg->sol points to the first char, and msg->sov
1511 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 */
1513 if (likely(!HTTP_IS_CRLF(*ptr)))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau12e48b32012-03-05 16:57:34 +01001516 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 /* Note: we could also copy eol into ->eoh so that we have the
1518 * real header end in case it ends with lots of LWS, but is this
1519 * really needed ?
1520 */
1521 if (likely(*ptr == '\r'))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1523 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001531 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1533 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001534 for (; buf->p + msg->eol < ptr; msg->eol++)
1535 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 goto http_msg_hdr_val;
1537 }
1538 http_msg_complete_header:
1539 /*
1540 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 * Assumes msg->sol points to the first char, msg->sov points
1542 * to the first character of the value and msg->eol to the
1543 * first CR or LF so we know how the line ends. We insert last
1544 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001546 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 idx, idx->tail) < 0))
1548 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreau3a215be2012-03-09 21:39:51 +01001550 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (likely(!HTTP_IS_CRLF(*ptr))) {
1552 goto http_msg_hdr_name;
1553 }
1554
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1557 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 /* Assumes msg->sol points to the first of either CR or LF */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001564 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001565 msg->eoh = msg->sol;
1566 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001567 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001569
1570 case HTTP_MSG_ERROR:
1571 /* this may only happen if we call http_msg_analyser() twice with an error */
1572 break;
1573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574#ifdef DEBUG_FULL
1575 default:
1576 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1577 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001578#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 }
1580 http_msg_ood:
1581 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001583 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 http_msg_invalid:
1587 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001589 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 return;
1591}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001592
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1594 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1595 * nothing is done and 1 is returned.
1596 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001597static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598{
1599 int delta;
1600 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001601 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602
1603 if (msg->sl.rq.v_l != 0)
1604 return 1;
1605
Willy Tarreau09d1e252012-05-18 22:36:34 +02001606 cur_end = msg->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 delta = 0;
1608
1609 if (msg->sl.rq.u_l == 0) {
1610 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001613 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 }
1615 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001616 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001617 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001618 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001619 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001620 HTTP_MSG_RQMETH,
Willy Tarreau09d1e252012-05-18 22:36:34 +02001621 msg->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 NULL, NULL);
1623 if (unlikely(!cur_end))
1624 return 0;
1625
1626 /* we have a full HTTP/1.0 request now and we know that
1627 * we have either a CR or an LF at <ptr>.
1628 */
1629 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1630 return 1;
1631}
1632
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001633/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001634 * and "keep-alive" values. If we already know that some headers may safely
1635 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001636 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1637 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1638 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1639 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1640 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001641 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001643void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001644{
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001646 const char *hdr_val = "Connection";
1647 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001648
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001650 return;
1651
Willy Tarreau88d349d2010-01-25 12:15:43 +01001652 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1653 hdr_val = "Proxy-Connection";
1654 hdr_len = 16;
1655 }
1656
Willy Tarreau5b154472009-12-21 20:11:07 +01001657 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001658 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001659 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1661 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001662 if (to_del & 2)
1663 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001664 else
1665 txn->flags |= TX_CON_KAL_SET;
1666 }
1667 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1668 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001669 if (to_del & 1)
1670 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 else
1672 txn->flags |= TX_CON_CLO_SET;
1673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001674 }
1675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676 txn->flags |= TX_HDR_CONN_PRS;
1677 return;
1678}
Willy Tarreau5b154472009-12-21 20:11:07 +01001679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680/* Apply desired changes on the Connection: header. Values may be removed and/or
1681 * added depending on the <wanted> flags, which are exclusively composed of
1682 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1683 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1684 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001685void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686{
1687 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688 const char *hdr_val = "Connection";
1689 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690
1691 ctx.idx = 0;
1692
Willy Tarreau88d349d2010-01-25 12:15:43 +01001693
1694 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1695 hdr_val = "Proxy-Connection";
1696 hdr_len = 16;
1697 }
1698
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001700 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1702 if (wanted & TX_CON_KAL_SET)
1703 txn->flags |= TX_CON_KAL_SET;
1704 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001705 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001706 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001707 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1708 if (wanted & TX_CON_CLO_SET)
1709 txn->flags |= TX_CON_CLO_SET;
1710 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001711 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001713 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001714
1715 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1716 return;
1717
1718 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1719 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001720 hdr_val = "Connection: close";
1721 hdr_len = 17;
1722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1723 hdr_val = "Proxy-Connection: close";
1724 hdr_len = 23;
1725 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001726 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001727 }
1728
1729 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1730 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001731 hdr_val = "Connection: keep-alive";
1732 hdr_len = 22;
1733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection: keep-alive";
1735 hdr_len = 28;
1736 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001737 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 }
1739 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001740}
1741
Willy Tarreaua458b672012-03-05 11:17:50 +01001742/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001744 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001745 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001747 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001748int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001749{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001750 const struct buffer *buf = msg->buf;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001751 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001752 const char *ptr_old = ptr;
1753 const char *end = buf->data + buf->size;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001754 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 unsigned int chunk = 0;
1756
1757 /* The chunk size is in the following form, though we are only
1758 * interested in the size and CRLF :
1759 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1760 */
1761 while (1) {
1762 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001763 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 if (c < 0) /* not a hex digit anymore */
1767 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 if (++ptr >= end)
1769 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001770 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001771 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001772 chunk = (chunk << 4) + c;
1773 }
1774
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001776 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001777 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778
1779 while (http_is_spht[(unsigned char)*ptr]) {
1780 if (++ptr >= end)
1781 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001782 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 }
1785
Willy Tarreaud98cf932009-12-27 22:54:55 +01001786 /* Up to there, we know that at least one byte is present at *ptr. Check
1787 * for the end of chunk size.
1788 */
1789 while (1) {
1790 if (likely(HTTP_IS_CRLF(*ptr))) {
1791 /* we now have a CR or an LF at ptr */
1792 if (likely(*ptr == '\r')) {
1793 if (++ptr >= end)
1794 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001795 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 return 0;
1797 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001798
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001800 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 if (++ptr >= end)
1802 ptr = buf->data;
1803 /* done */
1804 break;
1805 }
1806 else if (*ptr == ';') {
1807 /* chunk extension, ends at next CRLF */
1808 if (++ptr >= end)
1809 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001810 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001811 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812
1813 while (!HTTP_IS_CRLF(*ptr)) {
1814 if (++ptr >= end)
1815 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001816 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 return 0;
1818 }
1819 /* we have a CRLF now, loop above */
1820 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001823 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001824 }
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001830 msg->sov += ptr - ptr_old;
1831 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001832 msg->chunk_len = chunk;
1833 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001836 error:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001837 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001838 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001839}
1840
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001841/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001842 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001843 * the trailers is found, it is automatically scheduled to be forwarded,
1844 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1845 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001847 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001848 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001849 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1850 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001851 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001852 * matches the length of trailers already parsed and not forwarded. It is also
1853 * important to note that this function is designed to be able to parse wrapped
1854 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001856int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001858 const struct buffer *buf = msg->buf;
1859
Willy Tarreaua458b672012-03-05 11:17:50 +01001860 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001862 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001863 const char *ptr = b_ptr(buf, msg->next);
1864 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866
1867 /* scan current line and stop at LF or CRLF */
1868 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001869 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 return 0;
1871
1872 if (*ptr == '\n') {
1873 if (!p1)
1874 p1 = ptr;
1875 p2 = ptr;
1876 break;
1877 }
1878
1879 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001880 if (p1) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001881 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 p1 = ptr;
1885 }
1886
1887 ptr++;
1888 if (ptr >= buf->data + buf->size)
1889 ptr = buf->data;
1890 }
1891
1892 /* after LF; point to beginning of next line */
1893 p2++;
1894 if (p2 >= buf->data + buf->size)
1895 p2 = buf->data;
1896
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001897 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 if (bytes < 0)
1899 bytes += buf->size;
1900
1901 /* schedule this line for forwarding */
1902 msg->sov += bytes;
1903 if (msg->sov >= buf->size)
1904 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001906 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001907 /* LF/CRLF at beginning of line => end of trailers at p2.
1908 * Everything was scheduled for forwarding, there's nothing
1909 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001910 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001911 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 msg->msg_state = HTTP_MSG_DONE;
1913 return 1;
1914 }
1915 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 }
1918}
1919
1920/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1921 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001922 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001923 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001924 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1925 * not enough data are available, the function does not change anything and
1926 * returns zero. If a parse error is encountered, the function returns < 0 and
1927 * does not change anything. Note: this function is designed to parse wrapped
1928 * CRLF at the end of the buffer.
1929 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001930int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932 const struct buffer *buf = msg->buf;
1933 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 int bytes;
1935
1936 /* NB: we'll check data availabilty at the end. It's not a
1937 * problem because whatever we match first will be checked
1938 * against the correct length.
1939 */
1940 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001941 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942 if (*ptr == '\r') {
1943 bytes++;
1944 ptr++;
1945 if (ptr >= buf->data + buf->size)
1946 ptr = buf->data;
1947 }
1948
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001949 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 return 0;
1951
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 if (*ptr != '\n') {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001953 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956
1957 ptr++;
1958 if (ptr >= buf->data + buf->size)
1959 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001960 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1961 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001962 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1964 return 1;
1965}
Willy Tarreau5b154472009-12-21 20:11:07 +01001966
Willy Tarreaud787e662009-07-07 10:14:51 +02001967/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1968 * processing can continue on next analysers, or zero if it either needs more
1969 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1970 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1971 * when it has nothing left to do, and may remove any analyser when it wants to
1972 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001974int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975{
Willy Tarreau59234e92008-11-30 23:51:27 +01001976 /*
1977 * We will parse the partial (or complete) lines.
1978 * We will check the request syntax, and also join multi-line
1979 * headers. An index of all the lines will be elaborated while
1980 * parsing.
1981 *
1982 * For the parsing, we use a 28 states FSM.
1983 *
1984 * Here is the information we currently have :
Willy Tarreau26927362012-05-18 23:22:52 +02001985 * req->p = beginning of request
Willy Tarreauea1175a2012-03-05 15:52:30 +01001986 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau26927362012-05-18 23:22:52 +02001987 * req->p + req->i = end of input data
1988 * msg->eol = end of current header or line (LF or CRLF)
1989 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001990 *
1991 * At end of parsing, we may perform a capture of the error (if any), and
1992 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1993 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1994 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001995 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001996
Willy Tarreau59234e92008-11-30 23:51:27 +01001997 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001998 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001999 struct http_txn *txn = &s->txn;
2000 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002001 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002002
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002003 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 +01002004 now_ms, __FUNCTION__,
2005 s,
2006 req,
2007 req->rex, req->wex,
2008 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002009 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002010 req->analysers);
2011
Willy Tarreau52a0c602009-08-16 22:45:38 +02002012 /* we're speaking HTTP here, so let's speak HTTP to the client */
2013 s->srv_error = http_return_srv_error;
2014
Willy Tarreau83e3af02009-12-28 17:39:57 +01002015 /* There's a protected area at the end of the buffer for rewriting
2016 * purposes. We don't want to start to parse the request if the
2017 * protected area is affected, because we may have to move processed
2018 * data later, which is much more complicated.
2019 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002020 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002021 if ((txn->flags & TX_NOT_FIRST) &&
2022 unlikely((req->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002023 bi_end(req) < b_ptr(req, msg->next) ||
2024 bi_end(req) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002025 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002026 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2027 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002028 /* some data has still not left the buffer, wake us once that's done */
2029 buffer_dont_connect(req);
2030 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2031 return 0;
2032 }
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002033 if (bi_end(req) < b_ptr(req, msg->next) ||
2034 bi_end(req) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02002035 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002036 }
2037
Willy Tarreau065e8332010-01-08 00:30:20 +01002038 /* Note that we have the same problem with the response ; we
2039 * may want to send a redirect, error or anything which requires
2040 * some spare space. So we'll ensure that we have at least
2041 * maxrewrite bytes available in the response buffer before
2042 * processing that one. This will only affect pipelined
2043 * keep-alive requests.
2044 */
2045 if ((txn->flags & TX_NOT_FIRST) &&
2046 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002047 bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) ||
2048 bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002049 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002050 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2051 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002052 /* don't let a connection request be initiated */
2053 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002054 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002055 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002056 return 0;
2057 }
2058 }
2059
Willy Tarreaua458b672012-03-05 11:17:50 +01002060 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002061 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002062 }
2063
Willy Tarreau59234e92008-11-30 23:51:27 +01002064 /* 1: we might have to print this header in debug mode */
2065 if (unlikely((global.mode & MODE_DEBUG) &&
2066 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002067 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002068 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002069
Willy Tarreau26927362012-05-18 23:22:52 +02002070 sol = req->p;
Willy Tarreau59234e92008-11-30 23:51:27 +01002071 eol = sol + msg->sl.rq.l;
2072 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002073
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 sol += hdr_idx_first_pos(&txn->hdr_idx);
2075 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002076
Willy Tarreau59234e92008-11-30 23:51:27 +01002077 while (cur_idx) {
2078 eol = sol + txn->hdr_idx.v[cur_idx].len;
2079 debug_hdr("clihdr", s, sol, eol);
2080 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2081 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002082 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 }
2084
Willy Tarreau58f10d72006-12-04 02:26:12 +01002085
Willy Tarreau59234e92008-11-30 23:51:27 +01002086 /*
2087 * Now we quickly check if we have found a full valid request.
2088 * If not so, we check the FD and buffer states before leaving.
2089 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002090 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002091 * requests are checked first. When waiting for a second request
2092 * on a keep-alive session, if we encounter and error, close, t/o,
2093 * we note the error in the session flags but don't set any state.
2094 * Since the error will be noted there, it will not be counted by
2095 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002096 * Last, we may increase some tracked counters' http request errors on
2097 * the cases that are deliberately the client's fault. For instance,
2098 * a timeout or connection reset is not counted as an error. However
2099 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002101
Willy Tarreau655dce92009-11-08 13:10:58 +01002102 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002105 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002106 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002107 session_inc_http_req_ctr(s);
2108 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002109 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 /* 1: Since we are in header mode, if there's no space
2114 * left for headers, we won't be able to free more
2115 * later, so the session will never terminate. We
2116 * must terminate it now.
2117 */
2118 if (unlikely(req->flags & BF_FULL)) {
2119 /* FIXME: check if URI is set and return Status
2120 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002121 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002122 session_inc_http_req_ctr(s);
2123 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002124 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002125 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002126 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 goto return_bad_req;
2128 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 /* 2: have we encountered a read error ? */
2131 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002132 if (!(s->flags & SN_ERR_MASK))
2133 s->flags |= SN_ERR_CLICL;
2134
Willy Tarreaufcffa692010-01-10 14:21:19 +01002135 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002136 goto failed_keep_alive;
2137
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002139 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002140 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002141 session_inc_http_err_ctr(s);
2142 }
2143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 msg->msg_state = HTTP_MSG_ERROR;
2145 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002146
Willy Tarreauda7ff642010-06-23 11:44:09 +02002147 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002148 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002149 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002150 if (s->listener->counters)
2151 s->listener->counters->failed_req++;
2152
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 if (!(s->flags & SN_FINST_MASK))
2154 s->flags |= SN_FINST_R;
2155 return 0;
2156 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /* 3: has the read timeout expired ? */
2159 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002160 if (!(s->flags & SN_ERR_MASK))
2161 s->flags |= SN_ERR_CLITO;
2162
Willy Tarreaufcffa692010-01-10 14:21:19 +01002163 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002164 goto failed_keep_alive;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002168 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_err_ctr(s);
2170 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 txn->status = 408;
2172 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2173 msg->msg_state = HTTP_MSG_ERROR;
2174 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002175
Willy Tarreauda7ff642010-06-23 11:44:09 +02002176 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002177 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002178 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179 if (s->listener->counters)
2180 s->listener->counters->failed_req++;
2181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 if (!(s->flags & SN_FINST_MASK))
2183 s->flags |= SN_FINST_R;
2184 return 0;
2185 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 /* 4: have we encountered a close ? */
2188 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002189 if (!(s->flags & SN_ERR_MASK))
2190 s->flags |= SN_ERR_CLICL;
2191
Willy Tarreaufcffa692010-01-10 14:21:19 +01002192 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002193 goto failed_keep_alive;
2194
Willy Tarreau4076a152009-04-02 15:18:36 +02002195 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002196 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002197 txn->status = 400;
2198 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2199 msg->msg_state = HTTP_MSG_ERROR;
2200 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002201
Willy Tarreauda7ff642010-06-23 11:44:09 +02002202 session_inc_http_err_ctr(s);
2203 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002204 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002205 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002206 if (s->listener->counters)
2207 s->listener->counters->failed_req++;
2208
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 if (!(s->flags & SN_FINST_MASK))
2210 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002211 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002212 }
2213
Willy Tarreau520d95e2009-09-19 21:04:57 +02002214 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002215 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002216 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002217#ifdef TCP_QUICKACK
Willy Tarreau93548be2012-05-13 08:44:16 +02002218 if (s->listener->options & LI_O_NOQUICKACK && req->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002219 /* We need more data, we have to re-enable quick-ack in case we
2220 * previously disabled it, otherwise we might cause the client
2221 * to delay next data.
2222 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002223 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002224 }
2225#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002226
Willy Tarreaufcffa692010-01-10 14:21:19 +01002227 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2228 /* If the client starts to talk, let's fall back to
2229 * request timeout processing.
2230 */
2231 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002232 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002233 }
2234
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002236 if (!tick_isset(req->analyse_exp)) {
2237 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2238 (txn->flags & TX_WAIT_NEXT_RQ) &&
2239 tick_isset(s->be->timeout.httpka))
2240 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2241 else
2242 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002244
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 /* we're not ready yet */
2246 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002247
2248 failed_keep_alive:
2249 /* Here we process low-level errors for keep-alive requests. In
2250 * short, if the request is not the first one and it experiences
2251 * a timeout, read error or shutdown, we just silently close so
2252 * that the client can try again.
2253 */
2254 txn->status = 0;
2255 msg->msg_state = HTTP_MSG_RQBEFORE;
2256 req->analysers = 0;
2257 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002258 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002259 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002260 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002262
Willy Tarreaud787e662009-07-07 10:14:51 +02002263 /* OK now we have a complete HTTP request with indexed headers. Let's
2264 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002265 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2266 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002267 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002268 * byte after the last LF. msg->sov points to the first byte of data.
2269 * msg->eol cannot be trusted because it may have been left uninitialized
2270 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002271 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002272
Willy Tarreauda7ff642010-06-23 11:44:09 +02002273 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002274 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2275
Willy Tarreaub16a5742010-01-10 14:46:16 +01002276 if (txn->flags & TX_WAIT_NEXT_RQ) {
2277 /* kill the pending keep-alive timeout */
2278 txn->flags &= ~TX_WAIT_NEXT_RQ;
2279 req->analyse_exp = TICK_ETERNITY;
2280 }
2281
2282
Willy Tarreaud787e662009-07-07 10:14:51 +02002283 /* Maybe we found in invalid header name while we were configured not
2284 * to block on that, so we have to capture it now.
2285 */
2286 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002287 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002288
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 /*
2290 * 1: identify the method
2291 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002292 txn->meth = find_http_meth(req->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002293
2294 /* we can make use of server redirect on GET and HEAD */
2295 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2296 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002297
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 /*
2299 * 2: check if the URI matches the monitor_uri.
2300 * We have to do this for every request which gets in, because
2301 * the monitor-uri is defined by the frontend.
2302 */
2303 if (unlikely((s->fe->monitor_uri_len != 0) &&
2304 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002305 !memcmp(req->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 s->fe->monitor_uri,
2307 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002308 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002314 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002317 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002318 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002319
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 ret = acl_pass(ret);
2321 if (cond->pol == ACL_COND_UNLESS)
2322 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002323
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 if (ret) {
2325 /* we fail this request, let's return 503 service unavail */
2326 txn->status = 503;
2327 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2328 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002329 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002330 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* nothing to fail, let's reply normaly */
2333 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002334 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 goto return_prx_cond;
2336 }
2337
2338 /*
2339 * 3: Maybe we have to copy the original REQURI for the logs ?
2340 * Note: we cannot log anymore if the request has been
2341 * classified as invalid.
2342 */
2343 if (unlikely(s->logs.logwait & LW_REQ)) {
2344 /* we have a complete HTTP request that we must log */
2345 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2346 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 if (urilen >= REQURI_LEN)
2349 urilen = REQURI_LEN - 1;
Willy Tarreau26927362012-05-18 23:22:52 +02002350 memcpy(txn->uri, req->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 if (!(s->logs.logwait &= ~LW_REQ))
2354 s->do_log(s);
2355 } else {
2356 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002357 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 }
Willy Tarreau06619262006-12-17 08:37:22 +01002359
William Lallemanda73203e2012-03-12 12:48:57 +01002360 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2361 s->unique_id = pool_alloc2(pool2_uniqueid);
2362 }
2363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002365 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002366 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002367
Willy Tarreau5b154472009-12-21 20:11:07 +01002368 /* ... and check if the request is HTTP/1.1 or above */
2369 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002370 ((req->p[msg->sl.rq.v + 5] > '1') ||
2371 ((req->p[msg->sl.rq.v + 5] == '1') &&
2372 (req->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002373 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002374
2375 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002376 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002377
Willy Tarreau88d349d2010-01-25 12:15:43 +01002378 /* if the frontend has "option http-use-proxy-header", we'll check if
2379 * we have what looks like a proxied connection instead of a connection,
2380 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2381 * Note that this is *not* RFC-compliant, however browsers and proxies
2382 * happen to do that despite being non-standard :-(
2383 * We consider that a request not beginning with either '/' or '*' is
2384 * a proxied connection, which covers both "scheme://location" and
2385 * CONNECT ip:port.
2386 */
2387 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002388 req->p[msg->sl.rq.u] != '/' && req->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002389 txn->flags |= TX_USE_PX_CONN;
2390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002391 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002392 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002393
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002395 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02002396 capture_headers(req->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002398
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002399 /* 6: determine the transfer-length.
2400 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2401 * the presence of a message-body in a REQUEST and its transfer length
2402 * must be determined that way (in order of precedence) :
2403 * 1. The presence of a message-body in a request is signaled by the
2404 * inclusion of a Content-Length or Transfer-Encoding header field
2405 * in the request's header fields. When a request message contains
2406 * both a message-body of non-zero length and a method that does
2407 * not define any semantics for that request message-body, then an
2408 * origin server SHOULD either ignore the message-body or respond
2409 * with an appropriate error message (e.g., 413). A proxy or
2410 * gateway, when presented the same request, SHOULD either forward
2411 * the request inbound with the message- body or ignore the
2412 * message-body when determining a response.
2413 *
2414 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2415 * and the "chunked" transfer-coding (Section 6.2) is used, the
2416 * transfer-length is defined by the use of this transfer-coding.
2417 * If a Transfer-Encoding header field is present and the "chunked"
2418 * transfer-coding is not present, the transfer-length is defined
2419 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002420 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002421 * 3. If a Content-Length header field is present, its decimal value in
2422 * OCTETs represents both the entity-length and the transfer-length.
2423 * If a message is received with both a Transfer-Encoding header
2424 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002425 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426 * 4. By the server closing the connection. (Closing the connection
2427 * cannot be used to indicate the end of a request body, since that
2428 * would leave no possibility for the server to send back a response.)
2429 *
2430 * Whenever a transfer-coding is applied to a message-body, the set of
2431 * transfer-codings MUST include "chunked", unless the message indicates
2432 * it is terminated by closing the connection. When the "chunked"
2433 * transfer-coding is used, it MUST be the last transfer-coding applied
2434 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002435 */
2436
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002437 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002438 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002439 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002440 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002441 http_find_header2("Transfer-Encoding", 17, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002443 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2444 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 /* bad transfer-encoding (chunked followed by something else) */
2446 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002447 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 break;
2449 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002450 }
2451
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002453 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002454 http_find_header2("Content-Length", 14, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 signed long long cl;
2456
Willy Tarreauad14f752011-09-02 20:33:27 +02002457 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002458 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002460 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002461
Willy Tarreauad14f752011-09-02 20:33:27 +02002462 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002463 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002465 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002466
Willy Tarreauad14f752011-09-02 20:33:27 +02002467 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002468 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002469 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002470 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002471
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002472 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002473 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002475 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002477 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002478 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 }
2480
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 /* bodyless requests have a known length */
2482 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002483 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484
Willy Tarreaud787e662009-07-07 10:14:51 +02002485 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002486 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 req->analyse_exp = TICK_ETERNITY;
2488 return 1;
2489
2490 return_bad_req:
2491 /* We centralize bad requests processing here */
2492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2493 /* we detected a parsing error. We want to archive this request
2494 * in the dedicated proxy area for later troubleshooting.
2495 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002496 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002497 }
2498
2499 txn->req.msg_state = HTTP_MSG_ERROR;
2500 txn->status = 400;
2501 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002503 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504 if (s->listener->counters)
2505 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002506
2507 return_prx_cond:
2508 if (!(s->flags & SN_ERR_MASK))
2509 s->flags |= SN_ERR_PRXCOND;
2510 if (!(s->flags & SN_FINST_MASK))
2511 s->flags |= SN_FINST_R;
2512
2513 req->analysers = 0;
2514 req->analyse_exp = TICK_ETERNITY;
2515 return 0;
2516}
2517
Cyril Bonté70be45d2010-10-12 00:14:35 +02002518/* We reached the stats page through a POST request.
2519 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002520 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002521 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002522int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002523{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002524 struct proxy *px = NULL;
2525 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002526
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002527 char key[LINESIZE];
2528 int action = ST_ADM_ACTION_NONE;
2529 int reprocess = 0;
2530
2531 int total_servers = 0;
2532 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002533
2534 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002535 char *st_cur_param = NULL;
2536 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002537
Willy Tarreauea1175a2012-03-05 15:52:30 +01002538 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002539 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002540
2541 cur_param = next_param = end_params;
2542
Cyril Bonté23b39d92011-02-10 22:54:44 +01002543 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002544 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002545 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002546 return 1;
2547 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002548 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002549 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002550 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002551 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002552 }
2553
2554 *end_params = '\0';
2555
Willy Tarreau295a8372011-03-10 11:25:07 +01002556 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002557
2558 /*
2559 * Parse the parameters in reverse order to only store the last value.
2560 * From the html form, the backend and the action are at the end.
2561 */
2562 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002563 char *value;
2564 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565
2566 cur_param--;
2567 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002568 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002570 poffset = (cur_param != first_param ? 1 : 0);
2571 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2572 if ((plen > 0) && (plen <= sizeof(key))) {
2573 strncpy(key, cur_param + poffset, plen);
2574 key[plen - 1] = '\0';
2575 } else {
2576 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2577 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002578 }
2579
2580 /* Parse the value */
2581 value = key;
2582 while (*value != '\0' && *value != '=') {
2583 value++;
2584 }
2585 if (*value == '=') {
2586 /* Ok, a value is found, we can mark the end of the key */
2587 *value++ = '\0';
2588 }
2589
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002590 if (!url_decode(key) || !url_decode(value))
2591 break;
2592
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002594 if (!px && (strcmp(key, "b") == 0)) {
2595 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2596 /* the backend name is unknown or ambiguous (duplicate names) */
2597 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2598 goto out;
2599 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002600 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002601 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002602 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002603 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604 }
2605 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002606 action = ST_ADM_ACTION_ENABLE;
2607 }
2608 else {
2609 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2610 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002611 }
2612 }
2613 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002614 if (!(px && action)) {
2615 /*
2616 * Indicates that we'll need to reprocess the parameters
2617 * as soon as backend and action are known
2618 */
2619 if (!reprocess) {
2620 st_cur_param = cur_param;
2621 st_next_param = next_param;
2622 }
2623 reprocess = 1;
2624 }
2625 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002626 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002627 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002628 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002629 /* Not already in maintenance, we can change the server state */
2630 sv->state |= SRV_MAINTAIN;
2631 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002632 altered_servers++;
2633 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 }
2635 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002636 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002637 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002638 /* Already in maintenance, we can change the server state */
2639 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002640 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002641 altered_servers++;
2642 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 }
2644 break;
2645 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002646 } else {
2647 /* the server name is unknown or ambiguous (duplicate names) */
2648 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649 }
2650 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002651 if (reprocess && px && action) {
2652 /* Now, we know the backend and the action chosen by the user.
2653 * We can safely restart from the first server parameter
2654 * to reprocess them
2655 */
2656 cur_param = st_cur_param;
2657 next_param = st_next_param;
2658 reprocess = 0;
2659 goto reprocess_servers;
2660 }
2661
Cyril Bonté70be45d2010-10-12 00:14:35 +02002662 next_param = cur_param;
2663 }
2664 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002665
2666 if (total_servers == 0) {
2667 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2668 }
2669 else if (altered_servers == 0) {
2670 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2671 }
2672 else if (altered_servers == total_servers) {
2673 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2674 }
2675 else {
2676 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2677 }
2678 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002679 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002680}
2681
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002682/* returns a pointer to the first rule which forbids access (deny or http_auth),
2683 * or NULL if everything's OK.
2684 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002685static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002686http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2687{
Willy Tarreauff011f22011-01-06 17:51:27 +01002688 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002689
Willy Tarreauff011f22011-01-06 17:51:27 +01002690 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002691 int ret = 1;
2692
Willy Tarreauff011f22011-01-06 17:51:27 +01002693 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002694 continue;
2695
2696 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002697 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002698 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002699 ret = acl_pass(ret);
2700
Willy Tarreauff011f22011-01-06 17:51:27 +01002701 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002702 ret = !ret;
2703 }
2704
2705 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002706 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002707 return NULL; /* no problem */
2708 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002709 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002710 }
2711 }
2712 return NULL;
2713}
2714
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002715/* This stream analyser runs all HTTP request processing which is common to
2716 * frontends and backends, which means blocking ACLs, filters, connection-close,
2717 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002718 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 * either needs more data or wants to immediately abort the request (eg: deny,
2720 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002721 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002723{
Willy Tarreaud787e662009-07-07 10:14:51 +02002724 struct http_txn *txn = &s->txn;
2725 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002726 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002727 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002728 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002729 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002730 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002731
Willy Tarreau655dce92009-11-08 13:10:58 +01002732 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002733 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002734 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002735 return 0;
2736 }
2737
Willy Tarreau3a816292009-07-07 10:55:49 +02002738 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002739 req->analyse_exp = TICK_ETERNITY;
2740
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002741 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 +02002742 now_ms, __FUNCTION__,
2743 s,
2744 req,
2745 req->rex, req->wex,
2746 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002747 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002748 req->analysers);
2749
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 /* first check whether we have some ACLs set to block this request */
2751 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002752 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 ret = acl_pass(ret);
2755 if (cond->pol == ACL_COND_UNLESS)
2756 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002757
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002758 if (ret) {
2759 txn->status = 403;
2760 /* let's log the request time */
2761 s->logs.tv_request = now;
2762 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002763 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002764 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002766 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002767
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002768 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002769 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002770
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002771 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002772 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002773 do_stats = stats_check_uri(s->rep->prod, txn, px);
2774 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002775 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002776 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002777 else
2778 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002779
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002780 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002781 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002782 txn->status = 403;
2783 s->logs.tv_request = now;
2784 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002785 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002786 s->fe->fe_counters.denied_req++;
2787 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2788 s->be->be_counters.denied_req++;
2789 if (s->listener->counters)
2790 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002791 goto return_prx_cond;
2792 }
2793
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 /* try headers filters */
2795 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002796 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002798
Willy Tarreau59234e92008-11-30 23:51:27 +01002799 /* has the request been denied ? */
2800 if (txn->flags & TX_CLDENY) {
2801 /* no need to go further */
2802 txn->status = 403;
2803 /* let's log the request time */
2804 s->logs.tv_request = now;
2805 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002806 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 goto return_prx_cond;
2808 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002809
2810 /* When a connection is tarpitted, we use the tarpit timeout,
2811 * which may be the same as the connect timeout if unspecified.
2812 * If unset, then set it to zero because we really want it to
2813 * eventually expire. We build the tarpit as an analyser.
2814 */
2815 if (txn->flags & TX_CLTARPIT) {
2816 buffer_erase(s->req);
2817 /* wipe the request out so that we can drop the connection early
2818 * if the client closes first.
2819 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002820 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002821 req->analysers = 0; /* remove switching rules etc... */
2822 req->analysers |= AN_REQ_HTTP_TARPIT;
2823 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2824 if (!req->analyse_exp)
2825 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002827 return 1;
2828 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002829 }
Willy Tarreau06619262006-12-17 08:37:22 +01002830
Willy Tarreau5b154472009-12-21 20:11:07 +01002831 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2832 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002833 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2834 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002835 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2836 * avoid to redo the same work if FE and BE have the same settings (common).
2837 * The method consists in checking if options changed between the two calls
2838 * (implying that either one is non-null, or one of them is non-null and we
2839 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002840 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002841
Willy Tarreaudc008c52010-02-01 16:20:08 +01002842 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2843 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2844 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2845 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002846 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002847
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002848 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2849 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002850 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002851 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2852 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002853 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002854 tmp = TX_CON_WANT_CLO;
2855
Willy Tarreau5b154472009-12-21 20:11:07 +01002856 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2857 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002858
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002859 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2860 /* parse the Connection header and possibly clean it */
2861 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002862 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002863 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2864 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002865 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002866 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002867 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002868 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002869 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002870
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002871 /* check if client or config asks for explicit close in KAL/SCL */
2872 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2873 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2874 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002875 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002876 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002877 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002878 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002879 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2880 }
Willy Tarreau78599912009-10-17 20:12:21 +02002881
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002882 /* we can be blocked here because the request needs to be authenticated,
2883 * either to pass or to access stats.
2884 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002885 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002886 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002887 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002888
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002889 if (!realm)
2890 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2891
Willy Tarreau844a7e72010-01-31 21:46:18 +01002892 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002893 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002894 txn->status = 401;
2895 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002896 /* on 401 we still count one error, because normal browsing
2897 * won't significantly increase the counter but brute force
2898 * attempts will.
2899 */
2900 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002901 goto return_prx_cond;
2902 }
2903
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002904 /* add request headers from the rule sets in the same order */
2905 list_for_each_entry(wl, &px->req_add, list) {
2906 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002907 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002908 ret = acl_pass(ret);
2909 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2910 ret = !ret;
2911 if (!ret)
2912 continue;
2913 }
2914
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002915 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002916 goto return_bad_req;
2917 }
2918
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002919 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002920 struct stats_admin_rule *stats_admin_rule;
2921
2922 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002923 * FIXME!!! that one is rather dangerous, we want to
2924 * make it follow standard rules (eg: clear req->analysers).
2925 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002926
Cyril Bonté474be412010-10-12 00:14:36 +02002927 /* now check whether we have some admin rules for this request */
2928 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2929 int ret = 1;
2930
2931 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002932 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Cyril Bonté474be412010-10-12 00:14:36 +02002933 ret = acl_pass(ret);
2934 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2935 ret = !ret;
2936 }
2937
2938 if (ret) {
2939 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002940 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002941 break;
2942 }
2943 }
2944
Cyril Bonté70be45d2010-10-12 00:14:35 +02002945 /* Was the status page requested with a POST ? */
2946 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002947 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002948 if (msg->msg_state < HTTP_MSG_100_SENT) {
2949 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2950 * send an HTTP/1.1 100 Continue intermediate response.
2951 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002952 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002953 struct hdr_ctx ctx;
2954 ctx.idx = 0;
2955 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002956 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002957 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002958 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01002959 }
2960 }
2961 msg->msg_state = HTTP_MSG_100_SENT;
2962 s->logs.tv_request = now; /* update the request timer to reflect full request */
2963 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002964 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002965 /* we need more data */
2966 req->analysers |= an_bit;
2967 buffer_dont_connect(req);
2968 return 0;
2969 }
Cyril Bonté474be412010-10-12 00:14:36 +02002970 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002971 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002972 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002973 }
2974
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002975 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002976 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002977 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02002978 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +02002979 s->rep->prod->conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01002980 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002981 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02002982 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
2983 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002984
2985 return 0;
2986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989 /* check whether we have some ACLs set to redirect this request */
2990 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002991 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002992
Willy Tarreauf285f542010-01-03 20:03:03 +01002993 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002994 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002995 ret = acl_pass(ret);
2996 if (rule->cond->pol == ACL_COND_UNLESS)
2997 ret = !ret;
2998 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002999
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003000 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003001 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003003
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003004 /* build redirect message */
3005 switch(rule->code) {
3006 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 msg_fmt = HTTP_303;
3008 break;
3009 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 msg_fmt = HTTP_301;
3011 break;
3012 case 302:
3013 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 msg_fmt = HTTP_302;
3015 break;
3016 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003017
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003018 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003020
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 switch(rule->type) {
3022 case REDIRECT_TYPE_PREFIX: {
3023 const char *path;
3024 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003026 path = http_get_path(txn);
3027 /* build message using path */
3028 if (path) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003029 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3031 int qs = 0;
3032 while (qs < pathlen) {
3033 if (path[qs] == '?') {
3034 pathlen = qs;
3035 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003036 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003038 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003039 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003040 } else {
3041 path = "/";
3042 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003043 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003045 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 goto return_bad_req;
3047
3048 /* add prefix. Note that if prefix == "/", we don't want to
3049 * add anything, otherwise it makes it hard for the user to
3050 * configure a self-redirection.
3051 */
3052 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003053 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3054 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003055 }
3056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 /* add path */
3058 memcpy(rdr.str + rdr.len, path, pathlen);
3059 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003060
3061 /* append a slash at the end of the location is needed and missing */
3062 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3063 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3064 if (rdr.len > rdr.size - 5)
3065 goto return_bad_req;
3066 rdr.str[rdr.len] = '/';
3067 rdr.len++;
3068 }
3069
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003070 break;
3071 }
3072 case REDIRECT_TYPE_LOCATION:
3073 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003074 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 /* add location */
3078 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3079 rdr.len += rule->rdr_len;
3080 break;
3081 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 if (rule->cookie_len) {
3084 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3085 rdr.len += 14;
3086 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3087 rdr.len += rule->cookie_len;
3088 memcpy(rdr.str + rdr.len, "\r\n", 2);
3089 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003092 /* add end of headers and the keep-alive/close status.
3093 * We may choose to set keep-alive if the Location begins
3094 * with a slash, because the client will come back to the
3095 * same server.
3096 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003097 txn->status = rule->code;
3098 /* let's log the request time */
3099 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003100
3101 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003102 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3103 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003104 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3105 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3106 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003107 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003108 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3109 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3110 rdr.len += 30;
3111 } else {
3112 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3113 rdr.len += 24;
3114 }
Willy Tarreau75661452010-01-10 10:35:01 +01003115 }
3116 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3117 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003118 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003119 /* "eat" the request */
Willy Tarreau26927362012-05-18 23:22:52 +02003120 bi_fast_delete(req, msg->sov);
3121 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003122 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003123 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3124 txn->req.msg_state = HTTP_MSG_CLOSED;
3125 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003126 break;
3127 } else {
3128 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003129 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3130 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3131 rdr.len += 29;
3132 } else {
3133 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3134 rdr.len += 23;
3135 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003136 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003137 goto return_prx_cond;
3138 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003139 }
3140 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003141
Willy Tarreau2be39392010-01-03 17:24:51 +01003142 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3143 * If this happens, then the data will not come immediately, so we must
3144 * send all what we have without waiting. Note that due to the small gain
3145 * in waiting for the body of the request, it's easier to simply put the
3146 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3147 * itself once used.
3148 */
3149 req->flags |= BF_SEND_DONTWAIT;
3150
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003151 /* that's OK for us now, let's move on to next analysers */
3152 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003153
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 return_bad_req:
3155 /* We centralize bad requests processing here */
3156 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3157 /* we detected a parsing error. We want to archive this request
3158 * in the dedicated proxy area for later troubleshooting.
3159 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003160 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003161 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003162
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003163 txn->req.msg_state = HTTP_MSG_ERROR;
3164 txn->status = 400;
3165 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003166
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003167 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003168 if (s->listener->counters)
3169 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003170
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003171 return_prx_cond:
3172 if (!(s->flags & SN_ERR_MASK))
3173 s->flags |= SN_ERR_PRXCOND;
3174 if (!(s->flags & SN_FINST_MASK))
3175 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003176
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003177 req->analysers = 0;
3178 req->analyse_exp = TICK_ETERNITY;
3179 return 0;
3180}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003181
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003182/* This function performs all the processing enabled for the current request.
3183 * It returns 1 if the processing can continue on next analysers, or zero if it
3184 * needs more data, encounters an error, or wants to immediately abort the
3185 * request. It relies on buffers flags, and updates s->req->analysers.
3186 */
3187int http_process_request(struct session *s, struct buffer *req, int an_bit)
3188{
3189 struct http_txn *txn = &s->txn;
3190 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003191
Willy Tarreau655dce92009-11-08 13:10:58 +01003192 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003193 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003194 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003195 return 0;
3196 }
3197
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003198 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 +02003199 now_ms, __FUNCTION__,
3200 s,
3201 req,
3202 req->rex, req->wex,
3203 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003204 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003205 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003206
Willy Tarreau59234e92008-11-30 23:51:27 +01003207 /*
3208 * Right now, we know that we have processed the entire headers
3209 * and that unwanted requests have been filtered out. We can do
3210 * whatever we want with the remaining request. Also, now we
3211 * may have separate values for ->fe, ->be.
3212 */
Willy Tarreau06619262006-12-17 08:37:22 +01003213
Willy Tarreau59234e92008-11-30 23:51:27 +01003214 /*
3215 * If HTTP PROXY is set we simply get remote server address
3216 * parsing incoming request.
3217 */
3218 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003219 url2sa(req->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003221
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003223 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003224 * Note that doing so might move headers in the request, but
3225 * the fields will stay coherent and the URI will not move.
3226 * This should only be performed in the backend.
3227 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003228 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003229 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3230 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003231
Willy Tarreau59234e92008-11-30 23:51:27 +01003232 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003233 * 8: the appsession cookie was looked up very early in 1.2,
3234 * so let's do the same now.
3235 */
3236
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003237 /* It needs to look into the URI unless persistence must be ignored */
3238 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003239 get_srv_from_appsession(s, req->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003240 }
3241
William Lallemanda73203e2012-03-12 12:48:57 +01003242 /* add unique-id if "header-unique-id" is specified */
3243
3244 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3245 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3246
3247 if (s->fe->header_unique_id && s->unique_id) {
3248 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3249 if (ret < 0 || ret > global.tune.bufsize)
3250 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003251 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003252 goto return_bad_req;
3253 }
3254
Cyril Bontéb21570a2009-11-29 20:04:48 +01003255 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 * 9: add X-Forwarded-For if either the frontend or the backend
3257 * asks for it.
3258 */
3259 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003260 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003261 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003262 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3263 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
3264 req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003265 /* The header is set to be added only if none is present
3266 * and we found it, so don't do anything.
3267 */
3268 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003269 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 /* Add an X-Forwarded-For header unless the source IP is
3271 * in the 'except' network range.
3272 */
3273 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003274 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003275 != s->fe->except_net.s_addr) &&
3276 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003277 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003279 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003280 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003281 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003282
3283 /* Note: we rely on the backend to get the header name to be used for
3284 * x-forwarded-for, because the header is really meant for the backends.
3285 * However, if the backend did not specify any option, we have to rely
3286 * on the frontend's header name.
3287 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 if (s->be->fwdfor_hdr_len) {
3289 len = s->be->fwdfor_hdr_len;
3290 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003291 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 len = s->fe->fwdfor_hdr_len;
3293 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003294 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003295 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003296
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003297 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003298 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003299 }
3300 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003301 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 /* FIXME: for the sake of completeness, we should also support
3303 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003304 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 int len;
3306 char pn[INET6_ADDRSTRLEN];
3307 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003308 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003310
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 /* Note: we rely on the backend to get the header name to be used for
3312 * x-forwarded-for, because the header is really meant for the backends.
3313 * However, if the backend did not specify any option, we have to rely
3314 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003315 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 if (s->be->fwdfor_hdr_len) {
3317 len = s->be->fwdfor_hdr_len;
3318 memcpy(trash, s->be->fwdfor_hdr_name, len);
3319 } else {
3320 len = s->fe->fwdfor_hdr_len;
3321 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003322 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003323 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003324
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003325 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003326 goto return_bad_req;
3327 }
3328 }
3329
3330 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003331 * 10: add X-Original-To if either the frontend or the backend
3332 * asks for it.
3333 */
3334 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3335
3336 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003337 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003338 /* Add an X-Original-To header unless the destination IP is
3339 * in the 'except' network range.
3340 */
Willy Tarreau59b94792012-05-11 16:16:40 +02003341 si_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003342
Willy Tarreau6471afb2011-09-23 10:54:59 +02003343 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003344 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003345 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003346 != s->fe->except_to.s_addr) &&
3347 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003348 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003349 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003350 int len;
3351 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003352 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003353
3354 /* Note: we rely on the backend to get the header name to be used for
3355 * x-original-to, because the header is really meant for the backends.
3356 * However, if the backend did not specify any option, we have to rely
3357 * on the frontend's header name.
3358 */
3359 if (s->be->orgto_hdr_len) {
3360 len = s->be->orgto_hdr_len;
3361 memcpy(trash, s->be->orgto_hdr_name, len);
3362 } else {
3363 len = s->fe->orgto_hdr_len;
3364 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003365 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003366 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3367
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003368 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003369 goto return_bad_req;
3370 }
3371 }
3372 }
3373
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003374 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3375 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003376 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003377 unsigned int want_flags = 0;
3378
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003379 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003380 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3381 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3382 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003383 want_flags |= TX_CON_CLO_SET;
3384 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003385 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3386 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3387 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003388 want_flags |= TX_CON_KAL_SET;
3389 }
3390
3391 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003392 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003393 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003394
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003395
Willy Tarreau522d6c02009-12-06 18:49:18 +01003396 /* If we have no server assigned yet and we're balancing on url_param
3397 * with a POST request, we may be interested in checking the body for
3398 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 */
3400 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3401 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003402 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003403 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003404 buffer_dont_connect(req);
3405 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003406 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003407
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003408 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003409 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003410#ifdef TCP_QUICKACK
3411 /* We expect some data from the client. Unless we know for sure
3412 * we already have a full request, we have to re-enable quick-ack
3413 * in case we previously disabled it, otherwise we might cause
3414 * the client to delay further data.
3415 */
3416 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003417 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003418 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003419 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003420#endif
3421 }
Willy Tarreau03945942009-12-22 16:50:27 +01003422
Willy Tarreau59234e92008-11-30 23:51:27 +01003423 /*************************************************************
3424 * OK, that's finished for the headers. We have done what we *
3425 * could. Let's switch to the DATA state. *
3426 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003427 req->analyse_exp = TICK_ETERNITY;
3428 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003429
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003430 /* if the server closes the connection, we want to immediately react
3431 * and close the socket to save packets and syscalls.
3432 */
3433 req->cons->flags |= SI_FL_NOHALF;
3434
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 /* OK let's go on with the BODY now */
3437 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003438
Willy Tarreau59234e92008-11-30 23:51:27 +01003439 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003440 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003441 /* we detected a parsing error. We want to archive this request
3442 * in the dedicated proxy area for later troubleshooting.
3443 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003444 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003445 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003446
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 txn->req.msg_state = HTTP_MSG_ERROR;
3448 txn->status = 400;
3449 req->analysers = 0;
3450 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003451
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003452 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003453 if (s->listener->counters)
3454 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003455
Willy Tarreau59234e92008-11-30 23:51:27 +01003456 if (!(s->flags & SN_ERR_MASK))
3457 s->flags |= SN_ERR_PRXCOND;
3458 if (!(s->flags & SN_FINST_MASK))
3459 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003460 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003461}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003462
Willy Tarreau60b85b02008-11-30 23:28:40 +01003463/* This function is an analyser which processes the HTTP tarpit. It always
3464 * returns zero, at the beginning because it prevents any other processing
3465 * from occurring, and at the end because it terminates the request.
3466 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003467int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003468{
3469 struct http_txn *txn = &s->txn;
3470
3471 /* This connection is being tarpitted. The CLIENT side has
3472 * already set the connect expiration date to the right
3473 * timeout. We just have to check that the client is still
3474 * there and that the timeout has not expired.
3475 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003476 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003477 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3478 !tick_is_expired(req->analyse_exp, now_ms))
3479 return 0;
3480
3481 /* We will set the queue timer to the time spent, just for
3482 * logging purposes. We fake a 500 server error, so that the
3483 * attacker will not suspect his connection has been tarpitted.
3484 * It will not cause trouble to the logs because we can exclude
3485 * the tarpitted connections by filtering on the 'PT' status flags.
3486 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003487 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3488
3489 txn->status = 500;
3490 if (req->flags != BF_READ_ERROR)
3491 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3492
3493 req->analysers = 0;
3494 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003495
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003496 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003497 if (s->listener->counters)
3498 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003499
Willy Tarreau60b85b02008-11-30 23:28:40 +01003500 if (!(s->flags & SN_ERR_MASK))
3501 s->flags |= SN_ERR_PRXCOND;
3502 if (!(s->flags & SN_FINST_MASK))
3503 s->flags |= SN_FINST_T;
3504 return 0;
3505}
3506
Willy Tarreaud34af782008-11-30 23:36:37 +01003507/* This function is an analyser which processes the HTTP request body. It looks
3508 * for parameters to be used for the load balancing algorithm (url_param). It
3509 * must only be called after the standard HTTP request processing has occurred,
3510 * because it expects the request to be parsed. It returns zero if it needs to
3511 * read more data, or 1 once it has completed its analysis.
3512 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003513int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003514{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003515 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003516 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003517 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003518
3519 /* We have to parse the HTTP request body to find any required data.
3520 * "balance url_param check_post" should have been the only way to get
3521 * into this. We were brought here after HTTP header analysis, so all
3522 * related structures are ready.
3523 */
3524
Willy Tarreau522d6c02009-12-06 18:49:18 +01003525 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3526 goto missing_data;
3527
3528 if (msg->msg_state < HTTP_MSG_100_SENT) {
3529 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3530 * send an HTTP/1.1 100 Continue intermediate response.
3531 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003532 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533 struct hdr_ctx ctx;
3534 ctx.idx = 0;
3535 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02003536 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003537 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003538 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003539 }
3540 }
3541 msg->msg_state = HTTP_MSG_100_SENT;
3542 }
3543
3544 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003545 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02003546 * req->p still points to the beginning of the message and msg->sol
3547 * is still null. We must save the body in msg->next because it
3548 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003549 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003550 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003551
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003552 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3554 else
3555 msg->msg_state = HTTP_MSG_DATA;
3556 }
3557
3558 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003559 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003560 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003561 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003562 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003563 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003564
Willy Tarreau115acb92009-12-26 13:56:06 +01003565 if (!ret)
3566 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003567 else if (ret < 0) {
3568 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003569 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003570 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003571 }
3572
Willy Tarreaud98cf932009-12-27 22:54:55 +01003573 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003574 * We have the first data byte is in msg->sov. We're waiting for at
3575 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003576 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577
Willy Tarreau124d9912011-03-01 20:30:48 +01003578 if (msg->body_len < limit)
3579 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003580
Willy Tarreau26927362012-05-18 23:22:52 +02003581 if (req->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003582 goto http_end;
3583
3584 missing_data:
3585 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003586 if (req->flags & BF_FULL) {
3587 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003588 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003589 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003590
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3592 txn->status = 408;
3593 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003594
3595 if (!(s->flags & SN_ERR_MASK))
3596 s->flags |= SN_ERR_CLITO;
3597 if (!(s->flags & SN_FINST_MASK))
3598 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003599 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003600 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601
3602 /* we get here if we need to wait for more data */
3603 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003604 /* Not enough data. We'll re-use the http-request
3605 * timeout here. Ideally, we should set the timeout
3606 * relative to the accept() date. We just set the
3607 * request timeout once at the beginning of the
3608 * request.
3609 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003610 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003611 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003612 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003613 return 0;
3614 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003615
3616 http_end:
3617 /* The situation will not evolve, so let's give up on the analysis. */
3618 s->logs.tv_request = now; /* update the request timer to reflect full request */
3619 req->analysers &= ~an_bit;
3620 req->analyse_exp = TICK_ETERNITY;
3621 return 1;
3622
3623 return_bad_req: /* let's centralize all bad requests */
3624 txn->req.msg_state = HTTP_MSG_ERROR;
3625 txn->status = 400;
3626 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3627
Willy Tarreau79ebac62010-06-07 13:47:49 +02003628 if (!(s->flags & SN_ERR_MASK))
3629 s->flags |= SN_ERR_PRXCOND;
3630 if (!(s->flags & SN_FINST_MASK))
3631 s->flags |= SN_FINST_R;
3632
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633 return_err_msg:
3634 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003635 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 if (s->listener->counters)
3637 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003638 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003639}
3640
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003641/* send a server's name with an outgoing request over an established connection.
3642 * Note: this function is designed to be called once the request has been scheduled
3643 * for being forwarded. This is the reason why it rewinds the buffer before
3644 * proceeding.
3645 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003646int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003647
3648 struct hdr_ctx ctx;
3649
Mark Lamourinec2247f02012-01-04 13:02:01 -05003650 char *hdr_name = be->server_id_hdr_name;
3651 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003652 struct buffer *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003653 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003654 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003655
William Lallemandd9e90662012-01-30 17:27:17 +01003656 ctx.idx = 0;
3657
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003658 old_o = req->o;
3659 if (old_o) {
3660 /* The request was already skipped, let's restore it */
3661 b_rew(req, old_o);
3662 }
3663
3664 old_i = req->i;
Willy Tarreau09d1e252012-05-18 22:36:34 +02003665 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003666 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003667 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668 }
3669
3670 /* Add the new header requested with the server value */
3671 hdr_val = trash;
3672 memcpy(hdr_val, hdr_name, hdr_name_len);
3673 hdr_val += hdr_name_len;
3674 *hdr_val++ = ':';
3675 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003676 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003677 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003678
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003679 if (old_o) {
3680 /* If this was a forwarded request, we must readjust the amount of
3681 * data to be forwarded in order to take into account the size
3682 * variations.
3683 */
3684 b_adv(req, old_o + req->i - old_i);
3685 }
3686
Mark Lamourinec2247f02012-01-04 13:02:01 -05003687 return 0;
3688}
3689
Willy Tarreau610ecce2010-01-04 21:15:02 +01003690/* Terminate current transaction and prepare a new one. This is very tricky
3691 * right now but it works.
3692 */
3693void http_end_txn_clean_session(struct session *s)
3694{
3695 /* FIXME: We need a more portable way of releasing a backend's and a
3696 * server's connections. We need a safer way to reinitialize buffer
3697 * flags. We also need a more accurate method for computing per-request
3698 * data.
3699 */
3700 http_silent_debug(__LINE__, s);
3701
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003702 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003703 si_shutr(s->req->cons);
3704 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705
3706 http_silent_debug(__LINE__, s);
3707
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003708 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003710 if (unlikely(s->srv_conn))
3711 sess_change_server(s, NULL);
3712 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003713
3714 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3715 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003716 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717
3718 if (s->txn.status) {
3719 int n;
3720
3721 n = s->txn.status / 100;
3722 if (n < 1 || n > 5)
3723 n = 0;
3724
3725 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003726 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
Willy Tarreau24657792010-02-26 10:30:28 +01003728 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003730 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 }
3732
3733 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003734 s->logs.bytes_in -= s->req->i;
3735 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
3737 /* let's do a final log if we need it */
3738 if (s->logs.logwait &&
3739 !(s->flags & SN_MONITOR) &&
3740 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3741 s->do_log(s);
3742 }
3743
3744 s->logs.accept_date = date; /* user-visible date for logging */
3745 s->logs.tv_accept = now; /* corrected date for internal use */
3746 tv_zero(&s->logs.tv_request);
3747 s->logs.t_queue = -1;
3748 s->logs.t_connect = -1;
3749 s->logs.t_data = -1;
3750 s->logs.t_close = 0;
3751 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3752 s->logs.srv_queue_size = 0; /* we will get this number soon */
3753
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003754 s->logs.bytes_in = s->req->total = s->req->i;
3755 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
3757 if (s->pend_pos)
3758 pendconn_free(s->pend_pos);
3759
Willy Tarreau827aee92011-03-10 16:55:02 +01003760 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761 if (s->flags & SN_CURR_SESS) {
3762 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003763 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003765 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3766 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 }
3768
Willy Tarreau9e000c62011-03-10 14:03:36 +01003769 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770
3771 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003772 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003774 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775 s->req->cons->err_loc = NULL;
3776 s->req->cons->exp = TICK_ETERNITY;
3777 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003778 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3779 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL|BF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003780 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 +01003781 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3782 s->txn.meth = 0;
3783 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003784 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003785 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 s->req->cons->flags |= SI_FL_INDEP_STR;
3787
Willy Tarreau96e31212011-05-30 18:10:30 +02003788 if (s->fe->options2 & PR_O2_NODELAY) {
3789 s->req->flags |= BF_NEVER_WAIT;
3790 s->rep->flags |= BF_NEVER_WAIT;
3791 }
3792
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 /* if the request buffer is not empty, it means we're
3794 * about to process another request, so send pending
3795 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003796 * Just don't do this if the buffer is close to be full,
3797 * because the request will wait for it to flush a little
3798 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003800 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003801 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003802 !(s->rep->flags & BF_FULL) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02003803 bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003804 s->rep->flags |= BF_EXPECT_MORE;
3805 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003806
3807 /* we're removing the analysers, we MUST re-enable events detection */
3808 buffer_auto_read(s->req);
3809 buffer_auto_close(s->req);
3810 buffer_auto_read(s->rep);
3811 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812
Willy Tarreau342b11c2010-11-24 16:22:09 +01003813 s->req->analysers = s->listener->analysers;
3814 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 s->rep->analysers = 0;
3816
3817 http_silent_debug(__LINE__, s);
3818}
3819
3820
3821/* This function updates the request state machine according to the response
3822 * state machine and buffer flags. It returns 1 if it changes anything (flag
3823 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3824 * it is only used to find when a request/response couple is complete. Both
3825 * this function and its equivalent should loop until both return zero. It
3826 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3827 */
3828int http_sync_req_state(struct session *s)
3829{
3830 struct buffer *buf = s->req;
3831 struct http_txn *txn = &s->txn;
3832 unsigned int old_flags = buf->flags;
3833 unsigned int old_state = txn->req.msg_state;
3834
3835 http_silent_debug(__LINE__, s);
3836 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3837 return 0;
3838
3839 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003840 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003841 * We can shut the read side unless we want to abort_on_close,
3842 * or we have a POST request. The issue with POST requests is
3843 * that some browsers still send a CRLF after the request, and
3844 * this CRLF must be read so that it does not remain in the kernel
3845 * buffers, otherwise a close could cause an RST on some systems
3846 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003847 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003848 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003849 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850
3851 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3852 goto wait_other_side;
3853
3854 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3855 /* The server has not finished to respond, so we
3856 * don't want to move in order not to upset it.
3857 */
3858 goto wait_other_side;
3859 }
3860
3861 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3862 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003863 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 txn->req.msg_state = HTTP_MSG_TUNNEL;
3865 goto wait_other_side;
3866 }
3867
3868 /* When we get here, it means that both the request and the
3869 * response have finished receiving. Depending on the connection
3870 * mode, we'll have to wait for the last bytes to leave in either
3871 * direction, and sometimes for a close to be effective.
3872 */
3873
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003874 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3875 /* Server-close mode : queue a connection close to the server */
3876 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003878 }
3879 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3880 /* Option forceclose is set, or either side wants to close,
3881 * let's enforce it now that we're not expecting any new
3882 * data to come. The caller knows the session is complete
3883 * once both states are CLOSED.
3884 */
3885 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 buffer_shutr_now(buf);
3887 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003889 }
3890 else {
3891 /* The last possible modes are keep-alive and tunnel. Since tunnel
3892 * mode does not set the body analyser, we can't reach this place
3893 * in tunnel mode, so we're left with keep-alive only.
3894 * This mode is currently not implemented, we switch to tunnel mode.
3895 */
3896 buffer_auto_read(buf);
3897 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 }
3899
3900 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3901 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003902 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3903
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 if (!(buf->flags & BF_OUT_EMPTY)) {
3905 txn->req.msg_state = HTTP_MSG_CLOSING;
3906 goto http_msg_closing;
3907 }
3908 else {
3909 txn->req.msg_state = HTTP_MSG_CLOSED;
3910 goto http_msg_closed;
3911 }
3912 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003913 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003914 }
3915
3916 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3917 http_msg_closing:
3918 /* nothing else to forward, just waiting for the output buffer
3919 * to be empty and for the shutw_now to take effect.
3920 */
3921 if (buf->flags & BF_OUT_EMPTY) {
3922 txn->req.msg_state = HTTP_MSG_CLOSED;
3923 goto http_msg_closed;
3924 }
3925 else if (buf->flags & BF_SHUTW) {
3926 txn->req.msg_state = HTTP_MSG_ERROR;
3927 goto wait_other_side;
3928 }
3929 }
3930
3931 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3932 http_msg_closed:
3933 goto wait_other_side;
3934 }
3935
3936 wait_other_side:
3937 http_silent_debug(__LINE__, s);
3938 return txn->req.msg_state != old_state || buf->flags != old_flags;
3939}
3940
3941
3942/* This function updates the response state machine according to the request
3943 * state machine and buffer flags. It returns 1 if it changes anything (flag
3944 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3945 * it is only used to find when a request/response couple is complete. Both
3946 * this function and its equivalent should loop until both return zero. It
3947 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3948 */
3949int http_sync_res_state(struct session *s)
3950{
3951 struct buffer *buf = s->rep;
3952 struct http_txn *txn = &s->txn;
3953 unsigned int old_flags = buf->flags;
3954 unsigned int old_state = txn->rsp.msg_state;
3955
3956 http_silent_debug(__LINE__, s);
3957 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3958 return 0;
3959
3960 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3961 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003962 * still monitor the server connection for a possible close
3963 * while the request is being uploaded, so we don't disable
3964 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003965 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003966 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967
3968 if (txn->req.msg_state == HTTP_MSG_ERROR)
3969 goto wait_other_side;
3970
3971 if (txn->req.msg_state < HTTP_MSG_DONE) {
3972 /* The client seems to still be sending data, probably
3973 * because we got an error response during an upload.
3974 * We have the choice of either breaking the connection
3975 * or letting it pass through. Let's do the later.
3976 */
3977 goto wait_other_side;
3978 }
3979
3980 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3981 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003982 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3984 goto wait_other_side;
3985 }
3986
3987 /* When we get here, it means that both the request and the
3988 * response have finished receiving. Depending on the connection
3989 * mode, we'll have to wait for the last bytes to leave in either
3990 * direction, and sometimes for a close to be effective.
3991 */
3992
3993 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3994 /* Server-close mode : shut read and wait for the request
3995 * side to close its output buffer. The caller will detect
3996 * when we're in DONE and the other is in CLOSED and will
3997 * catch that for the final cleanup.
3998 */
3999 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4000 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004002 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4003 /* Option forceclose is set, or either side wants to close,
4004 * let's enforce it now that we're not expecting any new
4005 * data to come. The caller knows the session is complete
4006 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004008 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4009 buffer_shutr_now(buf);
4010 buffer_shutw_now(buf);
4011 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 /* The last possible modes are keep-alive and tunnel. Since tunnel
4015 * mode does not set the body analyser, we can't reach this place
4016 * in tunnel mode, so we're left with keep-alive only.
4017 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004019 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021 }
4022
4023 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4024 /* if we've just closed an output, let's switch */
4025 if (!(buf->flags & BF_OUT_EMPTY)) {
4026 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4027 goto http_msg_closing;
4028 }
4029 else {
4030 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4031 goto http_msg_closed;
4032 }
4033 }
4034 goto wait_other_side;
4035 }
4036
4037 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4038 http_msg_closing:
4039 /* nothing else to forward, just waiting for the output buffer
4040 * to be empty and for the shutw_now to take effect.
4041 */
4042 if (buf->flags & BF_OUT_EMPTY) {
4043 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4044 goto http_msg_closed;
4045 }
4046 else if (buf->flags & BF_SHUTW) {
4047 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004048 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004049 if (target_srv(&s->target))
4050 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 goto wait_other_side;
4052 }
4053 }
4054
4055 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4056 http_msg_closed:
4057 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004058 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004060 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 goto wait_other_side;
4062 }
4063
4064 wait_other_side:
4065 http_silent_debug(__LINE__, s);
4066 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4067}
4068
4069
4070/* Resync the request and response state machines. Return 1 if either state
4071 * changes.
4072 */
4073int http_resync_states(struct session *s)
4074{
4075 struct http_txn *txn = &s->txn;
4076 int old_req_state = txn->req.msg_state;
4077 int old_res_state = txn->rsp.msg_state;
4078
4079 http_silent_debug(__LINE__, s);
4080 http_sync_req_state(s);
4081 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004082 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 if (!http_sync_res_state(s))
4084 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004085 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086 if (!http_sync_req_state(s))
4087 break;
4088 }
4089 http_silent_debug(__LINE__, s);
4090 /* OK, both state machines agree on a compatible state.
4091 * There are a few cases we're interested in :
4092 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4093 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4094 * directions, so let's simply disable both analysers.
4095 * - HTTP_MSG_CLOSED on the response only means we must abort the
4096 * request.
4097 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4098 * with server-close mode means we've completed one request and we
4099 * must re-initialize the server connection.
4100 */
4101
4102 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4103 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4104 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4105 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4106 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004107 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004110 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004111 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004113 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4114 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004115 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004116 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004117 s->rep->analysers = 0;
4118 buffer_auto_close(s->rep);
4119 buffer_auto_read(s->rep);
4120 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 buffer_abort(s->req);
4122 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004124 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 }
4126 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4127 txn->rsp.msg_state == HTTP_MSG_DONE &&
4128 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4129 /* server-close: terminate this server connection and
4130 * reinitialize a fresh-new transaction.
4131 */
4132 http_end_txn_clean_session(s);
4133 }
4134
4135 http_silent_debug(__LINE__, s);
4136 return txn->req.msg_state != old_req_state ||
4137 txn->rsp.msg_state != old_res_state;
4138}
4139
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140/* This function is an analyser which forwards request body (including chunk
4141 * sizes if any). It is called as soon as we must forward, even if we forward
4142 * zero byte. The only situation where it must not be called is when we're in
4143 * tunnel mode and we want to forward till the close. It's used both to forward
4144 * remaining data and to resync after end of body. It expects the msg_state to
4145 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4146 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004147 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004148 * bytes of pending data + the headers if not already done (between sol and sov).
4149 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004150 */
4151int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4152{
4153 struct http_txn *txn = &s->txn;
4154 struct http_msg *msg = &s->txn.req;
4155
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004156 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4157 return 0;
4158
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004159 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004160 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004161 /* Output closed while we were sending data. We must abort and
4162 * wake the other side up.
4163 */
4164 msg->msg_state = HTTP_MSG_ERROR;
4165 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004166 return 1;
4167 }
4168
Willy Tarreau4fe41902010-06-07 22:27:41 +02004169 /* in most states, we should abort in case of early close */
4170 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004171
4172 /* Note that we don't have to send 100-continue back because we don't
4173 * need the data to complete our job, and it's up to the server to
4174 * decide whether to return 100, 417 or anything else in return of
4175 * an "Expect: 100-continue" header.
4176 */
4177
4178 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004179 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02004180 * req->p still points to the beginning of the message and msg->sol
4181 * is still null. We must save the body in msg->next because it
4182 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004183 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004184 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004185
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004186 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4188 else {
4189 msg->msg_state = HTTP_MSG_DATA;
4190 }
4191 }
4192
Willy Tarreaud98cf932009-12-27 22:54:55 +01004193 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004194 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004195
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004197 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004198 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004199 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004200 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004201 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004202 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004203 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004204 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004205
Willy Tarreaucaabe412010-01-03 23:08:28 +01004206 if (msg->msg_state == HTTP_MSG_DATA) {
4207 /* must still forward */
4208 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004209 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004210
4211 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004212 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004213 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004214 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004215 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004216 }
4217 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004218 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004219 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004220 * TRAILERS state.
4221 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004222 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004223
4224 if (!ret)
4225 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004226 else if (ret < 0) {
4227 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004228 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004229 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004230 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004231 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004232 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4235 /* we want the CRLF after the data */
4236 int ret;
4237
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004238 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239
4240 if (ret == 0)
4241 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004242 else if (ret < 0) {
4243 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004244 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004245 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004247 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248 /* we're in MSG_CHUNK_SIZE now */
4249 }
4250 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004251 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004252
4253 if (ret == 0)
4254 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004255 else if (ret < 0) {
4256 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004257 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004258 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004260 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 /* we're in HTTP_MSG_DONE now */
4262 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004264 int old_state = msg->msg_state;
4265
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004267 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004268 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4269 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4270 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 if (http_resync_states(s)) {
4272 /* some state changes occurred, maybe the analyser
4273 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004274 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4276 if (req->flags & BF_SHUTW) {
4277 /* request errors are most likely due to
4278 * the server aborting the transfer.
4279 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004280 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004281 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004282 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004283 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004285 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004287 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004288
4289 /* If "option abortonclose" is set on the backend, we
4290 * want to monitor the client's connection and forward
4291 * any shutdown notification to the server, which will
4292 * decide whether to close or to go on processing the
4293 * request.
4294 */
4295 if (s->be->options & PR_O_ABRT_CLOSE) {
4296 buffer_auto_read(req);
4297 buffer_auto_close(req);
4298 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004299 else if (s->txn.meth == HTTP_METH_POST) {
4300 /* POST requests may require to read extra CRLF
4301 * sent by broken browsers and which could cause
4302 * an RST to be sent upon close on some systems
4303 * (eg: Linux).
4304 */
4305 buffer_auto_read(req);
4306 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004307
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004309 }
4310 }
4311
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004313 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004314 if (req->flags & BF_SHUTR) {
4315 if (!(s->flags & SN_ERR_MASK))
4316 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004317 if (!(s->flags & SN_FINST_MASK)) {
4318 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4319 s->flags |= SN_FINST_H;
4320 else
4321 s->flags |= SN_FINST_D;
4322 }
4323
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004324 s->fe->fe_counters.cli_aborts++;
4325 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004326 if (target_srv(&s->target))
4327 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004328
4329 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004330 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004331
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004332 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004333 if (req->flags & BF_SHUTW)
4334 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004336 /* When TE: chunked is used, we need to get there again to parse remaining
4337 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4338 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004339 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004340 buffer_dont_close(req);
4341
Willy Tarreau5c620922011-05-11 19:56:11 +02004342 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004343 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4344 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004345 * modes are already handled by the stream sock layer. We must not do
4346 * this in content-length mode because it could present the MSG_MORE
4347 * flag with the last block of forwarded data, which would cause an
4348 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004349 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004350 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004351 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004352
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004354 return 0;
4355
Willy Tarreaud98cf932009-12-27 22:54:55 +01004356 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004357 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358 if (s->listener->counters)
4359 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004360 return_bad_req_stats_ok:
4361 txn->req.msg_state = HTTP_MSG_ERROR;
4362 if (txn->status) {
4363 /* Note: we don't send any error if some data were already sent */
4364 stream_int_retnclose(req->prod, NULL);
4365 } else {
4366 txn->status = 400;
4367 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4368 }
4369 req->analysers = 0;
4370 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004371
4372 if (!(s->flags & SN_ERR_MASK))
4373 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004374 if (!(s->flags & SN_FINST_MASK)) {
4375 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4376 s->flags |= SN_FINST_H;
4377 else
4378 s->flags |= SN_FINST_D;
4379 }
4380 return 0;
4381
4382 aborted_xfer:
4383 txn->req.msg_state = HTTP_MSG_ERROR;
4384 if (txn->status) {
4385 /* Note: we don't send any error if some data were already sent */
4386 stream_int_retnclose(req->prod, NULL);
4387 } else {
4388 txn->status = 502;
4389 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4390 }
4391 req->analysers = 0;
4392 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4393
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004394 s->fe->fe_counters.srv_aborts++;
4395 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004396 if (target_srv(&s->target))
4397 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004398
4399 if (!(s->flags & SN_ERR_MASK))
4400 s->flags |= SN_ERR_SRVCL;
4401 if (!(s->flags & SN_FINST_MASK)) {
4402 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4403 s->flags |= SN_FINST_H;
4404 else
4405 s->flags |= SN_FINST_D;
4406 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004407 return 0;
4408}
4409
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004410/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4411 * processing can continue on next analysers, or zero if it either needs more
4412 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4413 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4414 * when it has nothing left to do, and may remove any analyser when it wants to
4415 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004416 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004417int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004418{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 struct http_txn *txn = &s->txn;
4420 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004421 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004422 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004424 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004425
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004426 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 +02004427 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004429 rep,
4430 rep->rex, rep->wex,
4431 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004432 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004433 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435 /*
4436 * Now parse the partial (or complete) lines.
4437 * We will check the response syntax, and also join multi-line
4438 * headers. An index of all the lines will be elaborated while
4439 * parsing.
4440 *
4441 * For the parsing, we use a 28 states FSM.
4442 *
4443 * Here is the information we currently have :
Willy Tarreau26927362012-05-18 23:22:52 +02004444 * rep->p = beginning of response
4445 * rep->p + msg->eoh = end of processed headers / start of current one
4446 * rep->p + rep->i = end of input data
4447 * msg->eol = end of current header or line (LF or CRLF)
4448 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 */
4450
Willy Tarreau83e3af02009-12-28 17:39:57 +01004451 /* There's a protected area at the end of the buffer for rewriting
4452 * purposes. We don't want to start to parse the request if the
4453 * protected area is affected, because we may have to move processed
4454 * data later, which is much more complicated.
4455 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004456 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004457 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02004458 bi_end(rep) < b_ptr(rep, msg->next) ||
4459 bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004460 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004461 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004462 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4463 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004464 buffer_dont_close(rep);
4465 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4466 return 0;
4467 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004468 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02004469 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004470 }
4471
Willy Tarreaua458b672012-03-05 11:17:50 +01004472 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004473 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004474 }
4475
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 /* 1: we might have to print this header in debug mode */
4477 if (unlikely((global.mode & MODE_DEBUG) &&
4478 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004479 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004481
Willy Tarreau26927362012-05-18 23:22:52 +02004482 sol = rep->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004483 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004485
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 sol += hdr_idx_first_pos(&txn->hdr_idx);
4487 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 while (cur_idx) {
4490 eol = sol + txn->hdr_idx.v[cur_idx].len;
4491 debug_hdr("srvhdr", s, sol, eol);
4492 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4493 cur_idx = txn->hdr_idx.v[cur_idx].next;
4494 }
4495 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004496
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004497 /*
4498 * Now we quickly check if we have found a full valid response.
4499 * If not so, we check the FD and buffer states before leaving.
4500 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004501 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 * responses are checked first.
4503 *
4504 * Depending on whether the client is still there or not, we
4505 * may send an error response back or not. Note that normally
4506 * we should only check for HTTP status there, and check I/O
4507 * errors somewhere else.
4508 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004509
Willy Tarreau655dce92009-11-08 13:10:58 +01004510 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 /* Invalid response */
4512 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4513 /* we detected a parsing error. We want to archive this response
4514 * in the dedicated proxy area for later troubleshooting.
4515 */
4516 hdr_response_bad:
4517 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004518 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004520 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004521 if (target_srv(&s->target)) {
4522 target_srv(&s->target)->counters.failed_resp++;
4523 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004524 }
Willy Tarreau64648412010-03-05 10:41:54 +01004525 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004526 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527 rep->analysers = 0;
4528 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004529 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004530 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4532
4533 if (!(s->flags & SN_ERR_MASK))
4534 s->flags |= SN_ERR_PRXCOND;
4535 if (!(s->flags & SN_FINST_MASK))
4536 s->flags |= SN_FINST_H;
4537
4538 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004539 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004540
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 /* too large response does not fit in buffer. */
4542 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004543 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004544 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 goto hdr_response_bad;
4546 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004547
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004548 /* read error */
4549 else if (rep->flags & BF_READ_ERROR) {
4550 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004551 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004552
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004553 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004554 if (target_srv(&s->target)) {
4555 target_srv(&s->target)->counters.failed_resp++;
4556 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004557 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004558
Willy Tarreau90deb182010-01-07 00:20:41 +01004559 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 rep->analysers = 0;
4561 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004562 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004563 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004565
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 if (!(s->flags & SN_ERR_MASK))
4567 s->flags |= SN_ERR_SRVCL;
4568 if (!(s->flags & SN_FINST_MASK))
4569 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004570 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 /* read timeout : return a 504 to the client. */
4574 else if (rep->flags & BF_READ_TIMEOUT) {
4575 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004576 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004577
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004578 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004579 if (target_srv(&s->target)) {
4580 target_srv(&s->target)->counters.failed_resp++;
4581 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004582 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004583
Willy Tarreau90deb182010-01-07 00:20:41 +01004584 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 rep->analysers = 0;
4586 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004587 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004588 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004589 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004590
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 if (!(s->flags & SN_ERR_MASK))
4592 s->flags |= SN_ERR_SRVTO;
4593 if (!(s->flags & SN_FINST_MASK))
4594 s->flags |= SN_FINST_H;
4595 return 0;
4596 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004597
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004598 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004600 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004601 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004602
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004603 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004604 if (target_srv(&s->target)) {
4605 target_srv(&s->target)->counters.failed_resp++;
4606 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004607 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004608
Willy Tarreau90deb182010-01-07 00:20:41 +01004609 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 rep->analysers = 0;
4611 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004612 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004613 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004615
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 if (!(s->flags & SN_ERR_MASK))
4617 s->flags |= SN_ERR_SRVCL;
4618 if (!(s->flags & SN_FINST_MASK))
4619 s->flags |= SN_FINST_H;
4620 return 0;
4621 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004622
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 /* write error to client (we don't send any message then) */
4624 else if (rep->flags & BF_WRITE_ERROR) {
4625 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004626 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004627
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004628 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004630 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631
4632 if (!(s->flags & SN_ERR_MASK))
4633 s->flags |= SN_ERR_CLICL;
4634 if (!(s->flags & SN_FINST_MASK))
4635 s->flags |= SN_FINST_H;
4636
4637 /* process_session() will take care of the error */
4638 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004639 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641 buffer_dont_close(rep);
4642 return 0;
4643 }
4644
4645 /* More interesting part now : we know that we have a complete
4646 * response which at least looks like HTTP. We have an indicator
4647 * of each header's length, so we can parse them quickly.
4648 */
4649
4650 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004651 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653 /*
4654 * 1: get the status code
4655 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02004656 n = rep->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 if (n < 1 || n > 5)
4658 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004659 /* when the client triggers a 4xx from the server, it's most often due
4660 * to a missing object or permission. These events should be tracked
4661 * because if they happen often, it may indicate a brute force or a
4662 * vulnerability scan.
4663 */
4664 if (n == 4)
4665 session_inc_http_err_ctr(s);
4666
Willy Tarreau827aee92011-03-10 16:55:02 +01004667 if (target_srv(&s->target))
4668 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669
Willy Tarreau5b154472009-12-21 20:11:07 +01004670 /* check if the response is HTTP/1.1 or above */
4671 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004672 ((rep->p[5] > '1') ||
4673 ((rep->p[5] == '1') && (rep->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004674 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004675
4676 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004677 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004679 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004680 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004681
Willy Tarreau09d1e252012-05-18 22:36:34 +02004682 txn->status = strl2ui(rep->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683
Willy Tarreau39650402010-03-15 19:44:39 +01004684 /* Adjust server's health based on status code. Note: status codes 501
4685 * and 505 are triggered on demand by client request, so we must not
4686 * count them as server failures.
4687 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004688 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004689 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004690 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004691 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004692 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004693 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004694
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004695 /*
4696 * 2: check for cacheability.
4697 */
4698
4699 switch (txn->status) {
4700 case 200:
4701 case 203:
4702 case 206:
4703 case 300:
4704 case 301:
4705 case 410:
4706 /* RFC2616 @13.4:
4707 * "A response received with a status code of
4708 * 200, 203, 206, 300, 301 or 410 MAY be stored
4709 * by a cache (...) unless a cache-control
4710 * directive prohibits caching."
4711 *
4712 * RFC2616 @9.5: POST method :
4713 * "Responses to this method are not cacheable,
4714 * unless the response includes appropriate
4715 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004716 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717 if (likely(txn->meth != HTTP_METH_POST) &&
4718 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4719 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4720 break;
4721 default:
4722 break;
4723 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004724
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004725 /*
4726 * 3: we may need to capture headers
4727 */
4728 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004729 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02004730 capture_headers(rep->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 txn->rsp.cap, s->fe->rsp_cap);
4732
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004733 /* 4: determine the transfer-length.
4734 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4735 * the presence of a message-body in a RESPONSE and its transfer length
4736 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004737 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004738 * All responses to the HEAD request method MUST NOT include a
4739 * message-body, even though the presence of entity-header fields
4740 * might lead one to believe they do. All 1xx (informational), 204
4741 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4742 * message-body. All other responses do include a message-body,
4743 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004744 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004745 * 1. Any response which "MUST NOT" include a message-body (such as the
4746 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4747 * always terminated by the first empty line after the header fields,
4748 * regardless of the entity-header fields present in the message.
4749 *
4750 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4751 * the "chunked" transfer-coding (Section 6.2) is used, the
4752 * transfer-length is defined by the use of this transfer-coding.
4753 * If a Transfer-Encoding header field is present and the "chunked"
4754 * transfer-coding is not present, the transfer-length is defined by
4755 * the sender closing the connection.
4756 *
4757 * 3. If a Content-Length header field is present, its decimal value in
4758 * OCTETs represents both the entity-length and the transfer-length.
4759 * If a message is received with both a Transfer-Encoding header
4760 * field and a Content-Length header field, the latter MUST be ignored.
4761 *
4762 * 4. If the message uses the media type "multipart/byteranges", and
4763 * the transfer-length is not otherwise specified, then this self-
4764 * delimiting media type defines the transfer-length. This media
4765 * type MUST NOT be used unless the sender knows that the recipient
4766 * can parse it; the presence in a request of a Range header with
4767 * multiple byte-range specifiers from a 1.1 client implies that the
4768 * client can parse multipart/byteranges responses.
4769 *
4770 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004771 */
4772
4773 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004774 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004775 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004776 * FIXME: should we parse anyway and return an error on chunked encoding ?
4777 */
4778 if (txn->meth == HTTP_METH_HEAD ||
4779 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004780 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004781 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004782 goto skip_content_length;
4783 }
4784
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004785 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004786 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004787 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004788 http_find_header2("Transfer-Encoding", 17, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004789 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004790 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4791 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004792 /* bad transfer-encoding (chunked followed by something else) */
4793 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004794 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004795 break;
4796 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004797 }
4798
4799 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4800 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004801 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004802 http_find_header2("Content-Length", 14, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004803 signed long long cl;
4804
Willy Tarreauad14f752011-09-02 20:33:27 +02004805 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004806 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004807 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004808 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004809
Willy Tarreauad14f752011-09-02 20:33:27 +02004810 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004811 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004813 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004814
Willy Tarreauad14f752011-09-02 20:33:27 +02004815 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004816 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004818 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004820 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004821 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004823 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004824
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004825 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004826 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004827 }
4828
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004829 /* FIXME: we should also implement the multipart/byterange method.
4830 * For now on, we resort to close mode in this case (unknown length).
4831 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004832skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 /* end of job, return OK */
4835 rep->analysers &= ~an_bit;
4836 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004837 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004838 return 1;
4839}
4840
4841/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004842 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4843 * and updates t->rep->analysers. It might make sense to explode it into several
4844 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004845 */
4846int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4847{
4848 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004849 struct http_msg *msg = &txn->rsp;
4850 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004851 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004853 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 +02004854 now_ms, __FUNCTION__,
4855 t,
4856 rep,
4857 rep->rex, rep->wex,
4858 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004859 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004860 rep->analysers);
4861
Willy Tarreau655dce92009-11-08 13:10:58 +01004862 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004863 return 0;
4864
4865 rep->analysers &= ~an_bit;
4866 rep->analyse_exp = TICK_ETERNITY;
4867
Willy Tarreau5b154472009-12-21 20:11:07 +01004868 /* Now we have to check if we need to modify the Connection header.
4869 * This is more difficult on the response than it is on the request,
4870 * because we can have two different HTTP versions and we don't know
4871 * how the client will interprete a response. For instance, let's say
4872 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4873 * HTTP/1.1 response without any header. Maybe it will bound itself to
4874 * HTTP/1.0 because it only knows about it, and will consider the lack
4875 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4876 * the lack of header as a keep-alive. Thus we will use two flags
4877 * indicating how a request MAY be understood by the client. In case
4878 * of multiple possibilities, we'll fix the header to be explicit. If
4879 * ambiguous cases such as both close and keepalive are seen, then we
4880 * will fall back to explicit close. Note that we won't take risks with
4881 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004882 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004883 */
4884
Willy Tarreaudc008c52010-02-01 16:20:08 +01004885 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4886 txn->status == 101)) {
4887 /* Either we've established an explicit tunnel, or we're
4888 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004889 * to understand the next protocols. We have to switch to tunnel
4890 * mode, so that we transfer the request and responses then let
4891 * this protocol pass unmodified. When we later implement specific
4892 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004893 * header which contains information about that protocol for
4894 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004895 */
4896 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4897 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004898 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4899 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4900 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004901 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004902
Willy Tarreau60466522010-01-18 19:08:45 +01004903 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004904 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004905 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4906 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004907
Willy Tarreau60466522010-01-18 19:08:45 +01004908 /* now adjust header transformations depending on current state */
4909 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4910 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4911 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004912 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004913 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004914 }
Willy Tarreau60466522010-01-18 19:08:45 +01004915 else { /* SCL / KAL */
4916 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004917 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004918 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004919 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004920
Willy Tarreau60466522010-01-18 19:08:45 +01004921 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004922 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004923
Willy Tarreau60466522010-01-18 19:08:45 +01004924 /* Some keep-alive responses are converted to Server-close if
4925 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004926 */
Willy Tarreau60466522010-01-18 19:08:45 +01004927 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4928 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004929 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004930 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004931 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004932 }
4933
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935 /*
4936 * 3: we will have to evaluate the filters.
4937 * As opposed to version 1.2, now they will be evaluated in the
4938 * filters order and not in the header order. This means that
4939 * each filter has to be validated among all headers.
4940 *
4941 * Filters are tried with ->be first, then with ->fe if it is
4942 * different from ->be.
4943 */
4944
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004945 cur_proxy = t->be;
4946 while (1) {
4947 struct proxy *rule_set = cur_proxy;
4948
4949 /* try headers filters */
4950 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004951 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004952 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004953 if (target_srv(&t->target)) {
4954 target_srv(&t->target)->counters.failed_resp++;
4955 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004956 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004957 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004958 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004959 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004960 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004961 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004962 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004963 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004964 if (!(t->flags & SN_ERR_MASK))
4965 t->flags |= SN_ERR_PRXCOND;
4966 if (!(t->flags & SN_FINST_MASK))
4967 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004968 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004969 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004970 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972 /* has the response been denied ? */
4973 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004974 if (target_srv(&t->target))
4975 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004976
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004977 t->be->be_counters.denied_resp++;
4978 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004979 if (t->listener->counters)
4980 t->listener->counters->denied_resp++;
4981
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004982 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004983 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004984
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004985 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004986 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004987 if (txn->status < 200)
4988 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004989 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004990 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004991 ret = acl_pass(ret);
4992 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4993 ret = !ret;
4994 if (!ret)
4995 continue;
4996 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004997 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004999 }
5000
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005001 /* check whether we're already working on the frontend */
5002 if (cur_proxy == t->fe)
5003 break;
5004 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005005 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005006
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005007 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005008 * We may be facing a 100-continue response, in which case this
5009 * is not the right response, and we're waiting for the next one.
5010 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005011 * next one.
5012 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005013 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005014 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau09d1e252012-05-18 22:36:34 +02005015 msg->next -= buffer_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005016 msg->msg_state = HTTP_MSG_RPBEFORE;
5017 txn->status = 0;
5018 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5019 return 1;
5020 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005021 else if (unlikely(txn->status < 200))
5022 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005023
5024 /* we don't have any 1xx status code now */
5025
5026 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005027 * 4: check for server cookie.
5028 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005029 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5030 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005032
Willy Tarreaubaaee002006-06-26 02:48:02 +02005033
Willy Tarreaua15645d2007-03-18 16:22:39 +01005034 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005037 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 /*
5041 * 6: add server cookie in the response if needed
5042 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005043 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005044 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005045 (!(t->flags & SN_DIRECT) ||
5046 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5047 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5048 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5049 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005050 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5051 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005053 /* the server is known, it's not the one the client requested, or the
5054 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 * insert a set-cookie here, except if we want to insert only on POST
5056 * requests and this one isn't. Note that servers which don't have cookies
5057 * (eg: some backup servers) will return a full cookie removal request.
5058 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005059 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005060 len = sprintf(trash,
5061 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5062 t->be->cookie_name);
5063 }
5064 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005065 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005066
5067 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5068 /* emit last_date, which is mandatory */
5069 trash[len++] = COOKIE_DELIM_DATE;
5070 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5071 if (t->be->cookie_maxlife) {
5072 /* emit first_date, which is either the original one or
5073 * the current date.
5074 */
5075 trash[len++] = COOKIE_DELIM_DATE;
5076 s30tob64(txn->cookie_first_date ?
5077 txn->cookie_first_date >> 2 :
5078 (date.tv_sec+3) >> 2, trash + len);
5079 len += 5;
5080 }
5081 }
5082 len += sprintf(trash + len, "; path=/");
5083 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005084
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 if (t->be->cookie_domain)
5086 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005087
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005088 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005090
Willy Tarreauf1348312010-10-07 15:54:11 +02005091 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005092 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005093 /* the server did not change, only the date was updated */
5094 txn->flags |= TX_SCK_UPDATED;
5095 else
5096 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005097
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005098 /* Here, we will tell an eventual cache on the client side that we don't
5099 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5100 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5101 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5102 */
5103 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005104
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5106
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005107 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005108 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005109 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005110 }
5111 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005112
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005113 /*
5114 * 7: check if result will be cacheable with a cookie.
5115 * We'll block the response if security checks have caught
5116 * nasty things such as a cacheable cookie.
5117 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005118 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5119 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005120 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121
5122 /* we're in presence of a cacheable response containing
5123 * a set-cookie header. We'll block it as requested by
5124 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005125 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005126 if (target_srv(&t->target))
5127 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005128
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005129 t->be->be_counters.denied_resp++;
5130 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005131 if (t->listener->counters)
5132 t->listener->counters->denied_resp++;
5133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005134 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005135 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005136 send_log(t->be, LOG_ALERT,
5137 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005138 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 goto return_srv_prx_502;
5140 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005141
5142 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005143 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005144 */
Willy Tarreau60466522010-01-18 19:08:45 +01005145 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5146 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5147 unsigned int want_flags = 0;
5148
5149 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5150 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5151 /* we want a keep-alive response here. Keep-alive header
5152 * required if either side is not 1.1.
5153 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005154 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005155 want_flags |= TX_CON_KAL_SET;
5156 }
5157 else {
5158 /* we want a close response here. Close header required if
5159 * the server is 1.1, regardless of the client.
5160 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005161 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005162 want_flags |= TX_CON_CLO_SET;
5163 }
5164
5165 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005166 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005167 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005168
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005169 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005170 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005171 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005172 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005173
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 /*************************************************************
5175 * OK, that's finished for the headers. We have done what we *
5176 * could. Let's switch to the DATA state. *
5177 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005178
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005181 /* if the user wants to log as soon as possible, without counting
5182 * bytes from the server, then this is the right moment. We have
5183 * to temporarily assign bytes_out to log what we currently have.
5184 */
5185 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5186 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5187 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005188 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005189 t->logs.bytes_out = 0;
5190 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005191
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005192 /* Note: we must not try to cheat by jumping directly to DATA,
5193 * otherwise we would not let the client side wake up.
5194 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005195
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005196 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005197 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005198 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005200
Willy Tarreaud98cf932009-12-27 22:54:55 +01005201/* This function is an analyser which forwards response body (including chunk
5202 * sizes if any). It is called as soon as we must forward, even if we forward
5203 * zero byte. The only situation where it must not be called is when we're in
5204 * tunnel mode and we want to forward till the close. It's used both to forward
5205 * remaining data and to resync after end of body. It expects the msg_state to
5206 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5207 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005208 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005209 * bytes of pending data + the headers if not already done (between sol and sov).
5210 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005211 */
5212int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5213{
5214 struct http_txn *txn = &s->txn;
5215 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005216 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005217
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005218 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5219 return 0;
5220
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005221 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005222 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005223 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005224 /* Output closed while we were sending data. We must abort and
5225 * wake the other side up.
5226 */
5227 msg->msg_state = HTTP_MSG_ERROR;
5228 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005229 return 1;
5230 }
5231
Willy Tarreau4fe41902010-06-07 22:27:41 +02005232 /* in most states, we should abort in case of early close */
5233 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005234
Willy Tarreaud98cf932009-12-27 22:54:55 +01005235 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005236 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02005237 * rep->p still points to the beginning of the message and msg->sol
5238 * is still null. We must save the body in msg->next because it
5239 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005240 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005241 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005242
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005243 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005244 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5245 else {
5246 msg->msg_state = HTTP_MSG_DATA;
5247 }
5248 }
5249
Willy Tarreaud98cf932009-12-27 22:54:55 +01005250 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005252 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005253 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005254 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005255 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005256 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005257 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005258 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005259 }
5260
Willy Tarreaucaabe412010-01-03 23:08:28 +01005261 if (msg->msg_state == HTTP_MSG_DATA) {
5262 /* must still forward */
5263 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005264 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005265
5266 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005267 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005268 msg->msg_state = HTTP_MSG_DATA_CRLF;
5269 else
5270 msg->msg_state = HTTP_MSG_DONE;
5271 }
5272 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005273 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005274 * set ->sov and ->next to point to the body and switch to DATA or
5275 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005277 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278
5279 if (!ret)
5280 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005281 else if (ret < 0) {
5282 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005283 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005284 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005285 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005286 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005287 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005288 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5289 /* we want the CRLF after the data */
5290 int ret;
5291
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005292 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293
5294 if (!ret)
5295 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005296 else if (ret < 0) {
5297 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005298 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005300 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005301 /* we're in MSG_CHUNK_SIZE now */
5302 }
5303 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005304 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005305
Willy Tarreaud98cf932009-12-27 22:54:55 +01005306 if (ret == 0)
5307 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005308 else if (ret < 0) {
5309 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005310 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005312 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005313 /* we're in HTTP_MSG_DONE now */
5314 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005316 int old_state = msg->msg_state;
5317
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005319 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005320 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5321 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5322 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005323 if (http_resync_states(s)) {
5324 http_silent_debug(__LINE__, s);
5325 /* some state changes occurred, maybe the analyser
5326 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005327 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005328 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5329 if (res->flags & BF_SHUTW) {
5330 /* response errors are most likely due to
5331 * the client aborting the transfer.
5332 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005333 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005334 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005335 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005336 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005338 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005340 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 }
5343 }
5344
Willy Tarreaud98cf932009-12-27 22:54:55 +01005345 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005346 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005347 if (res->flags & BF_SHUTR) {
5348 if (!(s->flags & SN_ERR_MASK))
5349 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005350 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005351 if (target_srv(&s->target))
5352 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005353 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005354 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005355
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005356 if (res->flags & BF_SHUTW)
5357 goto aborted_xfer;
5358
Willy Tarreau40dba092010-03-04 18:14:51 +01005359 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 if (!s->req->analysers)
5361 goto return_bad_res;
5362
Willy Tarreauea953162012-05-18 23:41:28 +02005363 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005364 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005365 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005366 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005367 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005368 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005369 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005370 }
5371
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005372 /* When TE: chunked is used, we need to get there again to parse remaining
5373 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5374 * Similarly, with keep-alive on the client side, we don't want to forward a
5375 * close.
5376 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005377 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005378 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5379 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5380 buffer_dont_close(res);
5381
Willy Tarreau5c620922011-05-11 19:56:11 +02005382 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005383 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5384 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005385 * modes are already handled by the stream sock layer. We must not do
5386 * this in content-length mode because it could present the MSG_MORE
5387 * flag with the last block of forwarded data, which would cause an
5388 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005389 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005390 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005391 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005392
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005394 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005395 return 0;
5396
Willy Tarreau40dba092010-03-04 18:14:51 +01005397 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005398 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005399 if (target_srv(&s->target))
5400 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005401
5402 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005404 /* don't send any error message as we're in the body */
5405 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005407 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005408 if (target_srv(&s->target))
5409 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410
5411 if (!(s->flags & SN_ERR_MASK))
5412 s->flags |= SN_ERR_PRXCOND;
5413 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005414 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005415 return 0;
5416
5417 aborted_xfer:
5418 txn->rsp.msg_state = HTTP_MSG_ERROR;
5419 /* don't send any error message as we're in the body */
5420 stream_int_retnclose(res->cons, NULL);
5421 res->analysers = 0;
5422 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5423
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005424 s->fe->fe_counters.cli_aborts++;
5425 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005426 if (target_srv(&s->target))
5427 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005428
5429 if (!(s->flags & SN_ERR_MASK))
5430 s->flags |= SN_ERR_CLICL;
5431 if (!(s->flags & SN_FINST_MASK))
5432 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433 return 0;
5434}
5435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005436/* Iterate the same filter through all request headers.
5437 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005438 * Since it can manage the switch to another backend, it updates the per-proxy
5439 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005440 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005441int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005443 char term;
5444 char *cur_ptr, *cur_end, *cur_next;
5445 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005446 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005447 struct hdr_idx_elem *cur_hdr;
5448 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005450 last_hdr = 0;
5451
Willy Tarreau09d1e252012-05-18 22:36:34 +02005452 cur_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005453 old_idx = 0;
5454
5455 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005456 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005457 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005458 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005459 (exp->action == ACT_ALLOW ||
5460 exp->action == ACT_DENY ||
5461 exp->action == ACT_TARPIT))
5462 return 0;
5463
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005464 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005465 if (!cur_idx)
5466 break;
5467
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005468 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005469 cur_ptr = cur_next;
5470 cur_end = cur_ptr + cur_hdr->len;
5471 cur_next = cur_end + cur_hdr->cr + 1;
5472
5473 /* Now we have one header between cur_ptr and cur_end,
5474 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005475 */
5476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005477 /* The annoying part is that pattern matching needs
5478 * that we modify the contents to null-terminate all
5479 * strings before testing them.
5480 */
5481
5482 term = *cur_end;
5483 *cur_end = '\0';
5484
5485 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5486 switch (exp->action) {
5487 case ACT_SETBE:
5488 /* It is not possible to jump a second time.
5489 * FIXME: should we return an HTTP/500 here so that
5490 * the admin knows there's a problem ?
5491 */
5492 if (t->be != t->fe)
5493 break;
5494
5495 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005496 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005497 last_hdr = 1;
5498 break;
5499
5500 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005501 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502 last_hdr = 1;
5503 break;
5504
5505 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005506 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005508
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005509 t->fe->fe_counters.denied_req++;
5510 if (t->fe != t->be)
5511 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005512 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005513 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005515 break;
5516
5517 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005518 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005519 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005520
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005521 t->fe->fe_counters.denied_req++;
5522 if (t->fe != t->be)
5523 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005524 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005525 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005527 break;
5528
5529 case ACT_REPLACE:
5530 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5531 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5532 /* FIXME: if the user adds a newline in the replacement, the
5533 * index will not be recalculated for now, and the new line
5534 * will not be counted as a new header.
5535 */
5536
5537 cur_end += delta;
5538 cur_next += delta;
5539 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005540 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005541 break;
5542
5543 case ACT_REMOVE:
5544 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5545 cur_next += delta;
5546
Willy Tarreaufa355d42009-11-29 18:12:29 +01005547 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005548 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5549 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005550 cur_hdr->len = 0;
5551 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005552 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005553 break;
5554
5555 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005556 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005557 if (cur_end)
5558 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005560 /* keep the link from this header to next one in case of later
5561 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005562 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005563 old_idx = cur_idx;
5564 }
5565 return 0;
5566}
5567
5568
5569/* Apply the filter to the request line.
5570 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5571 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005572 * Since it can manage the switch to another backend, it updates the per-proxy
5573 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 */
5575int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5576{
5577 char term;
5578 char *cur_ptr, *cur_end;
5579 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005580 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005581 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005582
Willy Tarreau58f10d72006-12-04 02:26:12 +01005583
Willy Tarreau3d300592007-03-18 18:34:41 +01005584 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005585 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005586 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005587 (exp->action == ACT_ALLOW ||
5588 exp->action == ACT_DENY ||
5589 exp->action == ACT_TARPIT))
5590 return 0;
5591 else if (exp->action == ACT_REMOVE)
5592 return 0;
5593
5594 done = 0;
5595
Willy Tarreau09d1e252012-05-18 22:36:34 +02005596 cur_ptr = req->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005597 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598
5599 /* Now we have the request line between cur_ptr and cur_end */
5600
5601 /* The annoying part is that pattern matching needs
5602 * that we modify the contents to null-terminate all
5603 * strings before testing them.
5604 */
5605
5606 term = *cur_end;
5607 *cur_end = '\0';
5608
5609 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5610 switch (exp->action) {
5611 case ACT_SETBE:
5612 /* It is not possible to jump a second time.
5613 * FIXME: should we return an HTTP/500 here so that
5614 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005615 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 if (t->be != t->fe)
5617 break;
5618
5619 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005620 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 done = 1;
5622 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005624 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005625 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005626 done = 1;
5627 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005629 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005630 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005631
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005632 t->fe->fe_counters.denied_req++;
5633 if (t->fe != t->be)
5634 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005635 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005636 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005638 done = 1;
5639 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005641 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005642 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005643
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005644 t->fe->fe_counters.denied_req++;
5645 if (t->fe != t->be)
5646 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005647 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005648 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 done = 1;
5651 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653 case ACT_REPLACE:
5654 *cur_end = term; /* restore the string terminator */
5655 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5656 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5657 /* FIXME: if the user adds a newline in the replacement, the
5658 * index will not be recalculated for now, and the new line
5659 * will not be counted as a new header.
5660 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005661
Willy Tarreaufa355d42009-11-29 18:12:29 +01005662 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005664 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005665 HTTP_MSG_RQMETH,
5666 cur_ptr, cur_end + 1,
5667 NULL, NULL);
5668 if (unlikely(!cur_end))
5669 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 /* we have a full request and we know that we have either a CR
5672 * or an LF at <ptr>.
5673 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005674 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5675 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 /* there is no point trying this regex on headers */
5677 return 1;
5678 }
5679 }
5680 *cur_end = term; /* restore the string terminator */
5681 return done;
5682}
Willy Tarreau97de6242006-12-27 17:18:38 +01005683
Willy Tarreau58f10d72006-12-04 02:26:12 +01005684
Willy Tarreau58f10d72006-12-04 02:26:12 +01005685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005687 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005689 * unparsable request. Since it can manage the switch to another backend, it
5690 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005692int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005693{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005694 struct http_txn *txn = &s->txn;
5695 struct hdr_exp *exp;
5696
5697 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 /*
5701 * The interleaving of transformations and verdicts
5702 * makes it difficult to decide to continue or stop
5703 * the evaluation.
5704 */
5705
Willy Tarreau6c123b12010-01-28 20:22:06 +01005706 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5707 break;
5708
Willy Tarreau3d300592007-03-18 18:34:41 +01005709 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005711 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005713
5714 /* if this filter had a condition, evaluate it now and skip to
5715 * next filter if the condition does not match.
5716 */
5717 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005718 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005719 ret = acl_pass(ret);
5720 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5721 ret = !ret;
5722
5723 if (!ret)
5724 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725 }
5726
5727 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005728 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 if (unlikely(ret < 0))
5730 return -1;
5731
5732 if (likely(ret == 0)) {
5733 /* The filter did not match the request, it can be
5734 * iterated through all headers.
5735 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005736 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005738 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005740}
5741
5742
Willy Tarreaua15645d2007-03-18 16:22:39 +01005743
Willy Tarreau58f10d72006-12-04 02:26:12 +01005744/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005745 * Try to retrieve the server associated to the appsession.
5746 * If the server is found, it's assigned to the session.
5747 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005748void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005749 struct http_txn *txn = &t->txn;
5750 appsess *asession = NULL;
5751 char *sessid_temp = NULL;
5752
Cyril Bontéb21570a2009-11-29 20:04:48 +01005753 if (len > t->be->appsession_len) {
5754 len = t->be->appsession_len;
5755 }
5756
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005757 if (t->be->options2 & PR_O2_AS_REQL) {
5758 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005759 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005760 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005761 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005762 }
5763
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005764 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005765 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5766 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5767 return;
5768 }
5769
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005770 memcpy(txn->sessid, buf, len);
5771 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772 }
5773
5774 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5775 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5776 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5777 return;
5778 }
5779
Cyril Bontéb21570a2009-11-29 20:04:48 +01005780 memcpy(sessid_temp, buf, len);
5781 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005782
5783 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5784 /* free previously allocated memory */
5785 pool_free2(apools.sessid, sessid_temp);
5786
5787 if (asession != NULL) {
5788 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5789 if (!(t->be->options2 & PR_O2_AS_REQL))
5790 asession->request_count++;
5791
5792 if (asession->serverid != NULL) {
5793 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005794
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005795 while (srv) {
5796 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005797 if ((srv->state & SRV_RUNNING) ||
5798 (t->be->options & PR_O_PERSIST) ||
5799 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005800 /* we found the server and it's usable */
5801 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005802 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005803 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005804 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005805
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005806 break;
5807 } else {
5808 txn->flags &= ~TX_CK_MASK;
5809 txn->flags |= TX_CK_DOWN;
5810 }
5811 }
5812 srv = srv->next;
5813 }
5814 }
5815 }
5816}
5817
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005818/* Find the end of a cookie value contained between <s> and <e>. It works the
5819 * same way as with headers above except that the semi-colon also ends a token.
5820 * See RFC2965 for more information. Note that it requires a valid header to
5821 * return a valid result.
5822 */
5823char *find_cookie_value_end(char *s, const char *e)
5824{
5825 int quoted, qdpair;
5826
5827 quoted = qdpair = 0;
5828 for (; s < e; s++) {
5829 if (qdpair) qdpair = 0;
5830 else if (quoted) {
5831 if (*s == '\\') qdpair = 1;
5832 else if (*s == '"') quoted = 0;
5833 }
5834 else if (*s == '"') quoted = 1;
5835 else if (*s == ',' || *s == ';') return s;
5836 }
5837 return s;
5838}
5839
5840/* Delete a value in a header between delimiters <from> and <next> in buffer
5841 * <buf>. The number of characters displaced is returned, and the pointer to
5842 * the first delimiter is updated if required. The function tries as much as
5843 * possible to respect the following principles :
5844 * - replace <from> delimiter by the <next> one unless <from> points to a
5845 * colon, in which case <next> is simply removed
5846 * - set exactly one space character after the new first delimiter, unless
5847 * there are not enough characters in the block being moved to do so.
5848 * - remove unneeded spaces before the previous delimiter and after the new
5849 * one.
5850 *
5851 * It is the caller's responsibility to ensure that :
5852 * - <from> points to a valid delimiter or the colon ;
5853 * - <next> points to a valid delimiter or the final CR/LF ;
5854 * - there are non-space chars before <from> ;
5855 * - there is a CR/LF at or after <next>.
5856 */
5857int del_hdr_value(struct buffer *buf, char **from, char *next)
5858{
5859 char *prev = *from;
5860
5861 if (*prev == ':') {
5862 /* We're removing the first value, preserve the colon and add a
5863 * space if possible.
5864 */
5865 if (!http_is_crlf[(unsigned char)*next])
5866 next++;
5867 prev++;
5868 if (prev < next)
5869 *prev++ = ' ';
5870
5871 while (http_is_spht[(unsigned char)*next])
5872 next++;
5873 } else {
5874 /* Remove useless spaces before the old delimiter. */
5875 while (http_is_spht[(unsigned char)*(prev-1)])
5876 prev--;
5877 *from = prev;
5878
5879 /* copy the delimiter and if possible a space if we're
5880 * not at the end of the line.
5881 */
5882 if (!http_is_crlf[(unsigned char)*next]) {
5883 *prev++ = *next++;
5884 if (prev + 1 < next)
5885 *prev++ = ' ';
5886 while (http_is_spht[(unsigned char)*next])
5887 next++;
5888 }
5889 }
5890 return buffer_replace2(buf, prev, next, NULL, 0);
5891}
5892
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005893/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005894 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005895 * desirable to call it only when needed. This code is quite complex because
5896 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5897 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005898 */
5899void manage_client_side_cookies(struct session *t, struct buffer *req)
5900{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005901 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005902 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005904 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5905 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005907 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005908 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02005909 hdr_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005910
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005911 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005912 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005913 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005914
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005915 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 hdr_beg = hdr_next;
5917 hdr_end = hdr_beg + cur_hdr->len;
5918 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005920 /* We have one full header between hdr_beg and hdr_end, and the
5921 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005922 * "Cookie:" headers.
5923 */
5924
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005925 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005926 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927 old_idx = cur_idx;
5928 continue;
5929 }
5930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 del_from = NULL; /* nothing to be deleted */
5932 preserve_hdr = 0; /* assume we may kill the whole header */
5933
Willy Tarreau58f10d72006-12-04 02:26:12 +01005934 /* Now look for cookies. Conforming to RFC2109, we have to support
5935 * attributes whose name begin with a '$', and associate them with
5936 * the right cookie, if we want to delete this cookie.
5937 * So there are 3 cases for each cookie read :
5938 * 1) it's a special attribute, beginning with a '$' : ignore it.
5939 * 2) it's a server id cookie that we *MAY* want to delete : save
5940 * some pointers on it (last semi-colon, beginning of cookie...)
5941 * 3) it's an application cookie : we *MAY* have to delete a previous
5942 * "special" cookie.
5943 * At the end of loop, if a "special" cookie remains, we may have to
5944 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005945 * *MUST* delete it.
5946 *
5947 * Note: RFC2965 is unclear about the processing of spaces around
5948 * the equal sign in the ATTR=VALUE form. A careful inspection of
5949 * the RFC explicitly allows spaces before it, and not within the
5950 * tokens (attrs or values). An inspection of RFC2109 allows that
5951 * too but section 10.1.3 lets one think that spaces may be allowed
5952 * after the equal sign too, resulting in some (rare) buggy
5953 * implementations trying to do that. So let's do what servers do.
5954 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5955 * allowed quoted strings in values, with any possible character
5956 * after a backslash, including control chars and delimitors, which
5957 * causes parsing to become ambiguous. Browsers also allow spaces
5958 * within values even without quotes.
5959 *
5960 * We have to keep multiple pointers in order to support cookie
5961 * removal at the beginning, middle or end of header without
5962 * corrupting the header. All of these headers are valid :
5963 *
5964 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5965 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5966 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5967 * | | | | | | | | |
5968 * | | | | | | | | hdr_end <--+
5969 * | | | | | | | +--> next
5970 * | | | | | | +----> val_end
5971 * | | | | | +-----------> val_beg
5972 * | | | | +--------------> equal
5973 * | | | +----------------> att_end
5974 * | | +---------------------> att_beg
5975 * | +--------------------------> prev
5976 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005977 */
5978
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005979 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5980 /* Iterate through all cookies on this line */
5981
5982 /* find att_beg */
5983 att_beg = prev + 1;
5984 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5985 att_beg++;
5986
5987 /* find att_end : this is the first character after the last non
5988 * space before the equal. It may be equal to hdr_end.
5989 */
5990 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005992 while (equal < hdr_end) {
5993 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005994 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005995 if (http_is_spht[(unsigned char)*equal++])
5996 continue;
5997 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005998 }
5999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006000 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6001 * is between <att_beg> and <equal>, both may be identical.
6002 */
6003
6004 /* look for end of cookie if there is an equal sign */
6005 if (equal < hdr_end && *equal == '=') {
6006 /* look for the beginning of the value */
6007 val_beg = equal + 1;
6008 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6009 val_beg++;
6010
6011 /* find the end of the value, respecting quotes */
6012 next = find_cookie_value_end(val_beg, hdr_end);
6013
6014 /* make val_end point to the first white space or delimitor after the value */
6015 val_end = next;
6016 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6017 val_end--;
6018 } else {
6019 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006020 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006022 /* We have nothing to do with attributes beginning with '$'. However,
6023 * they will automatically be removed if a header before them is removed,
6024 * since they're supposed to be linked together.
6025 */
6026 if (*att_beg == '$')
6027 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006028
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006029 /* Ignore cookies with no equal sign */
6030 if (equal == next) {
6031 /* This is not our cookie, so we must preserve it. But if we already
6032 * scheduled another cookie for removal, we cannot remove the
6033 * complete header, but we can remove the previous block itself.
6034 */
6035 preserve_hdr = 1;
6036 if (del_from != NULL) {
6037 int delta = del_hdr_value(req, &del_from, prev);
6038 val_end += delta;
6039 next += delta;
6040 hdr_end += delta;
6041 hdr_next += delta;
6042 cur_hdr->len += delta;
6043 http_msg_move_end(&txn->req, delta);
6044 prev = del_from;
6045 del_from = NULL;
6046 }
6047 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006048 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006050 /* if there are spaces around the equal sign, we need to
6051 * strip them otherwise we'll get trouble for cookie captures,
6052 * or even for rewrites. Since this happens extremely rarely,
6053 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6056 int stripped_before = 0;
6057 int stripped_after = 0;
6058
6059 if (att_end != equal) {
6060 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6061 equal += stripped_before;
6062 val_beg += stripped_before;
6063 }
6064
6065 if (val_beg > equal + 1) {
6066 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6067 val_beg += stripped_after;
6068 stripped_before += stripped_after;
6069 }
6070
6071 val_end += stripped_before;
6072 next += stripped_before;
6073 hdr_end += stripped_before;
6074 hdr_next += stripped_before;
6075 cur_hdr->len += stripped_before;
6076 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006080 /* First, let's see if we want to capture this cookie. We check
6081 * that we don't already have a client side cookie, because we
6082 * can only capture one. Also as an optimisation, we ignore
6083 * cookies shorter than the declared name.
6084 */
6085 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6086 (val_end - att_beg >= t->fe->capture_namelen) &&
6087 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6088 int log_len = val_end - att_beg;
6089
6090 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6091 Alert("HTTP logging : out of memory.\n");
6092 } else {
6093 if (log_len > t->fe->capture_len)
6094 log_len = t->fe->capture_len;
6095 memcpy(txn->cli_cookie, att_beg, log_len);
6096 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006097 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006099
Willy Tarreaubca99692010-10-06 19:25:55 +02006100 /* Persistence cookies in passive, rewrite or insert mode have the
6101 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006102 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006103 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006105 * For cookies in prefix mode, the form is :
6106 *
6107 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 */
6109 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6110 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6111 struct server *srv = t->be->srv;
6112 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6115 * have the server ID between val_beg and delim, and the original cookie between
6116 * delim+1 and val_end. Otherwise, delim==val_end :
6117 *
6118 * Cookie: NAME=SRV; # in all but prefix modes
6119 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6120 * | || || | |+-> next
6121 * | || || | +--> val_end
6122 * | || || +---------> delim
6123 * | || |+------------> val_beg
6124 * | || +-------------> att_end = equal
6125 * | |+-----------------> att_beg
6126 * | +------------------> prev
6127 * +-------------------------> hdr_beg
6128 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006129
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006130 if (t->be->options & PR_O_COOK_PFX) {
6131 for (delim = val_beg; delim < val_end; delim++)
6132 if (*delim == COOKIE_DELIM)
6133 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006134 } else {
6135 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006137 /* Now check if the cookie contains a date field, which would
6138 * appear after a vertical bar ('|') just after the server name
6139 * and before the delimiter.
6140 */
6141 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6142 if (vbar1) {
6143 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006144 * right is the last seen date. It is a base64 encoded
6145 * 30-bit value representing the UNIX date since the
6146 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006147 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006148 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006149 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006150 if (val_end - vbar1 >= 5) {
6151 val = b64tos30(vbar1);
6152 if (val > 0)
6153 txn->cookie_last_date = val << 2;
6154 }
6155 /* look for a second vertical bar */
6156 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6157 if (vbar1 && (val_end - vbar1 > 5)) {
6158 val = b64tos30(vbar1 + 1);
6159 if (val > 0)
6160 txn->cookie_first_date = val << 2;
6161 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006162 }
6163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164
Willy Tarreauf64d1412010-10-07 20:06:11 +02006165 /* if the cookie has an expiration date and the proxy wants to check
6166 * it, then we do that now. We first check if the cookie is too old,
6167 * then only if it has expired. We detect strict overflow because the
6168 * time resolution here is not great (4 seconds). Cookies with dates
6169 * in the future are ignored if their offset is beyond one day. This
6170 * allows an admin to fix timezone issues without expiring everyone
6171 * and at the same time avoids keeping unwanted side effects for too
6172 * long.
6173 */
6174 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006175 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6176 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006177 txn->flags &= ~TX_CK_MASK;
6178 txn->flags |= TX_CK_OLD;
6179 delim = val_beg; // let's pretend we have not found the cookie
6180 txn->cookie_first_date = 0;
6181 txn->cookie_last_date = 0;
6182 }
6183 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006184 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6185 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006186 txn->flags &= ~TX_CK_MASK;
6187 txn->flags |= TX_CK_EXPIRED;
6188 delim = val_beg; // let's pretend we have not found the cookie
6189 txn->cookie_first_date = 0;
6190 txn->cookie_last_date = 0;
6191 }
6192
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 /* Here, we'll look for the first running server which supports the cookie.
6194 * This allows to share a same cookie between several servers, for example
6195 * to dedicate backup servers to specific servers only.
6196 * However, to prevent clients from sticking to cookie-less backup server
6197 * when they have incidentely learned an empty cookie, we simply ignore
6198 * empty cookies and mark them as invalid.
6199 * The same behaviour is applied when persistence must be ignored.
6200 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006201 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 while (srv) {
6205 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6206 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6207 if ((srv->state & SRV_RUNNING) ||
6208 (t->be->options & PR_O_PERSIST) ||
6209 (t->flags & SN_FORCE_PRST)) {
6210 /* we found the server and we can use it */
6211 txn->flags &= ~TX_CK_MASK;
6212 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6213 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006214 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 break;
6216 } else {
6217 /* we found a server, but it's down,
6218 * mark it as such and go on in case
6219 * another one is available.
6220 */
6221 txn->flags &= ~TX_CK_MASK;
6222 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 srv = srv->next;
6226 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006227
Willy Tarreauf64d1412010-10-07 20:06:11 +02006228 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006229 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006231 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6232 txn->flags |= TX_CK_UNUSED;
6233 else
6234 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 /* depending on the cookie mode, we may have to either :
6238 * - delete the complete cookie if we're in insert+indirect mode, so that
6239 * the server never sees it ;
6240 * - remove the server id from the cookie value, and tag the cookie as an
6241 * application cookie so that it does not get accidentely removed later,
6242 * if we're in cookie prefix mode
6243 */
6244 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6245 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6248 val_end += delta;
6249 next += delta;
6250 hdr_end += delta;
6251 hdr_next += delta;
6252 cur_hdr->len += delta;
6253 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006255 del_from = NULL;
6256 preserve_hdr = 1; /* we want to keep this cookie */
6257 }
6258 else if (del_from == NULL &&
6259 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6260 del_from = prev;
6261 }
6262 } else {
6263 /* This is not our cookie, so we must preserve it. But if we already
6264 * scheduled another cookie for removal, we cannot remove the
6265 * complete header, but we can remove the previous block itself.
6266 */
6267 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 if (del_from != NULL) {
6270 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006271 if (att_beg >= del_from)
6272 att_beg += delta;
6273 if (att_end >= del_from)
6274 att_end += delta;
6275 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006276 val_end += delta;
6277 next += delta;
6278 hdr_end += delta;
6279 hdr_next += delta;
6280 cur_hdr->len += delta;
6281 http_msg_move_end(&txn->req, delta);
6282 prev = del_from;
6283 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 /* Look for the appsession cookie unless persistence must be ignored */
6288 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6289 int cmp_len, value_len;
6290 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 if (t->be->options2 & PR_O2_AS_PFX) {
6293 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6294 value_begin = att_beg + t->be->appsession_name_len;
6295 value_len = val_end - att_beg - t->be->appsession_name_len;
6296 } else {
6297 cmp_len = att_end - att_beg;
6298 value_begin = val_beg;
6299 value_len = val_end - val_beg;
6300 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 /* let's see if the cookie is our appcookie */
6303 if (cmp_len == t->be->appsession_name_len &&
6304 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6305 manage_client_side_appsession(t, value_begin, value_len);
6306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307 }
6308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 /* continue with next cookie on this header line */
6310 att_beg = next;
6311 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006312
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 /* There are no more cookies on this line.
6314 * We may still have one (or several) marked for deletion at the
6315 * end of the line. We must do this now in two ways :
6316 * - if some cookies must be preserved, we only delete from the
6317 * mark to the end of line ;
6318 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 if (preserve_hdr) {
6323 delta = del_hdr_value(req, &del_from, hdr_end);
6324 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325 cur_hdr->len += delta;
6326 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
6329 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006330 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6331 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006333 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006334 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006336 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337 }
6338
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342}
6343
6344
Willy Tarreaua15645d2007-03-18 16:22:39 +01006345/* Iterate the same filter through all response headers contained in <rtr>.
6346 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6347 */
6348int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6349{
6350 char term;
6351 char *cur_ptr, *cur_end, *cur_next;
6352 int cur_idx, old_idx, last_hdr;
6353 struct http_txn *txn = &t->txn;
6354 struct hdr_idx_elem *cur_hdr;
6355 int len, delta;
6356
6357 last_hdr = 0;
6358
Willy Tarreau09d1e252012-05-18 22:36:34 +02006359 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006360 old_idx = 0;
6361
6362 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006363 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006364 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006365 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 (exp->action == ACT_ALLOW ||
6367 exp->action == ACT_DENY))
6368 return 0;
6369
6370 cur_idx = txn->hdr_idx.v[old_idx].next;
6371 if (!cur_idx)
6372 break;
6373
6374 cur_hdr = &txn->hdr_idx.v[cur_idx];
6375 cur_ptr = cur_next;
6376 cur_end = cur_ptr + cur_hdr->len;
6377 cur_next = cur_end + cur_hdr->cr + 1;
6378
6379 /* Now we have one header between cur_ptr and cur_end,
6380 * and the next header starts at cur_next.
6381 */
6382
6383 /* The annoying part is that pattern matching needs
6384 * that we modify the contents to null-terminate all
6385 * strings before testing them.
6386 */
6387
6388 term = *cur_end;
6389 *cur_end = '\0';
6390
6391 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6392 switch (exp->action) {
6393 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006394 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006395 last_hdr = 1;
6396 break;
6397
6398 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006399 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006400 last_hdr = 1;
6401 break;
6402
6403 case ACT_REPLACE:
6404 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6405 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6406 /* FIXME: if the user adds a newline in the replacement, the
6407 * index will not be recalculated for now, and the new line
6408 * will not be counted as a new header.
6409 */
6410
6411 cur_end += delta;
6412 cur_next += delta;
6413 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006414 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006415 break;
6416
6417 case ACT_REMOVE:
6418 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6419 cur_next += delta;
6420
Willy Tarreaufa355d42009-11-29 18:12:29 +01006421 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006422 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6423 txn->hdr_idx.used--;
6424 cur_hdr->len = 0;
6425 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006426 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 break;
6428
6429 }
6430 }
6431 if (cur_end)
6432 *cur_end = term; /* restore the string terminator */
6433
6434 /* keep the link from this header to next one in case of later
6435 * removal of next header.
6436 */
6437 old_idx = cur_idx;
6438 }
6439 return 0;
6440}
6441
6442
6443/* Apply the filter to the status line in the response buffer <rtr>.
6444 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6445 * or -1 if a replacement resulted in an invalid status line.
6446 */
6447int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6448{
6449 char term;
6450 char *cur_ptr, *cur_end;
6451 int done;
6452 struct http_txn *txn = &t->txn;
6453 int len, delta;
6454
6455
Willy Tarreau3d300592007-03-18 18:34:41 +01006456 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006458 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459 (exp->action == ACT_ALLOW ||
6460 exp->action == ACT_DENY))
6461 return 0;
6462 else if (exp->action == ACT_REMOVE)
6463 return 0;
6464
6465 done = 0;
6466
Willy Tarreau09d1e252012-05-18 22:36:34 +02006467 cur_ptr = rtr->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006468 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469
6470 /* Now we have the status line between cur_ptr and cur_end */
6471
6472 /* The annoying part is that pattern matching needs
6473 * that we modify the contents to null-terminate all
6474 * strings before testing them.
6475 */
6476
6477 term = *cur_end;
6478 *cur_end = '\0';
6479
6480 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6481 switch (exp->action) {
6482 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006483 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484 done = 1;
6485 break;
6486
6487 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006488 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006489 done = 1;
6490 break;
6491
6492 case ACT_REPLACE:
6493 *cur_end = term; /* restore the string terminator */
6494 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6495 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6496 /* FIXME: if the user adds a newline in the replacement, the
6497 * index will not be recalculated for now, and the new line
6498 * will not be counted as a new header.
6499 */
6500
Willy Tarreaufa355d42009-11-29 18:12:29 +01006501 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006502 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006503 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006504 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 cur_ptr, cur_end + 1,
6506 NULL, NULL);
6507 if (unlikely(!cur_end))
6508 return -1;
6509
6510 /* we have a full respnse and we know that we have either a CR
6511 * or an LF at <ptr>.
6512 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02006513 txn->status = strl2ui(rtr->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006514 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006515 /* there is no point trying this regex on headers */
6516 return 1;
6517 }
6518 }
6519 *cur_end = term; /* restore the string terminator */
6520 return done;
6521}
6522
6523
6524
6525/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006526 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006527 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6528 * unparsable response.
6529 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006530int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006532 struct http_txn *txn = &s->txn;
6533 struct hdr_exp *exp;
6534
6535 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 int ret;
6537
6538 /*
6539 * The interleaving of transformations and verdicts
6540 * makes it difficult to decide to continue or stop
6541 * the evaluation.
6542 */
6543
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006544 if (txn->flags & TX_SVDENY)
6545 break;
6546
Willy Tarreau3d300592007-03-18 18:34:41 +01006547 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6549 exp->action == ACT_PASS)) {
6550 exp = exp->next;
6551 continue;
6552 }
6553
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006554 /* if this filter had a condition, evaluate it now and skip to
6555 * next filter if the condition does not match.
6556 */
6557 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006558 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006559 ret = acl_pass(ret);
6560 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6561 ret = !ret;
6562 if (!ret)
6563 continue;
6564 }
6565
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006567 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568 if (unlikely(ret < 0))
6569 return -1;
6570
6571 if (likely(ret == 0)) {
6572 /* The filter did not match the response, it can be
6573 * iterated through all headers.
6574 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006575 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 }
6578 return 0;
6579}
6580
6581
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006583 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006584 * desirable to call it only when needed. This function is also used when we
6585 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006587void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006588{
6589 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006590 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006591 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006593 char *hdr_beg, *hdr_end, *hdr_next;
6594 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 /* Iterate through the headers.
6597 * we start with the start line.
6598 */
6599 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006600 hdr_next = res->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601
6602 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6603 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006604 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605
6606 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006607 hdr_beg = hdr_next;
6608 hdr_end = hdr_beg + cur_hdr->len;
6609 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610
Willy Tarreau24581ba2010-08-31 22:39:35 +02006611 /* We have one full header between hdr_beg and hdr_end, and the
6612 * next header starts at hdr_next. We're only interested in
6613 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614 */
6615
Willy Tarreau24581ba2010-08-31 22:39:35 +02006616 is_cookie2 = 0;
6617 prev = hdr_beg + 10;
6618 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006619 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006620 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6621 if (!val) {
6622 old_idx = cur_idx;
6623 continue;
6624 }
6625 is_cookie2 = 1;
6626 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 }
6628
Willy Tarreau24581ba2010-08-31 22:39:35 +02006629 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6630 * <prev> points to the colon.
6631 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006632 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6635 * check-cache is enabled) and we are not interested in checking
6636 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006637 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006638 if (t->be->cookie_name == NULL &&
6639 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006640 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 return;
6642
Willy Tarreau24581ba2010-08-31 22:39:35 +02006643 /* OK so now we know we have to process this response cookie.
6644 * The format of the Set-Cookie header is slightly different
6645 * from the format of the Cookie header in that it does not
6646 * support the comma as a cookie delimiter (thus the header
6647 * cannot be folded) because the Expires attribute described in
6648 * the original Netscape's spec may contain an unquoted date
6649 * with a comma inside. We have to live with this because
6650 * many browsers don't support Max-Age and some browsers don't
6651 * support quoted strings. However the Set-Cookie2 header is
6652 * clean.
6653 *
6654 * We have to keep multiple pointers in order to support cookie
6655 * removal at the beginning, middle or end of header without
6656 * corrupting the header (in case of set-cookie2). A special
6657 * pointer, <scav> points to the beginning of the set-cookie-av
6658 * fields after the first semi-colon. The <next> pointer points
6659 * either to the end of line (set-cookie) or next unquoted comma
6660 * (set-cookie2). All of these headers are valid :
6661 *
6662 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6663 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6664 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6665 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6666 * | | | | | | | | | |
6667 * | | | | | | | | +-> next hdr_end <--+
6668 * | | | | | | | +------------> scav
6669 * | | | | | | +--------------> val_end
6670 * | | | | | +--------------------> val_beg
6671 * | | | | +----------------------> equal
6672 * | | | +------------------------> att_end
6673 * | | +----------------------------> att_beg
6674 * | +------------------------------> prev
6675 * +-----------------------------------------> hdr_beg
6676 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006677
Willy Tarreau24581ba2010-08-31 22:39:35 +02006678 for (; prev < hdr_end; prev = next) {
6679 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680
Willy Tarreau24581ba2010-08-31 22:39:35 +02006681 /* find att_beg */
6682 att_beg = prev + 1;
6683 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6684 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685
Willy Tarreau24581ba2010-08-31 22:39:35 +02006686 /* find att_end : this is the first character after the last non
6687 * space before the equal. It may be equal to hdr_end.
6688 */
6689 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690
Willy Tarreau24581ba2010-08-31 22:39:35 +02006691 while (equal < hdr_end) {
6692 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6693 break;
6694 if (http_is_spht[(unsigned char)*equal++])
6695 continue;
6696 att_end = equal;
6697 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698
Willy Tarreau24581ba2010-08-31 22:39:35 +02006699 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6700 * is between <att_beg> and <equal>, both may be identical.
6701 */
6702
6703 /* look for end of cookie if there is an equal sign */
6704 if (equal < hdr_end && *equal == '=') {
6705 /* look for the beginning of the value */
6706 val_beg = equal + 1;
6707 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6708 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709
Willy Tarreau24581ba2010-08-31 22:39:35 +02006710 /* find the end of the value, respecting quotes */
6711 next = find_cookie_value_end(val_beg, hdr_end);
6712
6713 /* make val_end point to the first white space or delimitor after the value */
6714 val_end = next;
6715 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6716 val_end--;
6717 } else {
6718 /* <equal> points to next comma, semi-colon or EOL */
6719 val_beg = val_end = next = equal;
6720 }
6721
6722 if (next < hdr_end) {
6723 /* Set-Cookie2 supports multiple cookies, and <next> points to
6724 * a colon or semi-colon before the end. So skip all attr-value
6725 * pairs and look for the next comma. For Set-Cookie, since
6726 * commas are permitted in values, skip to the end.
6727 */
6728 if (is_cookie2)
6729 next = find_hdr_value_end(next, hdr_end);
6730 else
6731 next = hdr_end;
6732 }
6733
6734 /* Now everything is as on the diagram above */
6735
6736 /* Ignore cookies with no equal sign */
6737 if (equal == val_end)
6738 continue;
6739
6740 /* If there are spaces around the equal sign, we need to
6741 * strip them otherwise we'll get trouble for cookie captures,
6742 * or even for rewrites. Since this happens extremely rarely,
6743 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006745 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6746 int stripped_before = 0;
6747 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748
Willy Tarreau24581ba2010-08-31 22:39:35 +02006749 if (att_end != equal) {
6750 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6751 equal += stripped_before;
6752 val_beg += stripped_before;
6753 }
6754
6755 if (val_beg > equal + 1) {
6756 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6757 val_beg += stripped_after;
6758 stripped_before += stripped_after;
6759 }
6760
6761 val_end += stripped_before;
6762 next += stripped_before;
6763 hdr_end += stripped_before;
6764 hdr_next += stripped_before;
6765 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006766 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006767 }
6768
6769 /* First, let's see if we want to capture this cookie. We check
6770 * that we don't already have a server side cookie, because we
6771 * can only capture one. Also as an optimisation, we ignore
6772 * cookies shorter than the declared name.
6773 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006774 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006775 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006776 (val_end - att_beg >= t->fe->capture_namelen) &&
6777 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6778 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006779 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780 Alert("HTTP logging : out of memory.\n");
6781 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006782 else {
6783 if (log_len > t->fe->capture_len)
6784 log_len = t->fe->capture_len;
6785 memcpy(txn->srv_cookie, att_beg, log_len);
6786 txn->srv_cookie[log_len] = 0;
6787 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 }
6789
Willy Tarreau827aee92011-03-10 16:55:02 +01006790 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006792 if (!(t->flags & SN_IGNORE_PRST) &&
6793 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6794 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006795 /* assume passive cookie by default */
6796 txn->flags &= ~TX_SCK_MASK;
6797 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798
6799 /* If the cookie is in insert mode on a known server, we'll delete
6800 * this occurrence because we'll insert another one later.
6801 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006802 * a direct access.
6803 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006804 if (t->be->options2 & PR_O2_COOK_PSV) {
6805 /* The "preserve" flag was set, we don't want to touch the
6806 * server's cookie.
6807 */
6808 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006809 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006810 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 /* this cookie must be deleted */
6812 if (*prev == ':' && next == hdr_end) {
6813 /* whole header */
6814 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6815 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6816 txn->hdr_idx.used--;
6817 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006818 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 hdr_next += delta;
6820 http_msg_move_end(&txn->rsp, delta);
6821 /* note: while both invalid now, <next> and <hdr_end>
6822 * are still equal, so the for() will stop as expected.
6823 */
6824 } else {
6825 /* just remove the value */
6826 int delta = del_hdr_value(res, &prev, next);
6827 next = prev;
6828 hdr_end += delta;
6829 hdr_next += delta;
6830 cur_hdr->len += delta;
6831 http_msg_move_end(&txn->rsp, delta);
6832 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006833 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006834 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006837 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006838 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 * with this server since we know it.
6840 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006841 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006842 next += delta;
6843 hdr_end += delta;
6844 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006846 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847
Willy Tarreauf1348312010-10-07 15:54:11 +02006848 txn->flags &= ~TX_SCK_MASK;
6849 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006851 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006855 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 next += delta;
6857 hdr_end += delta;
6858 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006860 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861
Willy Tarreau827aee92011-03-10 16:55:02 +01006862 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006863 txn->flags &= ~TX_SCK_MASK;
6864 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 }
6866 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006867 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6868 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006869 int cmp_len, value_len;
6870 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871
Cyril Bontéb21570a2009-11-29 20:04:48 +01006872 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006873 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6874 value_begin = att_beg + t->be->appsession_name_len;
6875 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006876 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 cmp_len = att_end - att_beg;
6878 value_begin = val_beg;
6879 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006880 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006881
Cyril Bonté17530c32010-04-06 21:11:10 +02006882 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6884 /* free a possibly previously allocated memory */
6885 pool_free2(apools.sessid, txn->sessid);
6886
Cyril Bontéb21570a2009-11-29 20:04:48 +01006887 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006888 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006889 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6890 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6891 return;
6892 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006893 memcpy(txn->sessid, value_begin, value_len);
6894 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 }
6897 /* that's done for this cookie, check the next one on the same
6898 * line when next != hdr_end (only if is_cookie2).
6899 */
6900 }
6901 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006904
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006905 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006906 appsess *asession = NULL;
6907 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006908 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006909 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006910 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006911 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6912 Alert("Not enough Memory process_srv():asession:calloc().\n");
6913 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6914 return;
6915 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006916 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6917
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006918 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6919 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6920 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006921 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006922 return;
6923 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006924 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006925 asession->sessid[t->be->appsession_len] = 0;
6926
Willy Tarreau827aee92011-03-10 16:55:02 +01006927 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006928 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006929 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006930 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006931 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 return;
6933 }
6934 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006935 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006936
6937 asession->request_count = 0;
6938 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6939 }
6940
6941 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6942 asession->request_count++;
6943 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944}
6945
6946
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947/*
6948 * Check if response is cacheable or not. Updates t->flags.
6949 */
6950void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6951{
6952 struct http_txn *txn = &t->txn;
6953 char *p1, *p2;
6954
6955 char *cur_ptr, *cur_end, *cur_next;
6956 int cur_idx;
6957
Willy Tarreau5df51872007-11-25 16:20:08 +01006958 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006959 return;
6960
6961 /* Iterate through the headers.
6962 * we start with the start line.
6963 */
6964 cur_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006965 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966
6967 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6968 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006969 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970
6971 cur_hdr = &txn->hdr_idx.v[cur_idx];
6972 cur_ptr = cur_next;
6973 cur_end = cur_ptr + cur_hdr->len;
6974 cur_next = cur_end + cur_hdr->cr + 1;
6975
6976 /* We have one full header between cur_ptr and cur_end, and the
6977 * next header starts at cur_next. We're only interested in
6978 * "Cookie:" headers.
6979 */
6980
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006981 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6982 if (val) {
6983 if ((cur_end - (cur_ptr + val) >= 8) &&
6984 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6985 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6986 return;
6987 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 }
6989
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006990 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6991 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 continue;
6993
6994 /* OK, right now we know we have a cache-control header at cur_ptr */
6995
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006996 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997
6998 if (p1 >= cur_end) /* no more info */
6999 continue;
7000
7001 /* p1 is at the beginning of the value */
7002 p2 = p1;
7003
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007004 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 p2++;
7006
7007 /* we have a complete value between p1 and p2 */
7008 if (p2 < cur_end && *p2 == '=') {
7009 /* we have something of the form no-cache="set-cookie" */
7010 if ((cur_end - p1 >= 21) &&
7011 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7012 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007013 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 continue;
7015 }
7016
7017 /* OK, so we know that either p2 points to the end of string or to a comma */
7018 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7019 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7020 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7021 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 return;
7024 }
7025
7026 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007027 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007028 continue;
7029 }
7030 }
7031}
7032
7033
Willy Tarreau58f10d72006-12-04 02:26:12 +01007034/*
7035 * Try to retrieve a known appsession in the URI, then the associated server.
7036 * If the server is found, it's assigned to the session.
7037 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007040 char *end_params, *first_param, *cur_param, *next_param;
7041 char separator;
7042 int value_len;
7043
7044 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007046 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007047 (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 +01007048 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050
Cyril Bontéb21570a2009-11-29 20:04:48 +01007051 first_param = NULL;
7052 switch (mode) {
7053 case PR_O2_AS_M_PP:
7054 first_param = memchr(begin, ';', len);
7055 break;
7056 case PR_O2_AS_M_QS:
7057 first_param = memchr(begin, '?', len);
7058 break;
7059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060
Cyril Bontéb21570a2009-11-29 20:04:48 +01007061 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Cyril Bontéb21570a2009-11-29 20:04:48 +01007065 switch (mode) {
7066 case PR_O2_AS_M_PP:
7067 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7068 end_params = (char *) begin + len;
7069 }
7070 separator = ';';
7071 break;
7072 case PR_O2_AS_M_QS:
7073 end_params = (char *) begin + len;
7074 separator = '&';
7075 break;
7076 default:
7077 /* unknown mode, shouldn't happen */
7078 return;
7079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080
Cyril Bontéb21570a2009-11-29 20:04:48 +01007081 cur_param = next_param = end_params;
7082 while (cur_param > first_param) {
7083 cur_param--;
7084 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7085 /* let's see if this is the appsession parameter */
7086 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7087 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7088 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7089 /* Cool... it's the right one */
7090 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7091 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7092 if (value_len > 0) {
7093 manage_client_side_appsession(t, cur_param, value_len);
7094 }
7095 break;
7096 }
7097 next_param = cur_param;
7098 }
7099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007101 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007102 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104}
7105
Willy Tarreaub2513902006-12-17 14:52:38 +01007106/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007107 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007108 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007109 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007110 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007111 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007112 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007113 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007114 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007115int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007116{
7117 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007118 struct http_msg *msg = &txn->req;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007119 const char *uri = msg->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007120 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007121
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007122 if (!uri_auth)
7123 return 0;
7124
Cyril Bonté70be45d2010-10-12 00:14:35 +02007125 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007126 return 0;
7127
Willy Tarreau295a8372011-03-10 11:25:07 +01007128 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007129 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007131 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007132 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007133 return 0;
7134
Willy Tarreau3a215be2012-03-09 21:39:51 +01007135 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007136 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007137 return 0;
7138
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007139 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007140 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007141 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007142 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007143 break;
7144 }
7145 h++;
7146 }
7147
7148 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007149 h = uri + uri_auth->uri_len;
7150 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007151 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007152 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007153 break;
7154 }
7155 h++;
7156 }
7157 }
7158
Willy Tarreau3a215be2012-03-09 21:39:51 +01007159 h = uri + uri_auth->uri_len;
7160 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007161 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007162 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007163 break;
7164 }
7165 h++;
7166 }
7167
Willy Tarreau3a215be2012-03-09 21:39:51 +01007168 h = uri + uri_auth->uri_len;
7169 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007170 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007171 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007172 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007173 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007174 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7175 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7176 si->applet.ctx.stats.st_code = i;
7177 break;
7178 }
7179 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007180 break;
7181 }
7182 h++;
7183 }
7184
Willy Tarreau295a8372011-03-10 11:25:07 +01007185 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007186
Willy Tarreaub2513902006-12-17 14:52:38 +01007187 return 1;
7188}
7189
Willy Tarreau4076a152009-04-02 15:18:36 +02007190/*
7191 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007192 * By default it tries to report the error position as msg->err_pos. However if
7193 * this one is not set, it will then report msg->next, which is the last known
7194 * parsing point. The function is able to deal with wrapping buffers. It always
7195 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007196 */
7197void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007198 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007199 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007200{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007201 struct buffer *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007202 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007203
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007204 es->len = MIN(buf->i, sizeof(es->buf));
7205 len1 = buf->data + buf->size - buf->p;
7206 len1 = MIN(len1, es->len);
7207 len2 = es->len - len1; /* remaining data if buffer wraps */
7208
7209 memcpy(es->buf, buf->p, len1);
7210 if (len2)
7211 memcpy(es->buf + len1, buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007212
Willy Tarreau4076a152009-04-02 15:18:36 +02007213 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007214 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007215 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007216 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007217
Willy Tarreau4076a152009-04-02 15:18:36 +02007218 es->when = date; // user-visible date
7219 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007220 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007221 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007222 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007223 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007224 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007225 es->b_flags = buf->flags;
7226 es->s_flags = s->flags;
7227 es->t_flags = s->txn.flags;
7228 es->m_flags = msg->flags;
7229 es->b_out = buf->o;
7230 es->b_wrap = buf->data + buf->size - buf->p;
7231 es->b_tot = buf->total;
7232 es->m_clen = msg->chunk_len;
7233 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007234}
Willy Tarreaub2513902006-12-17 14:52:38 +01007235
Willy Tarreau294c4732011-12-16 21:35:50 +01007236/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7237 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7238 * performed over the whole headers. Otherwise it must contain a valid header
7239 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7240 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7241 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7242 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7243 * -1.
7244 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007245 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007246unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007247 struct hdr_idx *idx, int occ,
7248 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007249{
Willy Tarreau294c4732011-12-16 21:35:50 +01007250 struct hdr_ctx local_ctx;
7251 char *ptr_hist[MAX_HDR_HISTORY];
7252 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007253 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007254 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007255
Willy Tarreau294c4732011-12-16 21:35:50 +01007256 if (!ctx) {
7257 local_ctx.idx = 0;
7258 ctx = &local_ctx;
7259 }
7260
Willy Tarreaubce70882009-09-07 11:51:47 +02007261 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007262 /* search from the beginning */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007263 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007264 occ--;
7265 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007266 *vptr = ctx->line + ctx->val;
7267 *vlen = ctx->vlen;
7268 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007269 }
7270 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007271 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007272 }
7273
7274 /* negative occurrence, we scan all the list then walk back */
7275 if (-occ > MAX_HDR_HISTORY)
7276 return 0;
7277
Willy Tarreau294c4732011-12-16 21:35:50 +01007278 found = hist_ptr = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007279 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007280 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7281 len_hist[hist_ptr] = ctx->vlen;
7282 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007283 hist_ptr = 0;
7284 found++;
7285 }
7286 if (-occ > found)
7287 return 0;
7288 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7289 * find occurrence -occ, so we have to check [hist_ptr+occ].
7290 */
7291 hist_ptr += occ;
7292 if (hist_ptr >= MAX_HDR_HISTORY)
7293 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007294 *vptr = ptr_hist[hist_ptr];
7295 *vlen = len_hist[hist_ptr];
7296 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007297}
7298
Willy Tarreaubaaee002006-06-26 02:48:02 +02007299/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300 * Print a debug line with a header
7301 */
7302void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7303{
7304 int len, max;
7305 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007306 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307 max = end - start;
David du Colombier7af46052012-05-16 14:16:48 +02007308 UBOUND(max, trashlen - len - 1);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309 len += strlcpy2(trash + len, start, max + 1);
7310 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007311 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312}
7313
Willy Tarreau0937bc42009-12-22 15:03:09 +01007314/*
7315 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7316 * the required fields are properly allocated and that we only need to (re)init
7317 * them. This should be used before processing any new request.
7318 */
7319void http_init_txn(struct session *s)
7320{
7321 struct http_txn *txn = &s->txn;
7322 struct proxy *fe = s->fe;
7323
7324 txn->flags = 0;
7325 txn->status = -1;
7326
William Lallemand5f232402012-04-05 18:02:55 +02007327 global.req_count++;
7328
Willy Tarreauf64d1412010-10-07 20:06:11 +02007329 txn->cookie_first_date = 0;
7330 txn->cookie_last_date = 0;
7331
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007332 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007333 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007334 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007335 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007336 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007337 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007338 txn->req.chunk_len = 0LL;
7339 txn->req.body_len = 0LL;
7340 txn->rsp.chunk_len = 0LL;
7341 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007342 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7343 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007344 txn->req.buf = s->req;
7345 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007346
7347 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007348
7349 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7350 if (fe->options2 & PR_O2_REQBUG_OK)
7351 txn->req.err_pos = -1; /* let buggy requests pass */
7352
Willy Tarreau46023632010-01-07 22:51:47 +01007353 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007354 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7355
Willy Tarreau46023632010-01-07 22:51:47 +01007356 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007357 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7358
7359 if (txn->hdr_idx.v)
7360 hdr_idx_init(&txn->hdr_idx);
7361}
7362
7363/* to be used at the end of a transaction */
7364void http_end_txn(struct session *s)
7365{
7366 struct http_txn *txn = &s->txn;
7367
7368 /* these ones will have been dynamically allocated */
7369 pool_free2(pool2_requri, txn->uri);
7370 pool_free2(pool2_capture, txn->cli_cookie);
7371 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007372 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007373 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007374
William Lallemanda73203e2012-03-12 12:48:57 +01007375 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007376 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007377 txn->uri = NULL;
7378 txn->srv_cookie = NULL;
7379 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007380
7381 if (txn->req.cap) {
7382 struct cap_hdr *h;
7383 for (h = s->fe->req_cap; h; h = h->next)
7384 pool_free2(h->pool, txn->req.cap[h->index]);
7385 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7386 }
7387
7388 if (txn->rsp.cap) {
7389 struct cap_hdr *h;
7390 for (h = s->fe->rsp_cap; h; h = h->next)
7391 pool_free2(h->pool, txn->rsp.cap[h->index]);
7392 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7393 }
7394
Willy Tarreau0937bc42009-12-22 15:03:09 +01007395}
7396
7397/* to be used at the end of a transaction to prepare a new one */
7398void http_reset_txn(struct session *s)
7399{
7400 http_end_txn(s);
7401 http_init_txn(s);
7402
7403 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007404 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007405 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007406 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007407 /* re-init store persistence */
7408 s->store_count = 0;
7409
Willy Tarreau0937bc42009-12-22 15:03:09 +01007410 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007411
7412 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7413
Willy Tarreau739cfba2010-01-25 23:11:14 +01007414 /* We must trim any excess data from the response buffer, because we
7415 * may have blocked an invalid response from a server that we don't
7416 * want to accidentely forward once we disable the analysers, nor do
7417 * we want those data to come along with next response. A typical
7418 * example of such data would be from a buggy server responding to
7419 * a HEAD with some data, or sending more than the advertised
7420 * content-length.
7421 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007422 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007423 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007424
Willy Tarreau0937bc42009-12-22 15:03:09 +01007425 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007426 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007427
Willy Tarreaud04e8582010-05-31 12:31:35 +02007428 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007429 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007430
7431 s->req->rex = TICK_ETERNITY;
7432 s->req->wex = TICK_ETERNITY;
7433 s->req->analyse_exp = TICK_ETERNITY;
7434 s->rep->rex = TICK_ETERNITY;
7435 s->rep->wex = TICK_ETERNITY;
7436 s->rep->analyse_exp = TICK_ETERNITY;
7437}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438
Willy Tarreauff011f22011-01-06 17:51:27 +01007439void free_http_req_rules(struct list *r) {
7440 struct http_req_rule *tr, *pr;
7441
7442 list_for_each_entry_safe(pr, tr, r, list) {
7443 LIST_DEL(&pr->list);
7444 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7445 free(pr->http_auth.realm);
7446
7447 free(pr);
7448 }
7449}
7450
7451struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7452{
7453 struct http_req_rule *rule;
7454 int cur_arg;
7455
7456 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7457 if (!rule) {
7458 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7459 return NULL;
7460 }
7461
7462 if (!*args[0]) {
7463 goto req_error_parsing;
7464 } else if (!strcmp(args[0], "allow")) {
7465 rule->action = HTTP_REQ_ACT_ALLOW;
7466 cur_arg = 1;
7467 } else if (!strcmp(args[0], "deny")) {
7468 rule->action = HTTP_REQ_ACT_DENY;
7469 cur_arg = 1;
7470 } else if (!strcmp(args[0], "auth")) {
7471 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7472 cur_arg = 1;
7473
7474 while(*args[cur_arg]) {
7475 if (!strcmp(args[cur_arg], "realm")) {
7476 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7477 cur_arg+=2;
7478 continue;
7479 } else
7480 break;
7481 }
7482 } else {
7483req_error_parsing:
7484 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7485 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7486 return NULL;
7487 }
7488
7489 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7490 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007491 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007492
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007493 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7494 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7495 file, linenum, args[0], errmsg);
7496 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007497 return NULL;
7498 }
7499 rule->cond = cond;
7500 }
7501 else if (*args[cur_arg]) {
7502 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7503 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7504 file, linenum, args[0], args[cur_arg]);
7505 return NULL;
7506 }
7507
7508 return rule;
7509}
7510
Willy Tarreau8797c062007-05-07 00:55:35 +02007511/************************************************************************/
7512/* The code below is dedicated to ACL parsing and matching */
7513/************************************************************************/
7514
7515
Willy Tarreau14174bc2012-04-16 14:34:04 +02007516/* This function ensures that the prerequisites for an L7 fetch are ready,
7517 * which means that a request or response is ready. If some data is missing,
7518 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007519 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7520 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007521 *
7522 * The function returns :
7523 * 0 if some data is missing or if the requested data cannot be fetched
7524 * -1 if it is certain that we'll never have any HTTP message there
7525 * 1 if an HTTP message is ready
7526 */
7527static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007528acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007529 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007530{
7531 struct http_txn *txn = l7;
7532 struct http_msg *msg = &txn->req;
7533
7534 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7535 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7536 */
7537
7538 if (unlikely(!s || !txn))
7539 return 0;
7540
7541 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007542 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007543
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007544 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007545 if (unlikely(!s->req))
7546 return 0;
7547
7548 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
7549 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007550 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007551 return -1;
7552 }
7553
7554 /* Try to decode HTTP request */
7555 if (likely(msg->next < s->req->i))
7556 http_msg_analyzer(msg, &txn->hdr_idx);
7557
7558 /* Still no valid request ? */
7559 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7560 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007561 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007562 return -1;
7563 }
7564 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007565 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007566 return 0;
7567 }
7568
7569 /* OK we just got a valid HTTP request. We have some minor
7570 * preparation to perform so that further checks can rely
7571 * on HTTP tests.
7572 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007573 txn->meth = find_http_meth(msg->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007574 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7575 s->flags |= SN_REDIRECTABLE;
7576
7577 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007578 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007579 return -1;
7580 }
7581 }
7582
Willy Tarreau24e32d82012-04-23 23:55:44 +02007583 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007584 return 0; /* data might have moved and indexes changed */
7585
7586 /* otherwise everything's ready for the request */
7587 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007588 else {
7589 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007590 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7591 return 0;
7592 }
7593
7594 /* everything's OK */
7595 return 1;
7596}
Willy Tarreau8797c062007-05-07 00:55:35 +02007597
Willy Tarreauc0239e02012-04-16 14:42:55 +02007598#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007599 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02007600
Willy Tarreau24e32d82012-04-23 23:55:44 +02007601#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007602 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02007603
Willy Tarreau8797c062007-05-07 00:55:35 +02007604
7605/* 1. Check on METHOD
7606 * We use the pre-parsed method if it is known, and store its number as an
7607 * integer. If it is unknown, we use the pointer and the length.
7608 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007609static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007610{
7611 int len, meth;
7612
Willy Tarreauae8b7962007-06-09 23:10:04 +02007613 len = strlen(*text);
7614 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007615
7616 pattern->val.i = meth;
7617 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007618 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007619 if (!pattern->ptr.str) {
7620 if (err)
7621 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007622 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007623 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007624 pattern->len = len;
7625 }
7626 return 1;
7627}
7628
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007629/* This function fetches the method of current HTTP request and stores
7630 * it in the global pattern struct as a chunk. There are two possibilities :
7631 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7632 * in <len> and <ptr> is NULL ;
7633 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7634 * <len> to its length.
7635 * This is intended to be used with acl_match_meth() only.
7636 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007637static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007638acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007639 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007640{
7641 int meth;
7642 struct http_txn *txn = l7;
7643
Willy Tarreau24e32d82012-04-23 23:55:44 +02007644 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007645
Willy Tarreau8797c062007-05-07 00:55:35 +02007646 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007647 smp->type = SMP_T_UINT;
7648 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007649 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7651 /* ensure the indexes are not affected */
7652 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007653 smp->type = SMP_T_CSTR;
7654 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007655 smp->data.str.str = txn->req.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007656 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007657 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007658 return 1;
7659}
7660
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007661/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007662static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007663{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007664 int icase;
7665
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007666
Willy Tarreauf853c462012-04-23 18:53:56 +02007667 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007668 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007669 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007670 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007671 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007672 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007673
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007674 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7675 if (pattern->val.i != HTTP_METH_OTHER)
7676 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007677
7678 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007679 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007680 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007681
7682 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007683 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7684 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007685 return ACL_PAT_FAIL;
7686 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007687}
7688
7689/* 2. Check on Request/Status Version
7690 * We simply compare strings here.
7691 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007692static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007693{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007694 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007695 if (!pattern->ptr.str) {
7696 if (err)
7697 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007698 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007699 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007700 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007701 return 1;
7702}
7703
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007704static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007705acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007706 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007707{
7708 struct http_txn *txn = l7;
7709 char *ptr;
7710 int len;
7711
Willy Tarreauc0239e02012-04-16 14:42:55 +02007712 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007713
Willy Tarreau8797c062007-05-07 00:55:35 +02007714 len = txn->req.sl.rq.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007715 ptr = txn->req.buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716
7717 while ((len-- > 0) && (*ptr++ != '/'));
7718 if (len <= 0)
7719 return 0;
7720
Willy Tarreauf853c462012-04-23 18:53:56 +02007721 smp->type = SMP_T_CSTR;
7722 smp->data.str.str = ptr;
7723 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007724
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007725 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007726 return 1;
7727}
7728
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007729static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007730acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007731 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007732{
7733 struct http_txn *txn = l7;
7734 char *ptr;
7735 int len;
7736
Willy Tarreauc0239e02012-04-16 14:42:55 +02007737 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007738
Willy Tarreau8797c062007-05-07 00:55:35 +02007739 len = txn->rsp.sl.st.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007740 ptr = txn->rsp.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007741
7742 while ((len-- > 0) && (*ptr++ != '/'));
7743 if (len <= 0)
7744 return 0;
7745
Willy Tarreauf853c462012-04-23 18:53:56 +02007746 smp->type = SMP_T_CSTR;
7747 smp->data.str.str = ptr;
7748 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007749
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007750 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007751 return 1;
7752}
7753
7754/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007755static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007756acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007757 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007758{
7759 struct http_txn *txn = l7;
7760 char *ptr;
7761 int len;
7762
Willy Tarreauc0239e02012-04-16 14:42:55 +02007763 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007764
Willy Tarreau8797c062007-05-07 00:55:35 +02007765 len = txn->rsp.sl.st.c_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007766 ptr = txn->rsp.buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007767
Willy Tarreauf853c462012-04-23 18:53:56 +02007768 smp->type = SMP_T_UINT;
7769 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007770 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007771 return 1;
7772}
7773
7774/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007775static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007776acl_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007777 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007778{
7779 struct http_txn *txn = l7;
7780
Willy Tarreauc0239e02012-04-16 14:42:55 +02007781 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007782
Willy Tarreauf853c462012-04-23 18:53:56 +02007783 smp->type = SMP_T_CSTR;
7784 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007785 smp->data.str.str = txn->req.buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007786 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007787 return 1;
7788}
7789
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007790static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007791acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007792 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007793{
7794 struct http_txn *txn = l7;
7795
Willy Tarreauc0239e02012-04-16 14:42:55 +02007796 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007797
7798 /* Parse HTTP request */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007799 url2sa(txn->req.buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007800 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7801 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007802 smp->type = SMP_T_IPV4;
7803 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007804
7805 /*
7806 * If we are parsing url in frontend space, we prepare backend stage
7807 * to not parse again the same url ! optimization lazyness...
7808 */
7809 if (px->options & PR_O_HTTP_PROXY)
7810 l4->flags |= SN_ADDR_SET;
7811
Willy Tarreau37406352012-04-23 16:16:37 +02007812 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007813 return 1;
7814}
7815
7816static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007817acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007818 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007819{
7820 struct http_txn *txn = l7;
7821
Willy Tarreauc0239e02012-04-16 14:42:55 +02007822 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007823
7824 /* Same optimization as url_ip */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007825 url2sa(txn->req.buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02007826 smp->type = SMP_T_UINT;
7827 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007828
7829 if (px->options & PR_O_HTTP_PROXY)
7830 l4->flags |= SN_ADDR_SET;
7831
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007832 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007833 return 1;
7834}
7835
Willy Tarreau185b5c42012-04-26 15:11:51 +02007836/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7837 * Accepts an optional argument of type string containing the header field name,
7838 * and an optional argument of type signed or unsigned integer to request an
7839 * explicit occurrence of the header. Note that in the event of a missing name,
7840 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007841 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007842static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007843smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007844 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007845{
7846 struct http_txn *txn = l7;
7847 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007848 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007849 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007850 int occ = 0;
7851 const char *name_str = NULL;
7852 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007853
Willy Tarreau185b5c42012-04-26 15:11:51 +02007854 if (args) {
7855 if (args[0].type != ARGT_STR)
7856 return 0;
7857 name_str = args[0].data.str.str;
7858 name_len = args[0].data.str.len;
7859
7860 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7861 occ = args[1].data.uint;
7862 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007863
Willy Tarreaue333ec92012-04-16 16:26:40 +02007864 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007865
Willy Tarreau185b5c42012-04-26 15:11:51 +02007866 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007867 /* search for header from the beginning */
7868 ctx->idx = 0;
7869
Willy Tarreau185b5c42012-04-26 15:11:51 +02007870 if (!occ && !(opt & SMP_OPT_ITERATE))
7871 /* no explicit occurrence and single fetch => last header by default */
7872 occ = -1;
7873
7874 if (!occ)
7875 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007876 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007877
Willy Tarreau185b5c42012-04-26 15:11:51 +02007878 smp->type = SMP_T_CSTR;
7879 smp->flags |= SMP_F_VOL_HDR;
7880 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 +02007881 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007882
Willy Tarreau37406352012-04-23 16:16:37 +02007883 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007884 return 0;
7885}
7886
Willy Tarreauc11416f2007-06-17 16:58:38 +02007887/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007888 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007889 */
7890static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007891smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007892 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007893{
7894 struct http_txn *txn = l7;
7895 struct hdr_idx *idx = &txn->hdr_idx;
7896 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007897 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007898 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007899
Willy Tarreau24e32d82012-04-23 23:55:44 +02007900 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007901 return 0;
7902
Willy Tarreaue333ec92012-04-16 16:26:40 +02007903 CHECK_HTTP_MESSAGE_FIRST();
7904
Willy Tarreau33a7e692007-06-10 19:45:56 +02007905 ctx.idx = 0;
7906 cnt = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007907 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007908 cnt++;
7909
Willy Tarreauf853c462012-04-23 18:53:56 +02007910 smp->type = SMP_T_UINT;
7911 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007912 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007913 return 1;
7914}
7915
Willy Tarreau185b5c42012-04-26 15:11:51 +02007916/* Fetch an HTTP header's integer value. The integer value is returned. It
7917 * takes a mandatory argument of type string and an optional one of type int
7918 * to designate a specific occurrence. It returns an unsigned integer, which
7919 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007920 */
7921static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007922smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007923 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007925 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007926
Willy Tarreauf853c462012-04-23 18:53:56 +02007927 if (ret > 0) {
7928 smp->type = SMP_T_UINT;
7929 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7930 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007931
Willy Tarreaud53e2422012-04-16 17:21:11 +02007932 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007933}
7934
Willy Tarreau185b5c42012-04-26 15:11:51 +02007935/* Fetch an HTTP header's integer value. The integer value is returned. It
7936 * takes a mandatory argument of type string and an optional one of type int
7937 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02007938 */
7939static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007940smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007941 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02007942{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007943 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007944
Willy Tarreau185b5c42012-04-26 15:11:51 +02007945 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02007946 smp->type = SMP_T_IPV4;
7947 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02007948 break;
7949 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007950 if (!(smp->flags & SMP_F_NOT_LAST))
7951 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02007952 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02007953 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02007954}
7955
Willy Tarreau737b0c12007-06-10 21:28:46 +02007956/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7957 * the first '/' after the possible hostname, and ends before the possible '?'.
7958 */
7959static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007960acl_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007961 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007962{
7963 struct http_txn *txn = l7;
7964 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007965
Willy Tarreauc0239e02012-04-16 14:42:55 +02007966 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007967
Willy Tarreau09d1e252012-05-18 22:36:34 +02007968 end = txn->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007969 ptr = http_get_path(txn);
7970 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007971 return 0;
7972
7973 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02007974 smp->type = SMP_T_CSTR;
7975 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007976
7977 while (ptr < end && *ptr != '?')
7978 ptr++;
7979
Willy Tarreauf853c462012-04-23 18:53:56 +02007980 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02007981 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007982 return 1;
7983}
7984
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007985static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007986acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007987 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007988{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007989 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7990 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7991 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007992
Willy Tarreau24e32d82012-04-23 23:55:44 +02007993 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007994
Willy Tarreauf853c462012-04-23 18:53:56 +02007995 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02007996 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007997 return 1;
7998}
7999
Willy Tarreau7f18e522010-10-22 20:04:13 +02008000/* return a valid test if the current request is the first one on the connection */
8001static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008002acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008003 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008004{
8005 if (!s)
8006 return 0;
8007
Willy Tarreauf853c462012-04-23 18:53:56 +02008008 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008009 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008010 return 1;
8011}
8012
Willy Tarreau34db1082012-04-19 17:16:54 +02008013/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008014static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008015acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008016 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008017{
8018
Willy Tarreau24e32d82012-04-23 23:55:44 +02008019 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008020 return 0;
8021
Willy Tarreauc0239e02012-04-16 14:42:55 +02008022 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008023
Willy Tarreauc0239e02012-04-16 14:42:55 +02008024 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008025 return 0;
8026
Willy Tarreauf853c462012-04-23 18:53:56 +02008027 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008028 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 +01008029 return 1;
8030}
Willy Tarreau8797c062007-05-07 00:55:35 +02008031
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008032/* Accepts exactly 1 argument of type userlist */
8033static int
8034acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8035 const struct arg *args, struct sample *smp)
8036{
8037
8038 if (!args || args->type != ARGT_USR)
8039 return 0;
8040
8041 CHECK_HTTP_MESSAGE_FIRST();
8042
8043 if (!get_http_auth(l4))
8044 return 0;
8045
8046 /* acl_match_auth() will need several information at once */
8047 smp->ctx.a[0] = args->data.usr; /* user list */
8048 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8049 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8050
8051 /* if the user does not belong to the userlist or has a wrong password,
8052 * report that it unconditionally does not match. Otherwise we return
8053 * a non-zero integer which will be ignored anyway since all the params
8054 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8055 */
8056 smp->type = SMP_T_BOOL;
8057 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8058 if (smp->data.uint)
8059 smp->type = SMP_T_UINT;
8060
8061 return 1;
8062}
8063
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008064/* Try to find the next occurrence of a cookie name in a cookie header value.
8065 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8066 * the cookie value is returned into *value and *value_l, and the function
8067 * returns a pointer to the next pointer to search from if the value was found.
8068 * Otherwise if the cookie was not found, NULL is returned and neither value
8069 * nor value_l are touched. The input <hdr> string should first point to the
8070 * header's value, and the <hdr_end> pointer must point to the first character
8071 * not part of the value. <list> must be non-zero if value may represent a list
8072 * of values (cookie headers). This makes it faster to abort parsing when no
8073 * list is expected.
8074 */
8075static char *
8076extract_cookie_value(char *hdr, const char *hdr_end,
8077 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008078 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008079{
8080 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8081 char *next;
8082
8083 /* we search at least a cookie name followed by an equal, and more
8084 * generally something like this :
8085 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8086 */
8087 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8088 /* Iterate through all cookies on this line */
8089
8090 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8091 att_beg++;
8092
8093 /* find att_end : this is the first character after the last non
8094 * space before the equal. It may be equal to hdr_end.
8095 */
8096 equal = att_end = att_beg;
8097
8098 while (equal < hdr_end) {
8099 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8100 break;
8101 if (http_is_spht[(unsigned char)*equal++])
8102 continue;
8103 att_end = equal;
8104 }
8105
8106 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8107 * is between <att_beg> and <equal>, both may be identical.
8108 */
8109
8110 /* look for end of cookie if there is an equal sign */
8111 if (equal < hdr_end && *equal == '=') {
8112 /* look for the beginning of the value */
8113 val_beg = equal + 1;
8114 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8115 val_beg++;
8116
8117 /* find the end of the value, respecting quotes */
8118 next = find_cookie_value_end(val_beg, hdr_end);
8119
8120 /* make val_end point to the first white space or delimitor after the value */
8121 val_end = next;
8122 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8123 val_end--;
8124 } else {
8125 val_beg = val_end = next = equal;
8126 }
8127
8128 /* We have nothing to do with attributes beginning with '$'. However,
8129 * they will automatically be removed if a header before them is removed,
8130 * since they're supposed to be linked together.
8131 */
8132 if (*att_beg == '$')
8133 continue;
8134
8135 /* Ignore cookies with no equal sign */
8136 if (equal == next)
8137 continue;
8138
8139 /* Now we have the cookie name between att_beg and att_end, and
8140 * its value between val_beg and val_end.
8141 */
8142
8143 if (att_end - att_beg == cookie_name_l &&
8144 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8145 /* let's return this value and indicate where to go on from */
8146 *value = val_beg;
8147 *value_l = val_end - val_beg;
8148 return next + 1;
8149 }
8150
8151 /* Set-Cookie headers only have the name in the first attr=value part */
8152 if (!list)
8153 break;
8154 }
8155
8156 return NULL;
8157}
8158
Willy Tarreaue333ec92012-04-16 16:26:40 +02008159/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008160 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8161 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008162 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008163 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008164 * Accepts exactly 1 argument of type string. If the input options indicate
8165 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008166 */
8167static int
Willy Tarreau51539362012-05-08 12:46:28 +02008168smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8169 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008170{
8171 struct http_txn *txn = l7;
8172 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008173 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008174 const struct http_msg *msg;
8175 const char *hdr_name;
8176 int hdr_name_len;
8177 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008178 int occ = 0;
8179 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008180
Willy Tarreau24e32d82012-04-23 23:55:44 +02008181 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008182 return 0;
8183
Willy Tarreaue333ec92012-04-16 16:26:40 +02008184 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008185
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008186 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008187 msg = &txn->req;
8188 hdr_name = "Cookie";
8189 hdr_name_len = 6;
8190 } else {
8191 msg = &txn->rsp;
8192 hdr_name = "Set-Cookie";
8193 hdr_name_len = 10;
8194 }
8195
Willy Tarreau28376d62012-04-26 21:26:10 +02008196 if (!occ && !(opt & SMP_OPT_ITERATE))
8197 /* no explicit occurrence and single fetch => last cookie by default */
8198 occ = -1;
8199
8200 /* OK so basically here, either we want only one value and it's the
8201 * last one, or we want to iterate over all of them and we fetch the
8202 * next one.
8203 */
8204
Willy Tarreau09d1e252012-05-18 22:36:34 +02008205 sol = msg->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008206 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008207 /* search for the header from the beginning, we must first initialize
8208 * the search parameters.
8209 */
Willy Tarreau37406352012-04-23 16:16:37 +02008210 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008211 ctx->idx = 0;
8212 }
8213
Willy Tarreau28376d62012-04-26 21:26:10 +02008214 smp->flags |= SMP_F_VOL_HDR;
8215
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008216 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008217 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8218 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008219 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8220 goto out;
8221
Willy Tarreau24e32d82012-04-23 23:55:44 +02008222 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008223 continue;
8224
Willy Tarreau37406352012-04-23 16:16:37 +02008225 smp->ctx.a[0] = ctx->line + ctx->val;
8226 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008227 }
8228
Willy Tarreauf853c462012-04-23 18:53:56 +02008229 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008230 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008231 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008232 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008233 &smp->data.str.str,
8234 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008235 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008236 found = 1;
8237 if (occ >= 0) {
8238 /* one value was returned into smp->data.str.{str,len} */
8239 smp->flags |= SMP_F_NOT_LAST;
8240 return 1;
8241 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008242 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008243 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008244 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008245 /* all cookie headers and values were scanned. If we're looking for the
8246 * last occurrence, we may return it now.
8247 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008248 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008249 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008250 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008251}
8252
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008253/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008254 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008255 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008256 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008257 */
8258static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008259acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008260 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008261{
8262 struct http_txn *txn = l7;
8263 struct hdr_idx *idx = &txn->hdr_idx;
8264 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008265 const struct http_msg *msg;
8266 const char *hdr_name;
8267 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008268 int cnt;
8269 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008270 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008271
Willy Tarreau24e32d82012-04-23 23:55:44 +02008272 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008273 return 0;
8274
Willy Tarreaue333ec92012-04-16 16:26:40 +02008275 CHECK_HTTP_MESSAGE_FIRST();
8276
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008277 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008278 msg = &txn->req;
8279 hdr_name = "Cookie";
8280 hdr_name_len = 6;
8281 } else {
8282 msg = &txn->rsp;
8283 hdr_name = "Set-Cookie";
8284 hdr_name_len = 10;
8285 }
8286
Willy Tarreau09d1e252012-05-18 22:36:34 +02008287 sol = msg->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008288 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008289 ctx.idx = 0;
8290 cnt = 0;
8291
8292 while (1) {
8293 /* Note: val_beg == NULL every time we need to fetch a new header */
8294 if (!val_beg) {
8295 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8296 break;
8297
Willy Tarreau24e32d82012-04-23 23:55:44 +02008298 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008299 continue;
8300
8301 val_beg = ctx.line + ctx.val;
8302 val_end = val_beg + ctx.vlen;
8303 }
8304
Willy Tarreauf853c462012-04-23 18:53:56 +02008305 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008306 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008307 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008308 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008309 &smp->data.str.str,
8310 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008311 cnt++;
8312 }
8313 }
8314
Willy Tarreauf853c462012-04-23 18:53:56 +02008315 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008316 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008317 return 1;
8318}
8319
Willy Tarreau51539362012-05-08 12:46:28 +02008320/* Fetch an cookie's integer value. The integer value is returned. It
8321 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8322 */
8323static int
8324smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8325 const struct arg *args, struct sample *smp)
8326{
8327 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8328
8329 if (ret > 0) {
8330 smp->type = SMP_T_UINT;
8331 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8332 }
8333
8334 return ret;
8335}
8336
Willy Tarreau8797c062007-05-07 00:55:35 +02008337/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008338/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008339/************************************************************************/
8340
David Cournapeau16023ee2010-12-23 20:55:41 +09008341/*
8342 * Given a path string and its length, find the position of beginning of the
8343 * query string. Returns NULL if no query string is found in the path.
8344 *
8345 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8346 *
8347 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8348 */
8349static inline char *find_query_string(char *path, size_t path_l)
8350{
8351 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008352
David Cournapeau16023ee2010-12-23 20:55:41 +09008353 p = memchr(path, '?', path_l);
8354 return p ? p + 1 : NULL;
8355}
8356
8357static inline int is_param_delimiter(char c)
8358{
8359 return c == '&' || c == ';';
8360}
8361
8362/*
8363 * Given a url parameter, find the starting position of the first occurence,
8364 * or NULL if the parameter is not found.
8365 *
8366 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8367 * the function will return query_string+8.
8368 */
8369static char*
8370find_url_param_pos(char* query_string, size_t query_string_l,
8371 char* url_param_name, size_t url_param_name_l)
8372{
8373 char *pos, *last;
8374
8375 pos = query_string;
8376 last = query_string + query_string_l - url_param_name_l - 1;
8377
8378 while (pos <= last) {
8379 if (pos[url_param_name_l] == '=') {
8380 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8381 return pos;
8382 pos += url_param_name_l + 1;
8383 }
8384 while (pos <= last && !is_param_delimiter(*pos))
8385 pos++;
8386 pos++;
8387 }
8388 return NULL;
8389}
8390
8391/*
8392 * Given a url parameter name, returns its value and size into *value and
8393 * *value_l respectively, and returns non-zero. If the parameter is not found,
8394 * zero is returned and value/value_l are not touched.
8395 */
8396static int
8397find_url_param_value(char* path, size_t path_l,
8398 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008399 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008400{
8401 char *query_string, *qs_end;
8402 char *arg_start;
8403 char *value_start, *value_end;
8404
8405 query_string = find_query_string(path, path_l);
8406 if (!query_string)
8407 return 0;
8408
8409 qs_end = path + path_l;
8410 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8411 url_param_name, url_param_name_l);
8412 if (!arg_start)
8413 return 0;
8414
8415 value_start = arg_start + url_param_name_l + 1;
8416 value_end = value_start;
8417
8418 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8419 value_end++;
8420
8421 *value = value_start;
8422 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008423 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008424}
8425
8426static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008427smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8428 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008429{
8430 struct http_txn *txn = l7;
8431 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008432
8433 if (!args || args->type != ARGT_STR)
8434 return 0;
8435
8436 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008437
Willy Tarreau09d1e252012-05-18 22:36:34 +02008438 if (!find_url_param_value(msg->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008439 args->data.str.str, args->data.str.len,
8440 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008441 return 0;
8442
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008443 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008444 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008445 return 1;
8446}
8447
Willy Tarreau185b5c42012-04-26 15:11:51 +02008448/* This function is used to validate the arguments passed to any "hdr" fetch
8449 * keyword. These keywords support an optional positive or negative occurrence
8450 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8451 * is assumed that the types are already the correct ones. Returns 0 on error,
8452 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8453 * error message in case of error, that the caller is responsible for freeing.
8454 * The initial location must either be freeable or NULL.
8455 */
8456static int val_hdr(struct arg *arg, char **err_msg)
8457{
8458 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8459 if (err_msg)
8460 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8461 return 0;
8462 }
8463 return 1;
8464}
8465
Willy Tarreau4a568972010-05-12 08:08:50 +02008466/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008467/* All supported ACL keywords must be declared here. */
8468/************************************************************************/
8469
8470/* Note: must not be declared <const> as its list will be overwritten.
8471 * Please take care of keeping this list alphabetically sorted.
8472 */
8473static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau51539362012-05-08 12:46:28 +02008474 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8475 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008476 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008477 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8478 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8479 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8480 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8481 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8482 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8483 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008484
Willy Tarreau185b5c42012-04-26 15:11:51 +02008485 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8486 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8487 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8488 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8489 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8490 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8491 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8492 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8493 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8494 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8495 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008496
8497 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008498 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008499 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8500
8501 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8502
8503 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8504 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8505 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8506 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8507 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8508 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8509 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8510 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8511
8512 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8513 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8514 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8515
Willy Tarreau51539362012-05-08 12:46:28 +02008516 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8517 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008518 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008519 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8520 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8521 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8522 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8523 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8524 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8525 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008526
Willy Tarreau185b5c42012-04-26 15:11:51 +02008527 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8528 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8529 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8530 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8531 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8532 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8533 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8534 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8535 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8536 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8537 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008538
8539 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8540
8541 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8542 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8543 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8544 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8545 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8546 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8547 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8548 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8549 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8550 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8551
8552 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8553 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8554 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8555 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8556 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8557 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8558 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8559 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8560 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8561
8562 { NULL, NULL, NULL, NULL },
8563}};
8564
8565/************************************************************************/
8566/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008567/************************************************************************/
8568/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008569static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008570 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008571 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008572 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8573 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008574 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008575}};
8576
Willy Tarreau8797c062007-05-07 00:55:35 +02008577
8578__attribute__((constructor))
8579static void __http_protocol_init(void)
8580{
8581 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008582 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008583}
8584
8585
Willy Tarreau58f10d72006-12-04 02:26:12 +01008586/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008587 * Local variables:
8588 * c-indent-level: 8
8589 * c-basic-offset: 8
8590 * End:
8591 */