blob: 5d522ab42e86b051dc884b6406e158c8798e59df [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 Tarreau15de77e2010-01-02 21:59:16 +01001276 * fields. Note that msg->som and msg->sol will be initialized after completing
1277 * the first state, so that none of the msg pointers has to be initialized
1278 * prior to the 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 Tarreau89fa7062012-03-02 16:13:16 +01001306 char *beg = buf->p;
1307
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001309 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001311 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001312 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001314 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 hdr_idx_init(idx);
1316 state = HTTP_MSG_RPVER;
1317 goto http_msg_rpver;
1318 }
1319
1320 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1321 goto http_msg_invalid;
1322
1323 if (unlikely(*ptr == '\n'))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1326 /* stop here */
1327
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 EXPECT_LF_HERE(ptr, http_msg_invalid);
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1332 /* stop here */
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 case HTTP_MSG_RPVER_SP:
1337 case HTTP_MSG_RPCODE:
1338 case HTTP_MSG_RPCODE_SP:
1339 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001340 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001341 state, ptr, end,
1342 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 if (unlikely(!ptr))
1344 return;
1345
1346 /* we have a full response and we know that we have either a CR
1347 * or an LF at <ptr>.
1348 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1350
Willy Tarreau3a215be2012-03-09 21:39:51 +01001351 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (likely(*ptr == '\r'))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1354 goto http_msg_rpline_end;
1355
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001357 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 /* msg->sol must point to the first of CR or LF. */
1359 EXPECT_LF_HERE(ptr, http_msg_invalid);
1360 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1361 /* stop here */
1362
1363 /*
1364 * Second, states that are specific to the request only
1365 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001367 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 /* we have a start of message, but we have to check
1370 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001371 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001373 char *beg = buf->p;
1374
Willy Tarreau15de77e2010-01-02 21:59:16 +01001375 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001376 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001377 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001378 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001379 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001381 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001382 /* we will need this when keep-alive will be supported
1383 hdr_idx_init(idx);
1384 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 state = HTTP_MSG_RQMETH;
1386 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1390 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (unlikely(*ptr == '\n'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH_SP:
1406 case HTTP_MSG_RQURI:
1407 case HTTP_MSG_RQURI_SP:
1408 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001409 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001410 state, ptr, end,
1411 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (unlikely(!ptr))
1413 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 /* we have a full request and we know that we have either a CR
1416 * or an LF at <ptr>.
1417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreau3a215be2012-03-09 21:39:51 +01001420 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* check for HTTP/0.9 request : no version information available.
1428 * msg->sol must point to the first of CR or LF.
1429 */
1430 if (unlikely(msg->sl.rq.v_l == 0))
1431 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 EXPECT_LF_HERE(ptr, http_msg_invalid);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 /*
1438 * Common states below
1439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001442 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_CRLF(*ptr))) {
1444 goto http_msg_hdr_name;
1445 }
1446
1447 if (likely(*ptr == '\r'))
1448 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1449 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001452 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* assumes msg->sol points to the first char */
1454 if (likely(HTTP_IS_TOKEN(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001457 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001464 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001471 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001500 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001505 /* assumes msg->sol points to the first char, and msg->sov
1506 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
1508 if (likely(!HTTP_IS_CRLF(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau12e48b32012-03-05 16:57:34 +01001511 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* Note: we could also copy eol into ->eoh so that we have the
1513 * real header end in case it ends with lots of LWS, but is this
1514 * really needed ?
1515 */
1516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1518 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1528 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001529 for (; buf->p + msg->eol < ptr; msg->eol++)
1530 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_hdr_val;
1532 }
1533 http_msg_complete_header:
1534 /*
1535 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001536 * Assumes msg->sol points to the first char, msg->sov points
1537 * to the first character of the value and msg->eol to the
1538 * first CR or LF so we know how the line ends. We insert last
1539 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001541 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 idx, idx->tail) < 0))
1543 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001544
Willy Tarreau3a215be2012-03-09 21:39:51 +01001545 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (likely(!HTTP_IS_CRLF(*ptr))) {
1547 goto http_msg_hdr_name;
1548 }
1549
1550 if (likely(*ptr == '\r'))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1552 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /* Assumes msg->sol points to the first of either CR or LF */
1557 EXPECT_LF_HERE(ptr, http_msg_invalid);
1558 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001559 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001560 msg->eoh = msg->sol;
1561 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001562 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001564
1565 case HTTP_MSG_ERROR:
1566 /* this may only happen if we call http_msg_analyser() twice with an error */
1567 break;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569#ifdef DEBUG_FULL
1570 default:
1571 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1572 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
1575 http_msg_ood:
1576 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001578 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 http_msg_invalid:
1582 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
1586}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001587
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001588/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1589 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1590 * nothing is done and 1 is returned.
1591 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593{
1594 int delta;
1595 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau09d1e252012-05-18 22:36:34 +02001601 cur_end = msg->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001606 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001614 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 HTTP_MSG_RQMETH,
Willy Tarreau09d1e252012-05-18 22:36:34 +02001616 msg->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001629 * and "keep-alive" values. If we already know that some headers may safely
1630 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001631 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1632 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1633 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1634 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1635 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001636 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001638void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001639{
Willy Tarreau5b154472009-12-21 20:11:07 +01001640 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001641 const char *hdr_val = "Connection";
1642 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001643
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001644 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 return;
1646
Willy Tarreau88d349d2010-01-25 12:15:43 +01001647 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1648 hdr_val = "Proxy-Connection";
1649 hdr_len = 16;
1650 }
1651
Willy Tarreau5b154472009-12-21 20:11:07 +01001652 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001653 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001654 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1656 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001657 if (to_del & 2)
1658 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 else
1660 txn->flags |= TX_CON_KAL_SET;
1661 }
1662 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1663 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001664 if (to_del & 1)
1665 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001666 else
1667 txn->flags |= TX_CON_CLO_SET;
1668 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001669 }
1670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 txn->flags |= TX_HDR_CONN_PRS;
1672 return;
1673}
Willy Tarreau5b154472009-12-21 20:11:07 +01001674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675/* Apply desired changes on the Connection: header. Values may be removed and/or
1676 * added depending on the <wanted> flags, which are exclusively composed of
1677 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1678 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1679 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001680void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001681{
1682 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001683 const char *hdr_val = "Connection";
1684 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685
1686 ctx.idx = 0;
1687
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688
1689 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1690 hdr_val = "Proxy-Connection";
1691 hdr_len = 16;
1692 }
1693
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau09d1e252012-05-18 22:36:34 +02001695 while (http_find_header2(hdr_val, hdr_len, msg->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001696 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1697 if (wanted & TX_CON_KAL_SET)
1698 txn->flags |= TX_CON_KAL_SET;
1699 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001700 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001701 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001702 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1703 if (wanted & TX_CON_CLO_SET)
1704 txn->flags |= TX_CON_CLO_SET;
1705 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001707 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001708 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001709
1710 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1711 return;
1712
1713 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1714 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001715 hdr_val = "Connection: close";
1716 hdr_len = 17;
1717 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1718 hdr_val = "Proxy-Connection: close";
1719 hdr_len = 23;
1720 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001721 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001722 }
1723
1724 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1725 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001726 hdr_val = "Connection: keep-alive";
1727 hdr_len = 22;
1728 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1729 hdr_val = "Proxy-Connection: keep-alive";
1730 hdr_len = 28;
1731 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001732 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001733 }
1734 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001735}
1736
Willy Tarreaua458b672012-03-05 11:17:50 +01001737/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001738 * first byte of body, and increments msg->sov by the number of bytes parsed,
1739 * so that we know we can forward between ->som and ->sov. Note that due to
1740 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1741 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001742 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001745int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001746{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001747 const struct buffer *buf = msg->buf;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001748 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001749 const char *ptr_old = ptr;
1750 const char *end = buf->data + buf->size;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001751 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001752 unsigned int chunk = 0;
1753
1754 /* The chunk size is in the following form, though we are only
1755 * interested in the size and CRLF :
1756 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1757 */
1758 while (1) {
1759 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001760 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001761 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 if (c < 0) /* not a hex digit anymore */
1764 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 if (++ptr >= end)
1766 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001767 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001768 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 chunk = (chunk << 4) + c;
1770 }
1771
Willy Tarreaud98cf932009-12-27 22:54:55 +01001772 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001773 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001774 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775
1776 while (http_is_spht[(unsigned char)*ptr]) {
1777 if (++ptr >= end)
1778 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001779 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001781 }
1782
Willy Tarreaud98cf932009-12-27 22:54:55 +01001783 /* Up to there, we know that at least one byte is present at *ptr. Check
1784 * for the end of chunk size.
1785 */
1786 while (1) {
1787 if (likely(HTTP_IS_CRLF(*ptr))) {
1788 /* we now have a CR or an LF at ptr */
1789 if (likely(*ptr == '\r')) {
1790 if (++ptr >= end)
1791 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001792 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001793 return 0;
1794 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001795
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001797 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (++ptr >= end)
1799 ptr = buf->data;
1800 /* done */
1801 break;
1802 }
1803 else if (*ptr == ';') {
1804 /* chunk extension, ends at next CRLF */
1805 if (++ptr >= end)
1806 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001807 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001808 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001809
1810 while (!HTTP_IS_CRLF(*ptr)) {
1811 if (++ptr >= end)
1812 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001813 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 return 0;
1815 }
1816 /* we have a CRLF now, loop above */
1817 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001818 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001820 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
1822
Willy Tarreaud98cf932009-12-27 22:54:55 +01001823 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001824 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001826 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 msg->sov += ptr - ptr_old;
1828 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001829 msg->chunk_len = chunk;
1830 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001832 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001833 error:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001834 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001835 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001836}
1837
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001838/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 * the trailers is found, it is automatically scheduled to be forwarded,
1841 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1842 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001844 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001845 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1847 * which implies that all non-trailers data have already been scheduled for
1848 * forwarding, and that the difference between msg->som and msg->sov exactly
1849 * matches the length of trailers already parsed and not forwarded. It is also
1850 * important to note that this function is designed to be able to parse wrapped
1851 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001853int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001855 const struct buffer *buf = msg->buf;
1856
Willy Tarreaua458b672012-03-05 11:17:50 +01001857 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001859 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001860 const char *ptr = b_ptr(buf, msg->next);
1861 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001862 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863
1864 /* scan current line and stop at LF or CRLF */
1865 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001866 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 return 0;
1868
1869 if (*ptr == '\n') {
1870 if (!p1)
1871 p1 = ptr;
1872 p2 = ptr;
1873 break;
1874 }
1875
1876 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001877 if (p1) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001878 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001880 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 p1 = ptr;
1882 }
1883
1884 ptr++;
1885 if (ptr >= buf->data + buf->size)
1886 ptr = buf->data;
1887 }
1888
1889 /* after LF; point to beginning of next line */
1890 p2++;
1891 if (p2 >= buf->data + buf->size)
1892 p2 = buf->data;
1893
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001894 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001895 if (bytes < 0)
1896 bytes += buf->size;
1897
1898 /* schedule this line for forwarding */
1899 msg->sov += bytes;
1900 if (msg->sov >= buf->size)
1901 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001902
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001903 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001904 /* LF/CRLF at beginning of line => end of trailers at p2.
1905 * Everything was scheduled for forwarding, there's nothing
1906 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001907 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001908 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 msg->msg_state = HTTP_MSG_DONE;
1910 return 1;
1911 }
1912 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001913 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 }
1915}
1916
1917/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1918 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001919 * ->som, ->next in order to include this part into the next forwarding phase.
1920 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1922 * not enough data are available, the function does not change anything and
1923 * returns zero. If a parse error is encountered, the function returns < 0 and
1924 * does not change anything. Note: this function is designed to parse wrapped
1925 * CRLF at the end of the buffer.
1926 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001929 const struct buffer *buf = msg->buf;
1930 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 int bytes;
1932
1933 /* NB: we'll check data availabilty at the end. It's not a
1934 * problem because whatever we match first will be checked
1935 * against the correct length.
1936 */
1937 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001938 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001939 if (*ptr == '\r') {
1940 bytes++;
1941 ptr++;
1942 if (ptr >= buf->data + buf->size)
1943 ptr = buf->data;
1944 }
1945
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001946 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 return 0;
1948
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001949 if (*ptr != '\n') {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001950 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 ptr++;
1955 if (ptr >= buf->data + buf->size)
1956 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001957 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1958 msg->som = 0;
1959 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1961 return 1;
1962}
Willy Tarreau5b154472009-12-21 20:11:07 +01001963
Willy Tarreaud787e662009-07-07 10:14:51 +02001964/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1965 * processing can continue on next analysers, or zero if it either needs more
1966 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1967 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1968 * when it has nothing left to do, and may remove any analyser when it wants to
1969 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001971int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972{
Willy Tarreau59234e92008-11-30 23:51:27 +01001973 /*
1974 * We will parse the partial (or complete) lines.
1975 * We will check the request syntax, and also join multi-line
1976 * headers. An index of all the lines will be elaborated while
1977 * parsing.
1978 *
1979 * For the parsing, we use a 28 states FSM.
1980 *
1981 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01001982 * req->p + msg->som = beginning of request
1983 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01001984 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01001985 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01001986 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001987 *
1988 * At end of parsing, we may perform a capture of the error (if any), and
1989 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1990 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1991 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001992 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001993
Willy Tarreau59234e92008-11-30 23:51:27 +01001994 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001995 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001996 struct http_txn *txn = &s->txn;
1997 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001998 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001999
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002000 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 +01002001 now_ms, __FUNCTION__,
2002 s,
2003 req,
2004 req->rex, req->wex,
2005 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002006 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002007 req->analysers);
2008
Willy Tarreau52a0c602009-08-16 22:45:38 +02002009 /* we're speaking HTTP here, so let's speak HTTP to the client */
2010 s->srv_error = http_return_srv_error;
2011
Willy Tarreau83e3af02009-12-28 17:39:57 +01002012 /* There's a protected area at the end of the buffer for rewriting
2013 * purposes. We don't want to start to parse the request if the
2014 * protected area is affected, because we may have to move processed
2015 * data later, which is much more complicated.
2016 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002017 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002018 if ((txn->flags & TX_NOT_FIRST) &&
2019 unlikely((req->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002020 bi_end(req) < b_ptr(req, msg->next) ||
2021 bi_end(req) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002022 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002023 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2024 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002025 /* some data has still not left the buffer, wake us once that's done */
2026 buffer_dont_connect(req);
2027 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2028 return 0;
2029 }
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002030 if (bi_end(req) < b_ptr(req, msg->next) ||
2031 bi_end(req) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02002032 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002033 }
2034
Willy Tarreau065e8332010-01-08 00:30:20 +01002035 /* Note that we have the same problem with the response ; we
2036 * may want to send a redirect, error or anything which requires
2037 * some spare space. So we'll ensure that we have at least
2038 * maxrewrite bytes available in the response buffer before
2039 * processing that one. This will only affect pipelined
2040 * keep-alive requests.
2041 */
2042 if ((txn->flags & TX_NOT_FIRST) &&
2043 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002044 bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) ||
2045 bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002046 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002047 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2048 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002049 /* don't let a connection request be initiated */
2050 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002051 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002052 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002053 return 0;
2054 }
2055 }
2056
Willy Tarreaua458b672012-03-05 11:17:50 +01002057 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002058 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002059 }
2060
Willy Tarreau59234e92008-11-30 23:51:27 +01002061 /* 1: we might have to print this header in debug mode */
2062 if (unlikely((global.mode & MODE_DEBUG) &&
2063 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002064 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002065 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002066
Willy Tarreauea1175a2012-03-05 15:52:30 +01002067 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002068 eol = sol + msg->sl.rq.l;
2069 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002070
Willy Tarreau59234e92008-11-30 23:51:27 +01002071 sol += hdr_idx_first_pos(&txn->hdr_idx);
2072 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002073
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 while (cur_idx) {
2075 eol = sol + txn->hdr_idx.v[cur_idx].len;
2076 debug_hdr("clihdr", s, sol, eol);
2077 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2078 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 }
2081
Willy Tarreau58f10d72006-12-04 02:26:12 +01002082
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 /*
2084 * Now we quickly check if we have found a full valid request.
2085 * If not so, we check the FD and buffer states before leaving.
2086 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002087 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002088 * requests are checked first. When waiting for a second request
2089 * on a keep-alive session, if we encounter and error, close, t/o,
2090 * we note the error in the session flags but don't set any state.
2091 * Since the error will be noted there, it will not be counted by
2092 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002093 * Last, we may increase some tracked counters' http request errors on
2094 * the cases that are deliberately the client's fault. For instance,
2095 * a timeout or connection reset is not counted as an error. However
2096 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002097 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002098
Willy Tarreau655dce92009-11-08 13:10:58 +01002099 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002100 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002102 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002103 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002104 session_inc_http_req_ctr(s);
2105 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002106 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 /* 1: Since we are in header mode, if there's no space
2111 * left for headers, we won't be able to free more
2112 * later, so the session will never terminate. We
2113 * must terminate it now.
2114 */
2115 if (unlikely(req->flags & BF_FULL)) {
2116 /* FIXME: check if URI is set and return Status
2117 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002118 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002119 session_inc_http_req_ctr(s);
2120 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002121 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002122 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002123 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 goto return_bad_req;
2125 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002126
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 /* 2: have we encountered a read error ? */
2128 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002129 if (!(s->flags & SN_ERR_MASK))
2130 s->flags |= SN_ERR_CLICL;
2131
Willy Tarreaufcffa692010-01-10 14:21:19 +01002132 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002133 goto failed_keep_alive;
2134
Willy Tarreau59234e92008-11-30 23:51:27 +01002135 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002136 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002137 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002138 session_inc_http_err_ctr(s);
2139 }
2140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 msg->msg_state = HTTP_MSG_ERROR;
2142 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002143
Willy Tarreauda7ff642010-06-23 11:44:09 +02002144 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002145 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002146 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002147 if (s->listener->counters)
2148 s->listener->counters->failed_req++;
2149
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 if (!(s->flags & SN_FINST_MASK))
2151 s->flags |= SN_FINST_R;
2152 return 0;
2153 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002154
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 /* 3: has the read timeout expired ? */
2156 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002157 if (!(s->flags & SN_ERR_MASK))
2158 s->flags |= SN_ERR_CLITO;
2159
Willy Tarreaufcffa692010-01-10 14:21:19 +01002160 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002161 goto failed_keep_alive;
2162
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002164 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002165 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002166 session_inc_http_err_ctr(s);
2167 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002168 txn->status = 408;
2169 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2170 msg->msg_state = HTTP_MSG_ERROR;
2171 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002172
Willy Tarreauda7ff642010-06-23 11:44:09 +02002173 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002174 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002175 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002176 if (s->listener->counters)
2177 s->listener->counters->failed_req++;
2178
Willy Tarreau59234e92008-11-30 23:51:27 +01002179 if (!(s->flags & SN_FINST_MASK))
2180 s->flags |= SN_FINST_R;
2181 return 0;
2182 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 /* 4: have we encountered a close ? */
2185 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002186 if (!(s->flags & SN_ERR_MASK))
2187 s->flags |= SN_ERR_CLICL;
2188
Willy Tarreaufcffa692010-01-10 14:21:19 +01002189 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002190 goto failed_keep_alive;
2191
Willy Tarreau4076a152009-04-02 15:18:36 +02002192 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002193 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 txn->status = 400;
2195 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2196 msg->msg_state = HTTP_MSG_ERROR;
2197 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002198
Willy Tarreauda7ff642010-06-23 11:44:09 +02002199 session_inc_http_err_ctr(s);
2200 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002201 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002202 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203 if (s->listener->counters)
2204 s->listener->counters->failed_req++;
2205
Willy Tarreau59234e92008-11-30 23:51:27 +01002206 if (!(s->flags & SN_FINST_MASK))
2207 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002208 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209 }
2210
Willy Tarreau520d95e2009-09-19 21:04:57 +02002211 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002212 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002213 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002214#ifdef TCP_QUICKACK
Willy Tarreau93548be2012-05-13 08:44:16 +02002215 if (s->listener->options & LI_O_NOQUICKACK && req->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002216 /* We need more data, we have to re-enable quick-ack in case we
2217 * previously disabled it, otherwise we might cause the client
2218 * to delay next data.
2219 */
2220 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2221 }
2222#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002223
Willy Tarreaufcffa692010-01-10 14:21:19 +01002224 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2225 /* If the client starts to talk, let's fall back to
2226 * request timeout processing.
2227 */
2228 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002229 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002230 }
2231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002233 if (!tick_isset(req->analyse_exp)) {
2234 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2235 (txn->flags & TX_WAIT_NEXT_RQ) &&
2236 tick_isset(s->be->timeout.httpka))
2237 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2238 else
2239 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002241
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 /* we're not ready yet */
2243 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002244
2245 failed_keep_alive:
2246 /* Here we process low-level errors for keep-alive requests. In
2247 * short, if the request is not the first one and it experiences
2248 * a timeout, read error or shutdown, we just silently close so
2249 * that the client can try again.
2250 */
2251 txn->status = 0;
2252 msg->msg_state = HTTP_MSG_RQBEFORE;
2253 req->analysers = 0;
2254 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002255 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002256 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002257 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002259
Willy Tarreaud787e662009-07-07 10:14:51 +02002260 /* OK now we have a complete HTTP request with indexed headers. Let's
2261 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002262 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2263 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002264 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002265 * byte after the last LF. msg->sov points to the first byte of data.
2266 * msg->eol cannot be trusted because it may have been left uninitialized
2267 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002268 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002269
Willy Tarreauda7ff642010-06-23 11:44:09 +02002270 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002271 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2272
Willy Tarreaub16a5742010-01-10 14:46:16 +01002273 if (txn->flags & TX_WAIT_NEXT_RQ) {
2274 /* kill the pending keep-alive timeout */
2275 txn->flags &= ~TX_WAIT_NEXT_RQ;
2276 req->analyse_exp = TICK_ETERNITY;
2277 }
2278
2279
Willy Tarreaud787e662009-07-07 10:14:51 +02002280 /* Maybe we found in invalid header name while we were configured not
2281 * to block on that, so we have to capture it now.
2282 */
2283 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002284 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002285
Willy Tarreau59234e92008-11-30 23:51:27 +01002286 /*
2287 * 1: identify the method
2288 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002289 txn->meth = find_http_meth(req->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002290
2291 /* we can make use of server redirect on GET and HEAD */
2292 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2293 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002294
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 /*
2296 * 2: check if the URI matches the monitor_uri.
2297 * We have to do this for every request which gets in, because
2298 * the monitor-uri is defined by the frontend.
2299 */
2300 if (unlikely((s->fe->monitor_uri_len != 0) &&
2301 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002302 !memcmp(req->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002303 s->fe->monitor_uri,
2304 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002309
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002311 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002314 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002315 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002316
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 ret = acl_pass(ret);
2318 if (cond->pol == ACL_COND_UNLESS)
2319 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 if (ret) {
2322 /* we fail this request, let's return 503 service unavail */
2323 txn->status = 503;
2324 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2325 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002326 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002327 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002328
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 /* nothing to fail, let's reply normaly */
2330 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002331 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 goto return_prx_cond;
2333 }
2334
2335 /*
2336 * 3: Maybe we have to copy the original REQURI for the logs ?
2337 * Note: we cannot log anymore if the request has been
2338 * classified as invalid.
2339 */
2340 if (unlikely(s->logs.logwait & LW_REQ)) {
2341 /* we have a complete HTTP request that we must log */
2342 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2343 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 if (urilen >= REQURI_LEN)
2346 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002347 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 if (!(s->logs.logwait &= ~LW_REQ))
2351 s->do_log(s);
2352 } else {
2353 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002354 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 }
Willy Tarreau06619262006-12-17 08:37:22 +01002356
William Lallemanda73203e2012-03-12 12:48:57 +01002357 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2358 s->unique_id = pool_alloc2(pool2_uniqueid);
2359 }
2360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002362 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002363 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002364
Willy Tarreau5b154472009-12-21 20:11:07 +01002365 /* ... and check if the request is HTTP/1.1 or above */
2366 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002367 ((req->p[msg->sl.rq.v + 5] > '1') ||
2368 ((req->p[msg->sl.rq.v + 5] == '1') &&
2369 (req->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002370 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002371
2372 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002373 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002374
Willy Tarreau88d349d2010-01-25 12:15:43 +01002375 /* if the frontend has "option http-use-proxy-header", we'll check if
2376 * we have what looks like a proxied connection instead of a connection,
2377 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2378 * Note that this is *not* RFC-compliant, however browsers and proxies
2379 * happen to do that despite being non-standard :-(
2380 * We consider that a request not beginning with either '/' or '*' is
2381 * a proxied connection, which covers both "scheme://location" and
2382 * CONNECT ip:port.
2383 */
2384 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002385 req->p[msg->sl.rq.u] != '/' && req->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002386 txn->flags |= TX_USE_PX_CONN;
2387
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002388 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002389 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002392 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02002393 capture_headers(req->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002395
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002396 /* 6: determine the transfer-length.
2397 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2398 * the presence of a message-body in a REQUEST and its transfer length
2399 * must be determined that way (in order of precedence) :
2400 * 1. The presence of a message-body in a request is signaled by the
2401 * inclusion of a Content-Length or Transfer-Encoding header field
2402 * in the request's header fields. When a request message contains
2403 * both a message-body of non-zero length and a method that does
2404 * not define any semantics for that request message-body, then an
2405 * origin server SHOULD either ignore the message-body or respond
2406 * with an appropriate error message (e.g., 413). A proxy or
2407 * gateway, when presented the same request, SHOULD either forward
2408 * the request inbound with the message- body or ignore the
2409 * message-body when determining a response.
2410 *
2411 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2412 * and the "chunked" transfer-coding (Section 6.2) is used, the
2413 * transfer-length is defined by the use of this transfer-coding.
2414 * If a Transfer-Encoding header field is present and the "chunked"
2415 * transfer-coding is not present, the transfer-length is defined
2416 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002417 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002418 * 3. If a Content-Length header field is present, its decimal value in
2419 * OCTETs represents both the entity-length and the transfer-length.
2420 * If a message is received with both a Transfer-Encoding header
2421 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002422 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002423 * 4. By the server closing the connection. (Closing the connection
2424 * cannot be used to indicate the end of a request body, since that
2425 * would leave no possibility for the server to send back a response.)
2426 *
2427 * Whenever a transfer-coding is applied to a message-body, the set of
2428 * transfer-codings MUST include "chunked", unless the message indicates
2429 * it is terminated by closing the connection. When the "chunked"
2430 * transfer-coding is used, it MUST be the last transfer-coding applied
2431 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002432 */
2433
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002434 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002435 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002437 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002438 http_find_header2("Transfer-Encoding", 17, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002439 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002440 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2441 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 /* bad transfer-encoding (chunked followed by something else) */
2443 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002444 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 break;
2446 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002447 }
2448
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002450 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02002451 http_find_header2("Content-Length", 14, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452 signed long long cl;
2453
Willy Tarreauad14f752011-09-02 20:33:27 +02002454 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002455 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002456 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002457 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458
Willy Tarreauad14f752011-09-02 20:33:27 +02002459 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002460 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002461 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002462 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002463
Willy Tarreauad14f752011-09-02 20:33:27 +02002464 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002465 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002466 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002467 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002468
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002469 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002470 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002471 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002472 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002473
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002474 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002475 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 }
2477
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002478 /* bodyless requests have a known length */
2479 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002480 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481
Willy Tarreaud787e662009-07-07 10:14:51 +02002482 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002483 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002484 req->analyse_exp = TICK_ETERNITY;
2485 return 1;
2486
2487 return_bad_req:
2488 /* We centralize bad requests processing here */
2489 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2490 /* we detected a parsing error. We want to archive this request
2491 * in the dedicated proxy area for later troubleshooting.
2492 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002493 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002494 }
2495
2496 txn->req.msg_state = HTTP_MSG_ERROR;
2497 txn->status = 400;
2498 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002499
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002500 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501 if (s->listener->counters)
2502 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002503
2504 return_prx_cond:
2505 if (!(s->flags & SN_ERR_MASK))
2506 s->flags |= SN_ERR_PRXCOND;
2507 if (!(s->flags & SN_FINST_MASK))
2508 s->flags |= SN_FINST_R;
2509
2510 req->analysers = 0;
2511 req->analyse_exp = TICK_ETERNITY;
2512 return 0;
2513}
2514
Cyril Bonté70be45d2010-10-12 00:14:35 +02002515/* We reached the stats page through a POST request.
2516 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002517 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002518 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002519int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002520{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002521 struct proxy *px = NULL;
2522 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002523
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002524 char key[LINESIZE];
2525 int action = ST_ADM_ACTION_NONE;
2526 int reprocess = 0;
2527
2528 int total_servers = 0;
2529 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002530
2531 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002532 char *st_cur_param = NULL;
2533 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002534
Willy Tarreauea1175a2012-03-05 15:52:30 +01002535 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002536 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002537
2538 cur_param = next_param = end_params;
2539
Cyril Bonté23b39d92011-02-10 22:54:44 +01002540 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002541 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002542 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543 return 1;
2544 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002545 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002546 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002547 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002548 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002549 }
2550
2551 *end_params = '\0';
2552
Willy Tarreau295a8372011-03-10 11:25:07 +01002553 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002554
2555 /*
2556 * Parse the parameters in reverse order to only store the last value.
2557 * From the html form, the backend and the action are at the end.
2558 */
2559 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002560 char *value;
2561 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002562
2563 cur_param--;
2564 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002565 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002566 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002567 poffset = (cur_param != first_param ? 1 : 0);
2568 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2569 if ((plen > 0) && (plen <= sizeof(key))) {
2570 strncpy(key, cur_param + poffset, plen);
2571 key[plen - 1] = '\0';
2572 } else {
2573 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2574 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575 }
2576
2577 /* Parse the value */
2578 value = key;
2579 while (*value != '\0' && *value != '=') {
2580 value++;
2581 }
2582 if (*value == '=') {
2583 /* Ok, a value is found, we can mark the end of the key */
2584 *value++ = '\0';
2585 }
2586
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002587 if (!url_decode(key) || !url_decode(value))
2588 break;
2589
Cyril Bonté70be45d2010-10-12 00:14:35 +02002590 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002591 if (!px && (strcmp(key, "b") == 0)) {
2592 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2593 /* the backend name is unknown or ambiguous (duplicate names) */
2594 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2595 goto out;
2596 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002598 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002600 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002601 }
2602 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002603 action = ST_ADM_ACTION_ENABLE;
2604 }
2605 else {
2606 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2607 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608 }
2609 }
2610 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002611 if (!(px && action)) {
2612 /*
2613 * Indicates that we'll need to reprocess the parameters
2614 * as soon as backend and action are known
2615 */
2616 if (!reprocess) {
2617 st_cur_param = cur_param;
2618 st_next_param = next_param;
2619 }
2620 reprocess = 1;
2621 }
2622 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002623 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002624 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002625 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002626 /* Not already in maintenance, we can change the server state */
2627 sv->state |= SRV_MAINTAIN;
2628 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002629 altered_servers++;
2630 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002631 }
2632 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002633 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002634 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002635 /* Already in maintenance, we can change the server state */
2636 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002637 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002638 altered_servers++;
2639 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002640 }
2641 break;
2642 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002643 } else {
2644 /* the server name is unknown or ambiguous (duplicate names) */
2645 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646 }
2647 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002648 if (reprocess && px && action) {
2649 /* Now, we know the backend and the action chosen by the user.
2650 * We can safely restart from the first server parameter
2651 * to reprocess them
2652 */
2653 cur_param = st_cur_param;
2654 next_param = st_next_param;
2655 reprocess = 0;
2656 goto reprocess_servers;
2657 }
2658
Cyril Bonté70be45d2010-10-12 00:14:35 +02002659 next_param = cur_param;
2660 }
2661 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002662
2663 if (total_servers == 0) {
2664 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2665 }
2666 else if (altered_servers == 0) {
2667 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2668 }
2669 else if (altered_servers == total_servers) {
2670 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2671 }
2672 else {
2673 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2674 }
2675 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002676 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002677}
2678
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002679/* returns a pointer to the first rule which forbids access (deny or http_auth),
2680 * or NULL if everything's OK.
2681 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002682static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002683http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2684{
Willy Tarreauff011f22011-01-06 17:51:27 +01002685 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002686
Willy Tarreauff011f22011-01-06 17:51:27 +01002687 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002688 int ret = 1;
2689
Willy Tarreauff011f22011-01-06 17:51:27 +01002690 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002691 continue;
2692
2693 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002694 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002695 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002696 ret = acl_pass(ret);
2697
Willy Tarreauff011f22011-01-06 17:51:27 +01002698 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002699 ret = !ret;
2700 }
2701
2702 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002703 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002704 return NULL; /* no problem */
2705 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002706 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002707 }
2708 }
2709 return NULL;
2710}
2711
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002712/* This stream analyser runs all HTTP request processing which is common to
2713 * frontends and backends, which means blocking ACLs, filters, connection-close,
2714 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002715 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 * either needs more data or wants to immediately abort the request (eg: deny,
2717 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002718 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002720{
Willy Tarreaud787e662009-07-07 10:14:51 +02002721 struct http_txn *txn = &s->txn;
2722 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002723 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002724 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002726 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002727 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002728
Willy Tarreau655dce92009-11-08 13:10:58 +01002729 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002730 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002731 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002732 return 0;
2733 }
2734
Willy Tarreau3a816292009-07-07 10:55:49 +02002735 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002736 req->analyse_exp = TICK_ETERNITY;
2737
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002738 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 +02002739 now_ms, __FUNCTION__,
2740 s,
2741 req,
2742 req->rex, req->wex,
2743 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002744 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002745 req->analysers);
2746
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 /* first check whether we have some ACLs set to block this request */
2748 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002749 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002750
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002751 ret = acl_pass(ret);
2752 if (cond->pol == ACL_COND_UNLESS)
2753 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002754
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 if (ret) {
2756 txn->status = 403;
2757 /* let's log the request time */
2758 s->logs.tv_request = now;
2759 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002760 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002764
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002765 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002766 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002767
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002768 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002769 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002770 do_stats = stats_check_uri(s->rep->prod, txn, px);
2771 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002772 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 +01002773 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002774 else
2775 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002776
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002777 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002778 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002779 txn->status = 403;
2780 s->logs.tv_request = now;
2781 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002782 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002783 s->fe->fe_counters.denied_req++;
2784 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2785 s->be->be_counters.denied_req++;
2786 if (s->listener->counters)
2787 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002788 goto return_prx_cond;
2789 }
2790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 /* try headers filters */
2792 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002793 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002795
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 /* has the request been denied ? */
2797 if (txn->flags & TX_CLDENY) {
2798 /* no need to go further */
2799 txn->status = 403;
2800 /* let's log the request time */
2801 s->logs.tv_request = now;
2802 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002803 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 goto return_prx_cond;
2805 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002806
2807 /* When a connection is tarpitted, we use the tarpit timeout,
2808 * which may be the same as the connect timeout if unspecified.
2809 * If unset, then set it to zero because we really want it to
2810 * eventually expire. We build the tarpit as an analyser.
2811 */
2812 if (txn->flags & TX_CLTARPIT) {
2813 buffer_erase(s->req);
2814 /* wipe the request out so that we can drop the connection early
2815 * if the client closes first.
2816 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002817 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002818 req->analysers = 0; /* remove switching rules etc... */
2819 req->analysers |= AN_REQ_HTTP_TARPIT;
2820 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2821 if (!req->analyse_exp)
2822 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002823 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002824 return 1;
2825 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002826 }
Willy Tarreau06619262006-12-17 08:37:22 +01002827
Willy Tarreau5b154472009-12-21 20:11:07 +01002828 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2829 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002830 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2831 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002832 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2833 * avoid to redo the same work if FE and BE have the same settings (common).
2834 * The method consists in checking if options changed between the two calls
2835 * (implying that either one is non-null, or one of them is non-null and we
2836 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002837 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002838
Willy Tarreaudc008c52010-02-01 16:20:08 +01002839 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2840 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2841 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2842 (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 +01002843 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002844
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002845 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2846 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002847 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002848 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2849 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002850 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002851 tmp = TX_CON_WANT_CLO;
2852
Willy Tarreau5b154472009-12-21 20:11:07 +01002853 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2854 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002855
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002856 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2857 /* parse the Connection header and possibly clean it */
2858 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002859 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002860 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2861 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002862 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002863 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002864 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002865 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002866 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002867
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002868 /* check if client or config asks for explicit close in KAL/SCL */
2869 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2870 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2871 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002872 (!(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 +01002873 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002874 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002875 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002876 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2877 }
Willy Tarreau78599912009-10-17 20:12:21 +02002878
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002879 /* we can be blocked here because the request needs to be authenticated,
2880 * either to pass or to access stats.
2881 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002882 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002883 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002884 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002885
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002886 if (!realm)
2887 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2888
Willy Tarreau844a7e72010-01-31 21:46:18 +01002889 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002890 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002891 txn->status = 401;
2892 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002893 /* on 401 we still count one error, because normal browsing
2894 * won't significantly increase the counter but brute force
2895 * attempts will.
2896 */
2897 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002898 goto return_prx_cond;
2899 }
2900
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002901 /* add request headers from the rule sets in the same order */
2902 list_for_each_entry(wl, &px->req_add, list) {
2903 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002904 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002905 ret = acl_pass(ret);
2906 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2907 ret = !ret;
2908 if (!ret)
2909 continue;
2910 }
2911
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002912 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002913 goto return_bad_req;
2914 }
2915
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002916 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002917 struct stats_admin_rule *stats_admin_rule;
2918
2919 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002920 * FIXME!!! that one is rather dangerous, we want to
2921 * make it follow standard rules (eg: clear req->analysers).
2922 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002923
Cyril Bonté474be412010-10-12 00:14:36 +02002924 /* now check whether we have some admin rules for this request */
2925 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2926 int ret = 1;
2927
2928 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002929 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 +02002930 ret = acl_pass(ret);
2931 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2932 ret = !ret;
2933 }
2934
2935 if (ret) {
2936 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002938 break;
2939 }
2940 }
2941
Cyril Bonté70be45d2010-10-12 00:14:35 +02002942 /* Was the status page requested with a POST ? */
2943 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002944 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002945 if (msg->msg_state < HTTP_MSG_100_SENT) {
2946 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2947 * send an HTTP/1.1 100 Continue intermediate response.
2948 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002949 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002950 struct hdr_ctx ctx;
2951 ctx.idx = 0;
2952 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02002953 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002954 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002955 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01002956 }
2957 }
2958 msg->msg_state = HTTP_MSG_100_SENT;
2959 s->logs.tv_request = now; /* update the request timer to reflect full request */
2960 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002962 /* we need more data */
2963 req->analysers |= an_bit;
2964 buffer_dont_connect(req);
2965 return 0;
2966 }
Cyril Bonté474be412010-10-12 00:14:36 +02002967 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002968 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002969 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002970 }
2971
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002972 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002973 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002974 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02002975 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01002976 s->rep->prod->applet.private = s;
2977 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002978 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02002979 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
2980 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002981
2982 return 0;
2983
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002984 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002985
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002986 /* check whether we have some ACLs set to redirect this request */
2987 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002988 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002989
Willy Tarreauf285f542010-01-03 20:03:03 +01002990 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002991 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002992 ret = acl_pass(ret);
2993 if (rule->cond->pol == ACL_COND_UNLESS)
2994 ret = !ret;
2995 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002996
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002997 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02002998 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003000
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003001 /* build redirect message */
3002 switch(rule->code) {
3003 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003004 msg_fmt = HTTP_303;
3005 break;
3006 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 msg_fmt = HTTP_301;
3008 break;
3009 case 302:
3010 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003011 msg_fmt = HTTP_302;
3012 break;
3013 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003014
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003015 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003017
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003018 switch(rule->type) {
3019 case REDIRECT_TYPE_PREFIX: {
3020 const char *path;
3021 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003022
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 path = http_get_path(txn);
3024 /* build message using path */
3025 if (path) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003026 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3028 int qs = 0;
3029 while (qs < pathlen) {
3030 if (path[qs] == '?') {
3031 pathlen = qs;
3032 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003033 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003035 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003036 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 } else {
3038 path = "/";
3039 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003040 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003041
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003042 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 goto return_bad_req;
3044
3045 /* add prefix. Note that if prefix == "/", we don't want to
3046 * add anything, otherwise it makes it hard for the user to
3047 * configure a self-redirection.
3048 */
3049 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003050 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3051 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052 }
3053
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 /* add path */
3055 memcpy(rdr.str + rdr.len, path, pathlen);
3056 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003057
3058 /* append a slash at the end of the location is needed and missing */
3059 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3060 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3061 if (rdr.len > rdr.size - 5)
3062 goto return_bad_req;
3063 rdr.str[rdr.len] = '/';
3064 rdr.len++;
3065 }
3066
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 break;
3068 }
3069 case REDIRECT_TYPE_LOCATION:
3070 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003071 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003072 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 /* add location */
3075 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3076 rdr.len += rule->rdr_len;
3077 break;
3078 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 if (rule->cookie_len) {
3081 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3082 rdr.len += 14;
3083 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3084 rdr.len += rule->cookie_len;
3085 memcpy(rdr.str + rdr.len, "\r\n", 2);
3086 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003087 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003088
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003089 /* add end of headers and the keep-alive/close status.
3090 * We may choose to set keep-alive if the Location begins
3091 * with a slash, because the client will come back to the
3092 * same server.
3093 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003094 txn->status = rule->code;
3095 /* let's log the request time */
3096 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003097
3098 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003099 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3100 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003101 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3102 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3103 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003104 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003105 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3106 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3107 rdr.len += 30;
3108 } else {
3109 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3110 rdr.len += 24;
3111 }
Willy Tarreau75661452010-01-10 10:35:01 +01003112 }
3113 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3114 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003115 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003116 /* "eat" the request */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003117 bi_fast_delete(req, msg->sov - msg->som);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003118 msg->som = msg->sov;
3119 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003120 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3121 txn->req.msg_state = HTTP_MSG_CLOSED;
3122 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003123 break;
3124 } else {
3125 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003126 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3127 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3128 rdr.len += 29;
3129 } else {
3130 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3131 rdr.len += 23;
3132 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003133 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003134 goto return_prx_cond;
3135 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 }
3137 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003138
Willy Tarreau2be39392010-01-03 17:24:51 +01003139 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3140 * If this happens, then the data will not come immediately, so we must
3141 * send all what we have without waiting. Note that due to the small gain
3142 * in waiting for the body of the request, it's easier to simply put the
3143 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3144 * itself once used.
3145 */
3146 req->flags |= BF_SEND_DONTWAIT;
3147
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003148 /* that's OK for us now, let's move on to next analysers */
3149 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003150
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003151 return_bad_req:
3152 /* We centralize bad requests processing here */
3153 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3154 /* we detected a parsing error. We want to archive this request
3155 * in the dedicated proxy area for later troubleshooting.
3156 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003157 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003158 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003159
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003160 txn->req.msg_state = HTTP_MSG_ERROR;
3161 txn->status = 400;
3162 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003163
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003164 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003165 if (s->listener->counters)
3166 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003167
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003168 return_prx_cond:
3169 if (!(s->flags & SN_ERR_MASK))
3170 s->flags |= SN_ERR_PRXCOND;
3171 if (!(s->flags & SN_FINST_MASK))
3172 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003173
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003174 req->analysers = 0;
3175 req->analyse_exp = TICK_ETERNITY;
3176 return 0;
3177}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003178
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003179/* This function performs all the processing enabled for the current request.
3180 * It returns 1 if the processing can continue on next analysers, or zero if it
3181 * needs more data, encounters an error, or wants to immediately abort the
3182 * request. It relies on buffers flags, and updates s->req->analysers.
3183 */
3184int http_process_request(struct session *s, struct buffer *req, int an_bit)
3185{
3186 struct http_txn *txn = &s->txn;
3187 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003188
Willy Tarreau655dce92009-11-08 13:10:58 +01003189 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003190 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003191 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003192 return 0;
3193 }
3194
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003195 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 +02003196 now_ms, __FUNCTION__,
3197 s,
3198 req,
3199 req->rex, req->wex,
3200 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003201 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003202 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003203
Willy Tarreau59234e92008-11-30 23:51:27 +01003204 /*
3205 * Right now, we know that we have processed the entire headers
3206 * and that unwanted requests have been filtered out. We can do
3207 * whatever we want with the remaining request. Also, now we
3208 * may have separate values for ->fe, ->be.
3209 */
Willy Tarreau06619262006-12-17 08:37:22 +01003210
Willy Tarreau59234e92008-11-30 23:51:27 +01003211 /*
3212 * If HTTP PROXY is set we simply get remote server address
3213 * parsing incoming request.
3214 */
3215 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003216 url2sa(req->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003217 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003218
Willy Tarreau59234e92008-11-30 23:51:27 +01003219 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003220 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003221 * Note that doing so might move headers in the request, but
3222 * the fields will stay coherent and the URI will not move.
3223 * This should only be performed in the backend.
3224 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003225 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003226 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3227 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003228
Willy Tarreau59234e92008-11-30 23:51:27 +01003229 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003230 * 8: the appsession cookie was looked up very early in 1.2,
3231 * so let's do the same now.
3232 */
3233
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003234 /* It needs to look into the URI unless persistence must be ignored */
3235 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau09d1e252012-05-18 22:36:34 +02003236 get_srv_from_appsession(s, req->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003237 }
3238
William Lallemanda73203e2012-03-12 12:48:57 +01003239 /* add unique-id if "header-unique-id" is specified */
3240
3241 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3242 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3243
3244 if (s->fe->header_unique_id && s->unique_id) {
3245 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3246 if (ret < 0 || ret > global.tune.bufsize)
3247 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003248 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003249 goto return_bad_req;
3250 }
3251
Cyril Bontéb21570a2009-11-29 20:04:48 +01003252 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003253 * 9: add X-Forwarded-For if either the frontend or the backend
3254 * asks for it.
3255 */
3256 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003257 struct hdr_ctx ctx = { .idx = 0 };
3258
3259 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02003260 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 +02003261 /* The header is set to be added only if none is present
3262 * and we found it, so don't do anything.
3263 */
3264 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003265 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 /* Add an X-Forwarded-For header unless the source IP is
3267 * in the 'except' network range.
3268 */
3269 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003270 (((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 +01003271 != s->fe->except_net.s_addr) &&
3272 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003273 (((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 +01003274 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003275 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003277 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003278
3279 /* Note: we rely on the backend to get the header name to be used for
3280 * x-forwarded-for, because the header is really meant for the backends.
3281 * However, if the backend did not specify any option, we have to rely
3282 * on the frontend's header name.
3283 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 if (s->be->fwdfor_hdr_len) {
3285 len = s->be->fwdfor_hdr_len;
3286 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003287 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 len = s->fe->fwdfor_hdr_len;
3289 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003290 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003292
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003293 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003294 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003295 }
3296 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003297 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003298 /* FIXME: for the sake of completeness, we should also support
3299 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 int len;
3302 char pn[INET6_ADDRSTRLEN];
3303 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003304 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003306
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 /* Note: we rely on the backend to get the header name to be used for
3308 * x-forwarded-for, because the header is really meant for the backends.
3309 * However, if the backend did not specify any option, we have to rely
3310 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003311 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 if (s->be->fwdfor_hdr_len) {
3313 len = s->be->fwdfor_hdr_len;
3314 memcpy(trash, s->be->fwdfor_hdr_name, len);
3315 } else {
3316 len = s->fe->fwdfor_hdr_len;
3317 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003318 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003320
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003321 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 goto return_bad_req;
3323 }
3324 }
3325
3326 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003327 * 10: add X-Original-To if either the frontend or the backend
3328 * asks for it.
3329 */
3330 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3331
3332 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003333 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003334 /* Add an X-Original-To header unless the destination IP is
3335 * in the 'except' network range.
3336 */
Willy Tarreau59b94792012-05-11 16:16:40 +02003337 si_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003338
Willy Tarreau6471afb2011-09-23 10:54:59 +02003339 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003340 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003341 (((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 +02003342 != s->fe->except_to.s_addr) &&
3343 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003344 (((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 +02003345 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003346 int len;
3347 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003348 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003349
3350 /* Note: we rely on the backend to get the header name to be used for
3351 * x-original-to, because the header is really meant for the backends.
3352 * However, if the backend did not specify any option, we have to rely
3353 * on the frontend's header name.
3354 */
3355 if (s->be->orgto_hdr_len) {
3356 len = s->be->orgto_hdr_len;
3357 memcpy(trash, s->be->orgto_hdr_name, len);
3358 } else {
3359 len = s->fe->orgto_hdr_len;
3360 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003361 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003362 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3363
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003364 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003365 goto return_bad_req;
3366 }
3367 }
3368 }
3369
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003370 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3371 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003372 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 unsigned int want_flags = 0;
3374
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003375 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003376 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3377 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3378 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003379 want_flags |= TX_CON_CLO_SET;
3380 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003381 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3382 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3383 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003384 want_flags |= TX_CON_KAL_SET;
3385 }
3386
3387 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003388 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003389 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003390
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003391
Willy Tarreau522d6c02009-12-06 18:49:18 +01003392 /* If we have no server assigned yet and we're balancing on url_param
3393 * with a POST request, we may be interested in checking the body for
3394 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003395 */
3396 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3397 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003399 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003400 buffer_dont_connect(req);
3401 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003402 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003403
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003404 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003405 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003406#ifdef TCP_QUICKACK
3407 /* We expect some data from the client. Unless we know for sure
3408 * we already have a full request, we have to re-enable quick-ack
3409 * in case we previously disabled it, otherwise we might cause
3410 * the client to delay further data.
3411 */
3412 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003413 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003414 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003415 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3416#endif
3417 }
Willy Tarreau03945942009-12-22 16:50:27 +01003418
Willy Tarreau59234e92008-11-30 23:51:27 +01003419 /*************************************************************
3420 * OK, that's finished for the headers. We have done what we *
3421 * could. Let's switch to the DATA state. *
3422 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003423 req->analyse_exp = TICK_ETERNITY;
3424 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003425
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003426 /* if the server closes the connection, we want to immediately react
3427 * and close the socket to save packets and syscalls.
3428 */
3429 req->cons->flags |= SI_FL_NOHALF;
3430
Willy Tarreau59234e92008-11-30 23:51:27 +01003431 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 /* OK let's go on with the BODY now */
3433 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003434
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003436 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003437 /* we detected a parsing error. We want to archive this request
3438 * in the dedicated proxy area for later troubleshooting.
3439 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003440 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003441 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003442
Willy Tarreau59234e92008-11-30 23:51:27 +01003443 txn->req.msg_state = HTTP_MSG_ERROR;
3444 txn->status = 400;
3445 req->analysers = 0;
3446 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003447
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003448 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003449 if (s->listener->counters)
3450 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003451
Willy Tarreau59234e92008-11-30 23:51:27 +01003452 if (!(s->flags & SN_ERR_MASK))
3453 s->flags |= SN_ERR_PRXCOND;
3454 if (!(s->flags & SN_FINST_MASK))
3455 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003456 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003457}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003458
Willy Tarreau60b85b02008-11-30 23:28:40 +01003459/* This function is an analyser which processes the HTTP tarpit. It always
3460 * returns zero, at the beginning because it prevents any other processing
3461 * from occurring, and at the end because it terminates the request.
3462 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003463int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003464{
3465 struct http_txn *txn = &s->txn;
3466
3467 /* This connection is being tarpitted. The CLIENT side has
3468 * already set the connect expiration date to the right
3469 * timeout. We just have to check that the client is still
3470 * there and that the timeout has not expired.
3471 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003472 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003473 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3474 !tick_is_expired(req->analyse_exp, now_ms))
3475 return 0;
3476
3477 /* We will set the queue timer to the time spent, just for
3478 * logging purposes. We fake a 500 server error, so that the
3479 * attacker will not suspect his connection has been tarpitted.
3480 * It will not cause trouble to the logs because we can exclude
3481 * the tarpitted connections by filtering on the 'PT' status flags.
3482 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003483 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3484
3485 txn->status = 500;
3486 if (req->flags != BF_READ_ERROR)
3487 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3488
3489 req->analysers = 0;
3490 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003491
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003492 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003493 if (s->listener->counters)
3494 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003495
Willy Tarreau60b85b02008-11-30 23:28:40 +01003496 if (!(s->flags & SN_ERR_MASK))
3497 s->flags |= SN_ERR_PRXCOND;
3498 if (!(s->flags & SN_FINST_MASK))
3499 s->flags |= SN_FINST_T;
3500 return 0;
3501}
3502
Willy Tarreaud34af782008-11-30 23:36:37 +01003503/* This function is an analyser which processes the HTTP request body. It looks
3504 * for parameters to be used for the load balancing algorithm (url_param). It
3505 * must only be called after the standard HTTP request processing has occurred,
3506 * because it expects the request to be parsed. It returns zero if it needs to
3507 * read more data, or 1 once it has completed its analysis.
3508 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003509int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003510{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003511 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003512 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003513 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003514
3515 /* We have to parse the HTTP request body to find any required data.
3516 * "balance url_param check_post" should have been the only way to get
3517 * into this. We were brought here after HTTP header analysis, so all
3518 * related structures are ready.
3519 */
3520
Willy Tarreau522d6c02009-12-06 18:49:18 +01003521 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3522 goto missing_data;
3523
3524 if (msg->msg_state < HTTP_MSG_100_SENT) {
3525 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3526 * send an HTTP/1.1 100 Continue intermediate response.
3527 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003528 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529 struct hdr_ctx ctx;
3530 ctx.idx = 0;
3531 /* Expect is allowed in 1.1, look for it */
Willy Tarreau09d1e252012-05-18 22:36:34 +02003532 if (http_find_header2("Expect", 6, req->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003534 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003535 }
3536 }
3537 msg->msg_state = HTTP_MSG_100_SENT;
3538 }
3539
3540 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003541 /* we have msg->sov which points to the first byte of message body.
3542 * msg->som still points to the beginning of the message. We must
3543 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003544 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003545 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003546
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003547 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3549 else
3550 msg->msg_state = HTTP_MSG_DATA;
3551 }
3552
3553 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003554 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003555 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003556 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003557 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003558 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003559
Willy Tarreau115acb92009-12-26 13:56:06 +01003560 if (!ret)
3561 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003562 else if (ret < 0) {
3563 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003565 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003566 }
3567
Willy Tarreaud98cf932009-12-27 22:54:55 +01003568 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003569 * We have the first data byte is in msg->sov. We're waiting for at
3570 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003571 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003572
Willy Tarreau124d9912011-03-01 20:30:48 +01003573 if (msg->body_len < limit)
3574 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003575
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003576 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 goto http_end;
3578
3579 missing_data:
3580 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003581 if (req->flags & BF_FULL) {
3582 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003583 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003584 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003585
Willy Tarreau522d6c02009-12-06 18:49:18 +01003586 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3587 txn->status = 408;
3588 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003589
3590 if (!(s->flags & SN_ERR_MASK))
3591 s->flags |= SN_ERR_CLITO;
3592 if (!(s->flags & SN_FINST_MASK))
3593 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003595 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003596
3597 /* we get here if we need to wait for more data */
3598 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003599 /* Not enough data. We'll re-use the http-request
3600 * timeout here. Ideally, we should set the timeout
3601 * relative to the accept() date. We just set the
3602 * request timeout once at the beginning of the
3603 * request.
3604 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003605 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003607 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003608 return 0;
3609 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610
3611 http_end:
3612 /* The situation will not evolve, so let's give up on the analysis. */
3613 s->logs.tv_request = now; /* update the request timer to reflect full request */
3614 req->analysers &= ~an_bit;
3615 req->analyse_exp = TICK_ETERNITY;
3616 return 1;
3617
3618 return_bad_req: /* let's centralize all bad requests */
3619 txn->req.msg_state = HTTP_MSG_ERROR;
3620 txn->status = 400;
3621 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3622
Willy Tarreau79ebac62010-06-07 13:47:49 +02003623 if (!(s->flags & SN_ERR_MASK))
3624 s->flags |= SN_ERR_PRXCOND;
3625 if (!(s->flags & SN_FINST_MASK))
3626 s->flags |= SN_FINST_R;
3627
Willy Tarreau522d6c02009-12-06 18:49:18 +01003628 return_err_msg:
3629 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003630 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631 if (s->listener->counters)
3632 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003634}
3635
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003636/* send a server's name with an outgoing request over an established connection.
3637 * Note: this function is designed to be called once the request has been scheduled
3638 * for being forwarded. This is the reason why it rewinds the buffer before
3639 * proceeding.
3640 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003641int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003642
3643 struct hdr_ctx ctx;
3644
Mark Lamourinec2247f02012-01-04 13:02:01 -05003645 char *hdr_name = be->server_id_hdr_name;
3646 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003647 struct buffer *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003648 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003649 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003650
William Lallemandd9e90662012-01-30 17:27:17 +01003651 ctx.idx = 0;
3652
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003653 old_o = req->o;
3654 if (old_o) {
3655 /* The request was already skipped, let's restore it */
3656 b_rew(req, old_o);
3657 }
3658
3659 old_i = req->i;
Willy Tarreau09d1e252012-05-18 22:36:34 +02003660 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003661 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003662 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003663 }
3664
3665 /* Add the new header requested with the server value */
3666 hdr_val = trash;
3667 memcpy(hdr_val, hdr_name, hdr_name_len);
3668 hdr_val += hdr_name_len;
3669 *hdr_val++ = ':';
3670 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003671 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003672 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003674 if (old_o) {
3675 /* If this was a forwarded request, we must readjust the amount of
3676 * data to be forwarded in order to take into account the size
3677 * variations.
3678 */
3679 b_adv(req, old_o + req->i - old_i);
3680 }
3681
Mark Lamourinec2247f02012-01-04 13:02:01 -05003682 return 0;
3683}
3684
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685/* Terminate current transaction and prepare a new one. This is very tricky
3686 * right now but it works.
3687 */
3688void http_end_txn_clean_session(struct session *s)
3689{
3690 /* FIXME: We need a more portable way of releasing a backend's and a
3691 * server's connections. We need a safer way to reinitialize buffer
3692 * flags. We also need a more accurate method for computing per-request
3693 * data.
3694 */
3695 http_silent_debug(__LINE__, s);
3696
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003697 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau060781f2012-05-07 16:50:03 +02003698 s->req->cons->sock.shutr(s->req->cons);
3699 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003700
3701 http_silent_debug(__LINE__, s);
3702
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003703 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003704 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003705 if (unlikely(s->srv_conn))
3706 sess_change_server(s, NULL);
3707 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708
3709 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3710 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003711 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003712
3713 if (s->txn.status) {
3714 int n;
3715
3716 n = s->txn.status / 100;
3717 if (n < 1 || n > 5)
3718 n = 0;
3719
3720 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003721 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722
Willy Tarreau24657792010-02-26 10:30:28 +01003723 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003724 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003725 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726 }
3727
3728 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003729 s->logs.bytes_in -= s->req->i;
3730 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731
3732 /* let's do a final log if we need it */
3733 if (s->logs.logwait &&
3734 !(s->flags & SN_MONITOR) &&
3735 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3736 s->do_log(s);
3737 }
3738
3739 s->logs.accept_date = date; /* user-visible date for logging */
3740 s->logs.tv_accept = now; /* corrected date for internal use */
3741 tv_zero(&s->logs.tv_request);
3742 s->logs.t_queue = -1;
3743 s->logs.t_connect = -1;
3744 s->logs.t_data = -1;
3745 s->logs.t_close = 0;
3746 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3747 s->logs.srv_queue_size = 0; /* we will get this number soon */
3748
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003749 s->logs.bytes_in = s->req->total = s->req->i;
3750 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751
3752 if (s->pend_pos)
3753 pendconn_free(s->pend_pos);
3754
Willy Tarreau827aee92011-03-10 16:55:02 +01003755 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756 if (s->flags & SN_CURR_SESS) {
3757 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003758 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003760 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3761 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 }
3763
Willy Tarreau9e000c62011-03-10 14:03:36 +01003764 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765
3766 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3767 s->req->cons->fd = -1; /* just to help with debugging */
3768 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003769 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770 s->req->cons->err_loc = NULL;
3771 s->req->cons->exp = TICK_ETERNITY;
3772 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003773 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3774 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 +02003775 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 +01003776 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3777 s->txn.meth = 0;
3778 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003779 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003780 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 s->req->cons->flags |= SI_FL_INDEP_STR;
3782
Willy Tarreau96e31212011-05-30 18:10:30 +02003783 if (s->fe->options2 & PR_O2_NODELAY) {
3784 s->req->flags |= BF_NEVER_WAIT;
3785 s->rep->flags |= BF_NEVER_WAIT;
3786 }
3787
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 /* if the request buffer is not empty, it means we're
3789 * about to process another request, so send pending
3790 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003791 * Just don't do this if the buffer is close to be full,
3792 * because the request will wait for it to flush a little
3793 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003795 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003796 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003797 !(s->rep->flags & BF_FULL) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02003798 bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003799 s->rep->flags |= BF_EXPECT_MORE;
3800 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003801
3802 /* we're removing the analysers, we MUST re-enable events detection */
3803 buffer_auto_read(s->req);
3804 buffer_auto_close(s->req);
3805 buffer_auto_read(s->rep);
3806 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807
Willy Tarreau342b11c2010-11-24 16:22:09 +01003808 s->req->analysers = s->listener->analysers;
3809 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 s->rep->analysers = 0;
3811
3812 http_silent_debug(__LINE__, s);
3813}
3814
3815
3816/* This function updates the request state machine according to the response
3817 * state machine and buffer flags. It returns 1 if it changes anything (flag
3818 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3819 * it is only used to find when a request/response couple is complete. Both
3820 * this function and its equivalent should loop until both return zero. It
3821 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3822 */
3823int http_sync_req_state(struct session *s)
3824{
3825 struct buffer *buf = s->req;
3826 struct http_txn *txn = &s->txn;
3827 unsigned int old_flags = buf->flags;
3828 unsigned int old_state = txn->req.msg_state;
3829
3830 http_silent_debug(__LINE__, s);
3831 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3832 return 0;
3833
3834 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003835 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003836 * We can shut the read side unless we want to abort_on_close,
3837 * or we have a POST request. The issue with POST requests is
3838 * that some browsers still send a CRLF after the request, and
3839 * this CRLF must be read so that it does not remain in the kernel
3840 * buffers, otherwise a close could cause an RST on some systems
3841 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003842 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003843 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003844 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003845
3846 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3847 goto wait_other_side;
3848
3849 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3850 /* The server has not finished to respond, so we
3851 * don't want to move in order not to upset it.
3852 */
3853 goto wait_other_side;
3854 }
3855
3856 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3857 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003858 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003859 txn->req.msg_state = HTTP_MSG_TUNNEL;
3860 goto wait_other_side;
3861 }
3862
3863 /* When we get here, it means that both the request and the
3864 * response have finished receiving. Depending on the connection
3865 * mode, we'll have to wait for the last bytes to leave in either
3866 * direction, and sometimes for a close to be effective.
3867 */
3868
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003869 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3870 /* Server-close mode : queue a connection close to the server */
3871 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003872 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003873 }
3874 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3875 /* Option forceclose is set, or either side wants to close,
3876 * let's enforce it now that we're not expecting any new
3877 * data to come. The caller knows the session is complete
3878 * once both states are CLOSED.
3879 */
3880 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003881 buffer_shutr_now(buf);
3882 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003883 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003884 }
3885 else {
3886 /* The last possible modes are keep-alive and tunnel. Since tunnel
3887 * mode does not set the body analyser, we can't reach this place
3888 * in tunnel mode, so we're left with keep-alive only.
3889 * This mode is currently not implemented, we switch to tunnel mode.
3890 */
3891 buffer_auto_read(buf);
3892 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003893 }
3894
3895 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3896 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003897 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3898
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899 if (!(buf->flags & BF_OUT_EMPTY)) {
3900 txn->req.msg_state = HTTP_MSG_CLOSING;
3901 goto http_msg_closing;
3902 }
3903 else {
3904 txn->req.msg_state = HTTP_MSG_CLOSED;
3905 goto http_msg_closed;
3906 }
3907 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003908 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003909 }
3910
3911 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3912 http_msg_closing:
3913 /* nothing else to forward, just waiting for the output buffer
3914 * to be empty and for the shutw_now to take effect.
3915 */
3916 if (buf->flags & BF_OUT_EMPTY) {
3917 txn->req.msg_state = HTTP_MSG_CLOSED;
3918 goto http_msg_closed;
3919 }
3920 else if (buf->flags & BF_SHUTW) {
3921 txn->req.msg_state = HTTP_MSG_ERROR;
3922 goto wait_other_side;
3923 }
3924 }
3925
3926 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3927 http_msg_closed:
3928 goto wait_other_side;
3929 }
3930
3931 wait_other_side:
3932 http_silent_debug(__LINE__, s);
3933 return txn->req.msg_state != old_state || buf->flags != old_flags;
3934}
3935
3936
3937/* This function updates the response state machine according to the request
3938 * state machine and buffer flags. It returns 1 if it changes anything (flag
3939 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3940 * it is only used to find when a request/response couple is complete. Both
3941 * this function and its equivalent should loop until both return zero. It
3942 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3943 */
3944int http_sync_res_state(struct session *s)
3945{
3946 struct buffer *buf = s->rep;
3947 struct http_txn *txn = &s->txn;
3948 unsigned int old_flags = buf->flags;
3949 unsigned int old_state = txn->rsp.msg_state;
3950
3951 http_silent_debug(__LINE__, s);
3952 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3953 return 0;
3954
3955 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3956 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003957 * still monitor the server connection for a possible close
3958 * while the request is being uploaded, so we don't disable
3959 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003960 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003961 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003962
3963 if (txn->req.msg_state == HTTP_MSG_ERROR)
3964 goto wait_other_side;
3965
3966 if (txn->req.msg_state < HTTP_MSG_DONE) {
3967 /* The client seems to still be sending data, probably
3968 * because we got an error response during an upload.
3969 * We have the choice of either breaking the connection
3970 * or letting it pass through. Let's do the later.
3971 */
3972 goto wait_other_side;
3973 }
3974
3975 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3976 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003977 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003978 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3979 goto wait_other_side;
3980 }
3981
3982 /* When we get here, it means that both the request and the
3983 * response have finished receiving. Depending on the connection
3984 * mode, we'll have to wait for the last bytes to leave in either
3985 * direction, and sometimes for a close to be effective.
3986 */
3987
3988 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3989 /* Server-close mode : shut read and wait for the request
3990 * side to close its output buffer. The caller will detect
3991 * when we're in DONE and the other is in CLOSED and will
3992 * catch that for the final cleanup.
3993 */
3994 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3995 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003997 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3998 /* Option forceclose is set, or either side wants to close,
3999 * let's enforce it now that we're not expecting any new
4000 * data to come. The caller knows the session is complete
4001 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004003 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4004 buffer_shutr_now(buf);
4005 buffer_shutw_now(buf);
4006 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 }
4008 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004009 /* The last possible modes are keep-alive and tunnel. Since tunnel
4010 * mode does not set the body analyser, we can't reach this place
4011 * in tunnel mode, so we're left with keep-alive only.
4012 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004014 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004016 }
4017
4018 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4019 /* if we've just closed an output, let's switch */
4020 if (!(buf->flags & BF_OUT_EMPTY)) {
4021 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4022 goto http_msg_closing;
4023 }
4024 else {
4025 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4026 goto http_msg_closed;
4027 }
4028 }
4029 goto wait_other_side;
4030 }
4031
4032 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4033 http_msg_closing:
4034 /* nothing else to forward, just waiting for the output buffer
4035 * to be empty and for the shutw_now to take effect.
4036 */
4037 if (buf->flags & BF_OUT_EMPTY) {
4038 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4039 goto http_msg_closed;
4040 }
4041 else if (buf->flags & BF_SHUTW) {
4042 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004043 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004044 if (target_srv(&s->target))
4045 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 goto wait_other_side;
4047 }
4048 }
4049
4050 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4051 http_msg_closed:
4052 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004053 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004055 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004056 goto wait_other_side;
4057 }
4058
4059 wait_other_side:
4060 http_silent_debug(__LINE__, s);
4061 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4062}
4063
4064
4065/* Resync the request and response state machines. Return 1 if either state
4066 * changes.
4067 */
4068int http_resync_states(struct session *s)
4069{
4070 struct http_txn *txn = &s->txn;
4071 int old_req_state = txn->req.msg_state;
4072 int old_res_state = txn->rsp.msg_state;
4073
4074 http_silent_debug(__LINE__, s);
4075 http_sync_req_state(s);
4076 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004077 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 if (!http_sync_res_state(s))
4079 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004080 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081 if (!http_sync_req_state(s))
4082 break;
4083 }
4084 http_silent_debug(__LINE__, s);
4085 /* OK, both state machines agree on a compatible state.
4086 * There are a few cases we're interested in :
4087 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4088 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4089 * directions, so let's simply disable both analysers.
4090 * - HTTP_MSG_CLOSED on the response only means we must abort the
4091 * request.
4092 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4093 * with server-close mode means we've completed one request and we
4094 * must re-initialize the server connection.
4095 */
4096
4097 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4098 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4099 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4100 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4101 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004102 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004103 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004105 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004106 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004108 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4109 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004110 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004111 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 s->rep->analysers = 0;
4113 buffer_auto_close(s->rep);
4114 buffer_auto_read(s->rep);
4115 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116 buffer_abort(s->req);
4117 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004118 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004119 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120 }
4121 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4122 txn->rsp.msg_state == HTTP_MSG_DONE &&
4123 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4124 /* server-close: terminate this server connection and
4125 * reinitialize a fresh-new transaction.
4126 */
4127 http_end_txn_clean_session(s);
4128 }
4129
4130 http_silent_debug(__LINE__, s);
4131 return txn->req.msg_state != old_req_state ||
4132 txn->rsp.msg_state != old_res_state;
4133}
4134
Willy Tarreaud98cf932009-12-27 22:54:55 +01004135/* This function is an analyser which forwards request body (including chunk
4136 * sizes if any). It is called as soon as we must forward, even if we forward
4137 * zero byte. The only situation where it must not be called is when we're in
4138 * tunnel mode and we want to forward till the close. It's used both to forward
4139 * remaining data and to resync after end of body. It expects the msg_state to
4140 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4141 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004142 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004143 * bytes of pending data + the headers if not already done (between som and sov).
4144 * It eventually adjusts som to match sov after the data in between have been sent.
4145 */
4146int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4147{
4148 struct http_txn *txn = &s->txn;
4149 struct http_msg *msg = &s->txn.req;
4150
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004151 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4152 return 0;
4153
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004154 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004155 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004156 /* Output closed while we were sending data. We must abort and
4157 * wake the other side up.
4158 */
4159 msg->msg_state = HTTP_MSG_ERROR;
4160 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004161 return 1;
4162 }
4163
Willy Tarreau4fe41902010-06-07 22:27:41 +02004164 /* in most states, we should abort in case of early close */
4165 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004166
4167 /* Note that we don't have to send 100-continue back because we don't
4168 * need the data to complete our job, and it's up to the server to
4169 * decide whether to return 100, 417 or anything else in return of
4170 * an "Expect: 100-continue" header.
4171 */
4172
4173 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004174 /* we have msg->sov which points to the first byte of message body.
4175 * msg->som still points to the beginning of the message. We must
4176 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004177 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004178 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004179
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004180 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004181 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4182 else {
4183 msg->msg_state = HTTP_MSG_DATA;
4184 }
4185 }
4186
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004188 int bytes;
4189
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004191 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004192 bytes = msg->sov - msg->som;
4193 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004194 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004195 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4196 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004197 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004198 msg->chunk_len += (unsigned int)bytes;
4199 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004200 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004201
Willy Tarreaucaabe412010-01-03 23:08:28 +01004202 if (msg->msg_state == HTTP_MSG_DATA) {
4203 /* must still forward */
4204 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004205 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004206
4207 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004208 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004209 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004211 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004212 }
4213 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004214 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004215 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004216 * TRAILERS state.
4217 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004218 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004219
4220 if (!ret)
4221 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004222 else if (ret < 0) {
4223 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004224 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004225 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004226 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004227 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004228 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004229 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004230 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4231 /* we want the CRLF after the data */
4232 int ret;
4233
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004234 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235
4236 if (ret == 0)
4237 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004238 else if (ret < 0) {
4239 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004240 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004241 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004242 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004243 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004244 /* we're in MSG_CHUNK_SIZE now */
4245 }
4246 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004247 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248
4249 if (ret == 0)
4250 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004251 else if (ret < 0) {
4252 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004253 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004254 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004255 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004256 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004257 /* we're in HTTP_MSG_DONE now */
4258 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004260 int old_state = msg->msg_state;
4261
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004263 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004264 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4265 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4266 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267 if (http_resync_states(s)) {
4268 /* some state changes occurred, maybe the analyser
4269 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004270 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004271 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4272 if (req->flags & BF_SHUTW) {
4273 /* request errors are most likely due to
4274 * the server aborting the transfer.
4275 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004276 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004277 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004278 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004279 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004280 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004281 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004283 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004284
4285 /* If "option abortonclose" is set on the backend, we
4286 * want to monitor the client's connection and forward
4287 * any shutdown notification to the server, which will
4288 * decide whether to close or to go on processing the
4289 * request.
4290 */
4291 if (s->be->options & PR_O_ABRT_CLOSE) {
4292 buffer_auto_read(req);
4293 buffer_auto_close(req);
4294 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004295 else if (s->txn.meth == HTTP_METH_POST) {
4296 /* POST requests may require to read extra CRLF
4297 * sent by broken browsers and which could cause
4298 * an RST to be sent upon close on some systems
4299 * (eg: Linux).
4300 */
4301 buffer_auto_read(req);
4302 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004303
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004305 }
4306 }
4307
Willy Tarreaud98cf932009-12-27 22:54:55 +01004308 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004309 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004310 if (req->flags & BF_SHUTR) {
4311 if (!(s->flags & SN_ERR_MASK))
4312 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004313 if (!(s->flags & SN_FINST_MASK)) {
4314 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4315 s->flags |= SN_FINST_H;
4316 else
4317 s->flags |= SN_FINST_D;
4318 }
4319
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004320 s->fe->fe_counters.cli_aborts++;
4321 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004322 if (target_srv(&s->target))
4323 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004324
4325 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004326 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004327
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004328 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004329 if (req->flags & BF_SHUTW)
4330 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004332 /* When TE: chunked is used, we need to get there again to parse remaining
4333 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4334 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004335 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004336 buffer_dont_close(req);
4337
Willy Tarreau5c620922011-05-11 19:56:11 +02004338 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004339 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4340 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004341 * modes are already handled by the stream sock layer. We must not do
4342 * this in content-length mode because it could present the MSG_MORE
4343 * flag with the last block of forwarded data, which would cause an
4344 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004345 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004346 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004347 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004348
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004350 return 0;
4351
Willy Tarreaud98cf932009-12-27 22:54:55 +01004352 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004353 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 if (s->listener->counters)
4355 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004356 return_bad_req_stats_ok:
4357 txn->req.msg_state = HTTP_MSG_ERROR;
4358 if (txn->status) {
4359 /* Note: we don't send any error if some data were already sent */
4360 stream_int_retnclose(req->prod, NULL);
4361 } else {
4362 txn->status = 400;
4363 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4364 }
4365 req->analysers = 0;
4366 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004367
4368 if (!(s->flags & SN_ERR_MASK))
4369 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004370 if (!(s->flags & SN_FINST_MASK)) {
4371 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4372 s->flags |= SN_FINST_H;
4373 else
4374 s->flags |= SN_FINST_D;
4375 }
4376 return 0;
4377
4378 aborted_xfer:
4379 txn->req.msg_state = HTTP_MSG_ERROR;
4380 if (txn->status) {
4381 /* Note: we don't send any error if some data were already sent */
4382 stream_int_retnclose(req->prod, NULL);
4383 } else {
4384 txn->status = 502;
4385 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4386 }
4387 req->analysers = 0;
4388 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4389
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004390 s->fe->fe_counters.srv_aborts++;
4391 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004392 if (target_srv(&s->target))
4393 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004394
4395 if (!(s->flags & SN_ERR_MASK))
4396 s->flags |= SN_ERR_SRVCL;
4397 if (!(s->flags & SN_FINST_MASK)) {
4398 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4399 s->flags |= SN_FINST_H;
4400 else
4401 s->flags |= SN_FINST_D;
4402 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004403 return 0;
4404}
4405
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004406/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4407 * processing can continue on next analysers, or zero if it either needs more
4408 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4409 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4410 * when it has nothing left to do, and may remove any analyser when it wants to
4411 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004412 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004413int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004414{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 struct http_txn *txn = &s->txn;
4416 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004417 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004418 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004420 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004421
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004422 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 +02004423 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004424 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004425 rep,
4426 rep->rex, rep->wex,
4427 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004428 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004429 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004430
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004431 /*
4432 * Now parse the partial (or complete) lines.
4433 * We will check the response syntax, and also join multi-line
4434 * headers. An index of all the lines will be elaborated while
4435 * parsing.
4436 *
4437 * For the parsing, we use a 28 states FSM.
4438 *
4439 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004440 * rep->data + msg->som = beginning of response
4441 * rep->data + msg->eoh = end of processed headers / start of current one
4442 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004443 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 * rep->r = end of data
4445 */
4446
Willy Tarreau83e3af02009-12-28 17:39:57 +01004447 /* There's a protected area at the end of the buffer for rewriting
4448 * purposes. We don't want to start to parse the request if the
4449 * protected area is affected, because we may have to move processed
4450 * data later, which is much more complicated.
4451 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004452 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004453 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02004454 bi_end(rep) < b_ptr(rep, msg->next) ||
4455 bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004456 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004457 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004458 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4459 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004460 buffer_dont_close(rep);
4461 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4462 return 0;
4463 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004464 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreaua7fe8e52012-05-08 20:40:09 +02004465 buffer_slow_realign(msg->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004466 }
4467
Willy Tarreaua458b672012-03-05 11:17:50 +01004468 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004469 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004470 }
4471
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004472 /* 1: we might have to print this header in debug mode */
4473 if (unlikely((global.mode & MODE_DEBUG) &&
4474 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004475 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004477
Willy Tarreauea1175a2012-03-05 15:52:30 +01004478 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004479 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004481
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482 sol += hdr_idx_first_pos(&txn->hdr_idx);
4483 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004484
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004485 while (cur_idx) {
4486 eol = sol + txn->hdr_idx.v[cur_idx].len;
4487 debug_hdr("srvhdr", s, sol, eol);
4488 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4489 cur_idx = txn->hdr_idx.v[cur_idx].next;
4490 }
4491 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004492
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 /*
4494 * Now we quickly check if we have found a full valid response.
4495 * If not so, we check the FD and buffer states before leaving.
4496 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004497 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498 * responses are checked first.
4499 *
4500 * Depending on whether the client is still there or not, we
4501 * may send an error response back or not. Note that normally
4502 * we should only check for HTTP status there, and check I/O
4503 * errors somewhere else.
4504 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004505
Willy Tarreau655dce92009-11-08 13:10:58 +01004506 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 /* Invalid response */
4508 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4509 /* we detected a parsing error. We want to archive this response
4510 * in the dedicated proxy area for later troubleshooting.
4511 */
4512 hdr_response_bad:
4513 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004514 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004516 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004517 if (target_srv(&s->target)) {
4518 target_srv(&s->target)->counters.failed_resp++;
4519 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004520 }
Willy Tarreau64648412010-03-05 10:41:54 +01004521 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004522 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523 rep->analysers = 0;
4524 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004525 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004526 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4528
4529 if (!(s->flags & SN_ERR_MASK))
4530 s->flags |= SN_ERR_PRXCOND;
4531 if (!(s->flags & SN_FINST_MASK))
4532 s->flags |= SN_FINST_H;
4533
4534 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004535 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 /* too large response does not fit in buffer. */
4538 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004539 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004540 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 goto hdr_response_bad;
4542 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 /* read error */
4545 else if (rep->flags & BF_READ_ERROR) {
4546 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004547 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004548
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004549 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004550 if (target_srv(&s->target)) {
4551 target_srv(&s->target)->counters.failed_resp++;
4552 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004553 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004554
Willy Tarreau90deb182010-01-07 00:20:41 +01004555 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 rep->analysers = 0;
4557 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004558 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004559 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004561
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 if (!(s->flags & SN_ERR_MASK))
4563 s->flags |= SN_ERR_SRVCL;
4564 if (!(s->flags & SN_FINST_MASK))
4565 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004566 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004568
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 /* read timeout : return a 504 to the client. */
4570 else if (rep->flags & BF_READ_TIMEOUT) {
4571 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004572 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004573
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004574 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004575 if (target_srv(&s->target)) {
4576 target_srv(&s->target)->counters.failed_resp++;
4577 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004578 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004579
Willy Tarreau90deb182010-01-07 00:20:41 +01004580 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 rep->analysers = 0;
4582 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004583 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004584 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004587 if (!(s->flags & SN_ERR_MASK))
4588 s->flags |= SN_ERR_SRVTO;
4589 if (!(s->flags & SN_FINST_MASK))
4590 s->flags |= SN_FINST_H;
4591 return 0;
4592 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004593
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004594 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004596 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004597 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004598
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004599 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004600 if (target_srv(&s->target)) {
4601 target_srv(&s->target)->counters.failed_resp++;
4602 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004603 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004604
Willy Tarreau90deb182010-01-07 00:20:41 +01004605 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 rep->analysers = 0;
4607 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004608 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004609 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004611
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 if (!(s->flags & SN_ERR_MASK))
4613 s->flags |= SN_ERR_SRVCL;
4614 if (!(s->flags & SN_FINST_MASK))
4615 s->flags |= SN_FINST_H;
4616 return 0;
4617 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004618
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 /* write error to client (we don't send any message then) */
4620 else if (rep->flags & BF_WRITE_ERROR) {
4621 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004622 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004623
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004624 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004626 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627
4628 if (!(s->flags & SN_ERR_MASK))
4629 s->flags |= SN_ERR_CLICL;
4630 if (!(s->flags & SN_FINST_MASK))
4631 s->flags |= SN_FINST_H;
4632
4633 /* process_session() will take care of the error */
4634 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004635 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 buffer_dont_close(rep);
4638 return 0;
4639 }
4640
4641 /* More interesting part now : we know that we have a complete
4642 * response which at least looks like HTTP. We have an indicator
4643 * of each header's length, so we can parse them quickly.
4644 */
4645
4646 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004647 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 /*
4650 * 1: get the status code
4651 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02004652 n = rep->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653 if (n < 1 || n > 5)
4654 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004655 /* when the client triggers a 4xx from the server, it's most often due
4656 * to a missing object or permission. These events should be tracked
4657 * because if they happen often, it may indicate a brute force or a
4658 * vulnerability scan.
4659 */
4660 if (n == 4)
4661 session_inc_http_err_ctr(s);
4662
Willy Tarreau827aee92011-03-10 16:55:02 +01004663 if (target_srv(&s->target))
4664 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665
Willy Tarreau5b154472009-12-21 20:11:07 +01004666 /* check if the response is HTTP/1.1 or above */
4667 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004668 ((rep->p[5] > '1') ||
4669 ((rep->p[5] == '1') && (rep->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004670 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004671
4672 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004673 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 +01004674
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004675 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004676 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004677
Willy Tarreau09d1e252012-05-18 22:36:34 +02004678 txn->status = strl2ui(rep->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679
Willy Tarreau39650402010-03-15 19:44:39 +01004680 /* Adjust server's health based on status code. Note: status codes 501
4681 * and 505 are triggered on demand by client request, so we must not
4682 * count them as server failures.
4683 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004684 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004685 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004686 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004687 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004688 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004689 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004690
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004691 /*
4692 * 2: check for cacheability.
4693 */
4694
4695 switch (txn->status) {
4696 case 200:
4697 case 203:
4698 case 206:
4699 case 300:
4700 case 301:
4701 case 410:
4702 /* RFC2616 @13.4:
4703 * "A response received with a status code of
4704 * 200, 203, 206, 300, 301 or 410 MAY be stored
4705 * by a cache (...) unless a cache-control
4706 * directive prohibits caching."
4707 *
4708 * RFC2616 @9.5: POST method :
4709 * "Responses to this method are not cacheable,
4710 * unless the response includes appropriate
4711 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004713 if (likely(txn->meth != HTTP_METH_POST) &&
4714 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4715 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4716 break;
4717 default:
4718 break;
4719 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004720
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 /*
4722 * 3: we may need to capture headers
4723 */
4724 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004725 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau09d1e252012-05-18 22:36:34 +02004726 capture_headers(rep->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 txn->rsp.cap, s->fe->rsp_cap);
4728
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004729 /* 4: determine the transfer-length.
4730 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4731 * the presence of a message-body in a RESPONSE and its transfer length
4732 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004733 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004734 * All responses to the HEAD request method MUST NOT include a
4735 * message-body, even though the presence of entity-header fields
4736 * might lead one to believe they do. All 1xx (informational), 204
4737 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4738 * message-body. All other responses do include a message-body,
4739 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004740 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004741 * 1. Any response which "MUST NOT" include a message-body (such as the
4742 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4743 * always terminated by the first empty line after the header fields,
4744 * regardless of the entity-header fields present in the message.
4745 *
4746 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4747 * the "chunked" transfer-coding (Section 6.2) is used, the
4748 * transfer-length is defined by the use of this transfer-coding.
4749 * If a Transfer-Encoding header field is present and the "chunked"
4750 * transfer-coding is not present, the transfer-length is defined by
4751 * the sender closing the connection.
4752 *
4753 * 3. If a Content-Length header field is present, its decimal value in
4754 * OCTETs represents both the entity-length and the transfer-length.
4755 * If a message is received with both a Transfer-Encoding header
4756 * field and a Content-Length header field, the latter MUST be ignored.
4757 *
4758 * 4. If the message uses the media type "multipart/byteranges", and
4759 * the transfer-length is not otherwise specified, then this self-
4760 * delimiting media type defines the transfer-length. This media
4761 * type MUST NOT be used unless the sender knows that the recipient
4762 * can parse it; the presence in a request of a Range header with
4763 * multiple byte-range specifiers from a 1.1 client implies that the
4764 * client can parse multipart/byteranges responses.
4765 *
4766 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004767 */
4768
4769 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004770 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004771 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004772 * FIXME: should we parse anyway and return an error on chunked encoding ?
4773 */
4774 if (txn->meth == HTTP_METH_HEAD ||
4775 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004776 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004777 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004778 goto skip_content_length;
4779 }
4780
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004781 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004782 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004783 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004784 http_find_header2("Transfer-Encoding", 17, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004785 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004786 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4787 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004788 /* bad transfer-encoding (chunked followed by something else) */
4789 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004790 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004791 break;
4792 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004793 }
4794
4795 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4796 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau09d1e252012-05-18 22:36:34 +02004798 http_find_header2("Content-Length", 14, rep->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004799 signed long long cl;
4800
Willy Tarreauad14f752011-09-02 20:33:27 +02004801 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004802 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004803 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004804 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004805
Willy Tarreauad14f752011-09-02 20:33:27 +02004806 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004807 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004808 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004809 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004810
Willy Tarreauad14f752011-09-02 20:33:27 +02004811 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004812 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004813 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004814 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004815
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004816 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004817 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004818 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004819 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004820
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004821 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004822 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823 }
4824
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004825 /* FIXME: we should also implement the multipart/byterange method.
4826 * For now on, we resort to close mode in this case (unknown length).
4827 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 /* end of job, return OK */
4831 rep->analysers &= ~an_bit;
4832 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004833 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 return 1;
4835}
4836
4837/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004838 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4839 * and updates t->rep->analysers. It might make sense to explode it into several
4840 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 */
4842int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4843{
4844 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004845 struct http_msg *msg = &txn->rsp;
4846 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004847 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004849 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 +02004850 now_ms, __FUNCTION__,
4851 t,
4852 rep,
4853 rep->rex, rep->wex,
4854 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004855 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856 rep->analysers);
4857
Willy Tarreau655dce92009-11-08 13:10:58 +01004858 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 return 0;
4860
4861 rep->analysers &= ~an_bit;
4862 rep->analyse_exp = TICK_ETERNITY;
4863
Willy Tarreau5b154472009-12-21 20:11:07 +01004864 /* Now we have to check if we need to modify the Connection header.
4865 * This is more difficult on the response than it is on the request,
4866 * because we can have two different HTTP versions and we don't know
4867 * how the client will interprete a response. For instance, let's say
4868 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4869 * HTTP/1.1 response without any header. Maybe it will bound itself to
4870 * HTTP/1.0 because it only knows about it, and will consider the lack
4871 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4872 * the lack of header as a keep-alive. Thus we will use two flags
4873 * indicating how a request MAY be understood by the client. In case
4874 * of multiple possibilities, we'll fix the header to be explicit. If
4875 * ambiguous cases such as both close and keepalive are seen, then we
4876 * will fall back to explicit close. Note that we won't take risks with
4877 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004878 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004879 */
4880
Willy Tarreaudc008c52010-02-01 16:20:08 +01004881 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4882 txn->status == 101)) {
4883 /* Either we've established an explicit tunnel, or we're
4884 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004885 * to understand the next protocols. We have to switch to tunnel
4886 * mode, so that we transfer the request and responses then let
4887 * this protocol pass unmodified. When we later implement specific
4888 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004889 * header which contains information about that protocol for
4890 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004891 */
4892 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4893 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004894 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4895 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4896 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004897 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004898
Willy Tarreau60466522010-01-18 19:08:45 +01004899 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004900 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004901 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4902 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004903
Willy Tarreau60466522010-01-18 19:08:45 +01004904 /* now adjust header transformations depending on current state */
4905 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4906 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4907 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004908 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004909 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004910 }
Willy Tarreau60466522010-01-18 19:08:45 +01004911 else { /* SCL / KAL */
4912 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004913 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004914 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004915 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004916
Willy Tarreau60466522010-01-18 19:08:45 +01004917 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004918 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004919
Willy Tarreau60466522010-01-18 19:08:45 +01004920 /* Some keep-alive responses are converted to Server-close if
4921 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004922 */
Willy Tarreau60466522010-01-18 19:08:45 +01004923 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4924 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004925 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004926 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004927 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004928 }
4929
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004930 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004931 /*
4932 * 3: we will have to evaluate the filters.
4933 * As opposed to version 1.2, now they will be evaluated in the
4934 * filters order and not in the header order. This means that
4935 * each filter has to be validated among all headers.
4936 *
4937 * Filters are tried with ->be first, then with ->fe if it is
4938 * different from ->be.
4939 */
4940
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004941 cur_proxy = t->be;
4942 while (1) {
4943 struct proxy *rule_set = cur_proxy;
4944
4945 /* try headers filters */
4946 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004947 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004949 if (target_srv(&t->target)) {
4950 target_srv(&t->target)->counters.failed_resp++;
4951 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004952 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004953 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004955 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004956 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004957 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004958 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004959 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004960 if (!(t->flags & SN_ERR_MASK))
4961 t->flags |= SN_ERR_PRXCOND;
4962 if (!(t->flags & SN_FINST_MASK))
4963 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004964 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004965 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004966 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 /* has the response been denied ? */
4969 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004970 if (target_srv(&t->target))
4971 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004972
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004973 t->be->be_counters.denied_resp++;
4974 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004975 if (t->listener->counters)
4976 t->listener->counters->denied_resp++;
4977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004979 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004980
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004981 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004982 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004983 if (txn->status < 200)
4984 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004985 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004986 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004987 ret = acl_pass(ret);
4988 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4989 ret = !ret;
4990 if (!ret)
4991 continue;
4992 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004993 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004994 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004995 }
4996
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004997 /* check whether we're already working on the frontend */
4998 if (cur_proxy == t->fe)
4999 break;
5000 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005001 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005002
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005003 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005004 * We may be facing a 100-continue response, in which case this
5005 * is not the right response, and we're waiting for the next one.
5006 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005007 * next one.
5008 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005009 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005010 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau09d1e252012-05-18 22:36:34 +02005011 msg->next -= buffer_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005012 msg->msg_state = HTTP_MSG_RPBEFORE;
5013 txn->status = 0;
5014 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5015 return 1;
5016 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005017 else if (unlikely(txn->status < 200))
5018 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005019
5020 /* we don't have any 1xx status code now */
5021
5022 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005023 * 4: check for server cookie.
5024 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005025 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5026 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005027 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005028
Willy Tarreaubaaee002006-06-26 02:48:02 +02005029
Willy Tarreaua15645d2007-03-18 16:22:39 +01005030 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005032 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005033 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005034 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005036 /*
5037 * 6: add server cookie in the response if needed
5038 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005039 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005040 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005041 (!(t->flags & SN_DIRECT) ||
5042 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5043 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5044 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5045 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005046 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5047 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005049 /* the server is known, it's not the one the client requested, or the
5050 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051 * insert a set-cookie here, except if we want to insert only on POST
5052 * requests and this one isn't. Note that servers which don't have cookies
5053 * (eg: some backup servers) will return a full cookie removal request.
5054 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005055 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005056 len = sprintf(trash,
5057 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5058 t->be->cookie_name);
5059 }
5060 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005061 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005062
5063 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5064 /* emit last_date, which is mandatory */
5065 trash[len++] = COOKIE_DELIM_DATE;
5066 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5067 if (t->be->cookie_maxlife) {
5068 /* emit first_date, which is either the original one or
5069 * the current date.
5070 */
5071 trash[len++] = COOKIE_DELIM_DATE;
5072 s30tob64(txn->cookie_first_date ?
5073 txn->cookie_first_date >> 2 :
5074 (date.tv_sec+3) >> 2, trash + len);
5075 len += 5;
5076 }
5077 }
5078 len += sprintf(trash + len, "; path=/");
5079 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081 if (t->be->cookie_domain)
5082 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005083
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005084 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005086
Willy Tarreauf1348312010-10-07 15:54:11 +02005087 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005088 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005089 /* the server did not change, only the date was updated */
5090 txn->flags |= TX_SCK_UPDATED;
5091 else
5092 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 /* Here, we will tell an eventual cache on the client side that we don't
5095 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5096 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5097 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5098 */
5099 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005100
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5102
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005103 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005104 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005106 }
5107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005108
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005109 /*
5110 * 7: check if result will be cacheable with a cookie.
5111 * We'll block the response if security checks have caught
5112 * nasty things such as a cacheable cookie.
5113 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005114 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5115 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005116 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117
5118 /* we're in presence of a cacheable response containing
5119 * a set-cookie header. We'll block it as requested by
5120 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005121 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005122 if (target_srv(&t->target))
5123 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005124
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005125 t->be->be_counters.denied_resp++;
5126 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005127 if (t->listener->counters)
5128 t->listener->counters->denied_resp++;
5129
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005130 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005131 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005132 send_log(t->be, LOG_ALERT,
5133 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005134 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005135 goto return_srv_prx_502;
5136 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005137
5138 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005139 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005140 */
Willy Tarreau60466522010-01-18 19:08:45 +01005141 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5142 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5143 unsigned int want_flags = 0;
5144
5145 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5146 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5147 /* we want a keep-alive response here. Keep-alive header
5148 * required if either side is not 1.1.
5149 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005150 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005151 want_flags |= TX_CON_KAL_SET;
5152 }
5153 else {
5154 /* we want a close response here. Close header required if
5155 * the server is 1.1, regardless of the client.
5156 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005157 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005158 want_flags |= TX_CON_CLO_SET;
5159 }
5160
5161 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005162 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005163 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005164
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005165 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005166 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005167 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005168 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005169
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170 /*************************************************************
5171 * OK, that's finished for the headers. We have done what we *
5172 * could. Let's switch to the DATA state. *
5173 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005174
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005175 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005176
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177 /* if the user wants to log as soon as possible, without counting
5178 * bytes from the server, then this is the right moment. We have
5179 * to temporarily assign bytes_out to log what we currently have.
5180 */
5181 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5182 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5183 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005184 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 t->logs.bytes_out = 0;
5186 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005187
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 /* Note: we must not try to cheat by jumping directly to DATA,
5189 * otherwise we would not let the client side wake up.
5190 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005191
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005192 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005194 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005196
Willy Tarreaud98cf932009-12-27 22:54:55 +01005197/* This function is an analyser which forwards response body (including chunk
5198 * sizes if any). It is called as soon as we must forward, even if we forward
5199 * zero byte. The only situation where it must not be called is when we're in
5200 * tunnel mode and we want to forward till the close. It's used both to forward
5201 * remaining data and to resync after end of body. It expects the msg_state to
5202 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5203 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005204 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005205 * bytes of pending data + the headers if not already done (between som and sov).
5206 * It eventually adjusts som to match sov after the data in between have been sent.
5207 */
5208int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5209{
5210 struct http_txn *txn = &s->txn;
5211 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005212 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005213
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005214 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5215 return 0;
5216
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005217 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005218 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005219 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005220 /* Output closed while we were sending data. We must abort and
5221 * wake the other side up.
5222 */
5223 msg->msg_state = HTTP_MSG_ERROR;
5224 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005225 return 1;
5226 }
5227
Willy Tarreau4fe41902010-06-07 22:27:41 +02005228 /* in most states, we should abort in case of early close */
5229 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005230
Willy Tarreaud98cf932009-12-27 22:54:55 +01005231 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005232 /* we have msg->sov which points to the first byte of message body.
5233 * msg->som still points to the beginning of the message. We must
5234 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005235 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005236 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005237
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005238 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5240 else {
5241 msg->msg_state = HTTP_MSG_DATA;
5242 }
5243 }
5244
Willy Tarreaud98cf932009-12-27 22:54:55 +01005245 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005246 int bytes;
5247
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005249 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005250 bytes = msg->sov - msg->som;
5251 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005252 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005253 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5254 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005255 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005256 msg->chunk_len += (unsigned int)bytes;
5257 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005258 }
5259
Willy Tarreaucaabe412010-01-03 23:08:28 +01005260 if (msg->msg_state == HTTP_MSG_DATA) {
5261 /* must still forward */
5262 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005263 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005264
5265 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005266 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005267 msg->msg_state = HTTP_MSG_DATA_CRLF;
5268 else
5269 msg->msg_state = HTTP_MSG_DONE;
5270 }
5271 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005272 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005273 * set ->sov and ->next to point to the body and switch to DATA or
5274 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005275 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005276 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277
5278 if (!ret)
5279 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005280 else if (ret < 0) {
5281 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005282 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005284 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005285 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005286 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005287 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5288 /* we want the CRLF after the data */
5289 int ret;
5290
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005291 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005292
5293 if (!ret)
5294 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005295 else if (ret < 0) {
5296 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005297 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005298 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005299 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 /* we're in MSG_CHUNK_SIZE now */
5301 }
5302 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005303 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005304
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 if (ret == 0)
5306 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005307 else if (ret < 0) {
5308 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005309 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005310 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005311 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005312 /* we're in HTTP_MSG_DONE now */
5313 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005315 int old_state = msg->msg_state;
5316
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005318 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005319 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5320 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5321 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 if (http_resync_states(s)) {
5323 http_silent_debug(__LINE__, s);
5324 /* some state changes occurred, maybe the analyser
5325 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005326 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005327 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5328 if (res->flags & BF_SHUTW) {
5329 /* response errors are most likely due to
5330 * the client aborting the transfer.
5331 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005332 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005333 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005334 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005335 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005337 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005339 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005341 }
5342 }
5343
Willy Tarreaud98cf932009-12-27 22:54:55 +01005344 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005345 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005346 if (res->flags & BF_SHUTR) {
5347 if (!(s->flags & SN_ERR_MASK))
5348 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005349 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005350 if (target_srv(&s->target))
5351 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005352 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005353 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005354
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005355 if (res->flags & BF_SHUTW)
5356 goto aborted_xfer;
5357
Willy Tarreau40dba092010-03-04 18:14:51 +01005358 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 if (!s->req->analysers)
5360 goto return_bad_res;
5361
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005362 /* forward any pending data */
5363 bytes = msg->sov - msg->som;
5364 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005365 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005366 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5367 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005368 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005369 msg->chunk_len += (unsigned int)bytes;
5370 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 }
5372
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005373 /* When TE: chunked is used, we need to get there again to parse remaining
5374 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5375 * Similarly, with keep-alive on the client side, we don't want to forward a
5376 * close.
5377 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005378 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005379 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5380 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5381 buffer_dont_close(res);
5382
Willy Tarreau5c620922011-05-11 19:56:11 +02005383 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005384 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5385 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005386 * modes are already handled by the stream sock layer. We must not do
5387 * this in content-length mode because it could present the MSG_MORE
5388 * flag with the last block of forwarded data, which would cause an
5389 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005390 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005391 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005392 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005393
Willy Tarreaud98cf932009-12-27 22:54:55 +01005394 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 return 0;
5397
Willy Tarreau40dba092010-03-04 18:14:51 +01005398 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005399 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005400 if (target_srv(&s->target))
5401 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005402
5403 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005405 /* don't send any error message as we're in the body */
5406 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005408 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005409 if (target_srv(&s->target))
5410 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411
5412 if (!(s->flags & SN_ERR_MASK))
5413 s->flags |= SN_ERR_PRXCOND;
5414 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005415 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005416 return 0;
5417
5418 aborted_xfer:
5419 txn->rsp.msg_state = HTTP_MSG_ERROR;
5420 /* don't send any error message as we're in the body */
5421 stream_int_retnclose(res->cons, NULL);
5422 res->analysers = 0;
5423 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5424
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005425 s->fe->fe_counters.cli_aborts++;
5426 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005427 if (target_srv(&s->target))
5428 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005429
5430 if (!(s->flags & SN_ERR_MASK))
5431 s->flags |= SN_ERR_CLICL;
5432 if (!(s->flags & SN_FINST_MASK))
5433 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434 return 0;
5435}
5436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005437/* Iterate the same filter through all request headers.
5438 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005439 * Since it can manage the switch to another backend, it updates the per-proxy
5440 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005441 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005442int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005443{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005444 char term;
5445 char *cur_ptr, *cur_end, *cur_next;
5446 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005447 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005448 struct hdr_idx_elem *cur_hdr;
5449 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451 last_hdr = 0;
5452
Willy Tarreau09d1e252012-05-18 22:36:34 +02005453 cur_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005454 old_idx = 0;
5455
5456 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005457 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005458 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005459 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005460 (exp->action == ACT_ALLOW ||
5461 exp->action == ACT_DENY ||
5462 exp->action == ACT_TARPIT))
5463 return 0;
5464
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005465 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005466 if (!cur_idx)
5467 break;
5468
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005469 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005470 cur_ptr = cur_next;
5471 cur_end = cur_ptr + cur_hdr->len;
5472 cur_next = cur_end + cur_hdr->cr + 1;
5473
5474 /* Now we have one header between cur_ptr and cur_end,
5475 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005476 */
5477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005478 /* The annoying part is that pattern matching needs
5479 * that we modify the contents to null-terminate all
5480 * strings before testing them.
5481 */
5482
5483 term = *cur_end;
5484 *cur_end = '\0';
5485
5486 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5487 switch (exp->action) {
5488 case ACT_SETBE:
5489 /* It is not possible to jump a second time.
5490 * FIXME: should we return an HTTP/500 here so that
5491 * the admin knows there's a problem ?
5492 */
5493 if (t->be != t->fe)
5494 break;
5495
5496 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005497 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005498 last_hdr = 1;
5499 break;
5500
5501 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005502 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005503 last_hdr = 1;
5504 break;
5505
5506 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005507 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005508 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005509
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005510 t->fe->fe_counters.denied_req++;
5511 if (t->fe != t->be)
5512 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005513 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005514 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 break;
5517
5518 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005519 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005520 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005521
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005522 t->fe->fe_counters.denied_req++;
5523 if (t->fe != t->be)
5524 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005525 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005526 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 break;
5529
5530 case ACT_REPLACE:
5531 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5532 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5533 /* FIXME: if the user adds a newline in the replacement, the
5534 * index will not be recalculated for now, and the new line
5535 * will not be counted as a new header.
5536 */
5537
5538 cur_end += delta;
5539 cur_next += delta;
5540 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005541 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005542 break;
5543
5544 case ACT_REMOVE:
5545 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5546 cur_next += delta;
5547
Willy Tarreaufa355d42009-11-29 18:12:29 +01005548 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5550 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005551 cur_hdr->len = 0;
5552 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005553 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005554 break;
5555
5556 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005557 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005558 if (cur_end)
5559 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 /* keep the link from this header to next one in case of later
5562 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005563 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 old_idx = cur_idx;
5565 }
5566 return 0;
5567}
5568
5569
5570/* Apply the filter to the request line.
5571 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5572 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005573 * Since it can manage the switch to another backend, it updates the per-proxy
5574 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 */
5576int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5577{
5578 char term;
5579 char *cur_ptr, *cur_end;
5580 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005581 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005582 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005583
Willy Tarreau58f10d72006-12-04 02:26:12 +01005584
Willy Tarreau3d300592007-03-18 18:34:41 +01005585 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005587 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005588 (exp->action == ACT_ALLOW ||
5589 exp->action == ACT_DENY ||
5590 exp->action == ACT_TARPIT))
5591 return 0;
5592 else if (exp->action == ACT_REMOVE)
5593 return 0;
5594
5595 done = 0;
5596
Willy Tarreau09d1e252012-05-18 22:36:34 +02005597 cur_ptr = req->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005598 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005599
5600 /* Now we have the request line between cur_ptr and cur_end */
5601
5602 /* The annoying part is that pattern matching needs
5603 * that we modify the contents to null-terminate all
5604 * strings before testing them.
5605 */
5606
5607 term = *cur_end;
5608 *cur_end = '\0';
5609
5610 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5611 switch (exp->action) {
5612 case ACT_SETBE:
5613 /* It is not possible to jump a second time.
5614 * FIXME: should we return an HTTP/500 here so that
5615 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005616 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 if (t->be != t->fe)
5618 break;
5619
5620 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005621 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 done = 1;
5623 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005625 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005626 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 done = 1;
5628 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005630 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005631 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005632
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005633 t->fe->fe_counters.denied_req++;
5634 if (t->fe != t->be)
5635 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005636 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005637 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 done = 1;
5640 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005643 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005644
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005645 t->fe->fe_counters.denied_req++;
5646 if (t->fe != t->be)
5647 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005648 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005649 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 done = 1;
5652 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 case ACT_REPLACE:
5655 *cur_end = term; /* restore the string terminator */
5656 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5657 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5658 /* FIXME: if the user adds a newline in the replacement, the
5659 * index will not be recalculated for now, and the new line
5660 * will not be counted as a new header.
5661 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005662
Willy Tarreaufa355d42009-11-29 18:12:29 +01005663 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005665 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 HTTP_MSG_RQMETH,
5667 cur_ptr, cur_end + 1,
5668 NULL, NULL);
5669 if (unlikely(!cur_end))
5670 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 /* we have a full request and we know that we have either a CR
5673 * or an LF at <ptr>.
5674 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005675 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5676 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 /* there is no point trying this regex on headers */
5678 return 1;
5679 }
5680 }
5681 *cur_end = term; /* restore the string terminator */
5682 return done;
5683}
Willy Tarreau97de6242006-12-27 17:18:38 +01005684
Willy Tarreau58f10d72006-12-04 02:26:12 +01005685
Willy Tarreau58f10d72006-12-04 02:26:12 +01005686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005688 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005689 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005690 * unparsable request. Since it can manage the switch to another backend, it
5691 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005692 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005693int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005694{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005695 struct http_txn *txn = &s->txn;
5696 struct hdr_exp *exp;
5697
5698 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 /*
5702 * The interleaving of transformations and verdicts
5703 * makes it difficult to decide to continue or stop
5704 * the evaluation.
5705 */
5706
Willy Tarreau6c123b12010-01-28 20:22:06 +01005707 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5708 break;
5709
Willy Tarreau3d300592007-03-18 18:34:41 +01005710 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005712 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005714
5715 /* if this filter had a condition, evaluate it now and skip to
5716 * next filter if the condition does not match.
5717 */
5718 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005719 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005720 ret = acl_pass(ret);
5721 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5722 ret = !ret;
5723
5724 if (!ret)
5725 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005726 }
5727
5728 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005729 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 if (unlikely(ret < 0))
5731 return -1;
5732
5733 if (likely(ret == 0)) {
5734 /* The filter did not match the request, it can be
5735 * iterated through all headers.
5736 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005737 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005738 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005739 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005741}
5742
5743
Willy Tarreaua15645d2007-03-18 16:22:39 +01005744
Willy Tarreau58f10d72006-12-04 02:26:12 +01005745/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005746 * Try to retrieve the server associated to the appsession.
5747 * If the server is found, it's assigned to the session.
5748 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005749void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005750 struct http_txn *txn = &t->txn;
5751 appsess *asession = NULL;
5752 char *sessid_temp = NULL;
5753
Cyril Bontéb21570a2009-11-29 20:04:48 +01005754 if (len > t->be->appsession_len) {
5755 len = t->be->appsession_len;
5756 }
5757
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005758 if (t->be->options2 & PR_O2_AS_REQL) {
5759 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005760 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005761 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005762 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005763 }
5764
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005765 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005766 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5767 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5768 return;
5769 }
5770
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005771 memcpy(txn->sessid, buf, len);
5772 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005773 }
5774
5775 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5776 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5777 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5778 return;
5779 }
5780
Cyril Bontéb21570a2009-11-29 20:04:48 +01005781 memcpy(sessid_temp, buf, len);
5782 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005783
5784 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5785 /* free previously allocated memory */
5786 pool_free2(apools.sessid, sessid_temp);
5787
5788 if (asession != NULL) {
5789 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5790 if (!(t->be->options2 & PR_O2_AS_REQL))
5791 asession->request_count++;
5792
5793 if (asession->serverid != NULL) {
5794 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005795
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005796 while (srv) {
5797 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005798 if ((srv->state & SRV_RUNNING) ||
5799 (t->be->options & PR_O_PERSIST) ||
5800 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005801 /* we found the server and it's usable */
5802 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005803 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005804 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005805 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005806
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005807 break;
5808 } else {
5809 txn->flags &= ~TX_CK_MASK;
5810 txn->flags |= TX_CK_DOWN;
5811 }
5812 }
5813 srv = srv->next;
5814 }
5815 }
5816 }
5817}
5818
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005819/* Find the end of a cookie value contained between <s> and <e>. It works the
5820 * same way as with headers above except that the semi-colon also ends a token.
5821 * See RFC2965 for more information. Note that it requires a valid header to
5822 * return a valid result.
5823 */
5824char *find_cookie_value_end(char *s, const char *e)
5825{
5826 int quoted, qdpair;
5827
5828 quoted = qdpair = 0;
5829 for (; s < e; s++) {
5830 if (qdpair) qdpair = 0;
5831 else if (quoted) {
5832 if (*s == '\\') qdpair = 1;
5833 else if (*s == '"') quoted = 0;
5834 }
5835 else if (*s == '"') quoted = 1;
5836 else if (*s == ',' || *s == ';') return s;
5837 }
5838 return s;
5839}
5840
5841/* Delete a value in a header between delimiters <from> and <next> in buffer
5842 * <buf>. The number of characters displaced is returned, and the pointer to
5843 * the first delimiter is updated if required. The function tries as much as
5844 * possible to respect the following principles :
5845 * - replace <from> delimiter by the <next> one unless <from> points to a
5846 * colon, in which case <next> is simply removed
5847 * - set exactly one space character after the new first delimiter, unless
5848 * there are not enough characters in the block being moved to do so.
5849 * - remove unneeded spaces before the previous delimiter and after the new
5850 * one.
5851 *
5852 * It is the caller's responsibility to ensure that :
5853 * - <from> points to a valid delimiter or the colon ;
5854 * - <next> points to a valid delimiter or the final CR/LF ;
5855 * - there are non-space chars before <from> ;
5856 * - there is a CR/LF at or after <next>.
5857 */
5858int del_hdr_value(struct buffer *buf, char **from, char *next)
5859{
5860 char *prev = *from;
5861
5862 if (*prev == ':') {
5863 /* We're removing the first value, preserve the colon and add a
5864 * space if possible.
5865 */
5866 if (!http_is_crlf[(unsigned char)*next])
5867 next++;
5868 prev++;
5869 if (prev < next)
5870 *prev++ = ' ';
5871
5872 while (http_is_spht[(unsigned char)*next])
5873 next++;
5874 } else {
5875 /* Remove useless spaces before the old delimiter. */
5876 while (http_is_spht[(unsigned char)*(prev-1)])
5877 prev--;
5878 *from = prev;
5879
5880 /* copy the delimiter and if possible a space if we're
5881 * not at the end of the line.
5882 */
5883 if (!http_is_crlf[(unsigned char)*next]) {
5884 *prev++ = *next++;
5885 if (prev + 1 < next)
5886 *prev++ = ' ';
5887 while (http_is_spht[(unsigned char)*next])
5888 next++;
5889 }
5890 }
5891 return buffer_replace2(buf, prev, next, NULL, 0);
5892}
5893
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005894/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005895 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005896 * desirable to call it only when needed. This code is quite complex because
5897 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5898 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005899 */
5900void manage_client_side_cookies(struct session *t, struct buffer *req)
5901{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005902 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005903 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005905 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5906 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005908 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005909 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02005910 hdr_next = req->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005911
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005912 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005913 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005914 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005915
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005916 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917 hdr_beg = hdr_next;
5918 hdr_end = hdr_beg + cur_hdr->len;
5919 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005921 /* We have one full header between hdr_beg and hdr_end, and the
5922 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005923 * "Cookie:" headers.
5924 */
5925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005926 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005927 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005928 old_idx = cur_idx;
5929 continue;
5930 }
5931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005932 del_from = NULL; /* nothing to be deleted */
5933 preserve_hdr = 0; /* assume we may kill the whole header */
5934
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935 /* Now look for cookies. Conforming to RFC2109, we have to support
5936 * attributes whose name begin with a '$', and associate them with
5937 * the right cookie, if we want to delete this cookie.
5938 * So there are 3 cases for each cookie read :
5939 * 1) it's a special attribute, beginning with a '$' : ignore it.
5940 * 2) it's a server id cookie that we *MAY* want to delete : save
5941 * some pointers on it (last semi-colon, beginning of cookie...)
5942 * 3) it's an application cookie : we *MAY* have to delete a previous
5943 * "special" cookie.
5944 * At the end of loop, if a "special" cookie remains, we may have to
5945 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005946 * *MUST* delete it.
5947 *
5948 * Note: RFC2965 is unclear about the processing of spaces around
5949 * the equal sign in the ATTR=VALUE form. A careful inspection of
5950 * the RFC explicitly allows spaces before it, and not within the
5951 * tokens (attrs or values). An inspection of RFC2109 allows that
5952 * too but section 10.1.3 lets one think that spaces may be allowed
5953 * after the equal sign too, resulting in some (rare) buggy
5954 * implementations trying to do that. So let's do what servers do.
5955 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5956 * allowed quoted strings in values, with any possible character
5957 * after a backslash, including control chars and delimitors, which
5958 * causes parsing to become ambiguous. Browsers also allow spaces
5959 * within values even without quotes.
5960 *
5961 * We have to keep multiple pointers in order to support cookie
5962 * removal at the beginning, middle or end of header without
5963 * corrupting the header. All of these headers are valid :
5964 *
5965 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5966 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5967 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5968 * | | | | | | | | |
5969 * | | | | | | | | hdr_end <--+
5970 * | | | | | | | +--> next
5971 * | | | | | | +----> val_end
5972 * | | | | | +-----------> val_beg
5973 * | | | | +--------------> equal
5974 * | | | +----------------> att_end
5975 * | | +---------------------> att_beg
5976 * | +--------------------------> prev
5977 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005978 */
5979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005980 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5981 /* Iterate through all cookies on this line */
5982
5983 /* find att_beg */
5984 att_beg = prev + 1;
5985 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5986 att_beg++;
5987
5988 /* find att_end : this is the first character after the last non
5989 * space before the equal. It may be equal to hdr_end.
5990 */
5991 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005993 while (equal < hdr_end) {
5994 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005995 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005996 if (http_is_spht[(unsigned char)*equal++])
5997 continue;
5998 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005999 }
6000
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006001 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6002 * is between <att_beg> and <equal>, both may be identical.
6003 */
6004
6005 /* look for end of cookie if there is an equal sign */
6006 if (equal < hdr_end && *equal == '=') {
6007 /* look for the beginning of the value */
6008 val_beg = equal + 1;
6009 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6010 val_beg++;
6011
6012 /* find the end of the value, respecting quotes */
6013 next = find_cookie_value_end(val_beg, hdr_end);
6014
6015 /* make val_end point to the first white space or delimitor after the value */
6016 val_end = next;
6017 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6018 val_end--;
6019 } else {
6020 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006021 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006022
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006023 /* We have nothing to do with attributes beginning with '$'. However,
6024 * they will automatically be removed if a header before them is removed,
6025 * since they're supposed to be linked together.
6026 */
6027 if (*att_beg == '$')
6028 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006029
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006030 /* Ignore cookies with no equal sign */
6031 if (equal == next) {
6032 /* This is not our cookie, so we must preserve it. But if we already
6033 * scheduled another cookie for removal, we cannot remove the
6034 * complete header, but we can remove the previous block itself.
6035 */
6036 preserve_hdr = 1;
6037 if (del_from != NULL) {
6038 int delta = del_hdr_value(req, &del_from, prev);
6039 val_end += delta;
6040 next += delta;
6041 hdr_end += delta;
6042 hdr_next += delta;
6043 cur_hdr->len += delta;
6044 http_msg_move_end(&txn->req, delta);
6045 prev = del_from;
6046 del_from = NULL;
6047 }
6048 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006050
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 /* if there are spaces around the equal sign, we need to
6052 * strip them otherwise we'll get trouble for cookie captures,
6053 * or even for rewrites. Since this happens extremely rarely,
6054 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006055 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006056 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6057 int stripped_before = 0;
6058 int stripped_after = 0;
6059
6060 if (att_end != equal) {
6061 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6062 equal += stripped_before;
6063 val_beg += stripped_before;
6064 }
6065
6066 if (val_beg > equal + 1) {
6067 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6068 val_beg += stripped_after;
6069 stripped_before += stripped_after;
6070 }
6071
6072 val_end += stripped_before;
6073 next += stripped_before;
6074 hdr_end += stripped_before;
6075 hdr_next += stripped_before;
6076 cur_hdr->len += stripped_before;
6077 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006079 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006081 /* First, let's see if we want to capture this cookie. We check
6082 * that we don't already have a client side cookie, because we
6083 * can only capture one. Also as an optimisation, we ignore
6084 * cookies shorter than the declared name.
6085 */
6086 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6087 (val_end - att_beg >= t->fe->capture_namelen) &&
6088 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6089 int log_len = val_end - att_beg;
6090
6091 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6092 Alert("HTTP logging : out of memory.\n");
6093 } else {
6094 if (log_len > t->fe->capture_len)
6095 log_len = t->fe->capture_len;
6096 memcpy(txn->cli_cookie, att_beg, log_len);
6097 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100
Willy Tarreaubca99692010-10-06 19:25:55 +02006101 /* Persistence cookies in passive, rewrite or insert mode have the
6102 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006104 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006105 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006106 * For cookies in prefix mode, the form is :
6107 *
6108 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 */
6110 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6111 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6112 struct server *srv = t->be->srv;
6113 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6116 * have the server ID between val_beg and delim, and the original cookie between
6117 * delim+1 and val_end. Otherwise, delim==val_end :
6118 *
6119 * Cookie: NAME=SRV; # in all but prefix modes
6120 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6121 * | || || | |+-> next
6122 * | || || | +--> val_end
6123 * | || || +---------> delim
6124 * | || |+------------> val_beg
6125 * | || +-------------> att_end = equal
6126 * | |+-----------------> att_beg
6127 * | +------------------> prev
6128 * +-------------------------> hdr_beg
6129 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006131 if (t->be->options & PR_O_COOK_PFX) {
6132 for (delim = val_beg; delim < val_end; delim++)
6133 if (*delim == COOKIE_DELIM)
6134 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006135 } else {
6136 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006138 /* Now check if the cookie contains a date field, which would
6139 * appear after a vertical bar ('|') just after the server name
6140 * and before the delimiter.
6141 */
6142 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6143 if (vbar1) {
6144 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006145 * right is the last seen date. It is a base64 encoded
6146 * 30-bit value representing the UNIX date since the
6147 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006148 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006149 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006150 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006151 if (val_end - vbar1 >= 5) {
6152 val = b64tos30(vbar1);
6153 if (val > 0)
6154 txn->cookie_last_date = val << 2;
6155 }
6156 /* look for a second vertical bar */
6157 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6158 if (vbar1 && (val_end - vbar1 > 5)) {
6159 val = b64tos30(vbar1 + 1);
6160 if (val > 0)
6161 txn->cookie_first_date = val << 2;
6162 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006163 }
6164 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006165
Willy Tarreauf64d1412010-10-07 20:06:11 +02006166 /* if the cookie has an expiration date and the proxy wants to check
6167 * it, then we do that now. We first check if the cookie is too old,
6168 * then only if it has expired. We detect strict overflow because the
6169 * time resolution here is not great (4 seconds). Cookies with dates
6170 * in the future are ignored if their offset is beyond one day. This
6171 * allows an admin to fix timezone issues without expiring everyone
6172 * and at the same time avoids keeping unwanted side effects for too
6173 * long.
6174 */
6175 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006176 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6177 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006178 txn->flags &= ~TX_CK_MASK;
6179 txn->flags |= TX_CK_OLD;
6180 delim = val_beg; // let's pretend we have not found the cookie
6181 txn->cookie_first_date = 0;
6182 txn->cookie_last_date = 0;
6183 }
6184 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006185 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6186 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006187 txn->flags &= ~TX_CK_MASK;
6188 txn->flags |= TX_CK_EXPIRED;
6189 delim = val_beg; // let's pretend we have not found the cookie
6190 txn->cookie_first_date = 0;
6191 txn->cookie_last_date = 0;
6192 }
6193
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 /* Here, we'll look for the first running server which supports the cookie.
6195 * This allows to share a same cookie between several servers, for example
6196 * to dedicate backup servers to specific servers only.
6197 * However, to prevent clients from sticking to cookie-less backup server
6198 * when they have incidentely learned an empty cookie, we simply ignore
6199 * empty cookies and mark them as invalid.
6200 * The same behaviour is applied when persistence must be ignored.
6201 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006202 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006204
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 while (srv) {
6206 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6207 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6208 if ((srv->state & SRV_RUNNING) ||
6209 (t->be->options & PR_O_PERSIST) ||
6210 (t->flags & SN_FORCE_PRST)) {
6211 /* we found the server and we can use it */
6212 txn->flags &= ~TX_CK_MASK;
6213 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6214 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006215 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 break;
6217 } else {
6218 /* we found a server, but it's down,
6219 * mark it as such and go on in case
6220 * another one is available.
6221 */
6222 txn->flags &= ~TX_CK_MASK;
6223 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 srv = srv->next;
6227 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006228
Willy Tarreauf64d1412010-10-07 20:06:11 +02006229 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006230 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006232 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6233 txn->flags |= TX_CK_UNUSED;
6234 else
6235 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 /* depending on the cookie mode, we may have to either :
6239 * - delete the complete cookie if we're in insert+indirect mode, so that
6240 * the server never sees it ;
6241 * - remove the server id from the cookie value, and tag the cookie as an
6242 * application cookie so that it does not get accidentely removed later,
6243 * if we're in cookie prefix mode
6244 */
6245 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6246 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6249 val_end += delta;
6250 next += delta;
6251 hdr_end += delta;
6252 hdr_next += delta;
6253 cur_hdr->len += delta;
6254 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 del_from = NULL;
6257 preserve_hdr = 1; /* we want to keep this cookie */
6258 }
6259 else if (del_from == NULL &&
6260 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6261 del_from = prev;
6262 }
6263 } else {
6264 /* This is not our cookie, so we must preserve it. But if we already
6265 * scheduled another cookie for removal, we cannot remove the
6266 * complete header, but we can remove the previous block itself.
6267 */
6268 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 if (del_from != NULL) {
6271 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006272 if (att_beg >= del_from)
6273 att_beg += delta;
6274 if (att_end >= del_from)
6275 att_end += delta;
6276 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 val_end += delta;
6278 next += delta;
6279 hdr_end += delta;
6280 hdr_next += delta;
6281 cur_hdr->len += delta;
6282 http_msg_move_end(&txn->req, delta);
6283 prev = del_from;
6284 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* Look for the appsession cookie unless persistence must be ignored */
6289 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6290 int cmp_len, value_len;
6291 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006292
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006293 if (t->be->options2 & PR_O2_AS_PFX) {
6294 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6295 value_begin = att_beg + t->be->appsession_name_len;
6296 value_len = val_end - att_beg - t->be->appsession_name_len;
6297 } else {
6298 cmp_len = att_end - att_beg;
6299 value_begin = val_beg;
6300 value_len = val_end - val_beg;
6301 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 /* let's see if the cookie is our appcookie */
6304 if (cmp_len == t->be->appsession_name_len &&
6305 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6306 manage_client_side_appsession(t, value_begin, value_len);
6307 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308 }
6309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 /* continue with next cookie on this header line */
6311 att_beg = next;
6312 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006314 /* There are no more cookies on this line.
6315 * We may still have one (or several) marked for deletion at the
6316 * end of the line. We must do this now in two ways :
6317 * - if some cookies must be preserved, we only delete from the
6318 * mark to the end of line ;
6319 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 if (preserve_hdr) {
6324 delta = del_hdr_value(req, &del_from, hdr_end);
6325 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326 cur_hdr->len += delta;
6327 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329
6330 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006331 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6332 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006334 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006337 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 }
6339
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343}
6344
6345
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346/* Iterate the same filter through all response headers contained in <rtr>.
6347 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6348 */
6349int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6350{
6351 char term;
6352 char *cur_ptr, *cur_end, *cur_next;
6353 int cur_idx, old_idx, last_hdr;
6354 struct http_txn *txn = &t->txn;
6355 struct hdr_idx_elem *cur_hdr;
6356 int len, delta;
6357
6358 last_hdr = 0;
6359
Willy Tarreau09d1e252012-05-18 22:36:34 +02006360 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006361 old_idx = 0;
6362
6363 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006364 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006365 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006366 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006367 (exp->action == ACT_ALLOW ||
6368 exp->action == ACT_DENY))
6369 return 0;
6370
6371 cur_idx = txn->hdr_idx.v[old_idx].next;
6372 if (!cur_idx)
6373 break;
6374
6375 cur_hdr = &txn->hdr_idx.v[cur_idx];
6376 cur_ptr = cur_next;
6377 cur_end = cur_ptr + cur_hdr->len;
6378 cur_next = cur_end + cur_hdr->cr + 1;
6379
6380 /* Now we have one header between cur_ptr and cur_end,
6381 * and the next header starts at cur_next.
6382 */
6383
6384 /* The annoying part is that pattern matching needs
6385 * that we modify the contents to null-terminate all
6386 * strings before testing them.
6387 */
6388
6389 term = *cur_end;
6390 *cur_end = '\0';
6391
6392 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6393 switch (exp->action) {
6394 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006395 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006396 last_hdr = 1;
6397 break;
6398
6399 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006400 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006401 last_hdr = 1;
6402 break;
6403
6404 case ACT_REPLACE:
6405 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6406 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6407 /* FIXME: if the user adds a newline in the replacement, the
6408 * index will not be recalculated for now, and the new line
6409 * will not be counted as a new header.
6410 */
6411
6412 cur_end += delta;
6413 cur_next += delta;
6414 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006415 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006416 break;
6417
6418 case ACT_REMOVE:
6419 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6420 cur_next += delta;
6421
Willy Tarreaufa355d42009-11-29 18:12:29 +01006422 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6424 txn->hdr_idx.used--;
6425 cur_hdr->len = 0;
6426 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006427 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006428 break;
6429
6430 }
6431 }
6432 if (cur_end)
6433 *cur_end = term; /* restore the string terminator */
6434
6435 /* keep the link from this header to next one in case of later
6436 * removal of next header.
6437 */
6438 old_idx = cur_idx;
6439 }
6440 return 0;
6441}
6442
6443
6444/* Apply the filter to the status line in the response buffer <rtr>.
6445 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6446 * or -1 if a replacement resulted in an invalid status line.
6447 */
6448int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6449{
6450 char term;
6451 char *cur_ptr, *cur_end;
6452 int done;
6453 struct http_txn *txn = &t->txn;
6454 int len, delta;
6455
6456
Willy Tarreau3d300592007-03-18 18:34:41 +01006457 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006458 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006459 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 (exp->action == ACT_ALLOW ||
6461 exp->action == ACT_DENY))
6462 return 0;
6463 else if (exp->action == ACT_REMOVE)
6464 return 0;
6465
6466 done = 0;
6467
Willy Tarreau09d1e252012-05-18 22:36:34 +02006468 cur_ptr = rtr->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006469 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006470
6471 /* Now we have the status line between cur_ptr and cur_end */
6472
6473 /* The annoying part is that pattern matching needs
6474 * that we modify the contents to null-terminate all
6475 * strings before testing them.
6476 */
6477
6478 term = *cur_end;
6479 *cur_end = '\0';
6480
6481 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6482 switch (exp->action) {
6483 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485 done = 1;
6486 break;
6487
6488 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490 done = 1;
6491 break;
6492
6493 case ACT_REPLACE:
6494 *cur_end = term; /* restore the string terminator */
6495 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6496 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6497 /* FIXME: if the user adds a newline in the replacement, the
6498 * index will not be recalculated for now, and the new line
6499 * will not be counted as a new header.
6500 */
6501
Willy Tarreaufa355d42009-11-29 18:12:29 +01006502 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006504 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006505 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006506 cur_ptr, cur_end + 1,
6507 NULL, NULL);
6508 if (unlikely(!cur_end))
6509 return -1;
6510
6511 /* we have a full respnse and we know that we have either a CR
6512 * or an LF at <ptr>.
6513 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02006514 txn->status = strl2ui(rtr->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006515 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 /* there is no point trying this regex on headers */
6517 return 1;
6518 }
6519 }
6520 *cur_end = term; /* restore the string terminator */
6521 return done;
6522}
6523
6524
6525
6526/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006527 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6529 * unparsable response.
6530 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006531int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006532{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006533 struct http_txn *txn = &s->txn;
6534 struct hdr_exp *exp;
6535
6536 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 int ret;
6538
6539 /*
6540 * The interleaving of transformations and verdicts
6541 * makes it difficult to decide to continue or stop
6542 * the evaluation.
6543 */
6544
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006545 if (txn->flags & TX_SVDENY)
6546 break;
6547
Willy Tarreau3d300592007-03-18 18:34:41 +01006548 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6550 exp->action == ACT_PASS)) {
6551 exp = exp->next;
6552 continue;
6553 }
6554
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006555 /* if this filter had a condition, evaluate it now and skip to
6556 * next filter if the condition does not match.
6557 */
6558 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006559 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006560 ret = acl_pass(ret);
6561 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6562 ret = !ret;
6563 if (!ret)
6564 continue;
6565 }
6566
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006568 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 if (unlikely(ret < 0))
6570 return -1;
6571
6572 if (likely(ret == 0)) {
6573 /* The filter did not match the response, it can be
6574 * iterated through all headers.
6575 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006576 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 }
6579 return 0;
6580}
6581
6582
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006584 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006585 * desirable to call it only when needed. This function is also used when we
6586 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006588void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589{
6590 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006591 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006592 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006593 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006594 char *hdr_beg, *hdr_end, *hdr_next;
6595 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597 /* Iterate through the headers.
6598 * we start with the start line.
6599 */
6600 old_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006601 hdr_next = res->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602
6603 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6604 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006605 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606
6607 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006608 hdr_beg = hdr_next;
6609 hdr_end = hdr_beg + cur_hdr->len;
6610 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611
Willy Tarreau24581ba2010-08-31 22:39:35 +02006612 /* We have one full header between hdr_beg and hdr_end, and the
6613 * next header starts at hdr_next. We're only interested in
6614 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615 */
6616
Willy Tarreau24581ba2010-08-31 22:39:35 +02006617 is_cookie2 = 0;
6618 prev = hdr_beg + 10;
6619 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006620 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006621 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6622 if (!val) {
6623 old_idx = cur_idx;
6624 continue;
6625 }
6626 is_cookie2 = 1;
6627 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 }
6629
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6631 * <prev> points to the colon.
6632 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006633 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634
Willy Tarreau24581ba2010-08-31 22:39:35 +02006635 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6636 * check-cache is enabled) and we are not interested in checking
6637 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006638 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006639 if (t->be->cookie_name == NULL &&
6640 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006641 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 return;
6643
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 /* OK so now we know we have to process this response cookie.
6645 * The format of the Set-Cookie header is slightly different
6646 * from the format of the Cookie header in that it does not
6647 * support the comma as a cookie delimiter (thus the header
6648 * cannot be folded) because the Expires attribute described in
6649 * the original Netscape's spec may contain an unquoted date
6650 * with a comma inside. We have to live with this because
6651 * many browsers don't support Max-Age and some browsers don't
6652 * support quoted strings. However the Set-Cookie2 header is
6653 * clean.
6654 *
6655 * We have to keep multiple pointers in order to support cookie
6656 * removal at the beginning, middle or end of header without
6657 * corrupting the header (in case of set-cookie2). A special
6658 * pointer, <scav> points to the beginning of the set-cookie-av
6659 * fields after the first semi-colon. The <next> pointer points
6660 * either to the end of line (set-cookie) or next unquoted comma
6661 * (set-cookie2). All of these headers are valid :
6662 *
6663 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6664 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6665 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6666 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6667 * | | | | | | | | | |
6668 * | | | | | | | | +-> next hdr_end <--+
6669 * | | | | | | | +------------> scav
6670 * | | | | | | +--------------> val_end
6671 * | | | | | +--------------------> val_beg
6672 * | | | | +----------------------> equal
6673 * | | | +------------------------> att_end
6674 * | | +----------------------------> att_beg
6675 * | +------------------------------> prev
6676 * +-----------------------------------------> hdr_beg
6677 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678
Willy Tarreau24581ba2010-08-31 22:39:35 +02006679 for (; prev < hdr_end; prev = next) {
6680 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681
Willy Tarreau24581ba2010-08-31 22:39:35 +02006682 /* find att_beg */
6683 att_beg = prev + 1;
6684 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6685 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686
Willy Tarreau24581ba2010-08-31 22:39:35 +02006687 /* find att_end : this is the first character after the last non
6688 * space before the equal. It may be equal to hdr_end.
6689 */
6690 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691
Willy Tarreau24581ba2010-08-31 22:39:35 +02006692 while (equal < hdr_end) {
6693 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6694 break;
6695 if (http_is_spht[(unsigned char)*equal++])
6696 continue;
6697 att_end = equal;
6698 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6701 * is between <att_beg> and <equal>, both may be identical.
6702 */
6703
6704 /* look for end of cookie if there is an equal sign */
6705 if (equal < hdr_end && *equal == '=') {
6706 /* look for the beginning of the value */
6707 val_beg = equal + 1;
6708 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6709 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710
Willy Tarreau24581ba2010-08-31 22:39:35 +02006711 /* find the end of the value, respecting quotes */
6712 next = find_cookie_value_end(val_beg, hdr_end);
6713
6714 /* make val_end point to the first white space or delimitor after the value */
6715 val_end = next;
6716 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6717 val_end--;
6718 } else {
6719 /* <equal> points to next comma, semi-colon or EOL */
6720 val_beg = val_end = next = equal;
6721 }
6722
6723 if (next < hdr_end) {
6724 /* Set-Cookie2 supports multiple cookies, and <next> points to
6725 * a colon or semi-colon before the end. So skip all attr-value
6726 * pairs and look for the next comma. For Set-Cookie, since
6727 * commas are permitted in values, skip to the end.
6728 */
6729 if (is_cookie2)
6730 next = find_hdr_value_end(next, hdr_end);
6731 else
6732 next = hdr_end;
6733 }
6734
6735 /* Now everything is as on the diagram above */
6736
6737 /* Ignore cookies with no equal sign */
6738 if (equal == val_end)
6739 continue;
6740
6741 /* If there are spaces around the equal sign, we need to
6742 * strip them otherwise we'll get trouble for cookie captures,
6743 * or even for rewrites. Since this happens extremely rarely,
6744 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6747 int stripped_before = 0;
6748 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 if (att_end != equal) {
6751 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6752 equal += stripped_before;
6753 val_beg += stripped_before;
6754 }
6755
6756 if (val_beg > equal + 1) {
6757 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6758 val_beg += stripped_after;
6759 stripped_before += stripped_after;
6760 }
6761
6762 val_end += stripped_before;
6763 next += stripped_before;
6764 hdr_end += stripped_before;
6765 hdr_next += stripped_before;
6766 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006767 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 }
6769
6770 /* First, let's see if we want to capture this cookie. We check
6771 * that we don't already have a server side cookie, because we
6772 * can only capture one. Also as an optimisation, we ignore
6773 * cookies shorter than the declared name.
6774 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006775 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006776 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006777 (val_end - att_beg >= t->fe->capture_namelen) &&
6778 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6779 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006780 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 Alert("HTTP logging : out of memory.\n");
6782 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006783 else {
6784 if (log_len > t->fe->capture_len)
6785 log_len = t->fe->capture_len;
6786 memcpy(txn->srv_cookie, att_beg, log_len);
6787 txn->srv_cookie[log_len] = 0;
6788 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789 }
6790
Willy Tarreau827aee92011-03-10 16:55:02 +01006791 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 if (!(t->flags & SN_IGNORE_PRST) &&
6794 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6795 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006796 /* assume passive cookie by default */
6797 txn->flags &= ~TX_SCK_MASK;
6798 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799
6800 /* If the cookie is in insert mode on a known server, we'll delete
6801 * this occurrence because we'll insert another one later.
6802 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006803 * a direct access.
6804 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006805 if (t->be->options2 & PR_O2_COOK_PSV) {
6806 /* The "preserve" flag was set, we don't want to touch the
6807 * server's cookie.
6808 */
6809 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006810 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006811 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 /* this cookie must be deleted */
6813 if (*prev == ':' && next == hdr_end) {
6814 /* whole header */
6815 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6816 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6817 txn->hdr_idx.used--;
6818 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006819 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 hdr_next += delta;
6821 http_msg_move_end(&txn->rsp, delta);
6822 /* note: while both invalid now, <next> and <hdr_end>
6823 * are still equal, so the for() will stop as expected.
6824 */
6825 } else {
6826 /* just remove the value */
6827 int delta = del_hdr_value(res, &prev, next);
6828 next = prev;
6829 hdr_end += delta;
6830 hdr_next += delta;
6831 cur_hdr->len += delta;
6832 http_msg_move_end(&txn->rsp, delta);
6833 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006834 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006835 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006836 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006838 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 * with this server since we know it.
6841 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006842 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 next += delta;
6844 hdr_end += delta;
6845 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006847 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848
Willy Tarreauf1348312010-10-07 15:54:11 +02006849 txn->flags &= ~TX_SCK_MASK;
6850 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006852 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006856 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006857 next += delta;
6858 hdr_end += delta;
6859 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006861 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862
Willy Tarreau827aee92011-03-10 16:55:02 +01006863 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006864 txn->flags &= ~TX_SCK_MASK;
6865 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866 }
6867 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006868 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6869 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006870 int cmp_len, value_len;
6871 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872
Cyril Bontéb21570a2009-11-29 20:04:48 +01006873 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006874 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6875 value_begin = att_beg + t->be->appsession_name_len;
6876 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006877 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006878 cmp_len = att_end - att_beg;
6879 value_begin = val_beg;
6880 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006881 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006882
Cyril Bonté17530c32010-04-06 21:11:10 +02006883 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6885 /* free a possibly previously allocated memory */
6886 pool_free2(apools.sessid, txn->sessid);
6887
Cyril Bontéb21570a2009-11-29 20:04:48 +01006888 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006889 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006890 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6891 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6892 return;
6893 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006894 memcpy(txn->sessid, value_begin, value_len);
6895 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 }
6898 /* that's done for this cookie, check the next one on the same
6899 * line when next != hdr_end (only if is_cookie2).
6900 */
6901 }
6902 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006905
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006906 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006907 appsess *asession = NULL;
6908 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006909 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006910 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006911 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006912 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6913 Alert("Not enough Memory process_srv():asession:calloc().\n");
6914 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6915 return;
6916 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006917 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6918
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006919 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6920 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6921 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006922 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006923 return;
6924 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006925 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006926 asession->sessid[t->be->appsession_len] = 0;
6927
Willy Tarreau827aee92011-03-10 16:55:02 +01006928 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006929 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006930 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006931 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006932 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 return;
6934 }
6935 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006936 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937
6938 asession->request_count = 0;
6939 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6940 }
6941
6942 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6943 asession->request_count++;
6944 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945}
6946
6947
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948/*
6949 * Check if response is cacheable or not. Updates t->flags.
6950 */
6951void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6952{
6953 struct http_txn *txn = &t->txn;
6954 char *p1, *p2;
6955
6956 char *cur_ptr, *cur_end, *cur_next;
6957 int cur_idx;
6958
Willy Tarreau5df51872007-11-25 16:20:08 +01006959 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960 return;
6961
6962 /* Iterate through the headers.
6963 * we start with the start line.
6964 */
6965 cur_idx = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02006966 cur_next = rtr->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967
6968 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6969 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006970 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971
6972 cur_hdr = &txn->hdr_idx.v[cur_idx];
6973 cur_ptr = cur_next;
6974 cur_end = cur_ptr + cur_hdr->len;
6975 cur_next = cur_end + cur_hdr->cr + 1;
6976
6977 /* We have one full header between cur_ptr and cur_end, and the
6978 * next header starts at cur_next. We're only interested in
6979 * "Cookie:" headers.
6980 */
6981
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006982 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6983 if (val) {
6984 if ((cur_end - (cur_ptr + val) >= 8) &&
6985 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6986 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6987 return;
6988 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 }
6990
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006991 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6992 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 continue;
6994
6995 /* OK, right now we know we have a cache-control header at cur_ptr */
6996
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006997 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998
6999 if (p1 >= cur_end) /* no more info */
7000 continue;
7001
7002 /* p1 is at the beginning of the value */
7003 p2 = p1;
7004
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007005 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 p2++;
7007
7008 /* we have a complete value between p1 and p2 */
7009 if (p2 < cur_end && *p2 == '=') {
7010 /* we have something of the form no-cache="set-cookie" */
7011 if ((cur_end - p1 >= 21) &&
7012 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7013 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007014 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015 continue;
7016 }
7017
7018 /* OK, so we know that either p2 points to the end of string or to a comma */
7019 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7020 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7021 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7022 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007023 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024 return;
7025 }
7026
7027 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 continue;
7030 }
7031 }
7032}
7033
7034
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035/*
7036 * Try to retrieve a known appsession in the URI, then the associated server.
7037 * If the server is found, it's assigned to the session.
7038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007040{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007041 char *end_params, *first_param, *cur_param, *next_param;
7042 char separator;
7043 int value_len;
7044
7045 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007046
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007047 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007048 (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 +01007049 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007050 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051
Cyril Bontéb21570a2009-11-29 20:04:48 +01007052 first_param = NULL;
7053 switch (mode) {
7054 case PR_O2_AS_M_PP:
7055 first_param = memchr(begin, ';', len);
7056 break;
7057 case PR_O2_AS_M_QS:
7058 first_param = memchr(begin, '?', len);
7059 break;
7060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007061
Cyril Bontéb21570a2009-11-29 20:04:48 +01007062 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007064 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065
Cyril Bontéb21570a2009-11-29 20:04:48 +01007066 switch (mode) {
7067 case PR_O2_AS_M_PP:
7068 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7069 end_params = (char *) begin + len;
7070 }
7071 separator = ';';
7072 break;
7073 case PR_O2_AS_M_QS:
7074 end_params = (char *) begin + len;
7075 separator = '&';
7076 break;
7077 default:
7078 /* unknown mode, shouldn't happen */
7079 return;
7080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 cur_param = next_param = end_params;
7083 while (cur_param > first_param) {
7084 cur_param--;
7085 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7086 /* let's see if this is the appsession parameter */
7087 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7088 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7089 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7090 /* Cool... it's the right one */
7091 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7092 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7093 if (value_len > 0) {
7094 manage_client_side_appsession(t, cur_param, value_len);
7095 }
7096 break;
7097 }
7098 next_param = cur_param;
7099 }
7100 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007102 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007103 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105}
7106
Willy Tarreaub2513902006-12-17 14:52:38 +01007107/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007108 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007109 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007110 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007111 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007112 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007113 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007114 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007115 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007116int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007117{
7118 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007119 struct http_msg *msg = &txn->req;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007120 const char *uri = msg->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007121 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007122
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007123 if (!uri_auth)
7124 return 0;
7125
Cyril Bonté70be45d2010-10-12 00:14:35 +02007126 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007127 return 0;
7128
Willy Tarreau295a8372011-03-10 11:25:07 +01007129 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007130 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007133 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007134 return 0;
7135
Willy Tarreau3a215be2012-03-09 21:39:51 +01007136 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007137 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007138 return 0;
7139
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007140 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007141 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007142 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007143 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007144 break;
7145 }
7146 h++;
7147 }
7148
7149 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007150 h = uri + uri_auth->uri_len;
7151 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007152 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007153 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007154 break;
7155 }
7156 h++;
7157 }
7158 }
7159
Willy Tarreau3a215be2012-03-09 21:39:51 +01007160 h = uri + uri_auth->uri_len;
7161 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007162 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007163 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007164 break;
7165 }
7166 h++;
7167 }
7168
Willy Tarreau3a215be2012-03-09 21:39:51 +01007169 h = uri + uri_auth->uri_len;
7170 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007171 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007172 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007173 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007174 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007175 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7176 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7177 si->applet.ctx.stats.st_code = i;
7178 break;
7179 }
7180 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007181 break;
7182 }
7183 h++;
7184 }
7185
Willy Tarreau295a8372011-03-10 11:25:07 +01007186 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007187
Willy Tarreaub2513902006-12-17 14:52:38 +01007188 return 1;
7189}
7190
Willy Tarreau4076a152009-04-02 15:18:36 +02007191/*
7192 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007193 * By default it tries to report the error position as msg->err_pos. However if
7194 * this one is not set, it will then report msg->next, which is the last known
7195 * parsing point. The function is able to deal with wrapping buffers. It always
7196 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007197 */
7198void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007199 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007200 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007201{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007202 struct buffer *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007203 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007204
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007205 es->len = MIN(buf->i, sizeof(es->buf));
7206 len1 = buf->data + buf->size - buf->p;
7207 len1 = MIN(len1, es->len);
7208 len2 = es->len - len1; /* remaining data if buffer wraps */
7209
7210 memcpy(es->buf, buf->p, len1);
7211 if (len2)
7212 memcpy(es->buf + len1, buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007213
Willy Tarreau4076a152009-04-02 15:18:36 +02007214 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007215 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007216 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007217 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007218
Willy Tarreau4076a152009-04-02 15:18:36 +02007219 es->when = date; // user-visible date
7220 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007221 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007222 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007223 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007224 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007225 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007226 es->b_flags = buf->flags;
7227 es->s_flags = s->flags;
7228 es->t_flags = s->txn.flags;
7229 es->m_flags = msg->flags;
7230 es->b_out = buf->o;
7231 es->b_wrap = buf->data + buf->size - buf->p;
7232 es->b_tot = buf->total;
7233 es->m_clen = msg->chunk_len;
7234 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007235}
Willy Tarreaub2513902006-12-17 14:52:38 +01007236
Willy Tarreau294c4732011-12-16 21:35:50 +01007237/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7238 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7239 * performed over the whole headers. Otherwise it must contain a valid header
7240 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7241 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7242 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7243 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7244 * -1.
7245 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007246 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007247unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007248 struct hdr_idx *idx, int occ,
7249 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007250{
Willy Tarreau294c4732011-12-16 21:35:50 +01007251 struct hdr_ctx local_ctx;
7252 char *ptr_hist[MAX_HDR_HISTORY];
7253 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007254 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007255 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007256
Willy Tarreau294c4732011-12-16 21:35:50 +01007257 if (!ctx) {
7258 local_ctx.idx = 0;
7259 ctx = &local_ctx;
7260 }
7261
Willy Tarreaubce70882009-09-07 11:51:47 +02007262 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007263 /* search from the beginning */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007264 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007265 occ--;
7266 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007267 *vptr = ctx->line + ctx->val;
7268 *vlen = ctx->vlen;
7269 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007270 }
7271 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007272 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007273 }
7274
7275 /* negative occurrence, we scan all the list then walk back */
7276 if (-occ > MAX_HDR_HISTORY)
7277 return 0;
7278
Willy Tarreau294c4732011-12-16 21:35:50 +01007279 found = hist_ptr = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007280 while (http_find_header2(hname, hlen, msg->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007281 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7282 len_hist[hist_ptr] = ctx->vlen;
7283 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007284 hist_ptr = 0;
7285 found++;
7286 }
7287 if (-occ > found)
7288 return 0;
7289 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7290 * find occurrence -occ, so we have to check [hist_ptr+occ].
7291 */
7292 hist_ptr += occ;
7293 if (hist_ptr >= MAX_HDR_HISTORY)
7294 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007295 *vptr = ptr_hist[hist_ptr];
7296 *vlen = len_hist[hist_ptr];
7297 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007298}
7299
Willy Tarreaubaaee002006-06-26 02:48:02 +02007300/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301 * Print a debug line with a header
7302 */
7303void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7304{
7305 int len, max;
7306 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007307 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308 max = end - start;
David du Colombier7af46052012-05-16 14:16:48 +02007309 UBOUND(max, trashlen - len - 1);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310 len += strlcpy2(trash + len, start, max + 1);
7311 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007312 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313}
7314
Willy Tarreau0937bc42009-12-22 15:03:09 +01007315/*
7316 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7317 * the required fields are properly allocated and that we only need to (re)init
7318 * them. This should be used before processing any new request.
7319 */
7320void http_init_txn(struct session *s)
7321{
7322 struct http_txn *txn = &s->txn;
7323 struct proxy *fe = s->fe;
7324
7325 txn->flags = 0;
7326 txn->status = -1;
7327
William Lallemand5f232402012-04-05 18:02:55 +02007328 global.req_count++;
7329
Willy Tarreauf64d1412010-10-07 20:06:11 +02007330 txn->cookie_first_date = 0;
7331 txn->cookie_last_date = 0;
7332
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007333 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007334 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007335 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007336 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007337 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007338 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007339 txn->req.chunk_len = 0LL;
7340 txn->req.body_len = 0LL;
7341 txn->rsp.chunk_len = 0LL;
7342 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007343 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7344 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007345 txn->req.buf = s->req;
7346 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007347
7348 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007349
7350 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7351 if (fe->options2 & PR_O2_REQBUG_OK)
7352 txn->req.err_pos = -1; /* let buggy requests pass */
7353
Willy Tarreau46023632010-01-07 22:51:47 +01007354 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007355 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7356
Willy Tarreau46023632010-01-07 22:51:47 +01007357 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007358 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7359
7360 if (txn->hdr_idx.v)
7361 hdr_idx_init(&txn->hdr_idx);
7362}
7363
7364/* to be used at the end of a transaction */
7365void http_end_txn(struct session *s)
7366{
7367 struct http_txn *txn = &s->txn;
7368
7369 /* these ones will have been dynamically allocated */
7370 pool_free2(pool2_requri, txn->uri);
7371 pool_free2(pool2_capture, txn->cli_cookie);
7372 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007373 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007374 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007375
William Lallemanda73203e2012-03-12 12:48:57 +01007376 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007377 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007378 txn->uri = NULL;
7379 txn->srv_cookie = NULL;
7380 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007381
7382 if (txn->req.cap) {
7383 struct cap_hdr *h;
7384 for (h = s->fe->req_cap; h; h = h->next)
7385 pool_free2(h->pool, txn->req.cap[h->index]);
7386 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7387 }
7388
7389 if (txn->rsp.cap) {
7390 struct cap_hdr *h;
7391 for (h = s->fe->rsp_cap; h; h = h->next)
7392 pool_free2(h->pool, txn->rsp.cap[h->index]);
7393 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7394 }
7395
Willy Tarreau0937bc42009-12-22 15:03:09 +01007396}
7397
7398/* to be used at the end of a transaction to prepare a new one */
7399void http_reset_txn(struct session *s)
7400{
7401 http_end_txn(s);
7402 http_init_txn(s);
7403
7404 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007405 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007406 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007407 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007408 /* re-init store persistence */
7409 s->store_count = 0;
7410
Willy Tarreau0937bc42009-12-22 15:03:09 +01007411 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007412
7413 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7414
Willy Tarreau739cfba2010-01-25 23:11:14 +01007415 /* We must trim any excess data from the response buffer, because we
7416 * may have blocked an invalid response from a server that we don't
7417 * want to accidentely forward once we disable the analysers, nor do
7418 * we want those data to come along with next response. A typical
7419 * example of such data would be from a buggy server responding to
7420 * a HEAD with some data, or sending more than the advertised
7421 * content-length.
7422 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007423 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007424 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007425
Willy Tarreau0937bc42009-12-22 15:03:09 +01007426 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007427 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007428
Willy Tarreaud04e8582010-05-31 12:31:35 +02007429 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007430 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007431
7432 s->req->rex = TICK_ETERNITY;
7433 s->req->wex = TICK_ETERNITY;
7434 s->req->analyse_exp = TICK_ETERNITY;
7435 s->rep->rex = TICK_ETERNITY;
7436 s->rep->wex = TICK_ETERNITY;
7437 s->rep->analyse_exp = TICK_ETERNITY;
7438}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreauff011f22011-01-06 17:51:27 +01007440void free_http_req_rules(struct list *r) {
7441 struct http_req_rule *tr, *pr;
7442
7443 list_for_each_entry_safe(pr, tr, r, list) {
7444 LIST_DEL(&pr->list);
7445 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7446 free(pr->http_auth.realm);
7447
7448 free(pr);
7449 }
7450}
7451
7452struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7453{
7454 struct http_req_rule *rule;
7455 int cur_arg;
7456
7457 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7458 if (!rule) {
7459 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7460 return NULL;
7461 }
7462
7463 if (!*args[0]) {
7464 goto req_error_parsing;
7465 } else if (!strcmp(args[0], "allow")) {
7466 rule->action = HTTP_REQ_ACT_ALLOW;
7467 cur_arg = 1;
7468 } else if (!strcmp(args[0], "deny")) {
7469 rule->action = HTTP_REQ_ACT_DENY;
7470 cur_arg = 1;
7471 } else if (!strcmp(args[0], "auth")) {
7472 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7473 cur_arg = 1;
7474
7475 while(*args[cur_arg]) {
7476 if (!strcmp(args[cur_arg], "realm")) {
7477 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7478 cur_arg+=2;
7479 continue;
7480 } else
7481 break;
7482 }
7483 } else {
7484req_error_parsing:
7485 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7486 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7487 return NULL;
7488 }
7489
7490 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7491 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007492 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007493
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007494 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7495 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7496 file, linenum, args[0], errmsg);
7497 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007498 return NULL;
7499 }
7500 rule->cond = cond;
7501 }
7502 else if (*args[cur_arg]) {
7503 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7504 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7505 file, linenum, args[0], args[cur_arg]);
7506 return NULL;
7507 }
7508
7509 return rule;
7510}
7511
Willy Tarreau8797c062007-05-07 00:55:35 +02007512/************************************************************************/
7513/* The code below is dedicated to ACL parsing and matching */
7514/************************************************************************/
7515
7516
Willy Tarreau14174bc2012-04-16 14:34:04 +02007517/* This function ensures that the prerequisites for an L7 fetch are ready,
7518 * which means that a request or response is ready. If some data is missing,
7519 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007520 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7521 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007522 *
7523 * The function returns :
7524 * 0 if some data is missing or if the requested data cannot be fetched
7525 * -1 if it is certain that we'll never have any HTTP message there
7526 * 1 if an HTTP message is ready
7527 */
7528static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007529acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007530 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007531{
7532 struct http_txn *txn = l7;
7533 struct http_msg *msg = &txn->req;
7534
7535 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7536 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7537 */
7538
7539 if (unlikely(!s || !txn))
7540 return 0;
7541
7542 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007543 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007544
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007545 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007546 if (unlikely(!s->req))
7547 return 0;
7548
7549 if (unlikely(txn->req.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
7555 /* Try to decode HTTP request */
7556 if (likely(msg->next < s->req->i))
7557 http_msg_analyzer(msg, &txn->hdr_idx);
7558
7559 /* Still no valid request ? */
7560 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7561 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007562 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007563 return -1;
7564 }
7565 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007566 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007567 return 0;
7568 }
7569
7570 /* OK we just got a valid HTTP request. We have some minor
7571 * preparation to perform so that further checks can rely
7572 * on HTTP tests.
7573 */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007574 txn->meth = find_http_meth(msg->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007575 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7576 s->flags |= SN_REDIRECTABLE;
7577
7578 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007579 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007580 return -1;
7581 }
7582 }
7583
Willy Tarreau24e32d82012-04-23 23:55:44 +02007584 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007585 return 0; /* data might have moved and indexes changed */
7586
7587 /* otherwise everything's ready for the request */
7588 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007589 else {
7590 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007591 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7592 return 0;
7593 }
7594
7595 /* everything's OK */
7596 return 1;
7597}
Willy Tarreau8797c062007-05-07 00:55:35 +02007598
Willy Tarreauc0239e02012-04-16 14:42:55 +02007599#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007600 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 +02007601
Willy Tarreau24e32d82012-04-23 23:55:44 +02007602#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007603 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 +02007604
Willy Tarreau8797c062007-05-07 00:55:35 +02007605
7606/* 1. Check on METHOD
7607 * We use the pre-parsed method if it is known, and store its number as an
7608 * integer. If it is unknown, we use the pointer and the length.
7609 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007610static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007611{
7612 int len, meth;
7613
Willy Tarreauae8b7962007-06-09 23:10:04 +02007614 len = strlen(*text);
7615 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007616
7617 pattern->val.i = meth;
7618 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007619 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007620 if (!pattern->ptr.str) {
7621 if (err)
7622 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007623 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007624 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007625 pattern->len = len;
7626 }
7627 return 1;
7628}
7629
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007630/* This function fetches the method of current HTTP request and stores
7631 * it in the global pattern struct as a chunk. There are two possibilities :
7632 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7633 * in <len> and <ptr> is NULL ;
7634 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7635 * <len> to its length.
7636 * This is intended to be used with acl_match_meth() only.
7637 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007638static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007639acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007640 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007641{
7642 int meth;
7643 struct http_txn *txn = l7;
7644
Willy Tarreau24e32d82012-04-23 23:55:44 +02007645 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007646
Willy Tarreau8797c062007-05-07 00:55:35 +02007647 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007648 smp->type = SMP_T_UINT;
7649 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007650 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007651 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7652 /* ensure the indexes are not affected */
7653 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007654 smp->type = SMP_T_CSTR;
7655 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007656 smp->data.str.str = txn->req.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007657 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007658 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007659 return 1;
7660}
7661
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007662/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007663static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007664{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007665 int icase;
7666
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007667
Willy Tarreauf853c462012-04-23 18:53:56 +02007668 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007669 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007670 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007671 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007672 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007673 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007674
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007675 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7676 if (pattern->val.i != HTTP_METH_OTHER)
7677 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007678
7679 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007680 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007681 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007682
7683 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007684 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7685 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007686 return ACL_PAT_FAIL;
7687 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007688}
7689
7690/* 2. Check on Request/Status Version
7691 * We simply compare strings here.
7692 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007693static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007694{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007695 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007696 if (!pattern->ptr.str) {
7697 if (err)
7698 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007699 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007700 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007701 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007702 return 1;
7703}
7704
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007705static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007706acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007707 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007708{
7709 struct http_txn *txn = l7;
7710 char *ptr;
7711 int len;
7712
Willy Tarreauc0239e02012-04-16 14:42:55 +02007713 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007714
Willy Tarreau8797c062007-05-07 00:55:35 +02007715 len = txn->req.sl.rq.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007716 ptr = txn->req.buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007717
7718 while ((len-- > 0) && (*ptr++ != '/'));
7719 if (len <= 0)
7720 return 0;
7721
Willy Tarreauf853c462012-04-23 18:53:56 +02007722 smp->type = SMP_T_CSTR;
7723 smp->data.str.str = ptr;
7724 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007725
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007726 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007727 return 1;
7728}
7729
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007730static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007731acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007732 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007733{
7734 struct http_txn *txn = l7;
7735 char *ptr;
7736 int len;
7737
Willy Tarreauc0239e02012-04-16 14:42:55 +02007738 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007739
Willy Tarreau8797c062007-05-07 00:55:35 +02007740 len = txn->rsp.sl.st.v_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007741 ptr = txn->rsp.buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007742
7743 while ((len-- > 0) && (*ptr++ != '/'));
7744 if (len <= 0)
7745 return 0;
7746
Willy Tarreauf853c462012-04-23 18:53:56 +02007747 smp->type = SMP_T_CSTR;
7748 smp->data.str.str = ptr;
7749 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007750
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007751 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007752 return 1;
7753}
7754
7755/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007756static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007757acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007758 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007759{
7760 struct http_txn *txn = l7;
7761 char *ptr;
7762 int len;
7763
Willy Tarreauc0239e02012-04-16 14:42:55 +02007764 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007765
Willy Tarreau8797c062007-05-07 00:55:35 +02007766 len = txn->rsp.sl.st.c_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007767 ptr = txn->rsp.buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007768
Willy Tarreauf853c462012-04-23 18:53:56 +02007769 smp->type = SMP_T_UINT;
7770 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007771 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007772 return 1;
7773}
7774
7775/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007776static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007777acl_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007778 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007779{
7780 struct http_txn *txn = l7;
7781
Willy Tarreauc0239e02012-04-16 14:42:55 +02007782 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007783
Willy Tarreauf853c462012-04-23 18:53:56 +02007784 smp->type = SMP_T_CSTR;
7785 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007786 smp->data.str.str = txn->req.buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007787 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007788 return 1;
7789}
7790
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007791static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007792acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007793 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007794{
7795 struct http_txn *txn = l7;
7796
Willy Tarreauc0239e02012-04-16 14:42:55 +02007797 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007798
7799 /* Parse HTTP request */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007800 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 +01007801 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7802 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007803 smp->type = SMP_T_IPV4;
7804 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007805
7806 /*
7807 * If we are parsing url in frontend space, we prepare backend stage
7808 * to not parse again the same url ! optimization lazyness...
7809 */
7810 if (px->options & PR_O_HTTP_PROXY)
7811 l4->flags |= SN_ADDR_SET;
7812
Willy Tarreau37406352012-04-23 16:16:37 +02007813 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007814 return 1;
7815}
7816
7817static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007818acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007819 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007820{
7821 struct http_txn *txn = l7;
7822
Willy Tarreauc0239e02012-04-16 14:42:55 +02007823 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007824
7825 /* Same optimization as url_ip */
Willy Tarreau09d1e252012-05-18 22:36:34 +02007826 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 +02007827 smp->type = SMP_T_UINT;
7828 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007829
7830 if (px->options & PR_O_HTTP_PROXY)
7831 l4->flags |= SN_ADDR_SET;
7832
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007833 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007834 return 1;
7835}
7836
Willy Tarreau185b5c42012-04-26 15:11:51 +02007837/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7838 * Accepts an optional argument of type string containing the header field name,
7839 * and an optional argument of type signed or unsigned integer to request an
7840 * explicit occurrence of the header. Note that in the event of a missing name,
7841 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007842 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007843static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007844smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007845 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007846{
7847 struct http_txn *txn = l7;
7848 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007849 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007850 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007851 int occ = 0;
7852 const char *name_str = NULL;
7853 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007854
Willy Tarreau185b5c42012-04-26 15:11:51 +02007855 if (args) {
7856 if (args[0].type != ARGT_STR)
7857 return 0;
7858 name_str = args[0].data.str.str;
7859 name_len = args[0].data.str.len;
7860
7861 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7862 occ = args[1].data.uint;
7863 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007864
Willy Tarreaue333ec92012-04-16 16:26:40 +02007865 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007866
Willy Tarreau185b5c42012-04-26 15:11:51 +02007867 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007868 /* search for header from the beginning */
7869 ctx->idx = 0;
7870
Willy Tarreau185b5c42012-04-26 15:11:51 +02007871 if (!occ && !(opt & SMP_OPT_ITERATE))
7872 /* no explicit occurrence and single fetch => last header by default */
7873 occ = -1;
7874
7875 if (!occ)
7876 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007877 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007878
Willy Tarreau185b5c42012-04-26 15:11:51 +02007879 smp->type = SMP_T_CSTR;
7880 smp->flags |= SMP_F_VOL_HDR;
7881 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 +02007882 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007883
Willy Tarreau37406352012-04-23 16:16:37 +02007884 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007885 return 0;
7886}
7887
Willy Tarreauc11416f2007-06-17 16:58:38 +02007888/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007889 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007890 */
7891static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007892smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007893 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007894{
7895 struct http_txn *txn = l7;
7896 struct hdr_idx *idx = &txn->hdr_idx;
7897 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007898 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007899 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007900
Willy Tarreau24e32d82012-04-23 23:55:44 +02007901 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007902 return 0;
7903
Willy Tarreaue333ec92012-04-16 16:26:40 +02007904 CHECK_HTTP_MESSAGE_FIRST();
7905
Willy Tarreau33a7e692007-06-10 19:45:56 +02007906 ctx.idx = 0;
7907 cnt = 0;
Willy Tarreau09d1e252012-05-18 22:36:34 +02007908 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007909 cnt++;
7910
Willy Tarreauf853c462012-04-23 18:53:56 +02007911 smp->type = SMP_T_UINT;
7912 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007913 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007914 return 1;
7915}
7916
Willy Tarreau185b5c42012-04-26 15:11:51 +02007917/* Fetch an HTTP header's integer value. The integer value is returned. It
7918 * takes a mandatory argument of type string and an optional one of type int
7919 * to designate a specific occurrence. It returns an unsigned integer, which
7920 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007921 */
7922static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007923smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007924 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007925{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007926 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007927
Willy Tarreauf853c462012-04-23 18:53:56 +02007928 if (ret > 0) {
7929 smp->type = SMP_T_UINT;
7930 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7931 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007932
Willy Tarreaud53e2422012-04-16 17:21:11 +02007933 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007934}
7935
Willy Tarreau185b5c42012-04-26 15:11:51 +02007936/* Fetch an HTTP header's integer value. The integer value is returned. It
7937 * takes a mandatory argument of type string and an optional one of type int
7938 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02007939 */
7940static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007941smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007942 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02007943{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007944 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007945
Willy Tarreau185b5c42012-04-26 15:11:51 +02007946 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02007947 smp->type = SMP_T_IPV4;
7948 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02007949 break;
7950 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007951 if (!(smp->flags & SMP_F_NOT_LAST))
7952 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02007953 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02007954 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02007955}
7956
Willy Tarreau737b0c12007-06-10 21:28:46 +02007957/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7958 * the first '/' after the possible hostname, and ends before the possible '?'.
7959 */
7960static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007961acl_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007962 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007963{
7964 struct http_txn *txn = l7;
7965 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007966
Willy Tarreauc0239e02012-04-16 14:42:55 +02007967 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007968
Willy Tarreau09d1e252012-05-18 22:36:34 +02007969 end = txn->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007970 ptr = http_get_path(txn);
7971 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007972 return 0;
7973
7974 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02007975 smp->type = SMP_T_CSTR;
7976 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007977
7978 while (ptr < end && *ptr != '?')
7979 ptr++;
7980
Willy Tarreauf853c462012-04-23 18:53:56 +02007981 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02007982 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007983 return 1;
7984}
7985
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007986static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007987acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007988 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007989{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007990 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7991 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7992 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007993
Willy Tarreau24e32d82012-04-23 23:55:44 +02007994 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007995
Willy Tarreauf853c462012-04-23 18:53:56 +02007996 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02007997 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007998 return 1;
7999}
8000
Willy Tarreau7f18e522010-10-22 20:04:13 +02008001/* return a valid test if the current request is the first one on the connection */
8002static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008003acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008004 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008005{
8006 if (!s)
8007 return 0;
8008
Willy Tarreauf853c462012-04-23 18:53:56 +02008009 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008010 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008011 return 1;
8012}
8013
Willy Tarreau34db1082012-04-19 17:16:54 +02008014/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008015static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008016acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008017 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008018{
8019
Willy Tarreau24e32d82012-04-23 23:55:44 +02008020 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008021 return 0;
8022
Willy Tarreauc0239e02012-04-16 14:42:55 +02008023 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008024
Willy Tarreauc0239e02012-04-16 14:42:55 +02008025 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008026 return 0;
8027
Willy Tarreauf853c462012-04-23 18:53:56 +02008028 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008029 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 +01008030 return 1;
8031}
Willy Tarreau8797c062007-05-07 00:55:35 +02008032
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008033/* Accepts exactly 1 argument of type userlist */
8034static int
8035acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8036 const struct arg *args, struct sample *smp)
8037{
8038
8039 if (!args || args->type != ARGT_USR)
8040 return 0;
8041
8042 CHECK_HTTP_MESSAGE_FIRST();
8043
8044 if (!get_http_auth(l4))
8045 return 0;
8046
8047 /* acl_match_auth() will need several information at once */
8048 smp->ctx.a[0] = args->data.usr; /* user list */
8049 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8050 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8051
8052 /* if the user does not belong to the userlist or has a wrong password,
8053 * report that it unconditionally does not match. Otherwise we return
8054 * a non-zero integer which will be ignored anyway since all the params
8055 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8056 */
8057 smp->type = SMP_T_BOOL;
8058 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8059 if (smp->data.uint)
8060 smp->type = SMP_T_UINT;
8061
8062 return 1;
8063}
8064
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008065/* Try to find the next occurrence of a cookie name in a cookie header value.
8066 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8067 * the cookie value is returned into *value and *value_l, and the function
8068 * returns a pointer to the next pointer to search from if the value was found.
8069 * Otherwise if the cookie was not found, NULL is returned and neither value
8070 * nor value_l are touched. The input <hdr> string should first point to the
8071 * header's value, and the <hdr_end> pointer must point to the first character
8072 * not part of the value. <list> must be non-zero if value may represent a list
8073 * of values (cookie headers). This makes it faster to abort parsing when no
8074 * list is expected.
8075 */
8076static char *
8077extract_cookie_value(char *hdr, const char *hdr_end,
8078 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008079 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008080{
8081 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8082 char *next;
8083
8084 /* we search at least a cookie name followed by an equal, and more
8085 * generally something like this :
8086 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8087 */
8088 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8089 /* Iterate through all cookies on this line */
8090
8091 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8092 att_beg++;
8093
8094 /* find att_end : this is the first character after the last non
8095 * space before the equal. It may be equal to hdr_end.
8096 */
8097 equal = att_end = att_beg;
8098
8099 while (equal < hdr_end) {
8100 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8101 break;
8102 if (http_is_spht[(unsigned char)*equal++])
8103 continue;
8104 att_end = equal;
8105 }
8106
8107 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8108 * is between <att_beg> and <equal>, both may be identical.
8109 */
8110
8111 /* look for end of cookie if there is an equal sign */
8112 if (equal < hdr_end && *equal == '=') {
8113 /* look for the beginning of the value */
8114 val_beg = equal + 1;
8115 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8116 val_beg++;
8117
8118 /* find the end of the value, respecting quotes */
8119 next = find_cookie_value_end(val_beg, hdr_end);
8120
8121 /* make val_end point to the first white space or delimitor after the value */
8122 val_end = next;
8123 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8124 val_end--;
8125 } else {
8126 val_beg = val_end = next = equal;
8127 }
8128
8129 /* We have nothing to do with attributes beginning with '$'. However,
8130 * they will automatically be removed if a header before them is removed,
8131 * since they're supposed to be linked together.
8132 */
8133 if (*att_beg == '$')
8134 continue;
8135
8136 /* Ignore cookies with no equal sign */
8137 if (equal == next)
8138 continue;
8139
8140 /* Now we have the cookie name between att_beg and att_end, and
8141 * its value between val_beg and val_end.
8142 */
8143
8144 if (att_end - att_beg == cookie_name_l &&
8145 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8146 /* let's return this value and indicate where to go on from */
8147 *value = val_beg;
8148 *value_l = val_end - val_beg;
8149 return next + 1;
8150 }
8151
8152 /* Set-Cookie headers only have the name in the first attr=value part */
8153 if (!list)
8154 break;
8155 }
8156
8157 return NULL;
8158}
8159
Willy Tarreaue333ec92012-04-16 16:26:40 +02008160/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008161 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8162 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008163 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008164 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008165 * Accepts exactly 1 argument of type string. If the input options indicate
8166 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008167 */
8168static int
Willy Tarreau51539362012-05-08 12:46:28 +02008169smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8170 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008171{
8172 struct http_txn *txn = l7;
8173 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008174 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008175 const struct http_msg *msg;
8176 const char *hdr_name;
8177 int hdr_name_len;
8178 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008179 int occ = 0;
8180 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008181
Willy Tarreau24e32d82012-04-23 23:55:44 +02008182 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008183 return 0;
8184
Willy Tarreaue333ec92012-04-16 16:26:40 +02008185 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008186
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008187 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008188 msg = &txn->req;
8189 hdr_name = "Cookie";
8190 hdr_name_len = 6;
8191 } else {
8192 msg = &txn->rsp;
8193 hdr_name = "Set-Cookie";
8194 hdr_name_len = 10;
8195 }
8196
Willy Tarreau28376d62012-04-26 21:26:10 +02008197 if (!occ && !(opt & SMP_OPT_ITERATE))
8198 /* no explicit occurrence and single fetch => last cookie by default */
8199 occ = -1;
8200
8201 /* OK so basically here, either we want only one value and it's the
8202 * last one, or we want to iterate over all of them and we fetch the
8203 * next one.
8204 */
8205
Willy Tarreau09d1e252012-05-18 22:36:34 +02008206 sol = msg->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008207 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008208 /* search for the header from the beginning, we must first initialize
8209 * the search parameters.
8210 */
Willy Tarreau37406352012-04-23 16:16:37 +02008211 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008212 ctx->idx = 0;
8213 }
8214
Willy Tarreau28376d62012-04-26 21:26:10 +02008215 smp->flags |= SMP_F_VOL_HDR;
8216
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008217 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008218 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8219 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008220 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8221 goto out;
8222
Willy Tarreau24e32d82012-04-23 23:55:44 +02008223 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008224 continue;
8225
Willy Tarreau37406352012-04-23 16:16:37 +02008226 smp->ctx.a[0] = ctx->line + ctx->val;
8227 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008228 }
8229
Willy Tarreauf853c462012-04-23 18:53:56 +02008230 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008231 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008232 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008233 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008234 &smp->data.str.str,
8235 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008236 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008237 found = 1;
8238 if (occ >= 0) {
8239 /* one value was returned into smp->data.str.{str,len} */
8240 smp->flags |= SMP_F_NOT_LAST;
8241 return 1;
8242 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008243 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008244 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008245 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008246 /* all cookie headers and values were scanned. If we're looking for the
8247 * last occurrence, we may return it now.
8248 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008249 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008250 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008251 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008252}
8253
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008254/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008255 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008256 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008257 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008258 */
8259static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008260acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008261 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008262{
8263 struct http_txn *txn = l7;
8264 struct hdr_idx *idx = &txn->hdr_idx;
8265 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008266 const struct http_msg *msg;
8267 const char *hdr_name;
8268 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008269 int cnt;
8270 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008271 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008272
Willy Tarreau24e32d82012-04-23 23:55:44 +02008273 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008274 return 0;
8275
Willy Tarreaue333ec92012-04-16 16:26:40 +02008276 CHECK_HTTP_MESSAGE_FIRST();
8277
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008278 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008279 msg = &txn->req;
8280 hdr_name = "Cookie";
8281 hdr_name_len = 6;
8282 } else {
8283 msg = &txn->rsp;
8284 hdr_name = "Set-Cookie";
8285 hdr_name_len = 10;
8286 }
8287
Willy Tarreau09d1e252012-05-18 22:36:34 +02008288 sol = msg->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008289 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008290 ctx.idx = 0;
8291 cnt = 0;
8292
8293 while (1) {
8294 /* Note: val_beg == NULL every time we need to fetch a new header */
8295 if (!val_beg) {
8296 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8297 break;
8298
Willy Tarreau24e32d82012-04-23 23:55:44 +02008299 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008300 continue;
8301
8302 val_beg = ctx.line + ctx.val;
8303 val_end = val_beg + ctx.vlen;
8304 }
8305
Willy Tarreauf853c462012-04-23 18:53:56 +02008306 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008307 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008308 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008309 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008310 &smp->data.str.str,
8311 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008312 cnt++;
8313 }
8314 }
8315
Willy Tarreauf853c462012-04-23 18:53:56 +02008316 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008317 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008318 return 1;
8319}
8320
Willy Tarreau51539362012-05-08 12:46:28 +02008321/* Fetch an cookie's integer value. The integer value is returned. It
8322 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8323 */
8324static int
8325smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8326 const struct arg *args, struct sample *smp)
8327{
8328 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8329
8330 if (ret > 0) {
8331 smp->type = SMP_T_UINT;
8332 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8333 }
8334
8335 return ret;
8336}
8337
Willy Tarreau8797c062007-05-07 00:55:35 +02008338/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008339/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008340/************************************************************************/
8341
David Cournapeau16023ee2010-12-23 20:55:41 +09008342/*
8343 * Given a path string and its length, find the position of beginning of the
8344 * query string. Returns NULL if no query string is found in the path.
8345 *
8346 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8347 *
8348 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8349 */
8350static inline char *find_query_string(char *path, size_t path_l)
8351{
8352 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008353
David Cournapeau16023ee2010-12-23 20:55:41 +09008354 p = memchr(path, '?', path_l);
8355 return p ? p + 1 : NULL;
8356}
8357
8358static inline int is_param_delimiter(char c)
8359{
8360 return c == '&' || c == ';';
8361}
8362
8363/*
8364 * Given a url parameter, find the starting position of the first occurence,
8365 * or NULL if the parameter is not found.
8366 *
8367 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8368 * the function will return query_string+8.
8369 */
8370static char*
8371find_url_param_pos(char* query_string, size_t query_string_l,
8372 char* url_param_name, size_t url_param_name_l)
8373{
8374 char *pos, *last;
8375
8376 pos = query_string;
8377 last = query_string + query_string_l - url_param_name_l - 1;
8378
8379 while (pos <= last) {
8380 if (pos[url_param_name_l] == '=') {
8381 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8382 return pos;
8383 pos += url_param_name_l + 1;
8384 }
8385 while (pos <= last && !is_param_delimiter(*pos))
8386 pos++;
8387 pos++;
8388 }
8389 return NULL;
8390}
8391
8392/*
8393 * Given a url parameter name, returns its value and size into *value and
8394 * *value_l respectively, and returns non-zero. If the parameter is not found,
8395 * zero is returned and value/value_l are not touched.
8396 */
8397static int
8398find_url_param_value(char* path, size_t path_l,
8399 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008400 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008401{
8402 char *query_string, *qs_end;
8403 char *arg_start;
8404 char *value_start, *value_end;
8405
8406 query_string = find_query_string(path, path_l);
8407 if (!query_string)
8408 return 0;
8409
8410 qs_end = path + path_l;
8411 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8412 url_param_name, url_param_name_l);
8413 if (!arg_start)
8414 return 0;
8415
8416 value_start = arg_start + url_param_name_l + 1;
8417 value_end = value_start;
8418
8419 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8420 value_end++;
8421
8422 *value = value_start;
8423 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008424 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008425}
8426
8427static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008428smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8429 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008430{
8431 struct http_txn *txn = l7;
8432 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008433
8434 if (!args || args->type != ARGT_STR)
8435 return 0;
8436
8437 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008438
Willy Tarreau09d1e252012-05-18 22:36:34 +02008439 if (!find_url_param_value(msg->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008440 args->data.str.str, args->data.str.len,
8441 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008442 return 0;
8443
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008444 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008445 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008446 return 1;
8447}
8448
Willy Tarreau185b5c42012-04-26 15:11:51 +02008449/* This function is used to validate the arguments passed to any "hdr" fetch
8450 * keyword. These keywords support an optional positive or negative occurrence
8451 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8452 * is assumed that the types are already the correct ones. Returns 0 on error,
8453 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8454 * error message in case of error, that the caller is responsible for freeing.
8455 * The initial location must either be freeable or NULL.
8456 */
8457static int val_hdr(struct arg *arg, char **err_msg)
8458{
8459 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8460 if (err_msg)
8461 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8462 return 0;
8463 }
8464 return 1;
8465}
8466
Willy Tarreau4a568972010-05-12 08:08:50 +02008467/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008468/* All supported ACL keywords must be declared here. */
8469/************************************************************************/
8470
8471/* Note: must not be declared <const> as its list will be overwritten.
8472 * Please take care of keeping this list alphabetically sorted.
8473 */
8474static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau51539362012-05-08 12:46:28 +02008475 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8476 { "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 +02008477 { "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 +02008478 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8479 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8480 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8481 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8482 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8483 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8484 { "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 +02008485
Willy Tarreau185b5c42012-04-26 15:11:51 +02008486 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8487 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8488 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8489 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8490 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8491 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8492 { "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 },
8493 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8494 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8495 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8496 { "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 +02008497
8498 { "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 +02008499 { "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 +02008500 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8501
8502 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8503
8504 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8505 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8506 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8507 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8508 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8509 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8510 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8511 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8512
8513 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8514 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8515 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8516
Willy Tarreau51539362012-05-08 12:46:28 +02008517 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8518 { "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 +02008519 { "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 +02008520 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8521 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8522 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8523 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8524 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8525 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8526 { "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 +02008527
Willy Tarreau185b5c42012-04-26 15:11:51 +02008528 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8529 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8530 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8531 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8532 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8533 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8534 { "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 },
8535 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8536 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8537 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8538 { "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 +02008539
8540 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8541
8542 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8543 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8544 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8545 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8546 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8547 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8548 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8549 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8550 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8551 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8552
8553 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8554 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8555 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8556 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8557 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8558 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8559 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8560 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8561 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8562
8563 { NULL, NULL, NULL, NULL },
8564}};
8565
8566/************************************************************************/
8567/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008568/************************************************************************/
8569/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008570static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008571 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008572 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008573 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8574 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008575 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008576}};
8577
Willy Tarreau8797c062007-05-07 00:55:35 +02008578
8579__attribute__((constructor))
8580static void __http_protocol_init(void)
8581{
8582 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008583 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008584}
8585
8586
Willy Tarreau58f10d72006-12-04 02:26:12 +01008587/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008588 * Local variables:
8589 * c-indent-level: 8
8590 * c-basic-offset: 8
8591 * End:
8592 */