blob: 157694f40f6e029a9fc20fa267b318ea82a7b281 [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.
757 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
761 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 char *path;
764 int len;
765
766 /* 1: create the response header */
767 rdr.len = strlen(HTTP_302);
768 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200769 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 memcpy(rdr.str, HTTP_302, rdr.len);
771
Willy Tarreau827aee92011-03-10 16:55:02 +0100772 srv = target_srv(&s->target);
773
Willy Tarreauefb453c2008-10-26 20:49:47 +0100774 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 return;
777
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100778 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
780 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
781 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100782 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783
784 /* 3: add the request URI */
785 txn = &s->txn;
786 path = http_get_path(txn);
787 if (!path)
788 return;
789
Willy Tarreau09d1e252012-05-18 22:36:34 +0200790 len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200791 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 return;
793
794 memcpy(rdr.str + rdr.len, path, len);
795 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100796
797 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
798 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
799 rdr.len += 29;
800 } else {
801 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
802 rdr.len += 23;
803 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804
805 /* prepare to return without error. */
Willy Tarreau060781f2012-05-07 16:50:03 +0200806 si->sock.shutr(si);
807 si->sock.shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 si->err_type = SI_ET_NONE;
809 si->err_loc = NULL;
810 si->state = SI_ST_CLO;
811
812 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100813 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814
815 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (srv)
817 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818}
819
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100820/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821 * that the server side is closed. Note that err_type is actually a
822 * bitmask, where almost only aborts may be cumulated with other
823 * values. We consider that aborted operations are more important
824 * than timeouts or errors due to the fact that nobody else in the
825 * logs might explain incomplete retries. All others should avoid
826 * being cumulated. It should normally not be possible to have multiple
827 * aborts at once, but just in case, the first one in sequence is reported.
828 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100831 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832
833 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100834 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
835 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
838 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
841 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_SRVCL, 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_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_SRVTO, 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_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
850 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
853 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854}
855
Willy Tarreau42250582007-04-01 01:30:43 +0200856extern const char sess_term_cond[8];
857extern const char sess_fin_state[8];
858extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200859struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200860struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100861struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100862
Willy Tarreau117f59e2007-03-04 18:17:17 +0100863/*
864 * Capture headers from message starting at <som> according to header list
865 * <cap_hdr>, and fill the <idx> structure appropriately.
866 */
867void capture_headers(char *som, struct hdr_idx *idx,
868 char **cap, struct cap_hdr *cap_hdr)
869{
870 char *eol, *sol, *col, *sov;
871 int cur_idx;
872 struct cap_hdr *h;
873 int len;
874
875 sol = som + hdr_idx_first_pos(idx);
876 cur_idx = hdr_idx_first_idx(idx);
877
878 while (cur_idx) {
879 eol = sol + idx->v[cur_idx].len;
880
881 col = sol;
882 while (col < eol && *col != ':')
883 col++;
884
885 sov = col + 1;
886 while (sov < eol && http_is_lws[(unsigned char)*sov])
887 sov++;
888
889 for (h = cap_hdr; h; h = h->next) {
890 if ((h->namelen == col - sol) &&
891 (strncasecmp(sol, h->name, h->namelen) == 0)) {
892 if (cap[h->index] == NULL)
893 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200894 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100895
896 if (cap[h->index] == NULL) {
897 Alert("HTTP capture : out of memory.\n");
898 continue;
899 }
900
901 len = eol - sov;
902 if (len > h->len)
903 len = h->len;
904
905 memcpy(cap[h->index], sov, len);
906 cap[h->index][len]=0;
907 }
908 }
909 sol = eol + idx->v[cur_idx].cr + 1;
910 cur_idx = idx->v[cur_idx].next;
911 }
912}
913
914
Willy Tarreau42250582007-04-01 01:30:43 +0200915/* either we find an LF at <ptr> or we jump to <bad>.
916 */
917#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
918
919/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
920 * otherwise to <http_msg_ood> with <state> set to <st>.
921 */
922#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
923 ptr++; \
924 if (likely(ptr < end)) \
925 goto good; \
926 else { \
927 state = (st); \
928 goto http_msg_ood; \
929 } \
930 } while (0)
931
932
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100934 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
936 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
937 * will give undefined results.
938 * Note that it is upon the caller's responsibility to ensure that ptr < end,
939 * and that msg->sol points to the beginning of the response.
940 * If a complete line is found (which implies that at least one CR or LF is
941 * found before <end>, the updated <ptr> is returned, otherwise NULL is
942 * returned indicating an incomplete line (which does not mean that parts have
943 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
944 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
945 * upon next call.
946 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200947 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100948 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
949 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200952const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100953 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100954 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100955{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100956 const char *msg_start = msg->buf->p;
957
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200960 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100961 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
963
964 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100965 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
967 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100968 state = HTTP_MSG_ERROR;
969 break;
970
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200972 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100973 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 goto http_msg_rpcode;
976 }
977 if (likely(HTTP_IS_SPHT(*ptr)))
978 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
979 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100980 state = HTTP_MSG_ERROR;
981 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100982
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200984 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 if (likely(!HTTP_IS_LWS(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
987
988 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100989 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
991 }
992
993 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100994 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 http_msg_rsp_reason:
996 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100997 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 msg->sl.st.r_l = 0;
999 goto http_msg_rpline_eol;
1000
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001002 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001004 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 goto http_msg_rpreason;
1006 }
1007 if (likely(HTTP_IS_SPHT(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1009 /* so it's a CR/LF, so there is no reason phrase */
1010 goto http_msg_rsp_reason;
1011
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001013 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 if (likely(!HTTP_IS_CRLF(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 http_msg_rpline_eol:
1018 /* We have seen the end of line. Note that we do not
1019 * necessarily have the \n yet, but at least we know that we
1020 * have EITHER \r OR \n, otherwise the response would not be
1021 * complete. We can then record the response length and return
1022 * to the caller which will be able to register it.
1023 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001024 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001025 return ptr;
1026
1027#ifdef DEBUG_FULL
1028 default:
1029 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1030 exit(1);
1031#endif
1032 }
1033
1034 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001035 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 if (ret_state)
1037 *ret_state = state;
1038 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001039 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001041}
1042
Willy Tarreau8973c702007-01-21 23:58:29 +01001043/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 * This function parses a request line between <ptr> and <end>, starting with
1045 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1046 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1047 * will give undefined results.
1048 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1049 * and that msg->sol points to the beginning of the request.
1050 * If a complete line is found (which implies that at least one CR or LF is
1051 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1052 * returned indicating an incomplete line (which does not mean that parts have
1053 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1054 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1055 * upon next call.
1056 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001058 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1059 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001060 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001062const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001063 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001064 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001066 const char *msg_start = msg->buf->p;
1067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001070 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_TOKEN(*ptr)))
1072 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001075 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_CRLF(*ptr))) {
1080 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001081 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001083 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001087 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 msg->sl.rq.v_l = 0;
1089 goto http_msg_rqline_eol;
1090 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001091 state = HTTP_MSG_ERROR;
1092 break;
1093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001095 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001097 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 goto http_msg_rquri;
1099 }
1100 if (likely(HTTP_IS_SPHT(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1102 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1103 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001106 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001107 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001111 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001115 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001116 /* non-ASCII chars are forbidden unless option
1117 * accept-invalid-http-request is enabled in the frontend.
1118 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001120 if (msg->err_pos < -1)
1121 goto invalid_char;
1122 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001123 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1125 }
1126
1127 if (likely(HTTP_IS_CRLF(*ptr))) {
1128 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1129 goto http_msg_req09_uri_e;
1130 }
1131
1132 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001133 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001134 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001135 state = HTTP_MSG_ERROR;
1136 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001138 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 goto http_msg_rqver;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1146 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1147 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001151 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001153
1154 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001155 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001156 http_msg_rqline_eol:
1157 /* We have seen the end of line. Note that we do not
1158 * necessarily have the \n yet, but at least we know that we
1159 * have EITHER \r OR \n, otherwise the request would not be
1160 * complete. We can then record the request length and return
1161 * to the caller which will be able to register it.
1162 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001163 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001164 return ptr;
1165 }
1166
1167 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001168 state = HTTP_MSG_ERROR;
1169 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171#ifdef DEBUG_FULL
1172 default:
1173 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1174 exit(1);
1175#endif
1176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 if (ret_state)
1181 *ret_state = state;
1182 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001183 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001187/*
1188 * Returns the data from Authorization header. Function may be called more
1189 * than once so data is stored in txn->auth_data. When no header is found
1190 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1191 * searching again for something we are unable to find anyway.
1192 */
1193
1194char get_http_auth_buff[BUFSIZE];
1195
1196int
1197get_http_auth(struct session *s)
1198{
1199
1200 struct http_txn *txn = &s->txn;
1201 struct chunk auth_method;
1202 struct hdr_ctx ctx;
1203 char *h, *p;
1204 int len;
1205
1206#ifdef DEBUG_AUTH
1207 printf("Auth for session %p: %d\n", s, txn->auth.method);
1208#endif
1209
1210 if (txn->auth.method == HTTP_AUTH_WRONG)
1211 return 0;
1212
1213 if (txn->auth.method)
1214 return 1;
1215
1216 txn->auth.method = HTTP_AUTH_WRONG;
1217
1218 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001219
1220 if (txn->flags & TX_USE_PX_CONN) {
1221 h = "Proxy-Authorization";
1222 len = strlen(h);
1223 } else {
1224 h = "Authorization";
1225 len = strlen(h);
1226 }
1227
Willy Tarreau09d1e252012-05-18 22:36:34 +02001228 if (!http_find_header2(h, len, s->req->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001229 return 0;
1230
1231 h = ctx.line + ctx.val;
1232
1233 p = memchr(h, ' ', ctx.vlen);
1234 if (!p || p == h)
1235 return 0;
1236
1237 chunk_initlen(&auth_method, h, 0, p-h);
1238 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1239
1240 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1241
1242 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1243 get_http_auth_buff, BUFSIZE - 1);
1244
1245 if (len < 0)
1246 return 0;
1247
1248
1249 get_http_auth_buff[len] = '\0';
1250
1251 p = strchr(get_http_auth_buff, ':');
1252
1253 if (!p)
1254 return 0;
1255
1256 txn->auth.user = get_http_auth_buff;
1257 *p = '\0';
1258 txn->auth.pass = p+1;
1259
1260 txn->auth.method = HTTP_AUTH_BASIC;
1261 return 1;
1262 }
1263
1264 return 0;
1265}
1266
Willy Tarreau58f10d72006-12-04 02:26:12 +01001267
Willy Tarreau8973c702007-01-21 23:58:29 +01001268/*
1269 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001270 * depending on the initial msg->msg_state. The caller is responsible for
1271 * ensuring that the message does not wrap. The function can be preempted
1272 * everywhere when data are missing and recalled at the exact same location
1273 * with no information loss. The message may even be realigned between two
1274 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001275 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001276 * fields. Note that msg->sol will be initialized after completing the first
1277 * state, so that none of the msg pointers has to be initialized prior to the
1278 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001280void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001282 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001284 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 state = msg->msg_state;
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001287 ptr = buf->p + msg->next;
1288 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 if (unlikely(ptr >= end))
1291 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001292
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 /*
1295 * First, states that are specific to the response only.
1296 * We check them first so that request and headers are
1297 * closer to each other (accessed more often).
1298 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001300 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001302 /* we have a start of message, but we have to check
1303 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001304 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001306 if (unlikely(ptr != buf->p)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001307 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001309 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001310 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 }
Willy Tarreau26927362012-05-18 23:22:52 +02001312 msg->sol = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 hdr_idx_init(idx);
1314 state = HTTP_MSG_RPVER;
1315 goto http_msg_rpver;
1316 }
1317
1318 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1319 goto http_msg_invalid;
1320
1321 if (unlikely(*ptr == '\n'))
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1324 /* stop here */
1325
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 EXPECT_LF_HERE(ptr, http_msg_invalid);
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1330 /* stop here */
1331
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001333 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPVER_SP:
1335 case HTTP_MSG_RPCODE:
1336 case HTTP_MSG_RPCODE_SP:
1337 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001338 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001339 state, ptr, end,
1340 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (unlikely(!ptr))
1342 return;
1343
1344 /* we have a full response and we know that we have either a CR
1345 * or an LF at <ptr>.
1346 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1348
Willy Tarreau3a215be2012-03-09 21:39:51 +01001349 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (likely(*ptr == '\r'))
1351 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1352 goto http_msg_rpline_end;
1353
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001355 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 /* msg->sol must point to the first of CR or LF. */
1357 EXPECT_LF_HERE(ptr, http_msg_invalid);
1358 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1359 /* stop here */
1360
1361 /*
1362 * Second, states that are specific to the request only
1363 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001365 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001367 /* we have a start of message, but we have to check
1368 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001369 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001370 */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001371 if (likely(ptr != buf->p)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001372 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001373 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001374 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau06a000f2012-05-18 23:04:32 +02001375 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 }
Willy Tarreau26927362012-05-18 23:22:52 +02001377 msg->sol = 0;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001378 /* we will need this when keep-alive will be supported
1379 hdr_idx_init(idx);
1380 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 state = HTTP_MSG_RQMETH;
1382 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1386 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(*ptr == '\n'))
1389 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 EXPECT_LF_HERE(ptr, http_msg_invalid);
1396 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001400 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH_SP:
1402 case HTTP_MSG_RQURI:
1403 case HTTP_MSG_RQURI_SP:
1404 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001405 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001406 state, ptr, end,
1407 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (unlikely(!ptr))
1409 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 /* we have a full request and we know that we have either a CR
1412 * or an LF at <ptr>.
1413 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau3a215be2012-03-09 21:39:51 +01001416 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(*ptr == '\r'))
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 /* check for HTTP/0.9 request : no version information available.
1424 * msg->sol must point to the first of CR or LF.
1425 */
1426 if (unlikely(msg->sl.rq.v_l == 0))
1427 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 EXPECT_LF_HERE(ptr, http_msg_invalid);
1430 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * Common states below
1435 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001437 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001438 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_CRLF(*ptr))) {
1440 goto http_msg_hdr_name;
1441 }
1442
1443 if (likely(*ptr == '\r'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1445 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001448 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 /* assumes msg->sol points to the first char */
1450 if (likely(HTTP_IS_TOKEN(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001453 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001456 if (likely(msg->err_pos < -1) || *ptr == '\n')
1457 goto http_msg_invalid;
1458
1459 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001460 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001461
1462 /* and we still accept this non-token character */
1463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001467 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 if (likely(HTTP_IS_SPHT(*ptr)))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001472 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (likely(!HTTP_IS_CRLF(*ptr))) {
1475 goto http_msg_hdr_val;
1476 }
1477
1478 if (likely(*ptr == '\r'))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1480 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 if (likely(HTTP_IS_SPHT(*ptr))) {
1490 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001491 for (; buf->p + msg->sov < ptr; msg->sov++)
1492 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 goto http_msg_hdr_l1_sp;
1494 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001495 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001496 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_complete_header;
1498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001501 /* assumes msg->sol points to the first char, and msg->sov
1502 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 */
1504 if (likely(!HTTP_IS_CRLF(*ptr)))
1505 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau12e48b32012-03-05 16:57:34 +01001507 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 /* Note: we could also copy eol into ->eoh so that we have the
1509 * real header end in case it ends with lots of LWS, but is this
1510 * really needed ?
1511 */
1512 if (likely(*ptr == '\r'))
1513 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1514 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 EXPECT_LF_HERE(ptr, http_msg_invalid);
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1524 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001525 for (; buf->p + msg->eol < ptr; msg->eol++)
1526 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 goto http_msg_hdr_val;
1528 }
1529 http_msg_complete_header:
1530 /*
1531 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001532 * Assumes msg->sol points to the first char, msg->sov points
1533 * to the first character of the value and msg->eol to the
1534 * first CR or LF so we know how the line ends. We insert last
1535 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001537 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 idx, idx->tail) < 0))
1539 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001540
Willy Tarreau3a215be2012-03-09 21:39:51 +01001541 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (likely(!HTTP_IS_CRLF(*ptr))) {
1543 goto http_msg_hdr_name;
1544 }
1545
1546 if (likely(*ptr == '\r'))
1547 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1548 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001551 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 /* Assumes msg->sol points to the first of either CR or LF */
1553 EXPECT_LF_HERE(ptr, http_msg_invalid);
1554 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001555 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001556 msg->eoh = msg->sol;
1557 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001558 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001560
1561 case HTTP_MSG_ERROR:
1562 /* this may only happen if we call http_msg_analyser() twice with an error */
1563 break;
1564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565#ifdef DEBUG_FULL
1566 default:
1567 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1568 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001569#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 }
1571 http_msg_ood:
1572 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001573 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001574 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 http_msg_invalid:
1578 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001579 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001580 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 return;
1582}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001583
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001584/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1585 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1586 * nothing is done and 1 is returned.
1587 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001588static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589{
1590 int delta;
1591 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593
1594 if (msg->sl.rq.v_l != 0)
1595 return 1;
1596
Willy Tarreau09d1e252012-05-18 22:36:34 +02001597 cur_end = msg->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598 delta = 0;
1599
1600 if (msg->sl.rq.u_l == 0) {
1601 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001602 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001604 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001605 }
1606 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001607 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001610 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001611 HTTP_MSG_RQMETH,
Willy Tarreau09d1e252012-05-18 22:36:34 +02001612 msg->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 NULL, NULL);
1614 if (unlikely(!cur_end))
1615 return 0;
1616
1617 /* we have a full HTTP/1.0 request now and we know that
1618 * we have either a CR or an LF at <ptr>.
1619 */
1620 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1621 return 1;
1622}
1623
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001624/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001625 * and "keep-alive" values. If we already know that some headers may safely
1626 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001627 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1628 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1629 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1630 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1631 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001632 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001633 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001634void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001635{
Willy Tarreau5b154472009-12-21 20:11:07 +01001636 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001637 const char *hdr_val = "Connection";
1638 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001639
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001640 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 return;
1642
Willy Tarreau88d349d2010-01-25 12:15:43 +01001643 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1644 hdr_val = "Proxy-Connection";
1645 hdr_len = 16;
1646 }
1647
Willy Tarreau5b154472009-12-21 20:11:07 +01001648 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001650 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001651 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1652 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001653 if (to_del & 2)
1654 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 else
1656 txn->flags |= TX_CON_KAL_SET;
1657 }
1658 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1659 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001660 if (to_del & 1)
1661 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001662 else
1663 txn->flags |= TX_CON_CLO_SET;
1664 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001665 }
1666
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001667 txn->flags |= TX_HDR_CONN_PRS;
1668 return;
1669}
Willy Tarreau5b154472009-12-21 20:11:07 +01001670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671/* Apply desired changes on the Connection: header. Values may be removed and/or
1672 * added depending on the <wanted> flags, which are exclusively composed of
1673 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1674 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1675 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001676void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001677{
1678 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001679 const char *hdr_val = "Connection";
1680 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001681
1682 ctx.idx = 0;
1683
Willy Tarreau88d349d2010-01-25 12:15:43 +01001684
1685 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1686 hdr_val = "Proxy-Connection";
1687 hdr_len = 16;
1688 }
1689
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001691 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001692 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1693 if (wanted & TX_CON_KAL_SET)
1694 txn->flags |= TX_CON_KAL_SET;
1695 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001696 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001697 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1699 if (wanted & TX_CON_CLO_SET)
1700 txn->flags |= TX_CON_CLO_SET;
1701 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001702 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001703 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001704 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001705
1706 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1707 return;
1708
1709 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1710 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001711 hdr_val = "Connection: close";
1712 hdr_len = 17;
1713 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1714 hdr_val = "Proxy-Connection: close";
1715 hdr_len = 23;
1716 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001717 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 }
1719
1720 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1721 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001722 hdr_val = "Connection: keep-alive";
1723 hdr_len = 22;
1724 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1725 hdr_val = "Proxy-Connection: keep-alive";
1726 hdr_len = 28;
1727 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001728 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 }
1730 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001731}
1732
Willy Tarreaua458b672012-03-05 11:17:50 +01001733/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001734 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001735 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001736 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001737 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001738 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001739int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001740{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001741 const struct buffer *buf = msg->buf;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001742 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001743 const char *ptr_old = ptr;
1744 const char *end = buf->data + buf->size;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001745 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 unsigned int chunk = 0;
1747
1748 /* The chunk size is in the following form, though we are only
1749 * interested in the size and CRLF :
1750 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1751 */
1752 while (1) {
1753 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001754 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001756 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001757 if (c < 0) /* not a hex digit anymore */
1758 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 if (++ptr >= end)
1760 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001761 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001762 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 chunk = (chunk << 4) + c;
1764 }
1765
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001767 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001768 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769
1770 while (http_is_spht[(unsigned char)*ptr]) {
1771 if (++ptr >= end)
1772 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001773 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001774 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001775 }
1776
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777 /* Up to there, we know that at least one byte is present at *ptr. Check
1778 * for the end of chunk size.
1779 */
1780 while (1) {
1781 if (likely(HTTP_IS_CRLF(*ptr))) {
1782 /* we now have a CR or an LF at ptr */
1783 if (likely(*ptr == '\r')) {
1784 if (++ptr >= end)
1785 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001786 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001787 return 0;
1788 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001789
Willy Tarreaud98cf932009-12-27 22:54:55 +01001790 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001791 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 if (++ptr >= end)
1793 ptr = buf->data;
1794 /* done */
1795 break;
1796 }
1797 else if (*ptr == ';') {
1798 /* chunk extension, ends at next CRLF */
1799 if (++ptr >= end)
1800 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001801 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001802 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803
1804 while (!HTTP_IS_CRLF(*ptr)) {
1805 if (++ptr >= end)
1806 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001807 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808 return 0;
1809 }
1810 /* we have a CRLF now, loop above */
1811 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001812 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001814 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001815 }
1816
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001818 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001821 msg->sov += ptr - ptr_old;
1822 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001823 msg->chunk_len = chunk;
1824 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001826 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001827 error:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001828 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001829 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001830}
1831
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001832/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001833 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834 * the trailers is found, it is automatically scheduled to be forwarded,
1835 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1836 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001837 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001838 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001840 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1841 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001842 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001843 * matches the length of trailers already parsed and not forwarded. It is also
1844 * important to note that this function is designed to be able to parse wrapped
1845 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001846 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001847int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001849 const struct buffer *buf = msg->buf;
1850
Willy Tarreaua458b672012-03-05 11:17:50 +01001851 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001853 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001854 const char *ptr = b_ptr(buf, msg->next);
1855 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001856 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857
1858 /* scan current line and stop at LF or CRLF */
1859 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001860 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 return 0;
1862
1863 if (*ptr == '\n') {
1864 if (!p1)
1865 p1 = ptr;
1866 p2 = ptr;
1867 break;
1868 }
1869
1870 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001871 if (p1) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001872 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 p1 = ptr;
1876 }
1877
1878 ptr++;
1879 if (ptr >= buf->data + buf->size)
1880 ptr = buf->data;
1881 }
1882
1883 /* after LF; point to beginning of next line */
1884 p2++;
1885 if (p2 >= buf->data + buf->size)
1886 p2 = buf->data;
1887
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001888 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001889 if (bytes < 0)
1890 bytes += buf->size;
1891
1892 /* schedule this line for forwarding */
1893 msg->sov += bytes;
1894 if (msg->sov >= buf->size)
1895 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001897 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 /* LF/CRLF at beginning of line => end of trailers at p2.
1899 * Everything was scheduled for forwarding, there's nothing
1900 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001901 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001902 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001903 msg->msg_state = HTTP_MSG_DONE;
1904 return 1;
1905 }
1906 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001907 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 }
1909}
1910
1911/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1912 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001913 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001914 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1916 * not enough data are available, the function does not change anything and
1917 * returns zero. If a parse error is encountered, the function returns < 0 and
1918 * does not change anything. Note: this function is designed to parse wrapped
1919 * CRLF at the end of the buffer.
1920 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001921int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001923 const struct buffer *buf = msg->buf;
1924 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 int bytes;
1926
1927 /* NB: we'll check data availabilty at the end. It's not a
1928 * problem because whatever we match first will be checked
1929 * against the correct length.
1930 */
1931 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001932 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 if (*ptr == '\r') {
1934 bytes++;
1935 ptr++;
1936 if (ptr >= buf->data + buf->size)
1937 ptr = buf->data;
1938 }
1939
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001940 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 return 0;
1942
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001943 if (*ptr != '\n') {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001944 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947
1948 ptr++;
1949 if (ptr >= buf->data + buf->size)
1950 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001951 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1952 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001953 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1955 return 1;
1956}
Willy Tarreau5b154472009-12-21 20:11:07 +01001957
Willy Tarreaud787e662009-07-07 10:14:51 +02001958/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1959 * processing can continue on next analysers, or zero if it either needs more
1960 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1961 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1962 * when it has nothing left to do, and may remove any analyser when it wants to
1963 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001965int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966{
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 /*
1968 * We will parse the partial (or complete) lines.
1969 * We will check the request syntax, and also join multi-line
1970 * headers. An index of all the lines will be elaborated while
1971 * parsing.
1972 *
1973 * For the parsing, we use a 28 states FSM.
1974 *
1975 * Here is the information we currently have :
Willy Tarreau26927362012-05-18 23:22:52 +02001976 * req->p = beginning of request
Willy Tarreauea1175a2012-03-05 15:52:30 +01001977 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau26927362012-05-18 23:22:52 +02001978 * req->p + req->i = end of input data
1979 * msg->eol = end of current header or line (LF or CRLF)
1980 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001981 *
1982 * At end of parsing, we may perform a capture of the error (if any), and
1983 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1984 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1985 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001986 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001987
Willy Tarreau59234e92008-11-30 23:51:27 +01001988 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001989 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001990 struct http_txn *txn = &s->txn;
1991 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001992 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001993
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001994 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 +01001995 now_ms, __FUNCTION__,
1996 s,
1997 req,
1998 req->rex, req->wex,
1999 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002000 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002001 req->analysers);
2002
Willy Tarreau52a0c602009-08-16 22:45:38 +02002003 /* we're speaking HTTP here, so let's speak HTTP to the client */
2004 s->srv_error = http_return_srv_error;
2005
Willy Tarreau83e3af02009-12-28 17:39:57 +01002006 /* There's a protected area at the end of the buffer for rewriting
2007 * purposes. We don't want to start to parse the request if the
2008 * protected area is affected, because we may have to move processed
2009 * data later, which is much more complicated.
2010 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002011 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002012 if ((txn->flags & TX_NOT_FIRST) &&
2013 unlikely((req->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002014 bi_end(req) < b_ptr(req, msg->next) ||
2015 bi_end(req) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002016 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002017 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2018 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002019 /* some data has still not left the buffer, wake us once that's done */
2020 buffer_dont_connect(req);
2021 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2022 return 0;
2023 }
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002024 if (bi_end(req) < b_ptr(req, msg->next) ||
2025 bi_end(req) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02002026 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002027 }
2028
Willy Tarreau065e8332010-01-08 00:30:20 +01002029 /* Note that we have the same problem with the response ; we
2030 * may want to send a redirect, error or anything which requires
2031 * some spare space. So we'll ensure that we have at least
2032 * maxrewrite bytes available in the response buffer before
2033 * processing that one. This will only affect pipelined
2034 * keep-alive requests.
2035 */
2036 if ((txn->flags & TX_NOT_FIRST) &&
2037 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002038 bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) ||
2039 bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002040 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002041 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2042 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002043 /* don't let a connection request be initiated */
2044 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002045 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002046 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002047 return 0;
2048 }
2049 }
2050
Willy Tarreaua458b672012-03-05 11:17:50 +01002051 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002052 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002053 }
2054
Willy Tarreau59234e92008-11-30 23:51:27 +01002055 /* 1: we might have to print this header in debug mode */
2056 if (unlikely((global.mode & MODE_DEBUG) &&
2057 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002058 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002059 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002060
Willy Tarreau26927362012-05-18 23:22:52 +02002061 sol = req->p;
Willy Tarreau59234e92008-11-30 23:51:27 +01002062 eol = sol + msg->sl.rq.l;
2063 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002064
Willy Tarreau59234e92008-11-30 23:51:27 +01002065 sol += hdr_idx_first_pos(&txn->hdr_idx);
2066 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002067
Willy Tarreau59234e92008-11-30 23:51:27 +01002068 while (cur_idx) {
2069 eol = sol + txn->hdr_idx.v[cur_idx].len;
2070 debug_hdr("clihdr", s, sol, eol);
2071 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2072 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 }
2075
Willy Tarreau58f10d72006-12-04 02:26:12 +01002076
Willy Tarreau59234e92008-11-30 23:51:27 +01002077 /*
2078 * Now we quickly check if we have found a full valid request.
2079 * If not so, we check the FD and buffer states before leaving.
2080 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002081 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002082 * requests are checked first. When waiting for a second request
2083 * on a keep-alive session, if we encounter and error, close, t/o,
2084 * we note the error in the session flags but don't set any state.
2085 * Since the error will be noted there, it will not be counted by
2086 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002087 * Last, we may increase some tracked counters' http request errors on
2088 * the cases that are deliberately the client's fault. For instance,
2089 * a timeout or connection reset is not counted as an error. However
2090 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002092
Willy Tarreau655dce92009-11-08 13:10:58 +01002093 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002094 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002096 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002097 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002098 session_inc_http_req_ctr(s);
2099 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002100 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 /* 1: Since we are in header mode, if there's no space
2105 * left for headers, we won't be able to free more
2106 * later, so the session will never terminate. We
2107 * must terminate it now.
2108 */
2109 if (unlikely(req->flags & BF_FULL)) {
2110 /* FIXME: check if URI is set and return Status
2111 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002113 session_inc_http_req_ctr(s);
2114 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002115 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002116 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002117 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002118 goto return_bad_req;
2119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002120
Willy Tarreau59234e92008-11-30 23:51:27 +01002121 /* 2: have we encountered a read error ? */
2122 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002123 if (!(s->flags & SN_ERR_MASK))
2124 s->flags |= SN_ERR_CLICL;
2125
Willy Tarreaufcffa692010-01-10 14:21:19 +01002126 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002127 goto failed_keep_alive;
2128
Willy Tarreau59234e92008-11-30 23:51:27 +01002129 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002130 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002131 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002132 session_inc_http_err_ctr(s);
2133 }
2134
Willy Tarreau59234e92008-11-30 23:51:27 +01002135 msg->msg_state = HTTP_MSG_ERROR;
2136 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002137
Willy Tarreauda7ff642010-06-23 11:44:09 +02002138 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002139 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002140 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002141 if (s->listener->counters)
2142 s->listener->counters->failed_req++;
2143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 if (!(s->flags & SN_FINST_MASK))
2145 s->flags |= SN_FINST_R;
2146 return 0;
2147 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002148
Willy Tarreau59234e92008-11-30 23:51:27 +01002149 /* 3: has the read timeout expired ? */
2150 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002151 if (!(s->flags & SN_ERR_MASK))
2152 s->flags |= SN_ERR_CLITO;
2153
Willy Tarreaufcffa692010-01-10 14:21:19 +01002154 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002155 goto failed_keep_alive;
2156
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002158 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002159 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002160 session_inc_http_err_ctr(s);
2161 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002162 txn->status = 408;
2163 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2164 msg->msg_state = HTTP_MSG_ERROR;
2165 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002166
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002168 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002169 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002170 if (s->listener->counters)
2171 s->listener->counters->failed_req++;
2172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 if (!(s->flags & SN_FINST_MASK))
2174 s->flags |= SN_FINST_R;
2175 return 0;
2176 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002177
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 /* 4: have we encountered a close ? */
2179 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002180 if (!(s->flags & SN_ERR_MASK))
2181 s->flags |= SN_ERR_CLICL;
2182
Willy Tarreaufcffa692010-01-10 14:21:19 +01002183 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002184 goto failed_keep_alive;
2185
Willy Tarreau4076a152009-04-02 15:18:36 +02002186 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002187 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 txn->status = 400;
2189 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2190 msg->msg_state = HTTP_MSG_ERROR;
2191 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002192
Willy Tarreauda7ff642010-06-23 11:44:09 +02002193 session_inc_http_err_ctr(s);
2194 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002195 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002196 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002197 if (s->listener->counters)
2198 s->listener->counters->failed_req++;
2199
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 if (!(s->flags & SN_FINST_MASK))
2201 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002202 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002203 }
2204
Willy Tarreau520d95e2009-09-19 21:04:57 +02002205 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002206 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002207 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002208#ifdef TCP_QUICKACK
Willy Tarreau93548be2012-05-13 08:44:16 +02002209 if (s->listener->options & LI_O_NOQUICKACK && req->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002210 /* We need more data, we have to re-enable quick-ack in case we
2211 * previously disabled it, otherwise we might cause the client
2212 * to delay next data.
2213 */
2214 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2215 }
2216#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002217
Willy Tarreaufcffa692010-01-10 14:21:19 +01002218 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2219 /* If the client starts to talk, let's fall back to
2220 * request timeout processing.
2221 */
2222 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002223 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002224 }
2225
Willy Tarreau59234e92008-11-30 23:51:27 +01002226 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002227 if (!tick_isset(req->analyse_exp)) {
2228 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2229 (txn->flags & TX_WAIT_NEXT_RQ) &&
2230 tick_isset(s->be->timeout.httpka))
2231 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2232 else
2233 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002235
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 /* we're not ready yet */
2237 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002238
2239 failed_keep_alive:
2240 /* Here we process low-level errors for keep-alive requests. In
2241 * short, if the request is not the first one and it experiences
2242 * a timeout, read error or shutdown, we just silently close so
2243 * that the client can try again.
2244 */
2245 txn->status = 0;
2246 msg->msg_state = HTTP_MSG_RQBEFORE;
2247 req->analysers = 0;
2248 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002249 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002250 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002251 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002252 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002253
Willy Tarreaud787e662009-07-07 10:14:51 +02002254 /* OK now we have a complete HTTP request with indexed headers. Let's
2255 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002256 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2257 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002258 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002259 * byte after the last LF. msg->sov points to the first byte of data.
2260 * msg->eol cannot be trusted because it may have been left uninitialized
2261 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002262 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002263
Willy Tarreauda7ff642010-06-23 11:44:09 +02002264 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002265 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2266
Willy Tarreaub16a5742010-01-10 14:46:16 +01002267 if (txn->flags & TX_WAIT_NEXT_RQ) {
2268 /* kill the pending keep-alive timeout */
2269 txn->flags &= ~TX_WAIT_NEXT_RQ;
2270 req->analyse_exp = TICK_ETERNITY;
2271 }
2272
2273
Willy Tarreaud787e662009-07-07 10:14:51 +02002274 /* Maybe we found in invalid header name while we were configured not
2275 * to block on that, so we have to capture it now.
2276 */
2277 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002278 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002279
Willy Tarreau59234e92008-11-30 23:51:27 +01002280 /*
2281 * 1: identify the method
2282 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002283 txn->meth = find_http_meth(req->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002284
2285 /* we can make use of server redirect on GET and HEAD */
2286 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2287 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002288
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 /*
2290 * 2: check if the URI matches the monitor_uri.
2291 * We have to do this for every request which gets in, because
2292 * the monitor-uri is defined by the frontend.
2293 */
2294 if (unlikely((s->fe->monitor_uri_len != 0) &&
2295 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002296 !memcmp(req->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 s->fe->monitor_uri,
2298 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002299 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002300 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002305 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002306
Willy Tarreau59234e92008-11-30 23:51:27 +01002307 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002308 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002309 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 ret = acl_pass(ret);
2312 if (cond->pol == ACL_COND_UNLESS)
2313 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 if (ret) {
2316 /* we fail this request, let's return 503 service unavail */
2317 txn->status = 503;
2318 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2319 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002320 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002321 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /* nothing to fail, let's reply normaly */
2324 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002325 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 goto return_prx_cond;
2327 }
2328
2329 /*
2330 * 3: Maybe we have to copy the original REQURI for the logs ?
2331 * Note: we cannot log anymore if the request has been
2332 * classified as invalid.
2333 */
2334 if (unlikely(s->logs.logwait & LW_REQ)) {
2335 /* we have a complete HTTP request that we must log */
2336 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2337 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 if (urilen >= REQURI_LEN)
2340 urilen = REQURI_LEN - 1;
Willy Tarreau26927362012-05-18 23:22:52 +02002341 memcpy(txn->uri, req->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 if (!(s->logs.logwait &= ~LW_REQ))
2345 s->do_log(s);
2346 } else {
2347 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002348 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 }
Willy Tarreau06619262006-12-17 08:37:22 +01002350
William Lallemanda73203e2012-03-12 12:48:57 +01002351 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2352 s->unique_id = pool_alloc2(pool2_uniqueid);
2353 }
2354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002356 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002357 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002358
Willy Tarreau5b154472009-12-21 20:11:07 +01002359 /* ... and check if the request is HTTP/1.1 or above */
2360 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002361 ((req->p[msg->sl.rq.v + 5] > '1') ||
2362 ((req->p[msg->sl.rq.v + 5] == '1') &&
2363 (req->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002364 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002365
2366 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002367 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002368
Willy Tarreau88d349d2010-01-25 12:15:43 +01002369 /* if the frontend has "option http-use-proxy-header", we'll check if
2370 * we have what looks like a proxied connection instead of a connection,
2371 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2372 * Note that this is *not* RFC-compliant, however browsers and proxies
2373 * happen to do that despite being non-standard :-(
2374 * We consider that a request not beginning with either '/' or '*' is
2375 * a proxied connection, which covers both "scheme://location" and
2376 * CONNECT ip:port.
2377 */
2378 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002379 req->p[msg->sl.rq.u] != '/' && req->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002380 txn->flags |= TX_USE_PX_CONN;
2381
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002382 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002383 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002386 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02002387 capture_headers(req->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002389
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002390 /* 6: determine the transfer-length.
2391 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2392 * the presence of a message-body in a REQUEST and its transfer length
2393 * must be determined that way (in order of precedence) :
2394 * 1. The presence of a message-body in a request is signaled by the
2395 * inclusion of a Content-Length or Transfer-Encoding header field
2396 * in the request's header fields. When a request message contains
2397 * both a message-body of non-zero length and a method that does
2398 * not define any semantics for that request message-body, then an
2399 * origin server SHOULD either ignore the message-body or respond
2400 * with an appropriate error message (e.g., 413). A proxy or
2401 * gateway, when presented the same request, SHOULD either forward
2402 * the request inbound with the message- body or ignore the
2403 * message-body when determining a response.
2404 *
2405 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2406 * and the "chunked" transfer-coding (Section 6.2) is used, the
2407 * transfer-length is defined by the use of this transfer-coding.
2408 * If a Transfer-Encoding header field is present and the "chunked"
2409 * transfer-coding is not present, the transfer-length is defined
2410 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002411 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002412 * 3. If a Content-Length header field is present, its decimal value in
2413 * OCTETs represents both the entity-length and the transfer-length.
2414 * If a message is received with both a Transfer-Encoding header
2415 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002416 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002417 * 4. By the server closing the connection. (Closing the connection
2418 * cannot be used to indicate the end of a request body, since that
2419 * would leave no possibility for the server to send back a response.)
2420 *
2421 * Whenever a transfer-coding is applied to a message-body, the set of
2422 * transfer-codings MUST include "chunked", unless the message indicates
2423 * it is terminated by closing the connection. When the "chunked"
2424 * transfer-coding is used, it MUST be the last transfer-coding applied
2425 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002426 */
2427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002429 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002430 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002431 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002432 http_find_header2("Transfer-Encoding", 17, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002433 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002434 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2435 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 /* bad transfer-encoding (chunked followed by something else) */
2437 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002438 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002439 break;
2440 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002441 }
2442
Willy Tarreau32b47f42009-10-18 20:55:02 +02002443 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002444 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002445 http_find_header2("Content-Length", 14, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002446 signed long long cl;
2447
Willy Tarreauad14f752011-09-02 20:33:27 +02002448 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002449 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002450 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002451 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452
Willy Tarreauad14f752011-09-02 20:33:27 +02002453 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002454 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002456 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002457
Willy Tarreauad14f752011-09-02 20:33:27 +02002458 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002459 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002460 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002461 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002463 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002464 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002465 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002466 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002468 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002469 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470 }
2471
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002472 /* bodyless requests have a known length */
2473 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002474 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002475
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002477 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002478 req->analyse_exp = TICK_ETERNITY;
2479 return 1;
2480
2481 return_bad_req:
2482 /* We centralize bad requests processing here */
2483 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2484 /* we detected a parsing error. We want to archive this request
2485 * in the dedicated proxy area for later troubleshooting.
2486 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002487 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002488 }
2489
2490 txn->req.msg_state = HTTP_MSG_ERROR;
2491 txn->status = 400;
2492 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002493
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002494 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002495 if (s->listener->counters)
2496 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002497
2498 return_prx_cond:
2499 if (!(s->flags & SN_ERR_MASK))
2500 s->flags |= SN_ERR_PRXCOND;
2501 if (!(s->flags & SN_FINST_MASK))
2502 s->flags |= SN_FINST_R;
2503
2504 req->analysers = 0;
2505 req->analyse_exp = TICK_ETERNITY;
2506 return 0;
2507}
2508
Cyril Bonté70be45d2010-10-12 00:14:35 +02002509/* We reached the stats page through a POST request.
2510 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002511 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002512 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002513int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002514{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002515 struct proxy *px = NULL;
2516 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002517
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002518 char key[LINESIZE];
2519 int action = ST_ADM_ACTION_NONE;
2520 int reprocess = 0;
2521
2522 int total_servers = 0;
2523 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002524
2525 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002526 char *st_cur_param = NULL;
2527 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002528
Willy Tarreauea1175a2012-03-05 15:52:30 +01002529 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002530 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002531
2532 cur_param = next_param = end_params;
2533
Cyril Bonté23b39d92011-02-10 22:54:44 +01002534 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002535 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002536 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002537 return 1;
2538 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002539 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002540 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002541 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002542 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543 }
2544
2545 *end_params = '\0';
2546
Willy Tarreau295a8372011-03-10 11:25:07 +01002547 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002548
2549 /*
2550 * Parse the parameters in reverse order to only store the last value.
2551 * From the html form, the backend and the action are at the end.
2552 */
2553 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002554 char *value;
2555 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002556
2557 cur_param--;
2558 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002559 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002561 poffset = (cur_param != first_param ? 1 : 0);
2562 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2563 if ((plen > 0) && (plen <= sizeof(key))) {
2564 strncpy(key, cur_param + poffset, plen);
2565 key[plen - 1] = '\0';
2566 } else {
2567 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2568 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569 }
2570
2571 /* Parse the value */
2572 value = key;
2573 while (*value != '\0' && *value != '=') {
2574 value++;
2575 }
2576 if (*value == '=') {
2577 /* Ok, a value is found, we can mark the end of the key */
2578 *value++ = '\0';
2579 }
2580
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002581 if (!url_decode(key) || !url_decode(value))
2582 break;
2583
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002585 if (!px && (strcmp(key, "b") == 0)) {
2586 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2587 /* the backend name is unknown or ambiguous (duplicate names) */
2588 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2589 goto out;
2590 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002591 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002592 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002594 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002595 }
2596 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002597 action = ST_ADM_ACTION_ENABLE;
2598 }
2599 else {
2600 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2601 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002602 }
2603 }
2604 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002605 if (!(px && action)) {
2606 /*
2607 * Indicates that we'll need to reprocess the parameters
2608 * as soon as backend and action are known
2609 */
2610 if (!reprocess) {
2611 st_cur_param = cur_param;
2612 st_next_param = next_param;
2613 }
2614 reprocess = 1;
2615 }
2616 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002617 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002618 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002619 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620 /* Not already in maintenance, we can change the server state */
2621 sv->state |= SRV_MAINTAIN;
2622 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002623 altered_servers++;
2624 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 }
2626 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002627 case ST_ADM_ACTION_ENABLE:
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 /* Already in maintenance, we can change the server state */
2630 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002631 sv->health = sv->rise; /* up, but will fall down at first failure */
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;
2636 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002637 } else {
2638 /* the server name is unknown or ambiguous (duplicate names) */
2639 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002640 }
2641 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002642 if (reprocess && px && action) {
2643 /* Now, we know the backend and the action chosen by the user.
2644 * We can safely restart from the first server parameter
2645 * to reprocess them
2646 */
2647 cur_param = st_cur_param;
2648 next_param = st_next_param;
2649 reprocess = 0;
2650 goto reprocess_servers;
2651 }
2652
Cyril Bonté70be45d2010-10-12 00:14:35 +02002653 next_param = cur_param;
2654 }
2655 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002656
2657 if (total_servers == 0) {
2658 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2659 }
2660 else if (altered_servers == 0) {
2661 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2662 }
2663 else if (altered_servers == total_servers) {
2664 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2665 }
2666 else {
2667 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2668 }
2669 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002670 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002671}
2672
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002673/* returns a pointer to the first rule which forbids access (deny or http_auth),
2674 * or NULL if everything's OK.
2675 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002676static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002677http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2678{
Willy Tarreauff011f22011-01-06 17:51:27 +01002679 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002680
Willy Tarreauff011f22011-01-06 17:51:27 +01002681 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002682 int ret = 1;
2683
Willy Tarreauff011f22011-01-06 17:51:27 +01002684 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002685 continue;
2686
2687 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002688 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002689 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002690 ret = acl_pass(ret);
2691
Willy Tarreauff011f22011-01-06 17:51:27 +01002692 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002693 ret = !ret;
2694 }
2695
2696 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002697 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002698 return NULL; /* no problem */
2699 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002700 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002701 }
2702 }
2703 return NULL;
2704}
2705
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706/* This stream analyser runs all HTTP request processing which is common to
2707 * frontends and backends, which means blocking ACLs, filters, connection-close,
2708 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002709 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002710 * either needs more data or wants to immediately abort the request (eg: deny,
2711 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002712 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002713int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002714{
Willy Tarreaud787e662009-07-07 10:14:51 +02002715 struct http_txn *txn = &s->txn;
2716 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002717 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002718 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002720 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002721 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002722
Willy Tarreau655dce92009-11-08 13:10:58 +01002723 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002724 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002725 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002726 return 0;
2727 }
2728
Willy Tarreau3a816292009-07-07 10:55:49 +02002729 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002730 req->analyse_exp = TICK_ETERNITY;
2731
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002732 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 +02002733 now_ms, __FUNCTION__,
2734 s,
2735 req,
2736 req->rex, req->wex,
2737 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002738 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002739 req->analysers);
2740
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002741 /* first check whether we have some ACLs set to block this request */
2742 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002743 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002744
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002745 ret = acl_pass(ret);
2746 if (cond->pol == ACL_COND_UNLESS)
2747 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002748
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 if (ret) {
2750 txn->status = 403;
2751 /* let's log the request time */
2752 s->logs.tv_request = now;
2753 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002754 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002758
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002759 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002760 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002761
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002762 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002763 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002764 do_stats = stats_check_uri(s->rep->prod, txn, px);
2765 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002766 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 +01002767 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002768 else
2769 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002770
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002771 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002772 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002773 txn->status = 403;
2774 s->logs.tv_request = now;
2775 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002776 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002777 s->fe->fe_counters.denied_req++;
2778 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2779 s->be->be_counters.denied_req++;
2780 if (s->listener->counters)
2781 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002782 goto return_prx_cond;
2783 }
2784
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002785 /* try headers filters */
2786 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002787 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002788 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* has the request been denied ? */
2791 if (txn->flags & TX_CLDENY) {
2792 /* no need to go further */
2793 txn->status = 403;
2794 /* let's log the request time */
2795 s->logs.tv_request = now;
2796 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002797 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 goto return_prx_cond;
2799 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002800
2801 /* When a connection is tarpitted, we use the tarpit timeout,
2802 * which may be the same as the connect timeout if unspecified.
2803 * If unset, then set it to zero because we really want it to
2804 * eventually expire. We build the tarpit as an analyser.
2805 */
2806 if (txn->flags & TX_CLTARPIT) {
2807 buffer_erase(s->req);
2808 /* wipe the request out so that we can drop the connection early
2809 * if the client closes first.
2810 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002811 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002812 req->analysers = 0; /* remove switching rules etc... */
2813 req->analysers |= AN_REQ_HTTP_TARPIT;
2814 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2815 if (!req->analyse_exp)
2816 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002817 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002818 return 1;
2819 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 }
Willy Tarreau06619262006-12-17 08:37:22 +01002821
Willy Tarreau5b154472009-12-21 20:11:07 +01002822 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2823 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002824 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2825 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002826 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2827 * avoid to redo the same work if FE and BE have the same settings (common).
2828 * The method consists in checking if options changed between the two calls
2829 * (implying that either one is non-null, or one of them is non-null and we
2830 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002831 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002832
Willy Tarreaudc008c52010-02-01 16:20:08 +01002833 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2834 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2835 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2836 (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 +01002837 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002838
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002839 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2840 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002841 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002842 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2843 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002844 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002845 tmp = TX_CON_WANT_CLO;
2846
Willy Tarreau5b154472009-12-21 20:11:07 +01002847 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2848 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002849
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002850 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2851 /* parse the Connection header and possibly clean it */
2852 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002853 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002854 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2855 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002856 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002857 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002858 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002859 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002860 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002861
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002862 /* check if client or config asks for explicit close in KAL/SCL */
2863 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2864 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2865 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002866 (!(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 +01002867 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002868 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002869 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002870 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2871 }
Willy Tarreau78599912009-10-17 20:12:21 +02002872
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002873 /* we can be blocked here because the request needs to be authenticated,
2874 * either to pass or to access stats.
2875 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002876 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002877 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002878 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002879
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002880 if (!realm)
2881 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2882
Willy Tarreau844a7e72010-01-31 21:46:18 +01002883 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002884 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002885 txn->status = 401;
2886 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002887 /* on 401 we still count one error, because normal browsing
2888 * won't significantly increase the counter but brute force
2889 * attempts will.
2890 */
2891 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002892 goto return_prx_cond;
2893 }
2894
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002895 /* add request headers from the rule sets in the same order */
2896 list_for_each_entry(wl, &px->req_add, list) {
2897 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002898 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002899 ret = acl_pass(ret);
2900 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2901 ret = !ret;
2902 if (!ret)
2903 continue;
2904 }
2905
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002906 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002907 goto return_bad_req;
2908 }
2909
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002910 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002911 struct stats_admin_rule *stats_admin_rule;
2912
2913 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002914 * FIXME!!! that one is rather dangerous, we want to
2915 * make it follow standard rules (eg: clear req->analysers).
2916 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002917
Cyril Bonté474be412010-10-12 00:14:36 +02002918 /* now check whether we have some admin rules for this request */
2919 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2920 int ret = 1;
2921
2922 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002923 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 +02002924 ret = acl_pass(ret);
2925 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2926 ret = !ret;
2927 }
2928
2929 if (ret) {
2930 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002931 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002932 break;
2933 }
2934 }
2935
Cyril Bonté70be45d2010-10-12 00:14:35 +02002936 /* Was the status page requested with a POST ? */
2937 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002939 if (msg->msg_state < HTTP_MSG_100_SENT) {
2940 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2941 * send an HTTP/1.1 100 Continue intermediate response.
2942 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002943 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002944 struct hdr_ctx ctx;
2945 ctx.idx = 0;
2946 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002947 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002948 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002949 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01002950 }
2951 }
2952 msg->msg_state = HTTP_MSG_100_SENT;
2953 s->logs.tv_request = now; /* update the request timer to reflect full request */
2954 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002955 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002956 /* we need more data */
2957 req->analysers |= an_bit;
2958 buffer_dont_connect(req);
2959 return 0;
2960 }
Cyril Bonté474be412010-10-12 00:14:36 +02002961 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002962 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002963 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002964 }
2965
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002966 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002967 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002968 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02002969 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01002970 s->rep->prod->applet.private = s;
2971 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002972 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02002973 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
2974 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002975
2976 return 0;
2977
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002978 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002979
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002980 /* check whether we have some ACLs set to redirect this request */
2981 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002982 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002983
Willy Tarreauf285f542010-01-03 20:03:03 +01002984 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002985 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002986 ret = acl_pass(ret);
2987 if (rule->cond->pol == ACL_COND_UNLESS)
2988 ret = !ret;
2989 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002990
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02002992 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002993 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002994
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002995 /* build redirect message */
2996 switch(rule->code) {
2997 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002998 msg_fmt = HTTP_303;
2999 break;
3000 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003001 msg_fmt = HTTP_301;
3002 break;
3003 case 302:
3004 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 msg_fmt = HTTP_302;
3006 break;
3007 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003008
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003009 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003011
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003012 switch(rule->type) {
3013 case REDIRECT_TYPE_PREFIX: {
3014 const char *path;
3015 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003016
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003017 path = http_get_path(txn);
3018 /* build message using path */
3019 if (path) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003020 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3022 int qs = 0;
3023 while (qs < pathlen) {
3024 if (path[qs] == '?') {
3025 pathlen = qs;
3026 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003027 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003029 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003030 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 } else {
3032 path = "/";
3033 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003034 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003035
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003036 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 goto return_bad_req;
3038
3039 /* add prefix. Note that if prefix == "/", we don't want to
3040 * add anything, otherwise it makes it hard for the user to
3041 * configure a self-redirection.
3042 */
3043 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3045 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003046 }
3047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 /* add path */
3049 memcpy(rdr.str + rdr.len, path, pathlen);
3050 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003051
3052 /* append a slash at the end of the location is needed and missing */
3053 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3054 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3055 if (rdr.len > rdr.size - 5)
3056 goto return_bad_req;
3057 rdr.str[rdr.len] = '/';
3058 rdr.len++;
3059 }
3060
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 break;
3062 }
3063 case REDIRECT_TYPE_LOCATION:
3064 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003065 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 /* add location */
3069 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3070 rdr.len += rule->rdr_len;
3071 break;
3072 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 if (rule->cookie_len) {
3075 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3076 rdr.len += 14;
3077 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3078 rdr.len += rule->cookie_len;
3079 memcpy(rdr.str + rdr.len, "\r\n", 2);
3080 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003081 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003083 /* add end of headers and the keep-alive/close status.
3084 * We may choose to set keep-alive if the Location begins
3085 * with a slash, because the client will come back to the
3086 * same server.
3087 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003088 txn->status = rule->code;
3089 /* let's log the request time */
3090 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003091
3092 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003093 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3094 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003095 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3096 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3097 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003098 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003099 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3100 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3101 rdr.len += 30;
3102 } else {
3103 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3104 rdr.len += 24;
3105 }
Willy Tarreau75661452010-01-10 10:35:01 +01003106 }
3107 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3108 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003109 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003110 /* "eat" the request */
Willy Tarreau26927362012-05-18 23:22:52 +02003111 bi_fast_delete(req, msg->sov);
3112 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003113 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003114 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3115 txn->req.msg_state = HTTP_MSG_CLOSED;
3116 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003117 break;
3118 } else {
3119 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003120 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3121 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3122 rdr.len += 29;
3123 } else {
3124 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3125 rdr.len += 23;
3126 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003127 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003128 goto return_prx_cond;
3129 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 }
3131 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003132
Willy Tarreau2be39392010-01-03 17:24:51 +01003133 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3134 * If this happens, then the data will not come immediately, so we must
3135 * send all what we have without waiting. Note that due to the small gain
3136 * in waiting for the body of the request, it's easier to simply put the
3137 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3138 * itself once used.
3139 */
3140 req->flags |= BF_SEND_DONTWAIT;
3141
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003142 /* that's OK for us now, let's move on to next analysers */
3143 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003144
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003145 return_bad_req:
3146 /* We centralize bad requests processing here */
3147 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3148 /* we detected a parsing error. We want to archive this request
3149 * in the dedicated proxy area for later troubleshooting.
3150 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003151 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003152 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003153
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 txn->req.msg_state = HTTP_MSG_ERROR;
3155 txn->status = 400;
3156 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003157
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003158 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003159 if (s->listener->counters)
3160 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003161
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003162 return_prx_cond:
3163 if (!(s->flags & SN_ERR_MASK))
3164 s->flags |= SN_ERR_PRXCOND;
3165 if (!(s->flags & SN_FINST_MASK))
3166 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003167
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003168 req->analysers = 0;
3169 req->analyse_exp = TICK_ETERNITY;
3170 return 0;
3171}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003172
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003173/* This function performs all the processing enabled for the current request.
3174 * It returns 1 if the processing can continue on next analysers, or zero if it
3175 * needs more data, encounters an error, or wants to immediately abort the
3176 * request. It relies on buffers flags, and updates s->req->analysers.
3177 */
3178int http_process_request(struct session *s, struct buffer *req, int an_bit)
3179{
3180 struct http_txn *txn = &s->txn;
3181 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003182
Willy Tarreau655dce92009-11-08 13:10:58 +01003183 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003184 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003185 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003186 return 0;
3187 }
3188
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003189 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 +02003190 now_ms, __FUNCTION__,
3191 s,
3192 req,
3193 req->rex, req->wex,
3194 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003195 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003196 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003197
Willy Tarreau59234e92008-11-30 23:51:27 +01003198 /*
3199 * Right now, we know that we have processed the entire headers
3200 * and that unwanted requests have been filtered out. We can do
3201 * whatever we want with the remaining request. Also, now we
3202 * may have separate values for ->fe, ->be.
3203 */
Willy Tarreau06619262006-12-17 08:37:22 +01003204
Willy Tarreau59234e92008-11-30 23:51:27 +01003205 /*
3206 * If HTTP PROXY is set we simply get remote server address
3207 * parsing incoming request.
3208 */
3209 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003210 url2sa(req->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003211 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003212
Willy Tarreau59234e92008-11-30 23:51:27 +01003213 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003214 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003215 * Note that doing so might move headers in the request, but
3216 * the fields will stay coherent and the URI will not move.
3217 * This should only be performed in the backend.
3218 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003219 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3221 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003222
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003224 * 8: the appsession cookie was looked up very early in 1.2,
3225 * so let's do the same now.
3226 */
3227
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003228 /* It needs to look into the URI unless persistence must be ignored */
3229 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003230 get_srv_from_appsession(s, req->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003231 }
3232
William Lallemanda73203e2012-03-12 12:48:57 +01003233 /* add unique-id if "header-unique-id" is specified */
3234
3235 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3236 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3237
3238 if (s->fe->header_unique_id && s->unique_id) {
3239 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3240 if (ret < 0 || ret > global.tune.bufsize)
3241 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003242 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003243 goto return_bad_req;
3244 }
3245
Cyril Bontéb21570a2009-11-29 20:04:48 +01003246 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 * 9: add X-Forwarded-For if either the frontend or the backend
3248 * asks for it.
3249 */
3250 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003251 struct hdr_ctx ctx = { .idx = 0 };
3252
3253 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02003254 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003255 /* The header is set to be added only if none is present
3256 * and we found it, so don't do anything.
3257 */
3258 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003259 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003260 /* Add an X-Forwarded-For header unless the source IP is
3261 * in the 'except' network range.
3262 */
3263 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003264 (((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 +01003265 != s->fe->except_net.s_addr) &&
3266 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003267 (((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 +01003268 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003269 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003271 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003272
3273 /* Note: we rely on the backend to get the header name to be used for
3274 * x-forwarded-for, because the header is really meant for the backends.
3275 * However, if the backend did not specify any option, we have to rely
3276 * on the frontend's header name.
3277 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 if (s->be->fwdfor_hdr_len) {
3279 len = s->be->fwdfor_hdr_len;
3280 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003281 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003282 len = s->fe->fwdfor_hdr_len;
3283 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003284 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003286
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003287 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003288 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003289 }
3290 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003291 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 /* FIXME: for the sake of completeness, we should also support
3293 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003294 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003295 int len;
3296 char pn[INET6_ADDRSTRLEN];
3297 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003298 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 /* Note: we rely on the backend to get the header name to be used for
3302 * x-forwarded-for, because the header is really meant for the backends.
3303 * However, if the backend did not specify any option, we have to rely
3304 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003305 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 if (s->be->fwdfor_hdr_len) {
3307 len = s->be->fwdfor_hdr_len;
3308 memcpy(trash, s->be->fwdfor_hdr_name, len);
3309 } else {
3310 len = s->fe->fwdfor_hdr_len;
3311 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003312 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003313 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003314
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003315 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 goto return_bad_req;
3317 }
3318 }
3319
3320 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003321 * 10: add X-Original-To if either the frontend or the backend
3322 * asks for it.
3323 */
3324 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3325
3326 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003327 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003328 /* Add an X-Original-To header unless the destination IP is
3329 * in the 'except' network range.
3330 */
Willy Tarreau59b94792012-05-11 16:16:40 +02003331 si_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003332
Willy Tarreau6471afb2011-09-23 10:54:59 +02003333 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003334 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003335 (((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 +02003336 != s->fe->except_to.s_addr) &&
3337 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003338 (((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 +02003339 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003340 int len;
3341 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003342 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003343
3344 /* Note: we rely on the backend to get the header name to be used for
3345 * x-original-to, because the header is really meant for the backends.
3346 * However, if the backend did not specify any option, we have to rely
3347 * on the frontend's header name.
3348 */
3349 if (s->be->orgto_hdr_len) {
3350 len = s->be->orgto_hdr_len;
3351 memcpy(trash, s->be->orgto_hdr_name, len);
3352 } else {
3353 len = s->fe->orgto_hdr_len;
3354 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003355 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003356 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3357
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003358 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003359 goto return_bad_req;
3360 }
3361 }
3362 }
3363
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003364 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3365 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003366 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003367 unsigned int want_flags = 0;
3368
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003369 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003370 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3371 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3372 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 want_flags |= TX_CON_CLO_SET;
3374 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003375 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3376 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3377 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003378 want_flags |= TX_CON_KAL_SET;
3379 }
3380
3381 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003382 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003383 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003384
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003385
Willy Tarreau522d6c02009-12-06 18:49:18 +01003386 /* If we have no server assigned yet and we're balancing on url_param
3387 * with a POST request, we may be interested in checking the body for
3388 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003389 */
3390 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3391 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003392 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003393 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003394 buffer_dont_connect(req);
3395 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003396 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003397
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003398 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003399 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003400#ifdef TCP_QUICKACK
3401 /* We expect some data from the client. Unless we know for sure
3402 * we already have a full request, we have to re-enable quick-ack
3403 * in case we previously disabled it, otherwise we might cause
3404 * the client to delay further data.
3405 */
3406 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003407 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003408 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003409 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3410#endif
3411 }
Willy Tarreau03945942009-12-22 16:50:27 +01003412
Willy Tarreau59234e92008-11-30 23:51:27 +01003413 /*************************************************************
3414 * OK, that's finished for the headers. We have done what we *
3415 * could. Let's switch to the DATA state. *
3416 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003417 req->analyse_exp = TICK_ETERNITY;
3418 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003419
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003420 /* if the server closes the connection, we want to immediately react
3421 * and close the socket to save packets and syscalls.
3422 */
3423 req->cons->flags |= SI_FL_NOHALF;
3424
Willy Tarreau59234e92008-11-30 23:51:27 +01003425 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003426 /* OK let's go on with the BODY now */
3427 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003428
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003430 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003431 /* we detected a parsing error. We want to archive this request
3432 * in the dedicated proxy area for later troubleshooting.
3433 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003434 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003435 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003436
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 txn->req.msg_state = HTTP_MSG_ERROR;
3438 txn->status = 400;
3439 req->analysers = 0;
3440 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003441
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003442 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003443 if (s->listener->counters)
3444 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003445
Willy Tarreau59234e92008-11-30 23:51:27 +01003446 if (!(s->flags & SN_ERR_MASK))
3447 s->flags |= SN_ERR_PRXCOND;
3448 if (!(s->flags & SN_FINST_MASK))
3449 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003450 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003451}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003452
Willy Tarreau60b85b02008-11-30 23:28:40 +01003453/* This function is an analyser which processes the HTTP tarpit. It always
3454 * returns zero, at the beginning because it prevents any other processing
3455 * from occurring, and at the end because it terminates the request.
3456 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003457int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003458{
3459 struct http_txn *txn = &s->txn;
3460
3461 /* This connection is being tarpitted. The CLIENT side has
3462 * already set the connect expiration date to the right
3463 * timeout. We just have to check that the client is still
3464 * there and that the timeout has not expired.
3465 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003466 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3468 !tick_is_expired(req->analyse_exp, now_ms))
3469 return 0;
3470
3471 /* We will set the queue timer to the time spent, just for
3472 * logging purposes. We fake a 500 server error, so that the
3473 * attacker will not suspect his connection has been tarpitted.
3474 * It will not cause trouble to the logs because we can exclude
3475 * the tarpitted connections by filtering on the 'PT' status flags.
3476 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003477 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3478
3479 txn->status = 500;
3480 if (req->flags != BF_READ_ERROR)
3481 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3482
3483 req->analysers = 0;
3484 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003485
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003486 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003487 if (s->listener->counters)
3488 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489
Willy Tarreau60b85b02008-11-30 23:28:40 +01003490 if (!(s->flags & SN_ERR_MASK))
3491 s->flags |= SN_ERR_PRXCOND;
3492 if (!(s->flags & SN_FINST_MASK))
3493 s->flags |= SN_FINST_T;
3494 return 0;
3495}
3496
Willy Tarreaud34af782008-11-30 23:36:37 +01003497/* This function is an analyser which processes the HTTP request body. It looks
3498 * for parameters to be used for the load balancing algorithm (url_param). It
3499 * must only be called after the standard HTTP request processing has occurred,
3500 * because it expects the request to be parsed. It returns zero if it needs to
3501 * read more data, or 1 once it has completed its analysis.
3502 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003503int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003504{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003505 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003506 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003507 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003508
3509 /* We have to parse the HTTP request body to find any required data.
3510 * "balance url_param check_post" should have been the only way to get
3511 * into this. We were brought here after HTTP header analysis, so all
3512 * related structures are ready.
3513 */
3514
Willy Tarreau522d6c02009-12-06 18:49:18 +01003515 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3516 goto missing_data;
3517
3518 if (msg->msg_state < HTTP_MSG_100_SENT) {
3519 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3520 * send an HTTP/1.1 100 Continue intermediate response.
3521 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003522 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003523 struct hdr_ctx ctx;
3524 ctx.idx = 0;
3525 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02003526 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003527 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003528 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529 }
3530 }
3531 msg->msg_state = HTTP_MSG_100_SENT;
3532 }
3533
3534 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003535 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02003536 * req->p still points to the beginning of the message and msg->sol
3537 * is still null. We must save the body in msg->next because it
3538 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003539 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003540 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003541
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003542 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003543 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3544 else
3545 msg->msg_state = HTTP_MSG_DATA;
3546 }
3547
3548 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003549 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003550 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003551 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003552 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003553 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003554
Willy Tarreau115acb92009-12-26 13:56:06 +01003555 if (!ret)
3556 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003557 else if (ret < 0) {
3558 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003560 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003561 }
3562
Willy Tarreaud98cf932009-12-27 22:54:55 +01003563 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003564 * We have the first data byte is in msg->sov. We're waiting for at
3565 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003566 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003567
Willy Tarreau124d9912011-03-01 20:30:48 +01003568 if (msg->body_len < limit)
3569 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003570
Willy Tarreau26927362012-05-18 23:22:52 +02003571 if (req->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003572 goto http_end;
3573
3574 missing_data:
3575 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003576 if (req->flags & BF_FULL) {
3577 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003578 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003579 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003580
Willy Tarreau522d6c02009-12-06 18:49:18 +01003581 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3582 txn->status = 408;
3583 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003584
3585 if (!(s->flags & SN_ERR_MASK))
3586 s->flags |= SN_ERR_CLITO;
3587 if (!(s->flags & SN_FINST_MASK))
3588 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003590 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591
3592 /* we get here if we need to wait for more data */
3593 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003594 /* Not enough data. We'll re-use the http-request
3595 * timeout here. Ideally, we should set the timeout
3596 * relative to the accept() date. We just set the
3597 * request timeout once at the beginning of the
3598 * request.
3599 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003600 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003601 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003602 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003603 return 0;
3604 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003605
3606 http_end:
3607 /* The situation will not evolve, so let's give up on the analysis. */
3608 s->logs.tv_request = now; /* update the request timer to reflect full request */
3609 req->analysers &= ~an_bit;
3610 req->analyse_exp = TICK_ETERNITY;
3611 return 1;
3612
3613 return_bad_req: /* let's centralize all bad requests */
3614 txn->req.msg_state = HTTP_MSG_ERROR;
3615 txn->status = 400;
3616 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3617
Willy Tarreau79ebac62010-06-07 13:47:49 +02003618 if (!(s->flags & SN_ERR_MASK))
3619 s->flags |= SN_ERR_PRXCOND;
3620 if (!(s->flags & SN_FINST_MASK))
3621 s->flags |= SN_FINST_R;
3622
Willy Tarreau522d6c02009-12-06 18:49:18 +01003623 return_err_msg:
3624 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003625 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003626 if (s->listener->counters)
3627 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003628 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003629}
3630
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003631/* send a server's name with an outgoing request over an established connection.
3632 * Note: this function is designed to be called once the request has been scheduled
3633 * for being forwarded. This is the reason why it rewinds the buffer before
3634 * proceeding.
3635 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003636int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003637
3638 struct hdr_ctx ctx;
3639
Mark Lamourinec2247f02012-01-04 13:02:01 -05003640 char *hdr_name = be->server_id_hdr_name;
3641 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003642 struct buffer *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003643 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003644 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003645
William Lallemandd9e90662012-01-30 17:27:17 +01003646 ctx.idx = 0;
3647
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003648 old_o = req->o;
3649 if (old_o) {
3650 /* The request was already skipped, let's restore it */
3651 b_rew(req, old_o);
3652 }
3653
3654 old_i = req->i;
Willy Tarreau09d1e252012-05-18 22:36:34 +02003655 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003656 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003657 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003658 }
3659
3660 /* Add the new header requested with the server value */
3661 hdr_val = trash;
3662 memcpy(hdr_val, hdr_name, hdr_name_len);
3663 hdr_val += hdr_name_len;
3664 *hdr_val++ = ':';
3665 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003666 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003667 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003669 if (old_o) {
3670 /* If this was a forwarded request, we must readjust the amount of
3671 * data to be forwarded in order to take into account the size
3672 * variations.
3673 */
3674 b_adv(req, old_o + req->i - old_i);
3675 }
3676
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677 return 0;
3678}
3679
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680/* Terminate current transaction and prepare a new one. This is very tricky
3681 * right now but it works.
3682 */
3683void http_end_txn_clean_session(struct session *s)
3684{
3685 /* FIXME: We need a more portable way of releasing a backend's and a
3686 * server's connections. We need a safer way to reinitialize buffer
3687 * flags. We also need a more accurate method for computing per-request
3688 * data.
3689 */
3690 http_silent_debug(__LINE__, s);
3691
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003692 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau060781f2012-05-07 16:50:03 +02003693 s->req->cons->sock.shutr(s->req->cons);
3694 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003695
3696 http_silent_debug(__LINE__, s);
3697
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003698 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003699 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003700 if (unlikely(s->srv_conn))
3701 sess_change_server(s, NULL);
3702 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003703
3704 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3705 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003706 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707
3708 if (s->txn.status) {
3709 int n;
3710
3711 n = s->txn.status / 100;
3712 if (n < 1 || n > 5)
3713 n = 0;
3714
3715 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003716 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717
Willy Tarreau24657792010-02-26 10:30:28 +01003718 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003720 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003721 }
3722
3723 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003724 s->logs.bytes_in -= s->req->i;
3725 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726
3727 /* let's do a final log if we need it */
3728 if (s->logs.logwait &&
3729 !(s->flags & SN_MONITOR) &&
3730 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3731 s->do_log(s);
3732 }
3733
3734 s->logs.accept_date = date; /* user-visible date for logging */
3735 s->logs.tv_accept = now; /* corrected date for internal use */
3736 tv_zero(&s->logs.tv_request);
3737 s->logs.t_queue = -1;
3738 s->logs.t_connect = -1;
3739 s->logs.t_data = -1;
3740 s->logs.t_close = 0;
3741 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3742 s->logs.srv_queue_size = 0; /* we will get this number soon */
3743
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003744 s->logs.bytes_in = s->req->total = s->req->i;
3745 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746
3747 if (s->pend_pos)
3748 pendconn_free(s->pend_pos);
3749
Willy Tarreau827aee92011-03-10 16:55:02 +01003750 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 if (s->flags & SN_CURR_SESS) {
3752 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003753 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003755 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3756 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757 }
3758
Willy Tarreau9e000c62011-03-10 14:03:36 +01003759 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760
3761 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3762 s->req->cons->fd = -1; /* just to help with debugging */
3763 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003764 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765 s->req->cons->err_loc = NULL;
3766 s->req->cons->exp = TICK_ETERNITY;
3767 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003768 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3769 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 +02003770 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 +01003771 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3772 s->txn.meth = 0;
3773 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003774 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003775 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 s->req->cons->flags |= SI_FL_INDEP_STR;
3777
Willy Tarreau96e31212011-05-30 18:10:30 +02003778 if (s->fe->options2 & PR_O2_NODELAY) {
3779 s->req->flags |= BF_NEVER_WAIT;
3780 s->rep->flags |= BF_NEVER_WAIT;
3781 }
3782
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 /* if the request buffer is not empty, it means we're
3784 * about to process another request, so send pending
3785 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003786 * Just don't do this if the buffer is close to be full,
3787 * because the request will wait for it to flush a little
3788 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003790 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003791 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003792 !(s->rep->flags & BF_FULL) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02003793 bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003794 s->rep->flags |= BF_EXPECT_MORE;
3795 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003796
3797 /* we're removing the analysers, we MUST re-enable events detection */
3798 buffer_auto_read(s->req);
3799 buffer_auto_close(s->req);
3800 buffer_auto_read(s->rep);
3801 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802
Willy Tarreau342b11c2010-11-24 16:22:09 +01003803 s->req->analysers = s->listener->analysers;
3804 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 s->rep->analysers = 0;
3806
3807 http_silent_debug(__LINE__, s);
3808}
3809
3810
3811/* This function updates the request state machine according to the response
3812 * state machine and buffer flags. It returns 1 if it changes anything (flag
3813 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3814 * it is only used to find when a request/response couple is complete. Both
3815 * this function and its equivalent should loop until both return zero. It
3816 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3817 */
3818int http_sync_req_state(struct session *s)
3819{
3820 struct buffer *buf = s->req;
3821 struct http_txn *txn = &s->txn;
3822 unsigned int old_flags = buf->flags;
3823 unsigned int old_state = txn->req.msg_state;
3824
3825 http_silent_debug(__LINE__, s);
3826 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3827 return 0;
3828
3829 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003830 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003831 * We can shut the read side unless we want to abort_on_close,
3832 * or we have a POST request. The issue with POST requests is
3833 * that some browsers still send a CRLF after the request, and
3834 * this CRLF must be read so that it does not remain in the kernel
3835 * buffers, otherwise a close could cause an RST on some systems
3836 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003837 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003838 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003839 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840
3841 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3842 goto wait_other_side;
3843
3844 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3845 /* The server has not finished to respond, so we
3846 * don't want to move in order not to upset it.
3847 */
3848 goto wait_other_side;
3849 }
3850
3851 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3852 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003853 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003854 txn->req.msg_state = HTTP_MSG_TUNNEL;
3855 goto wait_other_side;
3856 }
3857
3858 /* When we get here, it means that both the request and the
3859 * response have finished receiving. Depending on the connection
3860 * mode, we'll have to wait for the last bytes to leave in either
3861 * direction, and sometimes for a close to be effective.
3862 */
3863
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003864 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3865 /* Server-close mode : queue a connection close to the server */
3866 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003867 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003868 }
3869 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3870 /* Option forceclose is set, or either side wants to close,
3871 * let's enforce it now that we're not expecting any new
3872 * data to come. The caller knows the session is complete
3873 * once both states are CLOSED.
3874 */
3875 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876 buffer_shutr_now(buf);
3877 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003878 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003879 }
3880 else {
3881 /* The last possible modes are keep-alive and tunnel. Since tunnel
3882 * mode does not set the body analyser, we can't reach this place
3883 * in tunnel mode, so we're left with keep-alive only.
3884 * This mode is currently not implemented, we switch to tunnel mode.
3885 */
3886 buffer_auto_read(buf);
3887 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 }
3889
3890 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3891 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003892 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3893
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894 if (!(buf->flags & BF_OUT_EMPTY)) {
3895 txn->req.msg_state = HTTP_MSG_CLOSING;
3896 goto http_msg_closing;
3897 }
3898 else {
3899 txn->req.msg_state = HTTP_MSG_CLOSED;
3900 goto http_msg_closed;
3901 }
3902 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003903 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 }
3905
3906 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3907 http_msg_closing:
3908 /* nothing else to forward, just waiting for the output buffer
3909 * to be empty and for the shutw_now to take effect.
3910 */
3911 if (buf->flags & BF_OUT_EMPTY) {
3912 txn->req.msg_state = HTTP_MSG_CLOSED;
3913 goto http_msg_closed;
3914 }
3915 else if (buf->flags & BF_SHUTW) {
3916 txn->req.msg_state = HTTP_MSG_ERROR;
3917 goto wait_other_side;
3918 }
3919 }
3920
3921 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3922 http_msg_closed:
3923 goto wait_other_side;
3924 }
3925
3926 wait_other_side:
3927 http_silent_debug(__LINE__, s);
3928 return txn->req.msg_state != old_state || buf->flags != old_flags;
3929}
3930
3931
3932/* This function updates the response state machine according to the request
3933 * state machine and buffer flags. It returns 1 if it changes anything (flag
3934 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3935 * it is only used to find when a request/response couple is complete. Both
3936 * this function and its equivalent should loop until both return zero. It
3937 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3938 */
3939int http_sync_res_state(struct session *s)
3940{
3941 struct buffer *buf = s->rep;
3942 struct http_txn *txn = &s->txn;
3943 unsigned int old_flags = buf->flags;
3944 unsigned int old_state = txn->rsp.msg_state;
3945
3946 http_silent_debug(__LINE__, s);
3947 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3948 return 0;
3949
3950 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3951 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003952 * still monitor the server connection for a possible close
3953 * while the request is being uploaded, so we don't disable
3954 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003955 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003956 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957
3958 if (txn->req.msg_state == HTTP_MSG_ERROR)
3959 goto wait_other_side;
3960
3961 if (txn->req.msg_state < HTTP_MSG_DONE) {
3962 /* The client seems to still be sending data, probably
3963 * because we got an error response during an upload.
3964 * We have the choice of either breaking the connection
3965 * or letting it pass through. Let's do the later.
3966 */
3967 goto wait_other_side;
3968 }
3969
3970 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3971 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003972 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3974 goto wait_other_side;
3975 }
3976
3977 /* When we get here, it means that both the request and the
3978 * response have finished receiving. Depending on the connection
3979 * mode, we'll have to wait for the last bytes to leave in either
3980 * direction, and sometimes for a close to be effective.
3981 */
3982
3983 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3984 /* Server-close mode : shut read and wait for the request
3985 * side to close its output buffer. The caller will detect
3986 * when we're in DONE and the other is in CLOSED and will
3987 * catch that for the final cleanup.
3988 */
3989 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3990 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003992 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3993 /* Option forceclose is set, or either side wants to close,
3994 * let's enforce it now that we're not expecting any new
3995 * data to come. The caller knows the session is complete
3996 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003998 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3999 buffer_shutr_now(buf);
4000 buffer_shutw_now(buf);
4001 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 }
4003 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004004 /* The last possible modes are keep-alive and tunnel. Since tunnel
4005 * mode does not set the body analyser, we can't reach this place
4006 * in tunnel mode, so we're left with keep-alive only.
4007 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004008 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004009 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 }
4012
4013 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4014 /* if we've just closed an output, let's switch */
4015 if (!(buf->flags & BF_OUT_EMPTY)) {
4016 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4017 goto http_msg_closing;
4018 }
4019 else {
4020 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4021 goto http_msg_closed;
4022 }
4023 }
4024 goto wait_other_side;
4025 }
4026
4027 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4028 http_msg_closing:
4029 /* nothing else to forward, just waiting for the output buffer
4030 * to be empty and for the shutw_now to take effect.
4031 */
4032 if (buf->flags & BF_OUT_EMPTY) {
4033 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4034 goto http_msg_closed;
4035 }
4036 else if (buf->flags & BF_SHUTW) {
4037 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004038 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004039 if (target_srv(&s->target))
4040 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004041 goto wait_other_side;
4042 }
4043 }
4044
4045 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4046 http_msg_closed:
4047 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004048 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004049 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004050 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 goto wait_other_side;
4052 }
4053
4054 wait_other_side:
4055 http_silent_debug(__LINE__, s);
4056 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4057}
4058
4059
4060/* Resync the request and response state machines. Return 1 if either state
4061 * changes.
4062 */
4063int http_resync_states(struct session *s)
4064{
4065 struct http_txn *txn = &s->txn;
4066 int old_req_state = txn->req.msg_state;
4067 int old_res_state = txn->rsp.msg_state;
4068
4069 http_silent_debug(__LINE__, s);
4070 http_sync_req_state(s);
4071 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 if (!http_sync_res_state(s))
4074 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004075 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004076 if (!http_sync_req_state(s))
4077 break;
4078 }
4079 http_silent_debug(__LINE__, s);
4080 /* OK, both state machines agree on a compatible state.
4081 * There are a few cases we're interested in :
4082 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4083 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4084 * directions, so let's simply disable both analysers.
4085 * - HTTP_MSG_CLOSED on the response only means we must abort the
4086 * request.
4087 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4088 * with server-close mode means we've completed one request and we
4089 * must re-initialize the server connection.
4090 */
4091
4092 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4093 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4094 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4095 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4096 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004097 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004098 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004099 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004100 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004101 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004102 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004103 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4104 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004105 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004106 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004107 s->rep->analysers = 0;
4108 buffer_auto_close(s->rep);
4109 buffer_auto_read(s->rep);
4110 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 buffer_abort(s->req);
4112 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004114 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 }
4116 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4117 txn->rsp.msg_state == HTTP_MSG_DONE &&
4118 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4119 /* server-close: terminate this server connection and
4120 * reinitialize a fresh-new transaction.
4121 */
4122 http_end_txn_clean_session(s);
4123 }
4124
4125 http_silent_debug(__LINE__, s);
4126 return txn->req.msg_state != old_req_state ||
4127 txn->rsp.msg_state != old_res_state;
4128}
4129
Willy Tarreaud98cf932009-12-27 22:54:55 +01004130/* This function is an analyser which forwards request body (including chunk
4131 * sizes if any). It is called as soon as we must forward, even if we forward
4132 * zero byte. The only situation where it must not be called is when we're in
4133 * tunnel mode and we want to forward till the close. It's used both to forward
4134 * remaining data and to resync after end of body. It expects the msg_state to
4135 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4136 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004137 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004138 * bytes of pending data + the headers if not already done (between sol and sov).
4139 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140 */
4141int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4142{
4143 struct http_txn *txn = &s->txn;
4144 struct http_msg *msg = &s->txn.req;
4145
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004146 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4147 return 0;
4148
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004149 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004150 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004151 /* Output closed while we were sending data. We must abort and
4152 * wake the other side up.
4153 */
4154 msg->msg_state = HTTP_MSG_ERROR;
4155 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004156 return 1;
4157 }
4158
Willy Tarreau4fe41902010-06-07 22:27:41 +02004159 /* in most states, we should abort in case of early close */
4160 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004161
4162 /* Note that we don't have to send 100-continue back because we don't
4163 * need the data to complete our job, and it's up to the server to
4164 * decide whether to return 100, 417 or anything else in return of
4165 * an "Expect: 100-continue" header.
4166 */
4167
4168 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004169 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02004170 * req->p still points to the beginning of the message and msg->sol
4171 * is still null. We must save the body in msg->next because it
4172 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004173 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004174 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004175
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004176 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004177 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4178 else {
4179 msg->msg_state = HTTP_MSG_DATA;
4180 }
4181 }
4182
Willy Tarreaud98cf932009-12-27 22:54:55 +01004183 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004184 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004185
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004187 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004188 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004189 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004190 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004191 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004192 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004193 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004194 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004195
Willy Tarreaucaabe412010-01-03 23:08:28 +01004196 if (msg->msg_state == HTTP_MSG_DATA) {
4197 /* must still forward */
4198 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004199 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004200
4201 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004202 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004203 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004205 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004206 }
4207 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004208 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004209 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004210 * TRAILERS state.
4211 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004212 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004213
4214 if (!ret)
4215 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004216 else if (ret < 0) {
4217 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004218 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004219 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004220 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004221 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004222 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004223 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004224 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4225 /* we want the CRLF after the data */
4226 int ret;
4227
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004228 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004229
4230 if (ret == 0)
4231 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004232 else if (ret < 0) {
4233 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004234 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004235 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004237 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 /* we're in MSG_CHUNK_SIZE now */
4239 }
4240 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004241 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004242
4243 if (ret == 0)
4244 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004245 else if (ret < 0) {
4246 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004247 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004248 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004250 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004251 /* we're in HTTP_MSG_DONE now */
4252 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004254 int old_state = msg->msg_state;
4255
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004257 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004258 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4259 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4260 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004261 if (http_resync_states(s)) {
4262 /* some state changes occurred, maybe the analyser
4263 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004264 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004265 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4266 if (req->flags & BF_SHUTW) {
4267 /* request errors are most likely due to
4268 * the server aborting the transfer.
4269 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004270 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004271 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004272 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004273 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004275 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004277 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004278
4279 /* If "option abortonclose" is set on the backend, we
4280 * want to monitor the client's connection and forward
4281 * any shutdown notification to the server, which will
4282 * decide whether to close or to go on processing the
4283 * request.
4284 */
4285 if (s->be->options & PR_O_ABRT_CLOSE) {
4286 buffer_auto_read(req);
4287 buffer_auto_close(req);
4288 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004289 else if (s->txn.meth == HTTP_METH_POST) {
4290 /* POST requests may require to read extra CRLF
4291 * sent by broken browsers and which could cause
4292 * an RST to be sent upon close on some systems
4293 * (eg: Linux).
4294 */
4295 buffer_auto_read(req);
4296 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004297
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004299 }
4300 }
4301
Willy Tarreaud98cf932009-12-27 22:54:55 +01004302 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004303 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004304 if (req->flags & BF_SHUTR) {
4305 if (!(s->flags & SN_ERR_MASK))
4306 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004307 if (!(s->flags & SN_FINST_MASK)) {
4308 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4309 s->flags |= SN_FINST_H;
4310 else
4311 s->flags |= SN_FINST_D;
4312 }
4313
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004314 s->fe->fe_counters.cli_aborts++;
4315 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004316 if (target_srv(&s->target))
4317 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004318
4319 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004320 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004321
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004322 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004323 if (req->flags & BF_SHUTW)
4324 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004326 /* When TE: chunked is used, we need to get there again to parse remaining
4327 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4328 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004329 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004330 buffer_dont_close(req);
4331
Willy Tarreau5c620922011-05-11 19:56:11 +02004332 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004333 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4334 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004335 * modes are already handled by the stream sock layer. We must not do
4336 * this in content-length mode because it could present the MSG_MORE
4337 * flag with the last block of forwarded data, which would cause an
4338 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004339 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004340 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004341 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004342
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004344 return 0;
4345
Willy Tarreaud98cf932009-12-27 22:54:55 +01004346 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004347 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004348 if (s->listener->counters)
4349 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004350 return_bad_req_stats_ok:
4351 txn->req.msg_state = HTTP_MSG_ERROR;
4352 if (txn->status) {
4353 /* Note: we don't send any error if some data were already sent */
4354 stream_int_retnclose(req->prod, NULL);
4355 } else {
4356 txn->status = 400;
4357 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4358 }
4359 req->analysers = 0;
4360 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004361
4362 if (!(s->flags & SN_ERR_MASK))
4363 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364 if (!(s->flags & SN_FINST_MASK)) {
4365 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4366 s->flags |= SN_FINST_H;
4367 else
4368 s->flags |= SN_FINST_D;
4369 }
4370 return 0;
4371
4372 aborted_xfer:
4373 txn->req.msg_state = HTTP_MSG_ERROR;
4374 if (txn->status) {
4375 /* Note: we don't send any error if some data were already sent */
4376 stream_int_retnclose(req->prod, NULL);
4377 } else {
4378 txn->status = 502;
4379 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4380 }
4381 req->analysers = 0;
4382 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4383
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004384 s->fe->fe_counters.srv_aborts++;
4385 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004386 if (target_srv(&s->target))
4387 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004388
4389 if (!(s->flags & SN_ERR_MASK))
4390 s->flags |= SN_ERR_SRVCL;
4391 if (!(s->flags & SN_FINST_MASK)) {
4392 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4393 s->flags |= SN_FINST_H;
4394 else
4395 s->flags |= SN_FINST_D;
4396 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004397 return 0;
4398}
4399
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004400/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4401 * processing can continue on next analysers, or zero if it either needs more
4402 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4403 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4404 * when it has nothing left to do, and may remove any analyser when it wants to
4405 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004406 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004407int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004408{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004409 struct http_txn *txn = &s->txn;
4410 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004411 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004412 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004413 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004414 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004415
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004416 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 +02004417 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004418 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004419 rep,
4420 rep->rex, rep->wex,
4421 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004422 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004423 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004424
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 /*
4426 * Now parse the partial (or complete) lines.
4427 * We will check the response syntax, and also join multi-line
4428 * headers. An index of all the lines will be elaborated while
4429 * parsing.
4430 *
4431 * For the parsing, we use a 28 states FSM.
4432 *
4433 * Here is the information we currently have :
Willy Tarreau26927362012-05-18 23:22:52 +02004434 * rep->p = beginning of response
4435 * rep->p + msg->eoh = end of processed headers / start of current one
4436 * rep->p + rep->i = end of input data
4437 * msg->eol = end of current header or line (LF or CRLF)
4438 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 */
4440
Willy Tarreau83e3af02009-12-28 17:39:57 +01004441 /* There's a protected area at the end of the buffer for rewriting
4442 * purposes. We don't want to start to parse the request if the
4443 * protected area is affected, because we may have to move processed
4444 * data later, which is much more complicated.
4445 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004446 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004447 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02004448 bi_end(rep) < b_ptr(rep, msg->next) ||
4449 bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004450 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004451 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004452 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4453 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004454 buffer_dont_close(rep);
4455 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4456 return 0;
4457 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004458 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02004459 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004460 }
4461
Willy Tarreaua458b672012-03-05 11:17:50 +01004462 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004463 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004464 }
4465
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004466 /* 1: we might have to print this header in debug mode */
4467 if (unlikely((global.mode & MODE_DEBUG) &&
4468 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004469 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004470 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004471
Willy Tarreau26927362012-05-18 23:22:52 +02004472 sol = rep->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004473 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 sol += hdr_idx_first_pos(&txn->hdr_idx);
4477 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004478
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004479 while (cur_idx) {
4480 eol = sol + txn->hdr_idx.v[cur_idx].len;
4481 debug_hdr("srvhdr", s, sol, eol);
4482 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4483 cur_idx = txn->hdr_idx.v[cur_idx].next;
4484 }
4485 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004486
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 /*
4488 * Now we quickly check if we have found a full valid response.
4489 * If not so, we check the FD and buffer states before leaving.
4490 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004491 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004492 * responses are checked first.
4493 *
4494 * Depending on whether the client is still there or not, we
4495 * may send an error response back or not. Note that normally
4496 * we should only check for HTTP status there, and check I/O
4497 * errors somewhere else.
4498 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004499
Willy Tarreau655dce92009-11-08 13:10:58 +01004500 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004501 /* Invalid response */
4502 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4503 /* we detected a parsing error. We want to archive this response
4504 * in the dedicated proxy area for later troubleshooting.
4505 */
4506 hdr_response_bad:
4507 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004508 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004510 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004511 if (target_srv(&s->target)) {
4512 target_srv(&s->target)->counters.failed_resp++;
4513 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004514 }
Willy Tarreau64648412010-03-05 10:41:54 +01004515 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004516 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004517 rep->analysers = 0;
4518 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004519 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004520 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4522
4523 if (!(s->flags & SN_ERR_MASK))
4524 s->flags |= SN_ERR_PRXCOND;
4525 if (!(s->flags & SN_FINST_MASK))
4526 s->flags |= SN_FINST_H;
4527
4528 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004529 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004530
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531 /* too large response does not fit in buffer. */
4532 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004533 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004534 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 goto hdr_response_bad;
4536 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004537
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538 /* read error */
4539 else if (rep->flags & BF_READ_ERROR) {
4540 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004541 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004542
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004543 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004544 if (target_srv(&s->target)) {
4545 target_srv(&s->target)->counters.failed_resp++;
4546 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004547 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004548
Willy Tarreau90deb182010-01-07 00:20:41 +01004549 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 rep->analysers = 0;
4551 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004552 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004553 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004554 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 if (!(s->flags & SN_ERR_MASK))
4557 s->flags |= SN_ERR_SRVCL;
4558 if (!(s->flags & SN_FINST_MASK))
4559 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004560 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004562
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004563 /* read timeout : return a 504 to the client. */
4564 else if (rep->flags & BF_READ_TIMEOUT) {
4565 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004566 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004567
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004568 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004569 if (target_srv(&s->target)) {
4570 target_srv(&s->target)->counters.failed_resp++;
4571 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004572 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004573
Willy Tarreau90deb182010-01-07 00:20:41 +01004574 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 rep->analysers = 0;
4576 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004577 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004578 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 if (!(s->flags & SN_ERR_MASK))
4582 s->flags |= SN_ERR_SRVTO;
4583 if (!(s->flags & SN_FINST_MASK))
4584 s->flags |= SN_FINST_H;
4585 return 0;
4586 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004587
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004588 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004589 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004590 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004591 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004592
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004593 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004594 if (target_srv(&s->target)) {
4595 target_srv(&s->target)->counters.failed_resp++;
4596 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004597 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004598
Willy Tarreau90deb182010-01-07 00:20:41 +01004599 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600 rep->analysers = 0;
4601 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004602 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004603 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004605
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 if (!(s->flags & SN_ERR_MASK))
4607 s->flags |= SN_ERR_SRVCL;
4608 if (!(s->flags & SN_FINST_MASK))
4609 s->flags |= SN_FINST_H;
4610 return 0;
4611 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004612
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 /* write error to client (we don't send any message then) */
4614 else if (rep->flags & BF_WRITE_ERROR) {
4615 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004616 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004617
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004618 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004620 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621
4622 if (!(s->flags & SN_ERR_MASK))
4623 s->flags |= SN_ERR_CLICL;
4624 if (!(s->flags & SN_FINST_MASK))
4625 s->flags |= SN_FINST_H;
4626
4627 /* process_session() will take care of the error */
4628 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004629 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004630
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 buffer_dont_close(rep);
4632 return 0;
4633 }
4634
4635 /* More interesting part now : we know that we have a complete
4636 * response which at least looks like HTTP. We have an indicator
4637 * of each header's length, so we can parse them quickly.
4638 */
4639
4640 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004641 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 /*
4644 * 1: get the status code
4645 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02004646 n = rep->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647 if (n < 1 || n > 5)
4648 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004649 /* when the client triggers a 4xx from the server, it's most often due
4650 * to a missing object or permission. These events should be tracked
4651 * because if they happen often, it may indicate a brute force or a
4652 * vulnerability scan.
4653 */
4654 if (n == 4)
4655 session_inc_http_err_ctr(s);
4656
Willy Tarreau827aee92011-03-10 16:55:02 +01004657 if (target_srv(&s->target))
4658 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659
Willy Tarreau5b154472009-12-21 20:11:07 +01004660 /* check if the response is HTTP/1.1 or above */
4661 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004662 ((rep->p[5] > '1') ||
4663 ((rep->p[5] == '1') && (rep->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004664 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004665
4666 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004667 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 +01004668
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004669 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004670 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004671
Willy Tarreau09d1e252012-05-18 22:36:34 +02004672 txn->status = strl2ui(rep->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673
Willy Tarreau39650402010-03-15 19:44:39 +01004674 /* Adjust server's health based on status code. Note: status codes 501
4675 * and 505 are triggered on demand by client request, so we must not
4676 * count them as server failures.
4677 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004678 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004679 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004680 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004681 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004682 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004683 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004684
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004685 /*
4686 * 2: check for cacheability.
4687 */
4688
4689 switch (txn->status) {
4690 case 200:
4691 case 203:
4692 case 206:
4693 case 300:
4694 case 301:
4695 case 410:
4696 /* RFC2616 @13.4:
4697 * "A response received with a status code of
4698 * 200, 203, 206, 300, 301 or 410 MAY be stored
4699 * by a cache (...) unless a cache-control
4700 * directive prohibits caching."
4701 *
4702 * RFC2616 @9.5: POST method :
4703 * "Responses to this method are not cacheable,
4704 * unless the response includes appropriate
4705 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004706 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 if (likely(txn->meth != HTTP_METH_POST) &&
4708 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4709 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4710 break;
4711 default:
4712 break;
4713 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 /*
4716 * 3: we may need to capture headers
4717 */
4718 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004719 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02004720 capture_headers(rep->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 txn->rsp.cap, s->fe->rsp_cap);
4722
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004723 /* 4: determine the transfer-length.
4724 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4725 * the presence of a message-body in a RESPONSE and its transfer length
4726 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004727 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004728 * All responses to the HEAD request method MUST NOT include a
4729 * message-body, even though the presence of entity-header fields
4730 * might lead one to believe they do. All 1xx (informational), 204
4731 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4732 * message-body. All other responses do include a message-body,
4733 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004734 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004735 * 1. Any response which "MUST NOT" include a message-body (such as the
4736 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4737 * always terminated by the first empty line after the header fields,
4738 * regardless of the entity-header fields present in the message.
4739 *
4740 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4741 * the "chunked" transfer-coding (Section 6.2) is used, the
4742 * transfer-length is defined by the use of this transfer-coding.
4743 * If a Transfer-Encoding header field is present and the "chunked"
4744 * transfer-coding is not present, the transfer-length is defined by
4745 * the sender closing the connection.
4746 *
4747 * 3. If a Content-Length header field is present, its decimal value in
4748 * OCTETs represents both the entity-length and the transfer-length.
4749 * If a message is received with both a Transfer-Encoding header
4750 * field and a Content-Length header field, the latter MUST be ignored.
4751 *
4752 * 4. If the message uses the media type "multipart/byteranges", and
4753 * the transfer-length is not otherwise specified, then this self-
4754 * delimiting media type defines the transfer-length. This media
4755 * type MUST NOT be used unless the sender knows that the recipient
4756 * can parse it; the presence in a request of a Range header with
4757 * multiple byte-range specifiers from a 1.1 client implies that the
4758 * client can parse multipart/byteranges responses.
4759 *
4760 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004761 */
4762
4763 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004764 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004765 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004766 * FIXME: should we parse anyway and return an error on chunked encoding ?
4767 */
4768 if (txn->meth == HTTP_METH_HEAD ||
4769 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004770 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004771 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004772 goto skip_content_length;
4773 }
4774
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004775 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004776 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004777 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004778 http_find_header2("Transfer-Encoding", 17, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004779 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004780 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4781 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004782 /* bad transfer-encoding (chunked followed by something else) */
4783 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004784 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004785 break;
4786 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004787 }
4788
4789 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4790 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004791 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004792 http_find_header2("Content-Length", 14, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004793 signed long long cl;
4794
Willy Tarreauad14f752011-09-02 20:33:27 +02004795 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004796 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004797 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004798 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004799
Willy Tarreauad14f752011-09-02 20:33:27 +02004800 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004801 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004802 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004803 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004804
Willy Tarreauad14f752011-09-02 20:33:27 +02004805 if (cl < 0) {
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 Tarreaua36fc4d2012-02-17 17:39:37 +01004810 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != 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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004813 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004814
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004815 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004816 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 }
4818
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004819 /* FIXME: we should also implement the multipart/byterange method.
4820 * For now on, we resort to close mode in this case (unknown length).
4821 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004824 /* end of job, return OK */
4825 rep->analysers &= ~an_bit;
4826 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004827 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828 return 1;
4829}
4830
4831/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004832 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4833 * and updates t->rep->analysers. It might make sense to explode it into several
4834 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004835 */
4836int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4837{
4838 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 struct http_msg *msg = &txn->rsp;
4840 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004841 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004843 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 +02004844 now_ms, __FUNCTION__,
4845 t,
4846 rep,
4847 rep->rex, rep->wex,
4848 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004849 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 rep->analysers);
4851
Willy Tarreau655dce92009-11-08 13:10:58 +01004852 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004853 return 0;
4854
4855 rep->analysers &= ~an_bit;
4856 rep->analyse_exp = TICK_ETERNITY;
4857
Willy Tarreau5b154472009-12-21 20:11:07 +01004858 /* Now we have to check if we need to modify the Connection header.
4859 * This is more difficult on the response than it is on the request,
4860 * because we can have two different HTTP versions and we don't know
4861 * how the client will interprete a response. For instance, let's say
4862 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4863 * HTTP/1.1 response without any header. Maybe it will bound itself to
4864 * HTTP/1.0 because it only knows about it, and will consider the lack
4865 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4866 * the lack of header as a keep-alive. Thus we will use two flags
4867 * indicating how a request MAY be understood by the client. In case
4868 * of multiple possibilities, we'll fix the header to be explicit. If
4869 * ambiguous cases such as both close and keepalive are seen, then we
4870 * will fall back to explicit close. Note that we won't take risks with
4871 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004872 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004873 */
4874
Willy Tarreaudc008c52010-02-01 16:20:08 +01004875 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4876 txn->status == 101)) {
4877 /* Either we've established an explicit tunnel, or we're
4878 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004879 * to understand the next protocols. We have to switch to tunnel
4880 * mode, so that we transfer the request and responses then let
4881 * this protocol pass unmodified. When we later implement specific
4882 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004883 * header which contains information about that protocol for
4884 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004885 */
4886 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4887 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004888 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4889 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4890 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004891 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004892
Willy Tarreau60466522010-01-18 19:08:45 +01004893 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004894 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004895 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4896 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004897
Willy Tarreau60466522010-01-18 19:08:45 +01004898 /* now adjust header transformations depending on current state */
4899 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4900 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4901 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004902 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004903 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004904 }
Willy Tarreau60466522010-01-18 19:08:45 +01004905 else { /* SCL / KAL */
4906 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004907 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004908 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004909 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004910
Willy Tarreau60466522010-01-18 19:08:45 +01004911 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004912 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004913
Willy Tarreau60466522010-01-18 19:08:45 +01004914 /* Some keep-alive responses are converted to Server-close if
4915 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004916 */
Willy Tarreau60466522010-01-18 19:08:45 +01004917 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4918 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004919 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004920 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004921 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004922 }
4923
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004924 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004925 /*
4926 * 3: we will have to evaluate the filters.
4927 * As opposed to version 1.2, now they will be evaluated in the
4928 * filters order and not in the header order. This means that
4929 * each filter has to be validated among all headers.
4930 *
4931 * Filters are tried with ->be first, then with ->fe if it is
4932 * different from ->be.
4933 */
4934
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935 cur_proxy = t->be;
4936 while (1) {
4937 struct proxy *rule_set = cur_proxy;
4938
4939 /* try headers filters */
4940 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004941 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004942 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004943 if (target_srv(&t->target)) {
4944 target_srv(&t->target)->counters.failed_resp++;
4945 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004946 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004947 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004949 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004950 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004951 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004952 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004953 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 if (!(t->flags & SN_ERR_MASK))
4955 t->flags |= SN_ERR_PRXCOND;
4956 if (!(t->flags & SN_FINST_MASK))
4957 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004958 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004959 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004960 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004962 /* has the response been denied ? */
4963 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004964 if (target_srv(&t->target))
4965 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004966
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004967 t->be->be_counters.denied_resp++;
4968 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004969 if (t->listener->counters)
4970 t->listener->counters->denied_resp++;
4971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004973 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004976 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004977 if (txn->status < 200)
4978 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004979 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004980 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004981 ret = acl_pass(ret);
4982 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4983 ret = !ret;
4984 if (!ret)
4985 continue;
4986 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004987 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004989 }
4990
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991 /* check whether we're already working on the frontend */
4992 if (cur_proxy == t->fe)
4993 break;
4994 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004995 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004996
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004997 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004998 * We may be facing a 100-continue response, in which case this
4999 * is not the right response, and we're waiting for the next one.
5000 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005001 * next one.
5002 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005003 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005004 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau09d1e252012-05-18 22:36:34 +02005005 msg->next -= buffer_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005006 msg->msg_state = HTTP_MSG_RPBEFORE;
5007 txn->status = 0;
5008 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5009 return 1;
5010 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005011 else if (unlikely(txn->status < 200))
5012 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005013
5014 /* we don't have any 1xx status code now */
5015
5016 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017 * 4: check for server cookie.
5018 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005019 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5020 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005021 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005022
Willy Tarreaubaaee002006-06-26 02:48:02 +02005023
Willy Tarreaua15645d2007-03-18 16:22:39 +01005024 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005025 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005026 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005027 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005028 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005029
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005030 /*
5031 * 6: add server cookie in the response if needed
5032 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005033 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005034 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005035 (!(t->flags & SN_DIRECT) ||
5036 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5037 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5038 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5039 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005040 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5041 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005042 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005043 /* the server is known, it's not the one the client requested, or the
5044 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045 * insert a set-cookie here, except if we want to insert only on POST
5046 * requests and this one isn't. Note that servers which don't have cookies
5047 * (eg: some backup servers) will return a full cookie removal request.
5048 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005049 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005050 len = sprintf(trash,
5051 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5052 t->be->cookie_name);
5053 }
5054 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005055 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005056
5057 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5058 /* emit last_date, which is mandatory */
5059 trash[len++] = COOKIE_DELIM_DATE;
5060 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5061 if (t->be->cookie_maxlife) {
5062 /* emit first_date, which is either the original one or
5063 * the current date.
5064 */
5065 trash[len++] = COOKIE_DELIM_DATE;
5066 s30tob64(txn->cookie_first_date ?
5067 txn->cookie_first_date >> 2 :
5068 (date.tv_sec+3) >> 2, trash + len);
5069 len += 5;
5070 }
5071 }
5072 len += sprintf(trash + len, "; path=/");
5073 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005074
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005075 if (t->be->cookie_domain)
5076 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005077
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005078 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005080
Willy Tarreauf1348312010-10-07 15:54:11 +02005081 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005082 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005083 /* the server did not change, only the date was updated */
5084 txn->flags |= TX_SCK_UPDATED;
5085 else
5086 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005087
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088 /* Here, we will tell an eventual cache on the client side that we don't
5089 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5090 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5091 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5092 */
5093 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005094
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005095 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5096
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005097 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005098 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005099 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005100 }
5101 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 /*
5104 * 7: check if result will be cacheable with a cookie.
5105 * We'll block the response if security checks have caught
5106 * nasty things such as a cacheable cookie.
5107 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005108 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5109 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005110 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005111
5112 /* we're in presence of a cacheable response containing
5113 * a set-cookie header. We'll block it as requested by
5114 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005115 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005116 if (target_srv(&t->target))
5117 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005118
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005119 t->be->be_counters.denied_resp++;
5120 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005121 if (t->listener->counters)
5122 t->listener->counters->denied_resp++;
5123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005124 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005125 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126 send_log(t->be, LOG_ALERT,
5127 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005128 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129 goto return_srv_prx_502;
5130 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005131
5132 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005133 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005134 */
Willy Tarreau60466522010-01-18 19:08:45 +01005135 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5136 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5137 unsigned int want_flags = 0;
5138
5139 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5140 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5141 /* we want a keep-alive response here. Keep-alive header
5142 * required if either side is not 1.1.
5143 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005144 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005145 want_flags |= TX_CON_KAL_SET;
5146 }
5147 else {
5148 /* we want a close response here. Close header required if
5149 * the server is 1.1, regardless of the client.
5150 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005151 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005152 want_flags |= TX_CON_CLO_SET;
5153 }
5154
5155 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005156 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005157 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005158
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005159 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005160 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005161 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005162 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005163
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005164 /*************************************************************
5165 * OK, that's finished for the headers. We have done what we *
5166 * could. Let's switch to the DATA state. *
5167 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005168
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005170
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 /* if the user wants to log as soon as possible, without counting
5172 * bytes from the server, then this is the right moment. We have
5173 * to temporarily assign bytes_out to log what we currently have.
5174 */
5175 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5176 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5177 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005178 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 t->logs.bytes_out = 0;
5180 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005181
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005182 /* Note: we must not try to cheat by jumping directly to DATA,
5183 * otherwise we would not let the client side wake up.
5184 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005185
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005186 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005187 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005188 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005189}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005190
Willy Tarreaud98cf932009-12-27 22:54:55 +01005191/* This function is an analyser which forwards response body (including chunk
5192 * sizes if any). It is called as soon as we must forward, even if we forward
5193 * zero byte. The only situation where it must not be called is when we're in
5194 * tunnel mode and we want to forward till the close. It's used both to forward
5195 * remaining data and to resync after end of body. It expects the msg_state to
5196 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5197 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005198 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005199 * bytes of pending data + the headers if not already done (between sol and sov).
5200 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005201 */
5202int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5203{
5204 struct http_txn *txn = &s->txn;
5205 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005206 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005208 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5209 return 0;
5210
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005211 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005212 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005213 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005214 /* Output closed while we were sending data. We must abort and
5215 * wake the other side up.
5216 */
5217 msg->msg_state = HTTP_MSG_ERROR;
5218 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005219 return 1;
5220 }
5221
Willy Tarreau4fe41902010-06-07 22:27:41 +02005222 /* in most states, we should abort in case of early close */
5223 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005224
Willy Tarreaud98cf932009-12-27 22:54:55 +01005225 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005226 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau26927362012-05-18 23:22:52 +02005227 * rep->p still points to the beginning of the message and msg->sol
5228 * is still null. We must save the body in msg->next because it
5229 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005230 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005231 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005232
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005233 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005234 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5235 else {
5236 msg->msg_state = HTTP_MSG_DATA;
5237 }
5238 }
5239
Willy Tarreaud98cf932009-12-27 22:54:55 +01005240 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005241 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005242 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005243 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005244 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005245 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005246 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005247 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005248 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005249 }
5250
Willy Tarreaucaabe412010-01-03 23:08:28 +01005251 if (msg->msg_state == HTTP_MSG_DATA) {
5252 /* must still forward */
5253 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005254 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005255
5256 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005257 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005258 msg->msg_state = HTTP_MSG_DATA_CRLF;
5259 else
5260 msg->msg_state = HTTP_MSG_DONE;
5261 }
5262 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005263 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005264 * set ->sov and ->next to point to the body and switch to DATA or
5265 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005266 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005267 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005268
5269 if (!ret)
5270 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005271 else if (ret < 0) {
5272 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005273 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005274 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005275 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5279 /* we want the CRLF after the data */
5280 int ret;
5281
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005282 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283
5284 if (!ret)
5285 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005286 else if (ret < 0) {
5287 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005288 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005290 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005291 /* we're in MSG_CHUNK_SIZE now */
5292 }
5293 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005294 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005295
Willy Tarreaud98cf932009-12-27 22:54:55 +01005296 if (ret == 0)
5297 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005298 else if (ret < 0) {
5299 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005300 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005301 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005302 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005303 /* we're in HTTP_MSG_DONE now */
5304 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005306 int old_state = msg->msg_state;
5307
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005309 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005310 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5311 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5312 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 if (http_resync_states(s)) {
5314 http_silent_debug(__LINE__, s);
5315 /* some state changes occurred, maybe the analyser
5316 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005317 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005318 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5319 if (res->flags & BF_SHUTW) {
5320 /* response errors are most likely due to
5321 * the client aborting the transfer.
5322 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005323 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005324 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005325 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005326 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005328 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005329 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005330 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332 }
5333 }
5334
Willy Tarreaud98cf932009-12-27 22:54:55 +01005335 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005336 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005337 if (res->flags & BF_SHUTR) {
5338 if (!(s->flags & SN_ERR_MASK))
5339 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005340 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005341 if (target_srv(&s->target))
5342 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005343 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005344 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005345
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005346 if (res->flags & BF_SHUTW)
5347 goto aborted_xfer;
5348
Willy Tarreau40dba092010-03-04 18:14:51 +01005349 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 if (!s->req->analysers)
5351 goto return_bad_res;
5352
Willy Tarreauea953162012-05-18 23:41:28 +02005353 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005354 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005355 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005356 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005357 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005358 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005359 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 }
5361
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005362 /* When TE: chunked is used, we need to get there again to parse remaining
5363 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5364 * Similarly, with keep-alive on the client side, we don't want to forward a
5365 * close.
5366 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005367 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005368 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5369 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5370 buffer_dont_close(res);
5371
Willy Tarreau5c620922011-05-11 19:56:11 +02005372 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005373 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5374 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005375 * modes are already handled by the stream sock layer. We must not do
5376 * this in content-length mode because it could present the MSG_MORE
5377 * flag with the last block of forwarded data, which would cause an
5378 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005379 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005380 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005381 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005382
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005384 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005385 return 0;
5386
Willy Tarreau40dba092010-03-04 18:14:51 +01005387 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005388 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005389 if (target_srv(&s->target))
5390 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005391
5392 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005394 /* don't send any error message as we're in the body */
5395 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005397 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005398 if (target_srv(&s->target))
5399 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005400
5401 if (!(s->flags & SN_ERR_MASK))
5402 s->flags |= SN_ERR_PRXCOND;
5403 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005404 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005405 return 0;
5406
5407 aborted_xfer:
5408 txn->rsp.msg_state = HTTP_MSG_ERROR;
5409 /* don't send any error message as we're in the body */
5410 stream_int_retnclose(res->cons, NULL);
5411 res->analysers = 0;
5412 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5413
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005414 s->fe->fe_counters.cli_aborts++;
5415 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005416 if (target_srv(&s->target))
5417 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005418
5419 if (!(s->flags & SN_ERR_MASK))
5420 s->flags |= SN_ERR_CLICL;
5421 if (!(s->flags & SN_FINST_MASK))
5422 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005423 return 0;
5424}
5425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005426/* Iterate the same filter through all request headers.
5427 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005428 * Since it can manage the switch to another backend, it updates the per-proxy
5429 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005430 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005431int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005432{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005433 char term;
5434 char *cur_ptr, *cur_end, *cur_next;
5435 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005436 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005437 struct hdr_idx_elem *cur_hdr;
5438 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005440 last_hdr = 0;
5441
Willy Tarreau09d1e252012-05-18 22:36:34 +02005442 cur_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005443 old_idx = 0;
5444
5445 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005446 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005447 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005448 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005449 (exp->action == ACT_ALLOW ||
5450 exp->action == ACT_DENY ||
5451 exp->action == ACT_TARPIT))
5452 return 0;
5453
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005454 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005455 if (!cur_idx)
5456 break;
5457
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005458 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005459 cur_ptr = cur_next;
5460 cur_end = cur_ptr + cur_hdr->len;
5461 cur_next = cur_end + cur_hdr->cr + 1;
5462
5463 /* Now we have one header between cur_ptr and cur_end,
5464 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005465 */
5466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005467 /* The annoying part is that pattern matching needs
5468 * that we modify the contents to null-terminate all
5469 * strings before testing them.
5470 */
5471
5472 term = *cur_end;
5473 *cur_end = '\0';
5474
5475 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5476 switch (exp->action) {
5477 case ACT_SETBE:
5478 /* It is not possible to jump a second time.
5479 * FIXME: should we return an HTTP/500 here so that
5480 * the admin knows there's a problem ?
5481 */
5482 if (t->be != t->fe)
5483 break;
5484
5485 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005486 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005487 last_hdr = 1;
5488 break;
5489
5490 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005491 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 last_hdr = 1;
5493 break;
5494
5495 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005496 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005497 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005498
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005499 t->fe->fe_counters.denied_req++;
5500 if (t->fe != t->be)
5501 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005502 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005503 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005505 break;
5506
5507 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005508 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005509 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005510
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005511 t->fe->fe_counters.denied_req++;
5512 if (t->fe != t->be)
5513 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005514 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005515 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 break;
5518
5519 case ACT_REPLACE:
5520 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5521 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5522 /* FIXME: if the user adds a newline in the replacement, the
5523 * index will not be recalculated for now, and the new line
5524 * will not be counted as a new header.
5525 */
5526
5527 cur_end += delta;
5528 cur_next += delta;
5529 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005530 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005531 break;
5532
5533 case ACT_REMOVE:
5534 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5535 cur_next += delta;
5536
Willy Tarreaufa355d42009-11-29 18:12:29 +01005537 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005538 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5539 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005540 cur_hdr->len = 0;
5541 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005542 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005543 break;
5544
5545 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005546 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005547 if (cur_end)
5548 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005550 /* keep the link from this header to next one in case of later
5551 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005552 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005553 old_idx = cur_idx;
5554 }
5555 return 0;
5556}
5557
5558
5559/* Apply the filter to the request line.
5560 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5561 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 * Since it can manage the switch to another backend, it updates the per-proxy
5563 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 */
5565int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5566{
5567 char term;
5568 char *cur_ptr, *cur_end;
5569 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005570 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005572
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573
Willy Tarreau3d300592007-03-18 18:34:41 +01005574 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005576 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005577 (exp->action == ACT_ALLOW ||
5578 exp->action == ACT_DENY ||
5579 exp->action == ACT_TARPIT))
5580 return 0;
5581 else if (exp->action == ACT_REMOVE)
5582 return 0;
5583
5584 done = 0;
5585
Willy Tarreau09d1e252012-05-18 22:36:34 +02005586 cur_ptr = req->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005587 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005588
5589 /* Now we have the request line between cur_ptr and cur_end */
5590
5591 /* The annoying part is that pattern matching needs
5592 * that we modify the contents to null-terminate all
5593 * strings before testing them.
5594 */
5595
5596 term = *cur_end;
5597 *cur_end = '\0';
5598
5599 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5600 switch (exp->action) {
5601 case ACT_SETBE:
5602 /* It is not possible to jump a second time.
5603 * FIXME: should we return an HTTP/500 here so that
5604 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005605 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005606 if (t->be != t->fe)
5607 break;
5608
5609 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005610 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005611 done = 1;
5612 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005614 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005615 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 done = 1;
5617 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005619 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005620 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005621
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005622 t->fe->fe_counters.denied_req++;
5623 if (t->fe != t->be)
5624 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005625 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005626 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 done = 1;
5629 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005631 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005632 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005633
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005634 t->fe->fe_counters.denied_req++;
5635 if (t->fe != t->be)
5636 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005637 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005638 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 done = 1;
5641 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 case ACT_REPLACE:
5644 *cur_end = term; /* restore the string terminator */
5645 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5646 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5647 /* FIXME: if the user adds a newline in the replacement, the
5648 * index will not be recalculated for now, and the new line
5649 * will not be counted as a new header.
5650 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005651
Willy Tarreaufa355d42009-11-29 18:12:29 +01005652 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005654 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005655 HTTP_MSG_RQMETH,
5656 cur_ptr, cur_end + 1,
5657 NULL, NULL);
5658 if (unlikely(!cur_end))
5659 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 /* we have a full request and we know that we have either a CR
5662 * or an LF at <ptr>.
5663 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005664 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5665 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 /* there is no point trying this regex on headers */
5667 return 1;
5668 }
5669 }
5670 *cur_end = term; /* restore the string terminator */
5671 return done;
5672}
Willy Tarreau97de6242006-12-27 17:18:38 +01005673
Willy Tarreau58f10d72006-12-04 02:26:12 +01005674
Willy Tarreau58f10d72006-12-04 02:26:12 +01005675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005677 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005679 * unparsable request. Since it can manage the switch to another backend, it
5680 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005681 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005682int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005684 struct http_txn *txn = &s->txn;
5685 struct hdr_exp *exp;
5686
5687 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005690 /*
5691 * The interleaving of transformations and verdicts
5692 * makes it difficult to decide to continue or stop
5693 * the evaluation.
5694 */
5695
Willy Tarreau6c123b12010-01-28 20:22:06 +01005696 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5697 break;
5698
Willy Tarreau3d300592007-03-18 18:34:41 +01005699 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005701 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005703
5704 /* if this filter had a condition, evaluate it now and skip to
5705 * next filter if the condition does not match.
5706 */
5707 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005708 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005709 ret = acl_pass(ret);
5710 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5711 ret = !ret;
5712
5713 if (!ret)
5714 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715 }
5716
5717 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005718 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 if (unlikely(ret < 0))
5720 return -1;
5721
5722 if (likely(ret == 0)) {
5723 /* The filter did not match the request, it can be
5724 * iterated through all headers.
5725 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005726 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005728 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005730}
5731
5732
Willy Tarreaua15645d2007-03-18 16:22:39 +01005733
Willy Tarreau58f10d72006-12-04 02:26:12 +01005734/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005735 * Try to retrieve the server associated to the appsession.
5736 * If the server is found, it's assigned to the session.
5737 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005738void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005739 struct http_txn *txn = &t->txn;
5740 appsess *asession = NULL;
5741 char *sessid_temp = NULL;
5742
Cyril Bontéb21570a2009-11-29 20:04:48 +01005743 if (len > t->be->appsession_len) {
5744 len = t->be->appsession_len;
5745 }
5746
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005747 if (t->be->options2 & PR_O2_AS_REQL) {
5748 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005749 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005750 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005751 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005752 }
5753
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005754 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005755 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5756 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5757 return;
5758 }
5759
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005760 memcpy(txn->sessid, buf, len);
5761 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005762 }
5763
5764 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5765 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
Cyril Bontéb21570a2009-11-29 20:04:48 +01005770 memcpy(sessid_temp, buf, len);
5771 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772
5773 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5774 /* free previously allocated memory */
5775 pool_free2(apools.sessid, sessid_temp);
5776
5777 if (asession != NULL) {
5778 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5779 if (!(t->be->options2 & PR_O2_AS_REQL))
5780 asession->request_count++;
5781
5782 if (asession->serverid != NULL) {
5783 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005784
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005785 while (srv) {
5786 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005787 if ((srv->state & SRV_RUNNING) ||
5788 (t->be->options & PR_O_PERSIST) ||
5789 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005790 /* we found the server and it's usable */
5791 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005792 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005793 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005794 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005795
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005796 break;
5797 } else {
5798 txn->flags &= ~TX_CK_MASK;
5799 txn->flags |= TX_CK_DOWN;
5800 }
5801 }
5802 srv = srv->next;
5803 }
5804 }
5805 }
5806}
5807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005808/* Find the end of a cookie value contained between <s> and <e>. It works the
5809 * same way as with headers above except that the semi-colon also ends a token.
5810 * See RFC2965 for more information. Note that it requires a valid header to
5811 * return a valid result.
5812 */
5813char *find_cookie_value_end(char *s, const char *e)
5814{
5815 int quoted, qdpair;
5816
5817 quoted = qdpair = 0;
5818 for (; s < e; s++) {
5819 if (qdpair) qdpair = 0;
5820 else if (quoted) {
5821 if (*s == '\\') qdpair = 1;
5822 else if (*s == '"') quoted = 0;
5823 }
5824 else if (*s == '"') quoted = 1;
5825 else if (*s == ',' || *s == ';') return s;
5826 }
5827 return s;
5828}
5829
5830/* Delete a value in a header between delimiters <from> and <next> in buffer
5831 * <buf>. The number of characters displaced is returned, and the pointer to
5832 * the first delimiter is updated if required. The function tries as much as
5833 * possible to respect the following principles :
5834 * - replace <from> delimiter by the <next> one unless <from> points to a
5835 * colon, in which case <next> is simply removed
5836 * - set exactly one space character after the new first delimiter, unless
5837 * there are not enough characters in the block being moved to do so.
5838 * - remove unneeded spaces before the previous delimiter and after the new
5839 * one.
5840 *
5841 * It is the caller's responsibility to ensure that :
5842 * - <from> points to a valid delimiter or the colon ;
5843 * - <next> points to a valid delimiter or the final CR/LF ;
5844 * - there are non-space chars before <from> ;
5845 * - there is a CR/LF at or after <next>.
5846 */
5847int del_hdr_value(struct buffer *buf, char **from, char *next)
5848{
5849 char *prev = *from;
5850
5851 if (*prev == ':') {
5852 /* We're removing the first value, preserve the colon and add a
5853 * space if possible.
5854 */
5855 if (!http_is_crlf[(unsigned char)*next])
5856 next++;
5857 prev++;
5858 if (prev < next)
5859 *prev++ = ' ';
5860
5861 while (http_is_spht[(unsigned char)*next])
5862 next++;
5863 } else {
5864 /* Remove useless spaces before the old delimiter. */
5865 while (http_is_spht[(unsigned char)*(prev-1)])
5866 prev--;
5867 *from = prev;
5868
5869 /* copy the delimiter and if possible a space if we're
5870 * not at the end of the line.
5871 */
5872 if (!http_is_crlf[(unsigned char)*next]) {
5873 *prev++ = *next++;
5874 if (prev + 1 < next)
5875 *prev++ = ' ';
5876 while (http_is_spht[(unsigned char)*next])
5877 next++;
5878 }
5879 }
5880 return buffer_replace2(buf, prev, next, NULL, 0);
5881}
5882
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005883/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005884 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005885 * desirable to call it only when needed. This code is quite complex because
5886 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5887 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005888 */
5889void manage_client_side_cookies(struct session *t, struct buffer *req)
5890{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005891 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005892 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005894 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5895 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005897 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005898 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02005899 hdr_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005900
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005901 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005903 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005905 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005906 hdr_beg = hdr_next;
5907 hdr_end = hdr_beg + cur_hdr->len;
5908 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005910 /* We have one full header between hdr_beg and hdr_end, and the
5911 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005912 * "Cookie:" headers.
5913 */
5914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005915 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005916 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917 old_idx = cur_idx;
5918 continue;
5919 }
5920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005921 del_from = NULL; /* nothing to be deleted */
5922 preserve_hdr = 0; /* assume we may kill the whole header */
5923
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924 /* Now look for cookies. Conforming to RFC2109, we have to support
5925 * attributes whose name begin with a '$', and associate them with
5926 * the right cookie, if we want to delete this cookie.
5927 * So there are 3 cases for each cookie read :
5928 * 1) it's a special attribute, beginning with a '$' : ignore it.
5929 * 2) it's a server id cookie that we *MAY* want to delete : save
5930 * some pointers on it (last semi-colon, beginning of cookie...)
5931 * 3) it's an application cookie : we *MAY* have to delete a previous
5932 * "special" cookie.
5933 * At the end of loop, if a "special" cookie remains, we may have to
5934 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935 * *MUST* delete it.
5936 *
5937 * Note: RFC2965 is unclear about the processing of spaces around
5938 * the equal sign in the ATTR=VALUE form. A careful inspection of
5939 * the RFC explicitly allows spaces before it, and not within the
5940 * tokens (attrs or values). An inspection of RFC2109 allows that
5941 * too but section 10.1.3 lets one think that spaces may be allowed
5942 * after the equal sign too, resulting in some (rare) buggy
5943 * implementations trying to do that. So let's do what servers do.
5944 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5945 * allowed quoted strings in values, with any possible character
5946 * after a backslash, including control chars and delimitors, which
5947 * causes parsing to become ambiguous. Browsers also allow spaces
5948 * within values even without quotes.
5949 *
5950 * We have to keep multiple pointers in order to support cookie
5951 * removal at the beginning, middle or end of header without
5952 * corrupting the header. All of these headers are valid :
5953 *
5954 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5955 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5956 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5957 * | | | | | | | | |
5958 * | | | | | | | | hdr_end <--+
5959 * | | | | | | | +--> next
5960 * | | | | | | +----> val_end
5961 * | | | | | +-----------> val_beg
5962 * | | | | +--------------> equal
5963 * | | | +----------------> att_end
5964 * | | +---------------------> att_beg
5965 * | +--------------------------> prev
5966 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967 */
5968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005969 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5970 /* Iterate through all cookies on this line */
5971
5972 /* find att_beg */
5973 att_beg = prev + 1;
5974 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5975 att_beg++;
5976
5977 /* find att_end : this is the first character after the last non
5978 * space before the equal. It may be equal to hdr_end.
5979 */
5980 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005981
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005982 while (equal < hdr_end) {
5983 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005984 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005985 if (http_is_spht[(unsigned char)*equal++])
5986 continue;
5987 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988 }
5989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 /* here, <equal> points to '=', a delimitor or the end. <att_end>
5991 * is between <att_beg> and <equal>, both may be identical.
5992 */
5993
5994 /* look for end of cookie if there is an equal sign */
5995 if (equal < hdr_end && *equal == '=') {
5996 /* look for the beginning of the value */
5997 val_beg = equal + 1;
5998 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
5999 val_beg++;
6000
6001 /* find the end of the value, respecting quotes */
6002 next = find_cookie_value_end(val_beg, hdr_end);
6003
6004 /* make val_end point to the first white space or delimitor after the value */
6005 val_end = next;
6006 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6007 val_end--;
6008 } else {
6009 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006010 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006011
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006012 /* We have nothing to do with attributes beginning with '$'. However,
6013 * they will automatically be removed if a header before them is removed,
6014 * since they're supposed to be linked together.
6015 */
6016 if (*att_beg == '$')
6017 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006018
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006019 /* Ignore cookies with no equal sign */
6020 if (equal == next) {
6021 /* This is not our cookie, so we must preserve it. But if we already
6022 * scheduled another cookie for removal, we cannot remove the
6023 * complete header, but we can remove the previous block itself.
6024 */
6025 preserve_hdr = 1;
6026 if (del_from != NULL) {
6027 int delta = del_hdr_value(req, &del_from, prev);
6028 val_end += delta;
6029 next += delta;
6030 hdr_end += delta;
6031 hdr_next += delta;
6032 cur_hdr->len += delta;
6033 http_msg_move_end(&txn->req, delta);
6034 prev = del_from;
6035 del_from = NULL;
6036 }
6037 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006040 /* if there are spaces around the equal sign, we need to
6041 * strip them otherwise we'll get trouble for cookie captures,
6042 * or even for rewrites. Since this happens extremely rarely,
6043 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006044 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006045 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6046 int stripped_before = 0;
6047 int stripped_after = 0;
6048
6049 if (att_end != equal) {
6050 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6051 equal += stripped_before;
6052 val_beg += stripped_before;
6053 }
6054
6055 if (val_beg > equal + 1) {
6056 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6057 val_beg += stripped_after;
6058 stripped_before += stripped_after;
6059 }
6060
6061 val_end += stripped_before;
6062 next += stripped_before;
6063 hdr_end += stripped_before;
6064 hdr_next += stripped_before;
6065 cur_hdr->len += stripped_before;
6066 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006067 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006068 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 /* First, let's see if we want to capture this cookie. We check
6071 * that we don't already have a client side cookie, because we
6072 * can only capture one. Also as an optimisation, we ignore
6073 * cookies shorter than the declared name.
6074 */
6075 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6076 (val_end - att_beg >= t->fe->capture_namelen) &&
6077 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6078 int log_len = val_end - att_beg;
6079
6080 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6081 Alert("HTTP logging : out of memory.\n");
6082 } else {
6083 if (log_len > t->fe->capture_len)
6084 log_len = t->fe->capture_len;
6085 memcpy(txn->cli_cookie, att_beg, log_len);
6086 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089
Willy Tarreaubca99692010-10-06 19:25:55 +02006090 /* Persistence cookies in passive, rewrite or insert mode have the
6091 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006093 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006094 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006095 * For cookies in prefix mode, the form is :
6096 *
6097 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098 */
6099 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6100 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6101 struct server *srv = t->be->srv;
6102 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6105 * have the server ID between val_beg and delim, and the original cookie between
6106 * delim+1 and val_end. Otherwise, delim==val_end :
6107 *
6108 * Cookie: NAME=SRV; # in all but prefix modes
6109 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6110 * | || || | |+-> next
6111 * | || || | +--> val_end
6112 * | || || +---------> delim
6113 * | || |+------------> val_beg
6114 * | || +-------------> att_end = equal
6115 * | |+-----------------> att_beg
6116 * | +------------------> prev
6117 * +-------------------------> hdr_beg
6118 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006120 if (t->be->options & PR_O_COOK_PFX) {
6121 for (delim = val_beg; delim < val_end; delim++)
6122 if (*delim == COOKIE_DELIM)
6123 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006124 } else {
6125 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006126 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006127 /* Now check if the cookie contains a date field, which would
6128 * appear after a vertical bar ('|') just after the server name
6129 * and before the delimiter.
6130 */
6131 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6132 if (vbar1) {
6133 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006134 * right is the last seen date. It is a base64 encoded
6135 * 30-bit value representing the UNIX date since the
6136 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006137 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006138 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006139 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006140 if (val_end - vbar1 >= 5) {
6141 val = b64tos30(vbar1);
6142 if (val > 0)
6143 txn->cookie_last_date = val << 2;
6144 }
6145 /* look for a second vertical bar */
6146 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6147 if (vbar1 && (val_end - vbar1 > 5)) {
6148 val = b64tos30(vbar1 + 1);
6149 if (val > 0)
6150 txn->cookie_first_date = val << 2;
6151 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006152 }
6153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154
Willy Tarreauf64d1412010-10-07 20:06:11 +02006155 /* if the cookie has an expiration date and the proxy wants to check
6156 * it, then we do that now. We first check if the cookie is too old,
6157 * then only if it has expired. We detect strict overflow because the
6158 * time resolution here is not great (4 seconds). Cookies with dates
6159 * in the future are ignored if their offset is beyond one day. This
6160 * allows an admin to fix timezone issues without expiring everyone
6161 * and at the same time avoids keeping unwanted side effects for too
6162 * long.
6163 */
6164 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006165 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6166 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006167 txn->flags &= ~TX_CK_MASK;
6168 txn->flags |= TX_CK_OLD;
6169 delim = val_beg; // let's pretend we have not found the cookie
6170 txn->cookie_first_date = 0;
6171 txn->cookie_last_date = 0;
6172 }
6173 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006174 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6175 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006176 txn->flags &= ~TX_CK_MASK;
6177 txn->flags |= TX_CK_EXPIRED;
6178 delim = val_beg; // let's pretend we have not found the cookie
6179 txn->cookie_first_date = 0;
6180 txn->cookie_last_date = 0;
6181 }
6182
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006183 /* Here, we'll look for the first running server which supports the cookie.
6184 * This allows to share a same cookie between several servers, for example
6185 * to dedicate backup servers to specific servers only.
6186 * However, to prevent clients from sticking to cookie-less backup server
6187 * when they have incidentely learned an empty cookie, we simply ignore
6188 * empty cookies and mark them as invalid.
6189 * The same behaviour is applied when persistence must be ignored.
6190 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006191 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006192 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006193
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 while (srv) {
6195 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6196 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6197 if ((srv->state & SRV_RUNNING) ||
6198 (t->be->options & PR_O_PERSIST) ||
6199 (t->flags & SN_FORCE_PRST)) {
6200 /* we found the server and we can use it */
6201 txn->flags &= ~TX_CK_MASK;
6202 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6203 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006204 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 break;
6206 } else {
6207 /* we found a server, but it's down,
6208 * mark it as such and go on in case
6209 * another one is available.
6210 */
6211 txn->flags &= ~TX_CK_MASK;
6212 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 srv = srv->next;
6216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Willy Tarreauf64d1412010-10-07 20:06:11 +02006218 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006219 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006221 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6222 txn->flags |= TX_CK_UNUSED;
6223 else
6224 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 /* depending on the cookie mode, we may have to either :
6228 * - delete the complete cookie if we're in insert+indirect mode, so that
6229 * the server never sees it ;
6230 * - remove the server id from the cookie value, and tag the cookie as an
6231 * application cookie so that it does not get accidentely removed later,
6232 * if we're in cookie prefix mode
6233 */
6234 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6235 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6238 val_end += delta;
6239 next += delta;
6240 hdr_end += delta;
6241 hdr_next += delta;
6242 cur_hdr->len += delta;
6243 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 del_from = NULL;
6246 preserve_hdr = 1; /* we want to keep this cookie */
6247 }
6248 else if (del_from == NULL &&
6249 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6250 del_from = prev;
6251 }
6252 } else {
6253 /* This is not our cookie, so we must preserve it. But if we already
6254 * scheduled another cookie for removal, we cannot remove the
6255 * complete header, but we can remove the previous block itself.
6256 */
6257 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 if (del_from != NULL) {
6260 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006261 if (att_beg >= del_from)
6262 att_beg += delta;
6263 if (att_end >= del_from)
6264 att_end += delta;
6265 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 val_end += delta;
6267 next += delta;
6268 hdr_end += delta;
6269 hdr_next += delta;
6270 cur_hdr->len += delta;
6271 http_msg_move_end(&txn->req, delta);
6272 prev = del_from;
6273 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 /* Look for the appsession cookie unless persistence must be ignored */
6278 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6279 int cmp_len, value_len;
6280 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006281
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 if (t->be->options2 & PR_O2_AS_PFX) {
6283 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6284 value_begin = att_beg + t->be->appsession_name_len;
6285 value_len = val_end - att_beg - t->be->appsession_name_len;
6286 } else {
6287 cmp_len = att_end - att_beg;
6288 value_begin = val_beg;
6289 value_len = val_end - val_beg;
6290 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 /* let's see if the cookie is our appcookie */
6293 if (cmp_len == t->be->appsession_name_len &&
6294 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6295 manage_client_side_appsession(t, value_begin, value_len);
6296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297 }
6298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 /* continue with next cookie on this header line */
6300 att_beg = next;
6301 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 /* There are no more cookies on this line.
6304 * We may still have one (or several) marked for deletion at the
6305 * end of the line. We must do this now in two ways :
6306 * - if some cookies must be preserved, we only delete from the
6307 * mark to the end of line ;
6308 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006311 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 if (preserve_hdr) {
6313 delta = del_hdr_value(req, &del_from, hdr_end);
6314 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 cur_hdr->len += delta;
6316 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006318
6319 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006320 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6321 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006323 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006324 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006326 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327 }
6328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332}
6333
6334
Willy Tarreaua15645d2007-03-18 16:22:39 +01006335/* Iterate the same filter through all response headers contained in <rtr>.
6336 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6337 */
6338int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6339{
6340 char term;
6341 char *cur_ptr, *cur_end, *cur_next;
6342 int cur_idx, old_idx, last_hdr;
6343 struct http_txn *txn = &t->txn;
6344 struct hdr_idx_elem *cur_hdr;
6345 int len, delta;
6346
6347 last_hdr = 0;
6348
Willy Tarreau09d1e252012-05-18 22:36:34 +02006349 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006350 old_idx = 0;
6351
6352 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006353 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006354 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006355 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006356 (exp->action == ACT_ALLOW ||
6357 exp->action == ACT_DENY))
6358 return 0;
6359
6360 cur_idx = txn->hdr_idx.v[old_idx].next;
6361 if (!cur_idx)
6362 break;
6363
6364 cur_hdr = &txn->hdr_idx.v[cur_idx];
6365 cur_ptr = cur_next;
6366 cur_end = cur_ptr + cur_hdr->len;
6367 cur_next = cur_end + cur_hdr->cr + 1;
6368
6369 /* Now we have one header between cur_ptr and cur_end,
6370 * and the next header starts at cur_next.
6371 */
6372
6373 /* The annoying part is that pattern matching needs
6374 * that we modify the contents to null-terminate all
6375 * strings before testing them.
6376 */
6377
6378 term = *cur_end;
6379 *cur_end = '\0';
6380
6381 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6382 switch (exp->action) {
6383 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006384 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006385 last_hdr = 1;
6386 break;
6387
6388 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006389 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006390 last_hdr = 1;
6391 break;
6392
6393 case ACT_REPLACE:
6394 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6395 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6396 /* FIXME: if the user adds a newline in the replacement, the
6397 * index will not be recalculated for now, and the new line
6398 * will not be counted as a new header.
6399 */
6400
6401 cur_end += delta;
6402 cur_next += delta;
6403 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006404 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405 break;
6406
6407 case ACT_REMOVE:
6408 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6409 cur_next += delta;
6410
Willy Tarreaufa355d42009-11-29 18:12:29 +01006411 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006412 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6413 txn->hdr_idx.used--;
6414 cur_hdr->len = 0;
6415 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006416 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006417 break;
6418
6419 }
6420 }
6421 if (cur_end)
6422 *cur_end = term; /* restore the string terminator */
6423
6424 /* keep the link from this header to next one in case of later
6425 * removal of next header.
6426 */
6427 old_idx = cur_idx;
6428 }
6429 return 0;
6430}
6431
6432
6433/* Apply the filter to the status line in the response buffer <rtr>.
6434 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6435 * or -1 if a replacement resulted in an invalid status line.
6436 */
6437int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6438{
6439 char term;
6440 char *cur_ptr, *cur_end;
6441 int done;
6442 struct http_txn *txn = &t->txn;
6443 int len, delta;
6444
6445
Willy Tarreau3d300592007-03-18 18:34:41 +01006446 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006447 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 (exp->action == ACT_ALLOW ||
6450 exp->action == ACT_DENY))
6451 return 0;
6452 else if (exp->action == ACT_REMOVE)
6453 return 0;
6454
6455 done = 0;
6456
Willy Tarreau09d1e252012-05-18 22:36:34 +02006457 cur_ptr = rtr->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006458 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459
6460 /* Now we have the status line between cur_ptr and cur_end */
6461
6462 /* The annoying part is that pattern matching needs
6463 * that we modify the contents to null-terminate all
6464 * strings before testing them.
6465 */
6466
6467 term = *cur_end;
6468 *cur_end = '\0';
6469
6470 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6471 switch (exp->action) {
6472 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 done = 1;
6475 break;
6476
6477 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006478 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006479 done = 1;
6480 break;
6481
6482 case ACT_REPLACE:
6483 *cur_end = term; /* restore the string terminator */
6484 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6485 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6486 /* FIXME: if the user adds a newline in the replacement, the
6487 * index will not be recalculated for now, and the new line
6488 * will not be counted as a new header.
6489 */
6490
Willy Tarreaufa355d42009-11-29 18:12:29 +01006491 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006493 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006494 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006495 cur_ptr, cur_end + 1,
6496 NULL, NULL);
6497 if (unlikely(!cur_end))
6498 return -1;
6499
6500 /* we have a full respnse and we know that we have either a CR
6501 * or an LF at <ptr>.
6502 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02006503 txn->status = strl2ui(rtr->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006504 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 /* there is no point trying this regex on headers */
6506 return 1;
6507 }
6508 }
6509 *cur_end = term; /* restore the string terminator */
6510 return done;
6511}
6512
6513
6514
6515/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006516 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6518 * unparsable response.
6519 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006520int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006522 struct http_txn *txn = &s->txn;
6523 struct hdr_exp *exp;
6524
6525 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 int ret;
6527
6528 /*
6529 * The interleaving of transformations and verdicts
6530 * makes it difficult to decide to continue or stop
6531 * the evaluation.
6532 */
6533
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006534 if (txn->flags & TX_SVDENY)
6535 break;
6536
Willy Tarreau3d300592007-03-18 18:34:41 +01006537 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6539 exp->action == ACT_PASS)) {
6540 exp = exp->next;
6541 continue;
6542 }
6543
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006544 /* if this filter had a condition, evaluate it now and skip to
6545 * next filter if the condition does not match.
6546 */
6547 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006548 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006549 ret = acl_pass(ret);
6550 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6551 ret = !ret;
6552 if (!ret)
6553 continue;
6554 }
6555
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006557 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 if (unlikely(ret < 0))
6559 return -1;
6560
6561 if (likely(ret == 0)) {
6562 /* The filter did not match the response, it can be
6563 * iterated through all headers.
6564 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006565 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 }
6568 return 0;
6569}
6570
6571
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006573 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006574 * desirable to call it only when needed. This function is also used when we
6575 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006577void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578{
6579 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006580 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006581 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006583 char *hdr_beg, *hdr_end, *hdr_next;
6584 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 /* Iterate through the headers.
6587 * we start with the start line.
6588 */
6589 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006590 hdr_next = res->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591
6592 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6593 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006594 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595
6596 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006597 hdr_beg = hdr_next;
6598 hdr_end = hdr_beg + cur_hdr->len;
6599 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600
Willy Tarreau24581ba2010-08-31 22:39:35 +02006601 /* We have one full header between hdr_beg and hdr_end, and the
6602 * next header starts at hdr_next. We're only interested in
6603 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604 */
6605
Willy Tarreau24581ba2010-08-31 22:39:35 +02006606 is_cookie2 = 0;
6607 prev = hdr_beg + 10;
6608 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006609 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006610 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6611 if (!val) {
6612 old_idx = cur_idx;
6613 continue;
6614 }
6615 is_cookie2 = 1;
6616 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 }
6618
Willy Tarreau24581ba2010-08-31 22:39:35 +02006619 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6620 * <prev> points to the colon.
6621 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006622 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623
Willy Tarreau24581ba2010-08-31 22:39:35 +02006624 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6625 * check-cache is enabled) and we are not interested in checking
6626 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006627 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006628 if (t->be->cookie_name == NULL &&
6629 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006630 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631 return;
6632
Willy Tarreau24581ba2010-08-31 22:39:35 +02006633 /* OK so now we know we have to process this response cookie.
6634 * The format of the Set-Cookie header is slightly different
6635 * from the format of the Cookie header in that it does not
6636 * support the comma as a cookie delimiter (thus the header
6637 * cannot be folded) because the Expires attribute described in
6638 * the original Netscape's spec may contain an unquoted date
6639 * with a comma inside. We have to live with this because
6640 * many browsers don't support Max-Age and some browsers don't
6641 * support quoted strings. However the Set-Cookie2 header is
6642 * clean.
6643 *
6644 * We have to keep multiple pointers in order to support cookie
6645 * removal at the beginning, middle or end of header without
6646 * corrupting the header (in case of set-cookie2). A special
6647 * pointer, <scav> points to the beginning of the set-cookie-av
6648 * fields after the first semi-colon. The <next> pointer points
6649 * either to the end of line (set-cookie) or next unquoted comma
6650 * (set-cookie2). All of these headers are valid :
6651 *
6652 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6653 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6654 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6655 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6656 * | | | | | | | | | |
6657 * | | | | | | | | +-> next hdr_end <--+
6658 * | | | | | | | +------------> scav
6659 * | | | | | | +--------------> val_end
6660 * | | | | | +--------------------> val_beg
6661 * | | | | +----------------------> equal
6662 * | | | +------------------------> att_end
6663 * | | +----------------------------> att_beg
6664 * | +------------------------------> prev
6665 * +-----------------------------------------> hdr_beg
6666 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667
Willy Tarreau24581ba2010-08-31 22:39:35 +02006668 for (; prev < hdr_end; prev = next) {
6669 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670
Willy Tarreau24581ba2010-08-31 22:39:35 +02006671 /* find att_beg */
6672 att_beg = prev + 1;
6673 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6674 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676 /* find att_end : this is the first character after the last non
6677 * space before the equal. It may be equal to hdr_end.
6678 */
6679 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680
Willy Tarreau24581ba2010-08-31 22:39:35 +02006681 while (equal < hdr_end) {
6682 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6683 break;
6684 if (http_is_spht[(unsigned char)*equal++])
6685 continue;
6686 att_end = equal;
6687 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6690 * is between <att_beg> and <equal>, both may be identical.
6691 */
6692
6693 /* look for end of cookie if there is an equal sign */
6694 if (equal < hdr_end && *equal == '=') {
6695 /* look for the beginning of the value */
6696 val_beg = equal + 1;
6697 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6698 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 /* find the end of the value, respecting quotes */
6701 next = find_cookie_value_end(val_beg, hdr_end);
6702
6703 /* make val_end point to the first white space or delimitor after the value */
6704 val_end = next;
6705 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6706 val_end--;
6707 } else {
6708 /* <equal> points to next comma, semi-colon or EOL */
6709 val_beg = val_end = next = equal;
6710 }
6711
6712 if (next < hdr_end) {
6713 /* Set-Cookie2 supports multiple cookies, and <next> points to
6714 * a colon or semi-colon before the end. So skip all attr-value
6715 * pairs and look for the next comma. For Set-Cookie, since
6716 * commas are permitted in values, skip to the end.
6717 */
6718 if (is_cookie2)
6719 next = find_hdr_value_end(next, hdr_end);
6720 else
6721 next = hdr_end;
6722 }
6723
6724 /* Now everything is as on the diagram above */
6725
6726 /* Ignore cookies with no equal sign */
6727 if (equal == val_end)
6728 continue;
6729
6730 /* If there are spaces around the equal sign, we need to
6731 * strip them otherwise we'll get trouble for cookie captures,
6732 * or even for rewrites. Since this happens extremely rarely,
6733 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006735 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6736 int stripped_before = 0;
6737 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738
Willy Tarreau24581ba2010-08-31 22:39:35 +02006739 if (att_end != equal) {
6740 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6741 equal += stripped_before;
6742 val_beg += stripped_before;
6743 }
6744
6745 if (val_beg > equal + 1) {
6746 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6747 val_beg += stripped_after;
6748 stripped_before += stripped_after;
6749 }
6750
6751 val_end += stripped_before;
6752 next += stripped_before;
6753 hdr_end += stripped_before;
6754 hdr_next += stripped_before;
6755 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006756 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006757 }
6758
6759 /* First, let's see if we want to capture this cookie. We check
6760 * that we don't already have a server side cookie, because we
6761 * can only capture one. Also as an optimisation, we ignore
6762 * cookies shorter than the declared name.
6763 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006764 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006765 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006766 (val_end - att_beg >= t->fe->capture_namelen) &&
6767 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6768 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006769 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770 Alert("HTTP logging : out of memory.\n");
6771 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006772 else {
6773 if (log_len > t->fe->capture_len)
6774 log_len = t->fe->capture_len;
6775 memcpy(txn->srv_cookie, att_beg, log_len);
6776 txn->srv_cookie[log_len] = 0;
6777 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778 }
6779
Willy Tarreau827aee92011-03-10 16:55:02 +01006780 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006782 if (!(t->flags & SN_IGNORE_PRST) &&
6783 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6784 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006785 /* assume passive cookie by default */
6786 txn->flags &= ~TX_SCK_MASK;
6787 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788
6789 /* If the cookie is in insert mode on a known server, we'll delete
6790 * this occurrence because we'll insert another one later.
6791 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006792 * a direct access.
6793 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006794 if (t->be->options2 & PR_O2_COOK_PSV) {
6795 /* The "preserve" flag was set, we don't want to touch the
6796 * server's cookie.
6797 */
6798 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006799 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006800 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006801 /* this cookie must be deleted */
6802 if (*prev == ':' && next == hdr_end) {
6803 /* whole header */
6804 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6805 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6806 txn->hdr_idx.used--;
6807 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006808 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006809 hdr_next += delta;
6810 http_msg_move_end(&txn->rsp, delta);
6811 /* note: while both invalid now, <next> and <hdr_end>
6812 * are still equal, so the for() will stop as expected.
6813 */
6814 } else {
6815 /* just remove the value */
6816 int delta = del_hdr_value(res, &prev, next);
6817 next = prev;
6818 hdr_end += delta;
6819 hdr_next += delta;
6820 cur_hdr->len += delta;
6821 http_msg_move_end(&txn->rsp, delta);
6822 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006823 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006824 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006827 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 * with this server since we know it.
6830 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006831 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006832 next += delta;
6833 hdr_end += delta;
6834 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006836 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837
Willy Tarreauf1348312010-10-07 15:54:11 +02006838 txn->flags &= ~TX_SCK_MASK;
6839 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006841 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006842 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006845 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 next += delta;
6847 hdr_end += delta;
6848 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006850 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851
Willy Tarreau827aee92011-03-10 16:55:02 +01006852 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006853 txn->flags &= ~TX_SCK_MASK;
6854 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 }
6856 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006857 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6858 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006859 int cmp_len, value_len;
6860 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861
Cyril Bontéb21570a2009-11-29 20:04:48 +01006862 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6864 value_begin = att_beg + t->be->appsession_name_len;
6865 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006866 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006867 cmp_len = att_end - att_beg;
6868 value_begin = val_beg;
6869 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006870 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006871
Cyril Bonté17530c32010-04-06 21:11:10 +02006872 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006873 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6874 /* free a possibly previously allocated memory */
6875 pool_free2(apools.sessid, txn->sessid);
6876
Cyril Bontéb21570a2009-11-29 20:04:48 +01006877 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006878 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006879 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6880 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6881 return;
6882 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006883 memcpy(txn->sessid, value_begin, value_len);
6884 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 }
6887 /* that's done for this cookie, check the next one on the same
6888 * line when next != hdr_end (only if is_cookie2).
6889 */
6890 }
6891 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006893 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006894
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006895 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006896 appsess *asession = NULL;
6897 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006898 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006899 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006900 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006901 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6902 Alert("Not enough Memory process_srv():asession:calloc().\n");
6903 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6904 return;
6905 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006906 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6907
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006908 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6909 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6910 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006911 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006912 return;
6913 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006914 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006915 asession->sessid[t->be->appsession_len] = 0;
6916
Willy Tarreau827aee92011-03-10 16:55:02 +01006917 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006918 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006919 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006920 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 }
6924 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006925 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006926
6927 asession->request_count = 0;
6928 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6929 }
6930
6931 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6932 asession->request_count++;
6933 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006934}
6935
6936
Willy Tarreaua15645d2007-03-18 16:22:39 +01006937/*
6938 * Check if response is cacheable or not. Updates t->flags.
6939 */
6940void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6941{
6942 struct http_txn *txn = &t->txn;
6943 char *p1, *p2;
6944
6945 char *cur_ptr, *cur_end, *cur_next;
6946 int cur_idx;
6947
Willy Tarreau5df51872007-11-25 16:20:08 +01006948 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949 return;
6950
6951 /* Iterate through the headers.
6952 * we start with the start line.
6953 */
6954 cur_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006955 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956
6957 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6958 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006959 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960
6961 cur_hdr = &txn->hdr_idx.v[cur_idx];
6962 cur_ptr = cur_next;
6963 cur_end = cur_ptr + cur_hdr->len;
6964 cur_next = cur_end + cur_hdr->cr + 1;
6965
6966 /* We have one full header between cur_ptr and cur_end, and the
6967 * next header starts at cur_next. We're only interested in
6968 * "Cookie:" headers.
6969 */
6970
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006971 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6972 if (val) {
6973 if ((cur_end - (cur_ptr + val) >= 8) &&
6974 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6975 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6976 return;
6977 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 }
6979
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006980 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6981 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982 continue;
6983
6984 /* OK, right now we know we have a cache-control header at cur_ptr */
6985
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006986 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
6988 if (p1 >= cur_end) /* no more info */
6989 continue;
6990
6991 /* p1 is at the beginning of the value */
6992 p2 = p1;
6993
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006994 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 p2++;
6996
6997 /* we have a complete value between p1 and p2 */
6998 if (p2 < cur_end && *p2 == '=') {
6999 /* we have something of the form no-cache="set-cookie" */
7000 if ((cur_end - p1 >= 21) &&
7001 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7002 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007003 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 continue;
7005 }
7006
7007 /* OK, so we know that either p2 points to the end of string or to a comma */
7008 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7009 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7010 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7011 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007012 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013 return;
7014 }
7015
7016 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007017 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018 continue;
7019 }
7020 }
7021}
7022
7023
Willy Tarreau58f10d72006-12-04 02:26:12 +01007024/*
7025 * Try to retrieve a known appsession in the URI, then the associated server.
7026 * If the server is found, it's assigned to the session.
7027 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007029{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007030 char *end_params, *first_param, *cur_param, *next_param;
7031 char separator;
7032 int value_len;
7033
7034 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007036 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007037 (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 +01007038 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007039 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007040
Cyril Bontéb21570a2009-11-29 20:04:48 +01007041 first_param = NULL;
7042 switch (mode) {
7043 case PR_O2_AS_M_PP:
7044 first_param = memchr(begin, ';', len);
7045 break;
7046 case PR_O2_AS_M_QS:
7047 first_param = memchr(begin, '?', len);
7048 break;
7049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050
Cyril Bontéb21570a2009-11-29 20:04:48 +01007051 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007052 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007053 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054
Cyril Bontéb21570a2009-11-29 20:04:48 +01007055 switch (mode) {
7056 case PR_O2_AS_M_PP:
7057 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7058 end_params = (char *) begin + len;
7059 }
7060 separator = ';';
7061 break;
7062 case PR_O2_AS_M_QS:
7063 end_params = (char *) begin + len;
7064 separator = '&';
7065 break;
7066 default:
7067 /* unknown mode, shouldn't happen */
7068 return;
7069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070
Cyril Bontéb21570a2009-11-29 20:04:48 +01007071 cur_param = next_param = end_params;
7072 while (cur_param > first_param) {
7073 cur_param--;
7074 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7075 /* let's see if this is the appsession parameter */
7076 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7077 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7078 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7079 /* Cool... it's the right one */
7080 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7081 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7082 if (value_len > 0) {
7083 manage_client_side_appsession(t, cur_param, value_len);
7084 }
7085 break;
7086 }
7087 next_param = cur_param;
7088 }
7089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007090#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007091 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007092 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094}
7095
Willy Tarreaub2513902006-12-17 14:52:38 +01007096/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007097 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007098 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007099 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007100 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007101 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007102 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007103 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007104 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007105int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007106{
7107 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007108 struct http_msg *msg = &txn->req;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007109 const char *uri = msg->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007110 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007111
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007112 if (!uri_auth)
7113 return 0;
7114
Cyril Bonté70be45d2010-10-12 00:14:35 +02007115 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007116 return 0;
7117
Willy Tarreau295a8372011-03-10 11:25:07 +01007118 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007119 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007122 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007123 return 0;
7124
Willy Tarreau3a215be2012-03-09 21:39:51 +01007125 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007126 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007127 return 0;
7128
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007129 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007130 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007131 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007132 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007133 break;
7134 }
7135 h++;
7136 }
7137
7138 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007139 h = uri + uri_auth->uri_len;
7140 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007141 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007142 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007143 break;
7144 }
7145 h++;
7146 }
7147 }
7148
Willy Tarreau3a215be2012-03-09 21:39:51 +01007149 h = uri + uri_auth->uri_len;
7150 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007151 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007152 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007153 break;
7154 }
7155 h++;
7156 }
7157
Willy Tarreau3a215be2012-03-09 21:39:51 +01007158 h = uri + uri_auth->uri_len;
7159 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007160 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007161 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007162 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007163 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007164 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7165 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7166 si->applet.ctx.stats.st_code = i;
7167 break;
7168 }
7169 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007170 break;
7171 }
7172 h++;
7173 }
7174
Willy Tarreau295a8372011-03-10 11:25:07 +01007175 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007176
Willy Tarreaub2513902006-12-17 14:52:38 +01007177 return 1;
7178}
7179
Willy Tarreau4076a152009-04-02 15:18:36 +02007180/*
7181 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007182 * By default it tries to report the error position as msg->err_pos. However if
7183 * this one is not set, it will then report msg->next, which is the last known
7184 * parsing point. The function is able to deal with wrapping buffers. It always
7185 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007186 */
7187void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007188 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007189 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007190{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007191 struct buffer *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007192 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007193
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007194 es->len = MIN(buf->i, sizeof(es->buf));
7195 len1 = buf->data + buf->size - buf->p;
7196 len1 = MIN(len1, es->len);
7197 len2 = es->len - len1; /* remaining data if buffer wraps */
7198
7199 memcpy(es->buf, buf->p, len1);
7200 if (len2)
7201 memcpy(es->buf + len1, buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007202
Willy Tarreau4076a152009-04-02 15:18:36 +02007203 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007204 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007205 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007206 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007207
Willy Tarreau4076a152009-04-02 15:18:36 +02007208 es->when = date; // user-visible date
7209 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007210 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007211 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007212 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007213 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007214 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007215 es->b_flags = buf->flags;
7216 es->s_flags = s->flags;
7217 es->t_flags = s->txn.flags;
7218 es->m_flags = msg->flags;
7219 es->b_out = buf->o;
7220 es->b_wrap = buf->data + buf->size - buf->p;
7221 es->b_tot = buf->total;
7222 es->m_clen = msg->chunk_len;
7223 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007224}
Willy Tarreaub2513902006-12-17 14:52:38 +01007225
Willy Tarreau294c4732011-12-16 21:35:50 +01007226/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7227 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7228 * performed over the whole headers. Otherwise it must contain a valid header
7229 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7230 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7231 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7232 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7233 * -1.
7234 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007235 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007236unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007237 struct hdr_idx *idx, int occ,
7238 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007239{
Willy Tarreau294c4732011-12-16 21:35:50 +01007240 struct hdr_ctx local_ctx;
7241 char *ptr_hist[MAX_HDR_HISTORY];
7242 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007243 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007244 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007245
Willy Tarreau294c4732011-12-16 21:35:50 +01007246 if (!ctx) {
7247 local_ctx.idx = 0;
7248 ctx = &local_ctx;
7249 }
7250
Willy Tarreaubce70882009-09-07 11:51:47 +02007251 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007252 /* search from the beginning */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007253 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007254 occ--;
7255 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007256 *vptr = ctx->line + ctx->val;
7257 *vlen = ctx->vlen;
7258 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007259 }
7260 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007261 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007262 }
7263
7264 /* negative occurrence, we scan all the list then walk back */
7265 if (-occ > MAX_HDR_HISTORY)
7266 return 0;
7267
Willy Tarreau294c4732011-12-16 21:35:50 +01007268 found = hist_ptr = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007269 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007270 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7271 len_hist[hist_ptr] = ctx->vlen;
7272 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007273 hist_ptr = 0;
7274 found++;
7275 }
7276 if (-occ > found)
7277 return 0;
7278 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7279 * find occurrence -occ, so we have to check [hist_ptr+occ].
7280 */
7281 hist_ptr += occ;
7282 if (hist_ptr >= MAX_HDR_HISTORY)
7283 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007284 *vptr = ptr_hist[hist_ptr];
7285 *vlen = len_hist[hist_ptr];
7286 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007287}
7288
Willy Tarreaubaaee002006-06-26 02:48:02 +02007289/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290 * Print a debug line with a header
7291 */
7292void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7293{
7294 int len, max;
7295 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007296 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007297 max = end - start;
David du Colombier7af46052012-05-16 14:16:48 +02007298 UBOUND(max, trashlen - len - 1);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007299 len += strlcpy2(trash + len, start, max + 1);
7300 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007301 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302}
7303
Willy Tarreau0937bc42009-12-22 15:03:09 +01007304/*
7305 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7306 * the required fields are properly allocated and that we only need to (re)init
7307 * them. This should be used before processing any new request.
7308 */
7309void http_init_txn(struct session *s)
7310{
7311 struct http_txn *txn = &s->txn;
7312 struct proxy *fe = s->fe;
7313
7314 txn->flags = 0;
7315 txn->status = -1;
7316
William Lallemand5f232402012-04-05 18:02:55 +02007317 global.req_count++;
7318
Willy Tarreauf64d1412010-10-07 20:06:11 +02007319 txn->cookie_first_date = 0;
7320 txn->cookie_last_date = 0;
7321
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007322 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007323 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007324 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007325 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007326 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007327 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007328 txn->req.chunk_len = 0LL;
7329 txn->req.body_len = 0LL;
7330 txn->rsp.chunk_len = 0LL;
7331 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007332 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7333 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007334 txn->req.buf = s->req;
7335 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007336
7337 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007338
7339 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7340 if (fe->options2 & PR_O2_REQBUG_OK)
7341 txn->req.err_pos = -1; /* let buggy requests pass */
7342
Willy Tarreau46023632010-01-07 22:51:47 +01007343 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007344 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7345
Willy Tarreau46023632010-01-07 22:51:47 +01007346 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007347 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7348
7349 if (txn->hdr_idx.v)
7350 hdr_idx_init(&txn->hdr_idx);
7351}
7352
7353/* to be used at the end of a transaction */
7354void http_end_txn(struct session *s)
7355{
7356 struct http_txn *txn = &s->txn;
7357
7358 /* these ones will have been dynamically allocated */
7359 pool_free2(pool2_requri, txn->uri);
7360 pool_free2(pool2_capture, txn->cli_cookie);
7361 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007362 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007363 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007364
William Lallemanda73203e2012-03-12 12:48:57 +01007365 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007366 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007367 txn->uri = NULL;
7368 txn->srv_cookie = NULL;
7369 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007370
7371 if (txn->req.cap) {
7372 struct cap_hdr *h;
7373 for (h = s->fe->req_cap; h; h = h->next)
7374 pool_free2(h->pool, txn->req.cap[h->index]);
7375 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7376 }
7377
7378 if (txn->rsp.cap) {
7379 struct cap_hdr *h;
7380 for (h = s->fe->rsp_cap; h; h = h->next)
7381 pool_free2(h->pool, txn->rsp.cap[h->index]);
7382 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7383 }
7384
Willy Tarreau0937bc42009-12-22 15:03:09 +01007385}
7386
7387/* to be used at the end of a transaction to prepare a new one */
7388void http_reset_txn(struct session *s)
7389{
7390 http_end_txn(s);
7391 http_init_txn(s);
7392
7393 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007394 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007395 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007396 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007397 /* re-init store persistence */
7398 s->store_count = 0;
7399
Willy Tarreau0937bc42009-12-22 15:03:09 +01007400 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007401
7402 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7403
Willy Tarreau739cfba2010-01-25 23:11:14 +01007404 /* We must trim any excess data from the response buffer, because we
7405 * may have blocked an invalid response from a server that we don't
7406 * want to accidentely forward once we disable the analysers, nor do
7407 * we want those data to come along with next response. A typical
7408 * example of such data would be from a buggy server responding to
7409 * a HEAD with some data, or sending more than the advertised
7410 * content-length.
7411 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007412 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007413 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007414
Willy Tarreau0937bc42009-12-22 15:03:09 +01007415 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007416 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007417
Willy Tarreaud04e8582010-05-31 12:31:35 +02007418 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007419 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007420
7421 s->req->rex = TICK_ETERNITY;
7422 s->req->wex = TICK_ETERNITY;
7423 s->req->analyse_exp = TICK_ETERNITY;
7424 s->rep->rex = TICK_ETERNITY;
7425 s->rep->wex = TICK_ETERNITY;
7426 s->rep->analyse_exp = TICK_ETERNITY;
7427}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007428
Willy Tarreauff011f22011-01-06 17:51:27 +01007429void free_http_req_rules(struct list *r) {
7430 struct http_req_rule *tr, *pr;
7431
7432 list_for_each_entry_safe(pr, tr, r, list) {
7433 LIST_DEL(&pr->list);
7434 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7435 free(pr->http_auth.realm);
7436
7437 free(pr);
7438 }
7439}
7440
7441struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7442{
7443 struct http_req_rule *rule;
7444 int cur_arg;
7445
7446 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7447 if (!rule) {
7448 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7449 return NULL;
7450 }
7451
7452 if (!*args[0]) {
7453 goto req_error_parsing;
7454 } else if (!strcmp(args[0], "allow")) {
7455 rule->action = HTTP_REQ_ACT_ALLOW;
7456 cur_arg = 1;
7457 } else if (!strcmp(args[0], "deny")) {
7458 rule->action = HTTP_REQ_ACT_DENY;
7459 cur_arg = 1;
7460 } else if (!strcmp(args[0], "auth")) {
7461 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7462 cur_arg = 1;
7463
7464 while(*args[cur_arg]) {
7465 if (!strcmp(args[cur_arg], "realm")) {
7466 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7467 cur_arg+=2;
7468 continue;
7469 } else
7470 break;
7471 }
7472 } else {
7473req_error_parsing:
7474 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7475 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7476 return NULL;
7477 }
7478
7479 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7480 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007481 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007482
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007483 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7484 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7485 file, linenum, args[0], errmsg);
7486 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007487 return NULL;
7488 }
7489 rule->cond = cond;
7490 }
7491 else if (*args[cur_arg]) {
7492 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7493 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7494 file, linenum, args[0], args[cur_arg]);
7495 return NULL;
7496 }
7497
7498 return rule;
7499}
7500
Willy Tarreau8797c062007-05-07 00:55:35 +02007501/************************************************************************/
7502/* The code below is dedicated to ACL parsing and matching */
7503/************************************************************************/
7504
7505
Willy Tarreau14174bc2012-04-16 14:34:04 +02007506/* This function ensures that the prerequisites for an L7 fetch are ready,
7507 * which means that a request or response is ready. If some data is missing,
7508 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007509 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7510 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007511 *
7512 * The function returns :
7513 * 0 if some data is missing or if the requested data cannot be fetched
7514 * -1 if it is certain that we'll never have any HTTP message there
7515 * 1 if an HTTP message is ready
7516 */
7517static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007518acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007519 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007520{
7521 struct http_txn *txn = l7;
7522 struct http_msg *msg = &txn->req;
7523
7524 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7525 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7526 */
7527
7528 if (unlikely(!s || !txn))
7529 return 0;
7530
7531 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007532 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007533
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007534 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007535 if (unlikely(!s->req))
7536 return 0;
7537
7538 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
7539 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007540 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007541 return -1;
7542 }
7543
7544 /* Try to decode HTTP request */
7545 if (likely(msg->next < s->req->i))
7546 http_msg_analyzer(msg, &txn->hdr_idx);
7547
7548 /* Still no valid request ? */
7549 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7550 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007551 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007552 return -1;
7553 }
7554 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007555 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007556 return 0;
7557 }
7558
7559 /* OK we just got a valid HTTP request. We have some minor
7560 * preparation to perform so that further checks can rely
7561 * on HTTP tests.
7562 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007563 txn->meth = find_http_meth(msg->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007564 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7565 s->flags |= SN_REDIRECTABLE;
7566
7567 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007568 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007569 return -1;
7570 }
7571 }
7572
Willy Tarreau24e32d82012-04-23 23:55:44 +02007573 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007574 return 0; /* data might have moved and indexes changed */
7575
7576 /* otherwise everything's ready for the request */
7577 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007578 else {
7579 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007580 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7581 return 0;
7582 }
7583
7584 /* everything's OK */
7585 return 1;
7586}
Willy Tarreau8797c062007-05-07 00:55:35 +02007587
Willy Tarreauc0239e02012-04-16 14:42:55 +02007588#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007589 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 +02007590
Willy Tarreau24e32d82012-04-23 23:55:44 +02007591#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007592 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 +02007593
Willy Tarreau8797c062007-05-07 00:55:35 +02007594
7595/* 1. Check on METHOD
7596 * We use the pre-parsed method if it is known, and store its number as an
7597 * integer. If it is unknown, we use the pointer and the length.
7598 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007599static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007600{
7601 int len, meth;
7602
Willy Tarreauae8b7962007-06-09 23:10:04 +02007603 len = strlen(*text);
7604 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007605
7606 pattern->val.i = meth;
7607 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007608 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007609 if (!pattern->ptr.str) {
7610 if (err)
7611 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007612 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007613 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007614 pattern->len = len;
7615 }
7616 return 1;
7617}
7618
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007619/* This function fetches the method of current HTTP request and stores
7620 * it in the global pattern struct as a chunk. There are two possibilities :
7621 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7622 * in <len> and <ptr> is NULL ;
7623 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7624 * <len> to its length.
7625 * This is intended to be used with acl_match_meth() only.
7626 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007627static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007628acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007629 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007630{
7631 int meth;
7632 struct http_txn *txn = l7;
7633
Willy Tarreau24e32d82012-04-23 23:55:44 +02007634 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007635
Willy Tarreau8797c062007-05-07 00:55:35 +02007636 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007637 smp->type = SMP_T_UINT;
7638 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007639 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007640 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7641 /* ensure the indexes are not affected */
7642 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007643 smp->type = SMP_T_CSTR;
7644 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007645 smp->data.str.str = txn->req.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007646 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007647 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007648 return 1;
7649}
7650
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007651/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007652static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007653{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007654 int icase;
7655
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007656
Willy Tarreauf853c462012-04-23 18:53:56 +02007657 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007658 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007659 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007660 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007661 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007662 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007663
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007664 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7665 if (pattern->val.i != HTTP_METH_OTHER)
7666 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007667
7668 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007669 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007670 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007671
7672 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007673 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7674 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007675 return ACL_PAT_FAIL;
7676 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007677}
7678
7679/* 2. Check on Request/Status Version
7680 * We simply compare strings here.
7681 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007682static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007683{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007684 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007685 if (!pattern->ptr.str) {
7686 if (err)
7687 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007688 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007689 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007690 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007691 return 1;
7692}
7693
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007694static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007695acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007696 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007697{
7698 struct http_txn *txn = l7;
7699 char *ptr;
7700 int len;
7701
Willy Tarreauc0239e02012-04-16 14:42:55 +02007702 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007703
Willy Tarreau8797c062007-05-07 00:55:35 +02007704 len = txn->req.sl.rq.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007705 ptr = txn->req.buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007706
7707 while ((len-- > 0) && (*ptr++ != '/'));
7708 if (len <= 0)
7709 return 0;
7710
Willy Tarreauf853c462012-04-23 18:53:56 +02007711 smp->type = SMP_T_CSTR;
7712 smp->data.str.str = ptr;
7713 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007714
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007715 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716 return 1;
7717}
7718
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007719static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007720acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007721 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007722{
7723 struct http_txn *txn = l7;
7724 char *ptr;
7725 int len;
7726
Willy Tarreauc0239e02012-04-16 14:42:55 +02007727 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007728
Willy Tarreau8797c062007-05-07 00:55:35 +02007729 len = txn->rsp.sl.st.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007730 ptr = txn->rsp.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007731
7732 while ((len-- > 0) && (*ptr++ != '/'));
7733 if (len <= 0)
7734 return 0;
7735
Willy Tarreauf853c462012-04-23 18:53:56 +02007736 smp->type = SMP_T_CSTR;
7737 smp->data.str.str = ptr;
7738 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007739
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007740 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007741 return 1;
7742}
7743
7744/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007745static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007746acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007747 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007748{
7749 struct http_txn *txn = l7;
7750 char *ptr;
7751 int len;
7752
Willy Tarreauc0239e02012-04-16 14:42:55 +02007753 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007754
Willy Tarreau8797c062007-05-07 00:55:35 +02007755 len = txn->rsp.sl.st.c_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007756 ptr = txn->rsp.buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007757
Willy Tarreauf853c462012-04-23 18:53:56 +02007758 smp->type = SMP_T_UINT;
7759 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007760 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007761 return 1;
7762}
7763
7764/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007765static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007766acl_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007767 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007768{
7769 struct http_txn *txn = l7;
7770
Willy Tarreauc0239e02012-04-16 14:42:55 +02007771 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007772
Willy Tarreauf853c462012-04-23 18:53:56 +02007773 smp->type = SMP_T_CSTR;
7774 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007775 smp->data.str.str = txn->req.buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007776 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007777 return 1;
7778}
7779
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007780static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007781acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007782 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007783{
7784 struct http_txn *txn = l7;
7785
Willy Tarreauc0239e02012-04-16 14:42:55 +02007786 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007787
7788 /* Parse HTTP request */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007789 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 +01007790 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7791 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007792 smp->type = SMP_T_IPV4;
7793 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007794
7795 /*
7796 * If we are parsing url in frontend space, we prepare backend stage
7797 * to not parse again the same url ! optimization lazyness...
7798 */
7799 if (px->options & PR_O_HTTP_PROXY)
7800 l4->flags |= SN_ADDR_SET;
7801
Willy Tarreau37406352012-04-23 16:16:37 +02007802 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007803 return 1;
7804}
7805
7806static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007807acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007808 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007809{
7810 struct http_txn *txn = l7;
7811
Willy Tarreauc0239e02012-04-16 14:42:55 +02007812 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007813
7814 /* Same optimization as url_ip */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007815 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 +02007816 smp->type = SMP_T_UINT;
7817 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007818
7819 if (px->options & PR_O_HTTP_PROXY)
7820 l4->flags |= SN_ADDR_SET;
7821
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007822 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007823 return 1;
7824}
7825
Willy Tarreau185b5c42012-04-26 15:11:51 +02007826/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7827 * Accepts an optional argument of type string containing the header field name,
7828 * and an optional argument of type signed or unsigned integer to request an
7829 * explicit occurrence of the header. Note that in the event of a missing name,
7830 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007831 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007832static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007833smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007834 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007835{
7836 struct http_txn *txn = l7;
7837 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007838 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007839 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007840 int occ = 0;
7841 const char *name_str = NULL;
7842 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007843
Willy Tarreau185b5c42012-04-26 15:11:51 +02007844 if (args) {
7845 if (args[0].type != ARGT_STR)
7846 return 0;
7847 name_str = args[0].data.str.str;
7848 name_len = args[0].data.str.len;
7849
7850 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7851 occ = args[1].data.uint;
7852 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007853
Willy Tarreaue333ec92012-04-16 16:26:40 +02007854 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007855
Willy Tarreau185b5c42012-04-26 15:11:51 +02007856 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007857 /* search for header from the beginning */
7858 ctx->idx = 0;
7859
Willy Tarreau185b5c42012-04-26 15:11:51 +02007860 if (!occ && !(opt & SMP_OPT_ITERATE))
7861 /* no explicit occurrence and single fetch => last header by default */
7862 occ = -1;
7863
7864 if (!occ)
7865 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007866 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007867
Willy Tarreau185b5c42012-04-26 15:11:51 +02007868 smp->type = SMP_T_CSTR;
7869 smp->flags |= SMP_F_VOL_HDR;
7870 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 +02007871 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007872
Willy Tarreau37406352012-04-23 16:16:37 +02007873 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007874 return 0;
7875}
7876
Willy Tarreauc11416f2007-06-17 16:58:38 +02007877/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007878 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007879 */
7880static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007881smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007882 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007883{
7884 struct http_txn *txn = l7;
7885 struct hdr_idx *idx = &txn->hdr_idx;
7886 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007887 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007888 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007889
Willy Tarreau24e32d82012-04-23 23:55:44 +02007890 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007891 return 0;
7892
Willy Tarreaue333ec92012-04-16 16:26:40 +02007893 CHECK_HTTP_MESSAGE_FIRST();
7894
Willy Tarreau33a7e692007-06-10 19:45:56 +02007895 ctx.idx = 0;
7896 cnt = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007897 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007898 cnt++;
7899
Willy Tarreauf853c462012-04-23 18:53:56 +02007900 smp->type = SMP_T_UINT;
7901 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007902 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007903 return 1;
7904}
7905
Willy Tarreau185b5c42012-04-26 15:11:51 +02007906/* Fetch an HTTP header's integer value. The integer value is returned. It
7907 * takes a mandatory argument of type string and an optional one of type int
7908 * to designate a specific occurrence. It returns an unsigned integer, which
7909 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007910 */
7911static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007912smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007913 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007914{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007915 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007916
Willy Tarreauf853c462012-04-23 18:53:56 +02007917 if (ret > 0) {
7918 smp->type = SMP_T_UINT;
7919 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7920 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007921
Willy Tarreaud53e2422012-04-16 17:21:11 +02007922 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007923}
7924
Willy Tarreau185b5c42012-04-26 15:11:51 +02007925/* Fetch an HTTP header's integer value. The integer value is returned. It
7926 * takes a mandatory argument of type string and an optional one of type int
7927 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02007928 */
7929static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007930smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007931 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02007932{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007933 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007934
Willy Tarreau185b5c42012-04-26 15:11:51 +02007935 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02007936 smp->type = SMP_T_IPV4;
7937 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02007938 break;
7939 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007940 if (!(smp->flags & SMP_F_NOT_LAST))
7941 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02007942 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02007943 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02007944}
7945
Willy Tarreau737b0c12007-06-10 21:28:46 +02007946/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7947 * the first '/' after the possible hostname, and ends before the possible '?'.
7948 */
7949static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007950acl_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007951 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007952{
7953 struct http_txn *txn = l7;
7954 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007955
Willy Tarreauc0239e02012-04-16 14:42:55 +02007956 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007957
Willy Tarreau09d1e252012-05-18 22:36:34 +02007958 end = txn->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007959 ptr = http_get_path(txn);
7960 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007961 return 0;
7962
7963 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02007964 smp->type = SMP_T_CSTR;
7965 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007966
7967 while (ptr < end && *ptr != '?')
7968 ptr++;
7969
Willy Tarreauf853c462012-04-23 18:53:56 +02007970 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02007971 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007972 return 1;
7973}
7974
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007975static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007976acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007977 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007978{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007979 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7980 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7981 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007982
Willy Tarreau24e32d82012-04-23 23:55:44 +02007983 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007984
Willy Tarreauf853c462012-04-23 18:53:56 +02007985 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02007986 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007987 return 1;
7988}
7989
Willy Tarreau7f18e522010-10-22 20:04:13 +02007990/* return a valid test if the current request is the first one on the connection */
7991static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007992acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007993 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02007994{
7995 if (!s)
7996 return 0;
7997
Willy Tarreauf853c462012-04-23 18:53:56 +02007998 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02007999 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008000 return 1;
8001}
8002
Willy Tarreau34db1082012-04-19 17:16:54 +02008003/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008004static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008005acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008006 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008007{
8008
Willy Tarreau24e32d82012-04-23 23:55:44 +02008009 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008010 return 0;
8011
Willy Tarreauc0239e02012-04-16 14:42:55 +02008012 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008013
Willy Tarreauc0239e02012-04-16 14:42:55 +02008014 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008015 return 0;
8016
Willy Tarreauf853c462012-04-23 18:53:56 +02008017 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008018 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 +01008019 return 1;
8020}
Willy Tarreau8797c062007-05-07 00:55:35 +02008021
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008022/* Accepts exactly 1 argument of type userlist */
8023static int
8024acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8025 const struct arg *args, struct sample *smp)
8026{
8027
8028 if (!args || args->type != ARGT_USR)
8029 return 0;
8030
8031 CHECK_HTTP_MESSAGE_FIRST();
8032
8033 if (!get_http_auth(l4))
8034 return 0;
8035
8036 /* acl_match_auth() will need several information at once */
8037 smp->ctx.a[0] = args->data.usr; /* user list */
8038 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8039 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8040
8041 /* if the user does not belong to the userlist or has a wrong password,
8042 * report that it unconditionally does not match. Otherwise we return
8043 * a non-zero integer which will be ignored anyway since all the params
8044 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8045 */
8046 smp->type = SMP_T_BOOL;
8047 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8048 if (smp->data.uint)
8049 smp->type = SMP_T_UINT;
8050
8051 return 1;
8052}
8053
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008054/* Try to find the next occurrence of a cookie name in a cookie header value.
8055 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8056 * the cookie value is returned into *value and *value_l, and the function
8057 * returns a pointer to the next pointer to search from if the value was found.
8058 * Otherwise if the cookie was not found, NULL is returned and neither value
8059 * nor value_l are touched. The input <hdr> string should first point to the
8060 * header's value, and the <hdr_end> pointer must point to the first character
8061 * not part of the value. <list> must be non-zero if value may represent a list
8062 * of values (cookie headers). This makes it faster to abort parsing when no
8063 * list is expected.
8064 */
8065static char *
8066extract_cookie_value(char *hdr, const char *hdr_end,
8067 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008068 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008069{
8070 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8071 char *next;
8072
8073 /* we search at least a cookie name followed by an equal, and more
8074 * generally something like this :
8075 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8076 */
8077 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8078 /* Iterate through all cookies on this line */
8079
8080 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8081 att_beg++;
8082
8083 /* find att_end : this is the first character after the last non
8084 * space before the equal. It may be equal to hdr_end.
8085 */
8086 equal = att_end = att_beg;
8087
8088 while (equal < hdr_end) {
8089 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8090 break;
8091 if (http_is_spht[(unsigned char)*equal++])
8092 continue;
8093 att_end = equal;
8094 }
8095
8096 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8097 * is between <att_beg> and <equal>, both may be identical.
8098 */
8099
8100 /* look for end of cookie if there is an equal sign */
8101 if (equal < hdr_end && *equal == '=') {
8102 /* look for the beginning of the value */
8103 val_beg = equal + 1;
8104 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8105 val_beg++;
8106
8107 /* find the end of the value, respecting quotes */
8108 next = find_cookie_value_end(val_beg, hdr_end);
8109
8110 /* make val_end point to the first white space or delimitor after the value */
8111 val_end = next;
8112 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8113 val_end--;
8114 } else {
8115 val_beg = val_end = next = equal;
8116 }
8117
8118 /* We have nothing to do with attributes beginning with '$'. However,
8119 * they will automatically be removed if a header before them is removed,
8120 * since they're supposed to be linked together.
8121 */
8122 if (*att_beg == '$')
8123 continue;
8124
8125 /* Ignore cookies with no equal sign */
8126 if (equal == next)
8127 continue;
8128
8129 /* Now we have the cookie name between att_beg and att_end, and
8130 * its value between val_beg and val_end.
8131 */
8132
8133 if (att_end - att_beg == cookie_name_l &&
8134 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8135 /* let's return this value and indicate where to go on from */
8136 *value = val_beg;
8137 *value_l = val_end - val_beg;
8138 return next + 1;
8139 }
8140
8141 /* Set-Cookie headers only have the name in the first attr=value part */
8142 if (!list)
8143 break;
8144 }
8145
8146 return NULL;
8147}
8148
Willy Tarreaue333ec92012-04-16 16:26:40 +02008149/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008150 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8151 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008152 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008153 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008154 * Accepts exactly 1 argument of type string. If the input options indicate
8155 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008156 */
8157static int
Willy Tarreau51539362012-05-08 12:46:28 +02008158smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8159 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008160{
8161 struct http_txn *txn = l7;
8162 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008163 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008164 const struct http_msg *msg;
8165 const char *hdr_name;
8166 int hdr_name_len;
8167 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008168 int occ = 0;
8169 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008170
Willy Tarreau24e32d82012-04-23 23:55:44 +02008171 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008172 return 0;
8173
Willy Tarreaue333ec92012-04-16 16:26:40 +02008174 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008175
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008176 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008177 msg = &txn->req;
8178 hdr_name = "Cookie";
8179 hdr_name_len = 6;
8180 } else {
8181 msg = &txn->rsp;
8182 hdr_name = "Set-Cookie";
8183 hdr_name_len = 10;
8184 }
8185
Willy Tarreau28376d62012-04-26 21:26:10 +02008186 if (!occ && !(opt & SMP_OPT_ITERATE))
8187 /* no explicit occurrence and single fetch => last cookie by default */
8188 occ = -1;
8189
8190 /* OK so basically here, either we want only one value and it's the
8191 * last one, or we want to iterate over all of them and we fetch the
8192 * next one.
8193 */
8194
Willy Tarreau09d1e252012-05-18 22:36:34 +02008195 sol = msg->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008196 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008197 /* search for the header from the beginning, we must first initialize
8198 * the search parameters.
8199 */
Willy Tarreau37406352012-04-23 16:16:37 +02008200 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008201 ctx->idx = 0;
8202 }
8203
Willy Tarreau28376d62012-04-26 21:26:10 +02008204 smp->flags |= SMP_F_VOL_HDR;
8205
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008206 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008207 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8208 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008209 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8210 goto out;
8211
Willy Tarreau24e32d82012-04-23 23:55:44 +02008212 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008213 continue;
8214
Willy Tarreau37406352012-04-23 16:16:37 +02008215 smp->ctx.a[0] = ctx->line + ctx->val;
8216 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008217 }
8218
Willy Tarreauf853c462012-04-23 18:53:56 +02008219 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008220 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008221 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008222 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008223 &smp->data.str.str,
8224 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008225 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008226 found = 1;
8227 if (occ >= 0) {
8228 /* one value was returned into smp->data.str.{str,len} */
8229 smp->flags |= SMP_F_NOT_LAST;
8230 return 1;
8231 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008232 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008233 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008234 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008235 /* all cookie headers and values were scanned. If we're looking for the
8236 * last occurrence, we may return it now.
8237 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008238 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008239 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008240 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008241}
8242
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008243/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008244 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008245 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008246 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008247 */
8248static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008249acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008250 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008251{
8252 struct http_txn *txn = l7;
8253 struct hdr_idx *idx = &txn->hdr_idx;
8254 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008255 const struct http_msg *msg;
8256 const char *hdr_name;
8257 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008258 int cnt;
8259 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008260 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008261
Willy Tarreau24e32d82012-04-23 23:55:44 +02008262 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008263 return 0;
8264
Willy Tarreaue333ec92012-04-16 16:26:40 +02008265 CHECK_HTTP_MESSAGE_FIRST();
8266
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008267 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008268 msg = &txn->req;
8269 hdr_name = "Cookie";
8270 hdr_name_len = 6;
8271 } else {
8272 msg = &txn->rsp;
8273 hdr_name = "Set-Cookie";
8274 hdr_name_len = 10;
8275 }
8276
Willy Tarreau09d1e252012-05-18 22:36:34 +02008277 sol = msg->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008278 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008279 ctx.idx = 0;
8280 cnt = 0;
8281
8282 while (1) {
8283 /* Note: val_beg == NULL every time we need to fetch a new header */
8284 if (!val_beg) {
8285 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8286 break;
8287
Willy Tarreau24e32d82012-04-23 23:55:44 +02008288 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008289 continue;
8290
8291 val_beg = ctx.line + ctx.val;
8292 val_end = val_beg + ctx.vlen;
8293 }
8294
Willy Tarreauf853c462012-04-23 18:53:56 +02008295 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008296 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008297 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008298 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008299 &smp->data.str.str,
8300 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008301 cnt++;
8302 }
8303 }
8304
Willy Tarreauf853c462012-04-23 18:53:56 +02008305 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008306 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008307 return 1;
8308}
8309
Willy Tarreau51539362012-05-08 12:46:28 +02008310/* Fetch an cookie's integer value. The integer value is returned. It
8311 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8312 */
8313static int
8314smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8315 const struct arg *args, struct sample *smp)
8316{
8317 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8318
8319 if (ret > 0) {
8320 smp->type = SMP_T_UINT;
8321 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8322 }
8323
8324 return ret;
8325}
8326
Willy Tarreau8797c062007-05-07 00:55:35 +02008327/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008328/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008329/************************************************************************/
8330
David Cournapeau16023ee2010-12-23 20:55:41 +09008331/*
8332 * Given a path string and its length, find the position of beginning of the
8333 * query string. Returns NULL if no query string is found in the path.
8334 *
8335 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8336 *
8337 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8338 */
8339static inline char *find_query_string(char *path, size_t path_l)
8340{
8341 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008342
David Cournapeau16023ee2010-12-23 20:55:41 +09008343 p = memchr(path, '?', path_l);
8344 return p ? p + 1 : NULL;
8345}
8346
8347static inline int is_param_delimiter(char c)
8348{
8349 return c == '&' || c == ';';
8350}
8351
8352/*
8353 * Given a url parameter, find the starting position of the first occurence,
8354 * or NULL if the parameter is not found.
8355 *
8356 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8357 * the function will return query_string+8.
8358 */
8359static char*
8360find_url_param_pos(char* query_string, size_t query_string_l,
8361 char* url_param_name, size_t url_param_name_l)
8362{
8363 char *pos, *last;
8364
8365 pos = query_string;
8366 last = query_string + query_string_l - url_param_name_l - 1;
8367
8368 while (pos <= last) {
8369 if (pos[url_param_name_l] == '=') {
8370 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8371 return pos;
8372 pos += url_param_name_l + 1;
8373 }
8374 while (pos <= last && !is_param_delimiter(*pos))
8375 pos++;
8376 pos++;
8377 }
8378 return NULL;
8379}
8380
8381/*
8382 * Given a url parameter name, returns its value and size into *value and
8383 * *value_l respectively, and returns non-zero. If the parameter is not found,
8384 * zero is returned and value/value_l are not touched.
8385 */
8386static int
8387find_url_param_value(char* path, size_t path_l,
8388 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008389 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008390{
8391 char *query_string, *qs_end;
8392 char *arg_start;
8393 char *value_start, *value_end;
8394
8395 query_string = find_query_string(path, path_l);
8396 if (!query_string)
8397 return 0;
8398
8399 qs_end = path + path_l;
8400 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8401 url_param_name, url_param_name_l);
8402 if (!arg_start)
8403 return 0;
8404
8405 value_start = arg_start + url_param_name_l + 1;
8406 value_end = value_start;
8407
8408 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8409 value_end++;
8410
8411 *value = value_start;
8412 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008413 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008414}
8415
8416static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008417smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8418 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008419{
8420 struct http_txn *txn = l7;
8421 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008422
8423 if (!args || args->type != ARGT_STR)
8424 return 0;
8425
8426 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008427
Willy Tarreau09d1e252012-05-18 22:36:34 +02008428 if (!find_url_param_value(msg->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008429 args->data.str.str, args->data.str.len,
8430 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008431 return 0;
8432
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008433 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008434 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008435 return 1;
8436}
8437
Willy Tarreau185b5c42012-04-26 15:11:51 +02008438/* This function is used to validate the arguments passed to any "hdr" fetch
8439 * keyword. These keywords support an optional positive or negative occurrence
8440 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8441 * is assumed that the types are already the correct ones. Returns 0 on error,
8442 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8443 * error message in case of error, that the caller is responsible for freeing.
8444 * The initial location must either be freeable or NULL.
8445 */
8446static int val_hdr(struct arg *arg, char **err_msg)
8447{
8448 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8449 if (err_msg)
8450 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8451 return 0;
8452 }
8453 return 1;
8454}
8455
Willy Tarreau4a568972010-05-12 08:08:50 +02008456/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008457/* All supported ACL keywords must be declared here. */
8458/************************************************************************/
8459
8460/* Note: must not be declared <const> as its list will be overwritten.
8461 * Please take care of keeping this list alphabetically sorted.
8462 */
8463static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau51539362012-05-08 12:46:28 +02008464 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8465 { "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 +02008466 { "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 +02008467 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8468 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8469 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8470 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8471 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8472 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8473 { "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 +02008474
Willy Tarreau185b5c42012-04-26 15:11:51 +02008475 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8476 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8477 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8478 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8479 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8480 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8481 { "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 },
8482 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8483 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8484 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8485 { "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 +02008486
8487 { "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 +02008488 { "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 +02008489 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8490
8491 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8492
8493 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8494 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8495 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8496 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8497 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8498 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8499 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8500 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8501
8502 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8503 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8504 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8505
Willy Tarreau51539362012-05-08 12:46:28 +02008506 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8507 { "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 +02008508 { "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 +02008509 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8510 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8511 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8512 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8513 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8514 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8515 { "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 +02008516
Willy Tarreau185b5c42012-04-26 15:11:51 +02008517 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8518 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8519 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8520 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8521 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8522 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8523 { "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 },
8524 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8525 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8526 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8527 { "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 +02008528
8529 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8530
8531 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8532 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8533 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8534 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8535 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8536 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8537 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8538 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8539 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8540 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8541
8542 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8543 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8544 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8545 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8546 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8547 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8548 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8549 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8550 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8551
8552 { NULL, NULL, NULL, NULL },
8553}};
8554
8555/************************************************************************/
8556/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008557/************************************************************************/
8558/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008559static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008560 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008561 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008562 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8563 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008564 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008565}};
8566
Willy Tarreau8797c062007-05-07 00:55:35 +02008567
8568__attribute__((constructor))
8569static void __http_protocol_init(void)
8570{
8571 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008572 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008573}
8574
8575
Willy Tarreau58f10d72006-12-04 02:26:12 +01008576/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008577 * Local variables:
8578 * c-indent-level: 8
8579 * c-basic-offset: 8
8580 * End:
8581 */