blob: de1cec680705b2d25623b74128cde91a7ad17fe7 [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 Tarreaucff64112008-11-03 06:26:53 +010062#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/task.h>
64
Willy Tarreau522d6c02009-12-06 18:49:18 +010065const char HTTP_100[] =
66 "HTTP/1.1 100 Continue\r\n\r\n";
67
68const struct chunk http_100_chunk = {
69 .str = (char *)&HTTP_100,
70 .len = sizeof(HTTP_100)-1
71};
72
Willy Tarreaua9679ac2010-01-03 17:32:57 +010073/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Location: "; /* not terminated since it will be concatenated with the URL */
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
86/* same as 302 except that the browser MUST retry with the GET method */
87const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
94const char *HTTP_401_fmt =
95 "HTTP/1.0 401 Unauthorized\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020098 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
100 "\r\n"
101 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
102
Willy Tarreau844a7e72010-01-31 21:46:18 +0100103const char *HTTP_407_fmt =
104 "HTTP/1.0 407 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Content-Type: text/html\r\n"
108 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200114 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100115 [HTTP_ERR_400] = 400,
116 [HTTP_ERR_403] = 403,
117 [HTTP_ERR_408] = 408,
118 [HTTP_ERR_500] = 500,
119 [HTTP_ERR_502] = 502,
120 [HTTP_ERR_503] = 503,
121 [HTTP_ERR_504] = 504,
122};
123
Willy Tarreau80587432006-12-24 17:47:20 +0100124static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200125 [HTTP_ERR_200] =
126 "HTTP/1.0 200 OK\r\n"
127 "Cache-Control: no-cache\r\n"
128 "Connection: close\r\n"
129 "Content-Type: text/html\r\n"
130 "\r\n"
131 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
132
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100134 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 "Cache-Control: no-cache\r\n"
136 "Connection: close\r\n"
137 "Content-Type: text/html\r\n"
138 "\r\n"
139 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
140
141 [HTTP_ERR_403] =
142 "HTTP/1.0 403 Forbidden\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
148
149 [HTTP_ERR_408] =
150 "HTTP/1.0 408 Request Time-out\r\n"
151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
156
157 [HTTP_ERR_500] =
158 "HTTP/1.0 500 Server Error\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
164
165 [HTTP_ERR_502] =
166 "HTTP/1.0 502 Bad Gateway\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
172
173 [HTTP_ERR_503] =
174 "HTTP/1.0 503 Service Unavailable\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
180
181 [HTTP_ERR_504] =
182 "HTTP/1.0 504 Gateway Time-out\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
188
189};
190
Cyril Bonté19979e12012-04-04 12:57:21 +0200191/* status codes available for the stats admin page (strictly 4 chars length) */
192const char *stat_status_codes[STAT_STATUS_SIZE] = {
193 [STAT_STATUS_DENY] = "DENY",
194 [STAT_STATUS_DONE] = "DONE",
195 [STAT_STATUS_ERRP] = "ERRP",
196 [STAT_STATUS_EXCD] = "EXCD",
197 [STAT_STATUS_NONE] = "NONE",
198 [STAT_STATUS_PART] = "PART",
199 [STAT_STATUS_UNKN] = "UNKN",
200};
201
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203/* We must put the messages here since GCC cannot initialize consts depending
204 * on strlen().
205 */
206struct chunk http_err_chunks[HTTP_ERR_SIZE];
207
Willy Tarreau42250582007-04-01 01:30:43 +0200208#define FD_SETS_ARE_BITFIELDS
209#ifdef FD_SETS_ARE_BITFIELDS
210/*
211 * This map is used with all the FD_* macros to check whether a particular bit
212 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
213 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
214 * byte should be encoded. Be careful to always pass bytes from 0 to 255
215 * exclusively to the macros.
216 */
217fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
218fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219
220#else
221#error "Check if your OS uses bitfields for fd_sets"
222#endif
223
Willy Tarreau80587432006-12-24 17:47:20 +0100224void init_proto_http()
225{
Willy Tarreau42250582007-04-01 01:30:43 +0200226 int i;
227 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100228 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200229
Willy Tarreau80587432006-12-24 17:47:20 +0100230 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
231 if (!http_err_msgs[msg]) {
232 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
233 abort();
234 }
235
236 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
237 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
238 }
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240 /* initialize the log header encoding map : '{|}"#' should be encoded with
241 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
242 * URL encoding only requires '"', '#' to be encoded as well as non-
243 * printable characters above.
244 */
245 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
246 memset(url_encode_map, 0, sizeof(url_encode_map));
247 for (i = 0; i < 32; i++) {
248 FD_SET(i, hdr_encode_map);
249 FD_SET(i, url_encode_map);
250 }
251 for (i = 127; i < 256; i++) {
252 FD_SET(i, hdr_encode_map);
253 FD_SET(i, url_encode_map);
254 }
255
256 tmp = "\"#{|}";
257 while (*tmp) {
258 FD_SET(*tmp, hdr_encode_map);
259 tmp++;
260 }
261
262 tmp = "\"#";
263 while (*tmp) {
264 FD_SET(*tmp, url_encode_map);
265 tmp++;
266 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200267
268 /* memory allocations */
269 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200270 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100271 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100272}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274/*
275 * We have 26 list of methods (1 per first letter), each of which can have
276 * up to 3 entries (2 valid, 1 null).
277 */
278struct http_method_desc {
279 http_meth_t meth;
280 int len;
281 const char text[8];
282};
283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100284const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100285 ['C' - 'A'] = {
286 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
287 },
288 ['D' - 'A'] = {
289 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
290 },
291 ['G' - 'A'] = {
292 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
293 },
294 ['H' - 'A'] = {
295 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
296 },
297 ['P' - 'A'] = {
298 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
299 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
300 },
301 ['T' - 'A'] = {
302 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
303 },
304 /* rest is empty like this :
305 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
306 */
307};
308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100309/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311 * RFC2616 for those chars.
312 */
313
314const char http_is_spht[256] = {
315 [' '] = 1, ['\t'] = 1,
316};
317
318const char http_is_crlf[256] = {
319 ['\r'] = 1, ['\n'] = 1,
320};
321
322const char http_is_lws[256] = {
323 [' '] = 1, ['\t'] = 1,
324 ['\r'] = 1, ['\n'] = 1,
325};
326
327const char http_is_sep[256] = {
328 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
329 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
330 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
331 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
332 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
333};
334
335const char http_is_ctl[256] = {
336 [0 ... 31] = 1,
337 [127] = 1,
338};
339
340/*
341 * A token is any ASCII char that is neither a separator nor a CTL char.
342 * Do not overwrite values in assignment since gcc-2.95 will not handle
343 * them correctly. Instead, define every non-CTL char's status.
344 */
345const char http_is_token[256] = {
346 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
347 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
348 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
349 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
350 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
351 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
352 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
353 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
354 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
355 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
356 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
357 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
358 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
359 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
360 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
361 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
362 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
363 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
364 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
365 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
366 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
367 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
368 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
369 ['|'] = 1, ['}'] = 0, ['~'] = 1,
370};
371
372
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100373/*
374 * An http ver_token is any ASCII which can be found in an HTTP version,
375 * which includes 'H', 'T', 'P', '/', '.' and any digit.
376 */
377const char http_is_ver_token[256] = {
378 ['.'] = 1, ['/'] = 1,
379 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
380 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
381 ['H'] = 1, ['P'] = 1, ['T'] = 1,
382};
383
384
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100386 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
387 */
388#if defined(DEBUG_FSM)
389static void http_silent_debug(int line, struct session *s)
390{
391 int size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200392 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100393 "[%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 +0100394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +0200396 s->req->buf.data, s->req->buf.size, s->req->l, s->req->w, s->req->r, s->req->buf.p, s->req->buf.o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100397 write(-1, trash, size);
398 size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200399 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100400 " %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 +0100401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +0200403 s->rep->buf.data, s->rep->buf.size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf.p, s->rep->buf.o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100404
405 write(-1, trash, size);
406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100412 * Adds a header and its CRLF at the tail of the message's buffer, just before
413 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100414 * The header is also automatically added to the index <hdr_idx>, and the end
415 * of headers is automatically adjusted. The number of bytes added is returned
416 * on success, otherwise <0 is returned indicating an error.
417 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100418int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100419{
420 int bytes, len;
421
422 len = strlen(text);
Willy Tarreau572bf902012-07-02 17:01:20 +0200423 bytes = buffer_insert_line2(msg->buf, msg->buf->buf.p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100424 if (!bytes)
425 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100426 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100427 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
428}
429
430/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100431 * Adds a header and its CRLF at the tail of the message's buffer, just before
432 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 * the buffer is only opened and the space reserved, but nothing is copied.
434 * The header is also automatically added to the index <hdr_idx>, and the end
435 * of headers is automatically adjusted. The number of bytes added is returned
436 * on success, otherwise <0 is returned indicating an error.
437 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100438int http_header_add_tail2(struct http_msg *msg,
439 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440{
441 int bytes;
442
Willy Tarreau572bf902012-07-02 17:01:20 +0200443 bytes = buffer_insert_line2(msg->buf, msg->buf->buf.p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100444 if (!bytes)
445 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100446 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100447 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
448}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
450/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100451 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
452 * If so, returns the position of the first non-space character relative to
453 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
454 * to return a pointer to the place after the first space. Returns 0 if the
455 * header name does not match. Checks are case-insensitive.
456 */
457int http_header_match2(const char *hdr, const char *end,
458 const char *name, int len)
459{
460 const char *val;
461
462 if (hdr + len >= end)
463 return 0;
464 if (hdr[len] != ':')
465 return 0;
466 if (strncasecmp(hdr, name, len) != 0)
467 return 0;
468 val = hdr + len + 1;
469 while (val < end && HTTP_IS_SPHT(*val))
470 val++;
471 if ((val >= end) && (len + 2 <= end - hdr))
472 return len + 2; /* we may replace starting from second space */
473 return val - hdr;
474}
475
Willy Tarreau68085d82010-01-18 14:54:04 +0100476/* Find the end of the header value contained between <s> and <e>. See RFC2616,
477 * par 2.2 for more information. Note that it requires a valid header to return
478 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100480char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481{
482 int quoted, qdpair;
483
484 quoted = qdpair = 0;
485 for (; s < e; s++) {
486 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200487 else if (quoted) {
488 if (*s == '\\') qdpair = 1;
489 else if (*s == '"') quoted = 0;
490 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 else if (*s == '"') quoted = 1;
492 else if (*s == ',') return s;
493 }
494 return s;
495}
496
497/* Find the first or next occurrence of header <name> in message buffer <sol>
498 * using headers index <idx>, and return it in the <ctx> structure. This
499 * structure holds everything necessary to use the header and find next
500 * occurrence. If its <idx> member is 0, the header is searched from the
501 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 * 1 when it finds a value, and 0 when there is no more. It is designed to work
503 * with headers defined as comma-separated lists. As a special case, if ctx->val
504 * is NULL when searching for a new values of a header, the current header is
505 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 */
507int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 struct hdr_ctx *ctx)
510{
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 char *eol, *sov;
512 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 cur_idx = ctx->idx;
515 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200516 /* We have previously returned a value, let's search
517 * another one on the same line.
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200520 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100521 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 eol = sol + idx->v[cur_idx].len;
523
524 if (sov >= eol)
525 /* no more values in this header */
526 goto next_hdr;
527
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 /* values remaining for this header, skip the comma but save it
529 * for later use (eg: for header deletion).
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sov++;
532 while (sov < eol && http_is_lws[(unsigned char)*sov])
533 sov++;
534
535 goto return_hdr;
536 }
537
538 /* first request for this header */
539 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 while (cur_idx) {
543 eol = sol + idx->v[cur_idx].len;
544
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200545 if (len == 0) {
546 /* No argument was passed, we want any header.
547 * To achieve this, we simply build a fake request. */
548 while (sol + len < eol && sol[len] != ':')
549 len++;
550 name = sol;
551 }
552
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 if ((len < eol - sol) &&
554 (sol[len] == ':') &&
555 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100556 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 sov = sol + len + 1;
558 while (sov < eol && http_is_lws[(unsigned char)*sov])
559 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100560
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 ctx->prev = old_idx;
563 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 ctx->idx = cur_idx;
565 ctx->val = sov - sol;
566
567 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200568 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200569 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 eol--;
571 ctx->tws++;
572 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->vlen = eol - sov;
574 return 1;
575 }
576 next_hdr:
577 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100578 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 cur_idx = idx->v[cur_idx].next;
580 }
581 return 0;
582}
583
584int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100585 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200586 struct hdr_ctx *ctx)
587{
588 return http_find_header2(name, strlen(name), sol, idx, ctx);
589}
590
Willy Tarreau68085d82010-01-18 14:54:04 +0100591/* Remove one value of a header. This only works on a <ctx> returned by one of
592 * the http_find_header functions. The value is removed, as well as surrounding
593 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100594 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100595 * message <msg>. The new index is returned. If it is zero, it means there is
596 * no more header, so any processing may stop. The ctx is always left in a form
597 * that can be handled by http_find_header2() to find next occurrence.
598 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100599int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100600{
601 int cur_idx = ctx->idx;
602 char *sol = ctx->line;
603 struct hdr_idx_elem *hdr;
604 int delta, skip_comma;
605
606 if (!cur_idx)
607 return 0;
608
609 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200610 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100611 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100612 delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 http_msg_move_end(msg, delta);
614 idx->used--;
615 hdr->len = 0; /* unused entry */
616 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100617 if (idx->tail == ctx->idx)
618 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100619 ctx->idx = ctx->prev; /* walk back to the end of previous header */
620 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
621 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 return ctx->idx;
624 }
625
626 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
628 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 */
630
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200631 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100632 delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 NULL, 0);
635 hdr->len += delta;
636 http_msg_move_end(msg, delta);
637 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200638 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 return ctx->idx;
640}
641
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100642/* This function handles a server error at the stream interface level. The
643 * stream interface is assumed to be already in a closed state. An optional
644 * message is copied into the input buffer, and an HTTP status code stored.
645 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100646 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648static void http_server_error(struct session *t, struct stream_interface *si,
649 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100651 buffer_auto_read(si->ob);
652 buffer_abort(si->ob);
653 buffer_auto_close(si->ob);
654 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200655 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100656 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100657 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100658 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200659 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 }
661 if (!(t->flags & SN_ERR_MASK))
662 t->flags |= err;
663 if (!(t->flags & SN_FINST_MASK))
664 t->flags |= finst;
665}
666
Willy Tarreau80587432006-12-24 17:47:20 +0100667/* This function returns the appropriate error location for the given session
668 * and message.
669 */
670
671struct chunk *error_message(struct session *s, int msgnum)
672{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200673 if (s->be->errmsg[msgnum].str)
674 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100675 else if (s->fe->errmsg[msgnum].str)
676 return &s->fe->errmsg[msgnum];
677 else
678 return &http_err_chunks[msgnum];
679}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Willy Tarreau53b6c742006-12-17 13:37:46 +0100681/*
682 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
683 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
684 */
685static http_meth_t find_http_meth(const char *str, const int len)
686{
687 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100689
690 m = ((unsigned)*str - 'A');
691
692 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100693 for (h = http_methods[m]; h->len > 0; h++) {
694 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100695 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100696 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 };
699 return HTTP_METH_OTHER;
700 }
701 return HTTP_METH_NONE;
702
703}
704
Willy Tarreau21d2af32008-02-14 20:25:24 +0100705/* Parse the URI from the given transaction (which is assumed to be in request
706 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
707 * It is returned otherwise.
708 */
709static char *
710http_get_path(struct http_txn *txn)
711{
712 char *ptr, *end;
713
Willy Tarreau572bf902012-07-02 17:01:20 +0200714 ptr = txn->req.buf->buf.p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100715 end = ptr + txn->req.sl.rq.u_l;
716
717 if (ptr >= end)
718 return NULL;
719
720 /* RFC2616, par. 5.1.2 :
721 * Request-URI = "*" | absuri | abspath | authority
722 */
723
724 if (*ptr == '*')
725 return NULL;
726
727 if (isalpha((unsigned char)*ptr)) {
728 /* this is a scheme as described by RFC3986, par. 3.1 */
729 ptr++;
730 while (ptr < end &&
731 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
732 ptr++;
733 /* skip '://' */
734 if (ptr == end || *ptr++ != ':')
735 return NULL;
736 if (ptr == end || *ptr++ != '/')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 }
741 /* skip [user[:passwd]@]host[:[port]] */
742
743 while (ptr < end && *ptr != '/')
744 ptr++;
745
746 if (ptr == end)
747 return NULL;
748
749 /* OK, we got the '/' ! */
750 return ptr;
751}
752
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753/* Returns a 302 for a redirectable request. This may only be called just after
754 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
755 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200756 * NOTE: this function is designed to support being called once data are scheduled
757 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760{
761 struct http_txn *txn;
762 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200765 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100766
767 /* 1: create the response header */
768 rdr.len = strlen(HTTP_302);
769 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200770 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 memcpy(rdr.str, HTTP_302, rdr.len);
772
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 srv = target_srv(&s->target);
774
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100777 return;
778
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
781 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
782 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100783 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200785 /* 3: add the request URI. Since it was already forwarded, we need
786 * to temporarily rewind the buffer.
787 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100788 txn = &s->txn;
Willy Tarreau572bf902012-07-02 17:01:20 +0200789 b_rew(s->req, rewind = s->req->buf.o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200790
Willy Tarreauefb453c2008-10-26 20:49:47 +0100791 path = http_get_path(txn);
Willy Tarreau572bf902012-07-02 17:01:20 +0200792 len = buffer_count(&s->req->buf, path, b_ptr(&s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200793
794 b_adv(s->req, rewind);
795
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 if (!path)
797 return;
798
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200799 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 return;
801
802 memcpy(rdr.str + rdr.len, path, len);
803 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100804
805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
806 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
807 rdr.len += 29;
808 } else {
809 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
810 rdr.len += 23;
811 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812
813 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200814 si_shutr(si);
815 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816 si->err_type = SI_ET_NONE;
817 si->err_loc = NULL;
818 si->state = SI_ST_CLO;
819
820 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822
823 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100824 if (srv)
825 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826}
827
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 * that the server side is closed. Note that err_type is actually a
830 * bitmask, where almost only aborts may be cumulated with other
831 * values. We consider that aborted operations are more important
832 * than timeouts or errors due to the fact that nobody else in the
833 * logs might explain incomplete retries. All others should avoid
834 * being cumulated. It should normally not be possible to have multiple
835 * aborts at once, but just in case, the first one in sequence is reported.
836 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100837void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100838{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840
841 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
843 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
846 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
849 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
852 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100854 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
855 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100856 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100857 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
858 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100860 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
861 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100862}
863
Willy Tarreau42250582007-04-01 01:30:43 +0200864extern const char sess_term_cond[8];
865extern const char sess_fin_state[8];
866extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200867struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200868struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100869struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100870
Willy Tarreau117f59e2007-03-04 18:17:17 +0100871/*
872 * Capture headers from message starting at <som> according to header list
873 * <cap_hdr>, and fill the <idx> structure appropriately.
874 */
875void capture_headers(char *som, struct hdr_idx *idx,
876 char **cap, struct cap_hdr *cap_hdr)
877{
878 char *eol, *sol, *col, *sov;
879 int cur_idx;
880 struct cap_hdr *h;
881 int len;
882
883 sol = som + hdr_idx_first_pos(idx);
884 cur_idx = hdr_idx_first_idx(idx);
885
886 while (cur_idx) {
887 eol = sol + idx->v[cur_idx].len;
888
889 col = sol;
890 while (col < eol && *col != ':')
891 col++;
892
893 sov = col + 1;
894 while (sov < eol && http_is_lws[(unsigned char)*sov])
895 sov++;
896
897 for (h = cap_hdr; h; h = h->next) {
898 if ((h->namelen == col - sol) &&
899 (strncasecmp(sol, h->name, h->namelen) == 0)) {
900 if (cap[h->index] == NULL)
901 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200902 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100903
904 if (cap[h->index] == NULL) {
905 Alert("HTTP capture : out of memory.\n");
906 continue;
907 }
908
909 len = eol - sov;
910 if (len > h->len)
911 len = h->len;
912
913 memcpy(cap[h->index], sov, len);
914 cap[h->index][len]=0;
915 }
916 }
917 sol = eol + idx->v[cur_idx].cr + 1;
918 cur_idx = idx->v[cur_idx].next;
919 }
920}
921
922
Willy Tarreau42250582007-04-01 01:30:43 +0200923/* either we find an LF at <ptr> or we jump to <bad>.
924 */
925#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
926
927/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
928 * otherwise to <http_msg_ood> with <state> set to <st>.
929 */
930#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
931 ptr++; \
932 if (likely(ptr < end)) \
933 goto good; \
934 else { \
935 state = (st); \
936 goto http_msg_ood; \
937 } \
938 } while (0)
939
940
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100942 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100943 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
944 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
945 * will give undefined results.
946 * Note that it is upon the caller's responsibility to ensure that ptr < end,
947 * and that msg->sol points to the beginning of the response.
948 * If a complete line is found (which implies that at least one CR or LF is
949 * found before <end>, the updated <ptr> is returned, otherwise NULL is
950 * returned indicating an incomplete line (which does not mean that parts have
951 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
952 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
953 * upon next call.
954 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200955 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
957 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200958 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200960const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100961 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100962 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100963{
Willy Tarreau572bf902012-07-02 17:01:20 +0200964 const char *msg_start = msg->buf->buf.p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100965
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200968 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100969 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
971
972 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100973 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
975 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100976 state = HTTP_MSG_ERROR;
977 break;
978
Willy Tarreau8973c702007-01-21 23:58:29 +0100979 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200980 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100981 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100982 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 goto http_msg_rpcode;
984 }
985 if (likely(HTTP_IS_SPHT(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
987 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100988 state = HTTP_MSG_ERROR;
989 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990
Willy Tarreau8973c702007-01-21 23:58:29 +0100991 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200992 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100993 if (likely(!HTTP_IS_LWS(*ptr)))
994 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
995
996 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100997 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
999 }
1000
1001 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001002 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 http_msg_rsp_reason:
1004 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001005 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001006 msg->sl.st.r_l = 0;
1007 goto http_msg_rpline_eol;
1008
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001010 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001012 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 goto http_msg_rpreason;
1014 }
1015 if (likely(HTTP_IS_SPHT(*ptr)))
1016 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1017 /* so it's a CR/LF, so there is no reason phrase */
1018 goto http_msg_rsp_reason;
1019
Willy Tarreau8973c702007-01-21 23:58:29 +01001020 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001021 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 if (likely(!HTTP_IS_CRLF(*ptr)))
1023 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001024 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001025 http_msg_rpline_eol:
1026 /* We have seen the end of line. Note that we do not
1027 * necessarily have the \n yet, but at least we know that we
1028 * have EITHER \r OR \n, otherwise the response would not be
1029 * complete. We can then record the response length and return
1030 * to the caller which will be able to register it.
1031 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001032 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 return ptr;
1034
1035#ifdef DEBUG_FULL
1036 default:
1037 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1038 exit(1);
1039#endif
1040 }
1041
1042 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001043 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 if (ret_state)
1045 *ret_state = state;
1046 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001047 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001049}
1050
Willy Tarreau8973c702007-01-21 23:58:29 +01001051/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001052 * This function parses a request line between <ptr> and <end>, starting with
1053 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1054 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1055 * will give undefined results.
1056 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1057 * and that msg->sol points to the beginning of the request.
1058 * If a complete line is found (which implies that at least one CR or LF is
1059 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1060 * returned indicating an incomplete line (which does not mean that parts have
1061 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1062 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1063 * upon next call.
1064 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001065 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1067 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001068 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001070const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001071 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001072 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073{
Willy Tarreau572bf902012-07-02 17:01:20 +02001074 const char *msg_start = msg->buf->buf.p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001078 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_TOKEN(*ptr)))
1080 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001083 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1085 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 if (likely(HTTP_IS_CRLF(*ptr))) {
1088 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001089 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001093 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001095 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 msg->sl.rq.v_l = 0;
1097 goto http_msg_rqline_eol;
1098 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001099 state = HTTP_MSG_ERROR;
1100 break;
1101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001103 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001105 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 goto http_msg_rquri;
1107 }
1108 if (likely(HTTP_IS_SPHT(*ptr)))
1109 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1110 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1111 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001115 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001119 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001122
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001124 /* non-ASCII chars are forbidden unless option
1125 * accept-invalid-http-request is enabled in the frontend.
1126 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001128 if (msg->err_pos < -1)
1129 goto invalid_char;
1130 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001131 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001132 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1133 }
1134
1135 if (likely(HTTP_IS_CRLF(*ptr))) {
1136 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1137 goto http_msg_req09_uri_e;
1138 }
1139
1140 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001141 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001142 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001143 state = HTTP_MSG_ERROR;
1144 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001147 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001149 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 goto http_msg_rqver;
1151 }
1152 if (likely(HTTP_IS_SPHT(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1154 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1155 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001159 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001161
1162 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001164 http_msg_rqline_eol:
1165 /* We have seen the end of line. Note that we do not
1166 * necessarily have the \n yet, but at least we know that we
1167 * have EITHER \r OR \n, otherwise the request would not be
1168 * complete. We can then record the request length and return
1169 * to the caller which will be able to register it.
1170 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001171 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172 return ptr;
1173 }
1174
1175 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001176 state = HTTP_MSG_ERROR;
1177 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179#ifdef DEBUG_FULL
1180 default:
1181 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1182 exit(1);
1183#endif
1184 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001187 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 if (ret_state)
1189 *ret_state = state;
1190 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001191 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001192 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001194
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001195/*
1196 * Returns the data from Authorization header. Function may be called more
1197 * than once so data is stored in txn->auth_data. When no header is found
1198 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1199 * searching again for something we are unable to find anyway.
1200 */
1201
1202char get_http_auth_buff[BUFSIZE];
1203
1204int
1205get_http_auth(struct session *s)
1206{
1207
1208 struct http_txn *txn = &s->txn;
1209 struct chunk auth_method;
1210 struct hdr_ctx ctx;
1211 char *h, *p;
1212 int len;
1213
1214#ifdef DEBUG_AUTH
1215 printf("Auth for session %p: %d\n", s, txn->auth.method);
1216#endif
1217
1218 if (txn->auth.method == HTTP_AUTH_WRONG)
1219 return 0;
1220
1221 if (txn->auth.method)
1222 return 1;
1223
1224 txn->auth.method = HTTP_AUTH_WRONG;
1225
1226 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001227
1228 if (txn->flags & TX_USE_PX_CONN) {
1229 h = "Proxy-Authorization";
1230 len = strlen(h);
1231 } else {
1232 h = "Authorization";
1233 len = strlen(h);
1234 }
1235
Willy Tarreau572bf902012-07-02 17:01:20 +02001236 if (!http_find_header2(h, len, s->req->buf.p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001237 return 0;
1238
1239 h = ctx.line + ctx.val;
1240
1241 p = memchr(h, ' ', ctx.vlen);
1242 if (!p || p == h)
1243 return 0;
1244
1245 chunk_initlen(&auth_method, h, 0, p-h);
1246 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1247
1248 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1249
1250 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1251 get_http_auth_buff, BUFSIZE - 1);
1252
1253 if (len < 0)
1254 return 0;
1255
1256
1257 get_http_auth_buff[len] = '\0';
1258
1259 p = strchr(get_http_auth_buff, ':');
1260
1261 if (!p)
1262 return 0;
1263
1264 txn->auth.user = get_http_auth_buff;
1265 *p = '\0';
1266 txn->auth.pass = p+1;
1267
1268 txn->auth.method = HTTP_AUTH_BASIC;
1269 return 1;
1270 }
1271
1272 return 0;
1273}
1274
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8973c702007-01-21 23:58:29 +01001276/*
1277 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001278 * depending on the initial msg->msg_state. The caller is responsible for
1279 * ensuring that the message does not wrap. The function can be preempted
1280 * everywhere when data are missing and recalled at the exact same location
1281 * with no information loss. The message may even be realigned between two
1282 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001284 * fields. Note that msg->sol will be initialized after completing the first
1285 * state, so that none of the msg pointers has to be initialized prior to the
1286 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001288void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001290 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001292 struct channel *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001294 state = msg->msg_state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001295 ptr = buf->buf.p + msg->next;
1296 end = buf->buf.p + buf->buf.i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 if (unlikely(ptr >= end))
1299 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 /*
1303 * First, states that are specific to the response only.
1304 * We check them first so that request and headers are
1305 * closer to each other (accessed more often).
1306 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001308 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 /* we have a start of message, but we have to check
1311 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001312 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001313 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001314 if (unlikely(ptr != buf->buf.p)) {
1315 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001316 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001317 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001318 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 }
Willy Tarreau26927362012-05-18 23:22:52 +02001320 msg->sol = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 hdr_idx_init(idx);
1322 state = HTTP_MSG_RPVER;
1323 goto http_msg_rpver;
1324 }
1325
1326 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1327 goto http_msg_invalid;
1328
1329 if (unlikely(*ptr == '\n'))
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1332 /* stop here */
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 EXPECT_LF_HERE(ptr, http_msg_invalid);
1337 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1338 /* stop here */
1339
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001341 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 case HTTP_MSG_RPVER_SP:
1343 case HTTP_MSG_RPCODE:
1344 case HTTP_MSG_RPCODE_SP:
1345 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001346 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001347 state, ptr, end,
1348 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 if (unlikely(!ptr))
1350 return;
1351
1352 /* we have a full response and we know that we have either a CR
1353 * or an LF at <ptr>.
1354 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1356
Willy Tarreau572bf902012-07-02 17:01:20 +02001357 msg->sol = ptr - buf->buf.p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 if (likely(*ptr == '\r'))
1359 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1360 goto http_msg_rpline_end;
1361
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001363 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 /* msg->sol must point to the first of CR or LF. */
1365 EXPECT_LF_HERE(ptr, http_msg_invalid);
1366 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1367 /* stop here */
1368
1369 /*
1370 * Second, states that are specific to the request only
1371 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001373 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001375 /* we have a start of message, but we have to check
1376 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001377 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001378 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001379 if (likely(ptr != buf->buf.p)) {
1380 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001381 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001382 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001383 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 }
Willy Tarreau26927362012-05-18 23:22:52 +02001385 msg->sol = 0;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001386 /* we will need this when keep-alive will be supported
1387 hdr_idx_init(idx);
1388 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 state = HTTP_MSG_RQMETH;
1390 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1394 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 if (unlikely(*ptr == '\n'))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 EXPECT_LF_HERE(ptr, http_msg_invalid);
1404 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQMETH_SP:
1410 case HTTP_MSG_RQURI:
1411 case HTTP_MSG_RQURI_SP:
1412 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001413 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001414 state, ptr, end,
1415 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 if (unlikely(!ptr))
1417 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 /* we have a full request and we know that we have either a CR
1420 * or an LF at <ptr>.
1421 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau572bf902012-07-02 17:01:20 +02001424 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 if (likely(*ptr == '\r'))
1426 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001430 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 /* check for HTTP/0.9 request : no version information available.
1432 * msg->sol must point to the first of CR or LF.
1433 */
1434 if (unlikely(msg->sl.rq.v_l == 0))
1435 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 EXPECT_LF_HERE(ptr, http_msg_invalid);
1438 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001440
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 /*
1442 * Common states below
1443 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001445 http_msg_hdr_first:
Willy Tarreau572bf902012-07-02 17:01:20 +02001446 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 if (likely(!HTTP_IS_CRLF(*ptr))) {
1448 goto http_msg_hdr_name;
1449 }
1450
1451 if (likely(*ptr == '\r'))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1453 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 /* assumes msg->sol points to the first char */
1458 if (likely(HTTP_IS_TOKEN(*ptr)))
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001461 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001463
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001464 if (likely(msg->err_pos < -1) || *ptr == '\n')
1465 goto http_msg_invalid;
1466
1467 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau572bf902012-07-02 17:01:20 +02001468 msg->err_pos = ptr - buf->buf.p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001469
1470 /* and we still accept this non-token character */
1471 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001474 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001475 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (likely(HTTP_IS_SPHT(*ptr)))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 /* header value can be basically anything except CR/LF */
Willy Tarreau572bf902012-07-02 17:01:20 +02001480 msg->sov = ptr - buf->buf.p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 if (likely(!HTTP_IS_CRLF(*ptr))) {
1483 goto http_msg_hdr_val;
1484 }
1485
1486 if (likely(*ptr == '\r'))
1487 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1488 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 EXPECT_LF_HERE(ptr, http_msg_invalid);
1493 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001496 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (likely(HTTP_IS_SPHT(*ptr))) {
1498 /* replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001499 for (; buf->buf.p + msg->sov < ptr; msg->sov++)
1500 buf->buf.p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_hdr_l1_sp;
1502 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001503 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001504 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 goto http_msg_complete_header;
1506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001508 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001509 /* assumes msg->sol points to the first char, and msg->sov
1510 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 */
1512 if (likely(!HTTP_IS_CRLF(*ptr)))
1513 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreau572bf902012-07-02 17:01:20 +02001515 msg->eol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 /* Note: we could also copy eol into ->eoh so that we have the
1517 * real header end in case it ends with lots of LWS, but is this
1518 * really needed ?
1519 */
1520 if (likely(*ptr == '\r'))
1521 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1522 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 EXPECT_LF_HERE(ptr, http_msg_invalid);
1527 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001530 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1532 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001533 for (; buf->buf.p + msg->eol < ptr; msg->eol++)
1534 buf->buf.p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 goto http_msg_hdr_val;
1536 }
1537 http_msg_complete_header:
1538 /*
1539 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001540 * Assumes msg->sol points to the first char, msg->sov points
1541 * to the first character of the value and msg->eol to the
1542 * first CR or LF so we know how the line ends. We insert last
1543 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001545 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->buf.p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 idx, idx->tail) < 0))
1547 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001548
Willy Tarreau572bf902012-07-02 17:01:20 +02001549 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(!HTTP_IS_CRLF(*ptr))) {
1551 goto http_msg_hdr_name;
1552 }
1553
1554 if (likely(*ptr == '\r'))
1555 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1556 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001559 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 /* Assumes msg->sol points to the first of either CR or LF */
1561 EXPECT_LF_HERE(ptr, http_msg_invalid);
1562 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001563 msg->sov = msg->next = ptr - buf->buf.p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001564 msg->eoh = msg->sol;
1565 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001566 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001568
1569 case HTTP_MSG_ERROR:
1570 /* this may only happen if we call http_msg_analyser() twice with an error */
1571 break;
1572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573#ifdef DEBUG_FULL
1574 default:
1575 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1576 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001577#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 }
1579 http_msg_ood:
1580 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001581 msg->msg_state = state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001582 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 http_msg_invalid:
1586 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001587 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau572bf902012-07-02 17:01:20 +02001588 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 return;
1590}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001591
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001592/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1593 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1594 * nothing is done and 1 is returned.
1595 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597{
1598 int delta;
1599 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001600 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601
1602 if (msg->sl.rq.v_l != 0)
1603 return 1;
1604
Willy Tarreau572bf902012-07-02 17:01:20 +02001605 cur_end = msg->buf->buf.p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 delta = 0;
1607
1608 if (msg->sl.rq.u_l == 0) {
1609 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001610 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001611 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 }
1614 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001615 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001616 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001618 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001619 HTTP_MSG_RQMETH,
Willy Tarreau572bf902012-07-02 17:01:20 +02001620 msg->buf->buf.p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001621 NULL, NULL);
1622 if (unlikely(!cur_end))
1623 return 0;
1624
1625 /* we have a full HTTP/1.0 request now and we know that
1626 * we have either a CR or an LF at <ptr>.
1627 */
1628 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1629 return 1;
1630}
1631
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001632/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633 * and "keep-alive" values. If we already know that some headers may safely
1634 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001635 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1636 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1637 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1638 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1639 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001640 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001642void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001643{
Willy Tarreau5b154472009-12-21 20:11:07 +01001644 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001645 const char *hdr_val = "Connection";
1646 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001647
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001648 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 return;
1650
Willy Tarreau88d349d2010-01-25 12:15:43 +01001651 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1652 hdr_val = "Proxy-Connection";
1653 hdr_len = 16;
1654 }
1655
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001658 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1660 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001661 if (to_del & 2)
1662 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 else
1664 txn->flags |= TX_CON_KAL_SET;
1665 }
1666 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1667 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001668 if (to_del & 1)
1669 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001670 else
1671 txn->flags |= TX_CON_CLO_SET;
1672 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001673 }
1674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 txn->flags |= TX_HDR_CONN_PRS;
1676 return;
1677}
Willy Tarreau5b154472009-12-21 20:11:07 +01001678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679/* Apply desired changes on the Connection: header. Values may be removed and/or
1680 * added depending on the <wanted> flags, which are exclusively composed of
1681 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1682 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001684void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685{
1686 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687 const char *hdr_val = "Connection";
1688 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689
1690 ctx.idx = 0;
1691
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692
1693 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1694 hdr_val = "Proxy-Connection";
1695 hdr_len = 16;
1696 }
1697
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001699 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1701 if (wanted & TX_CON_KAL_SET)
1702 txn->flags |= TX_CON_KAL_SET;
1703 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001704 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1707 if (wanted & TX_CON_CLO_SET)
1708 txn->flags |= TX_CON_CLO_SET;
1709 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001711 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001712 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713
1714 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1715 return;
1716
1717 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1718 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001719 hdr_val = "Connection: close";
1720 hdr_len = 17;
1721 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1722 hdr_val = "Proxy-Connection: close";
1723 hdr_len = 23;
1724 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 }
1727
1728 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1729 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001730 hdr_val = "Connection: keep-alive";
1731 hdr_len = 22;
1732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection: keep-alive";
1734 hdr_len = 28;
1735 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001736 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001737 }
1738 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739}
1740
Willy Tarreaua458b672012-03-05 11:17:50 +01001741/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001743 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001747int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001749 const struct channel *buf = msg->buf;
Willy Tarreau572bf902012-07-02 17:01:20 +02001750 const char *ptr = b_ptr(&buf->buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001751 const char *ptr_old = ptr;
Willy Tarreau572bf902012-07-02 17:01:20 +02001752 const char *end = buf->buf.data + buf->buf.size;
1753 const char *stop = bi_end(&buf->buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001754 unsigned int chunk = 0;
1755
1756 /* The chunk size is in the following form, though we are only
1757 * interested in the size and CRLF :
1758 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1759 */
1760 while (1) {
1761 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001762 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 if (c < 0) /* not a hex digit anymore */
1766 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001768 ptr = buf->buf.data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001769 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001771 chunk = (chunk << 4) + c;
1772 }
1773
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001775 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001776 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777
1778 while (http_is_spht[(unsigned char)*ptr]) {
1779 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001780 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001781 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 }
1784
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 /* Up to there, we know that at least one byte is present at *ptr. Check
1786 * for the end of chunk size.
1787 */
1788 while (1) {
1789 if (likely(HTTP_IS_CRLF(*ptr))) {
1790 /* we now have a CR or an LF at ptr */
1791 if (likely(*ptr == '\r')) {
1792 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001793 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001794 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 return 0;
1796 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001797
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001799 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001801 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 /* done */
1803 break;
1804 }
1805 else if (*ptr == ';') {
1806 /* chunk extension, ends at next CRLF */
1807 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001808 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001810 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811
1812 while (!HTTP_IS_CRLF(*ptr)) {
1813 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001814 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001815 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816 return 0;
1817 }
1818 /* we have a CRLF now, loop above */
1819 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001822 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 }
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001826 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001829 msg->sov += ptr - ptr_old;
Willy Tarreau572bf902012-07-02 17:01:20 +02001830 msg->next = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001831 msg->chunk_len = chunk;
1832 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001834 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001835 error:
Willy Tarreau572bf902012-07-02 17:01:20 +02001836 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838}
1839
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001840/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 * the trailers is found, it is automatically scheduled to be forwarded,
1843 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1844 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001845 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001847 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001848 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1849 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001850 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001851 * matches the length of trailers already parsed and not forwarded. It is also
1852 * important to note that this function is designed to be able to parse wrapped
1853 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001855int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001857 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001858
Willy Tarreaua458b672012-03-05 11:17:50 +01001859 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001861 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau572bf902012-07-02 17:01:20 +02001862 const char *ptr = b_ptr(&buf->buf, msg->next);
1863 const char *stop = bi_end(&buf->buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001864 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865
1866 /* scan current line and stop at LF or CRLF */
1867 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001868 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 return 0;
1870
1871 if (*ptr == '\n') {
1872 if (!p1)
1873 p1 = ptr;
1874 p2 = ptr;
1875 break;
1876 }
1877
1878 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001879 if (p1) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001880 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001882 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 p1 = ptr;
1884 }
1885
1886 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001887 if (ptr >= buf->buf.data + buf->buf.size)
1888 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001889 }
1890
1891 /* after LF; point to beginning of next line */
1892 p2++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001893 if (p2 >= buf->buf.data + buf->buf.size)
1894 p2 = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
Willy Tarreau572bf902012-07-02 17:01:20 +02001896 bytes = p2 - b_ptr(&buf->buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001897 if (bytes < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02001898 bytes += buf->buf.size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001899
1900 /* schedule this line for forwarding */
1901 msg->sov += bytes;
Willy Tarreau572bf902012-07-02 17:01:20 +02001902 if (msg->sov >= buf->buf.size)
1903 msg->sov -= buf->buf.size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001904
Willy Tarreau572bf902012-07-02 17:01:20 +02001905 if (p1 == b_ptr(&buf->buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001906 /* LF/CRLF at beginning of line => end of trailers at p2.
1907 * Everything was scheduled for forwarding, there's nothing
1908 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001909 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001910 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 msg->msg_state = HTTP_MSG_DONE;
1912 return 1;
1913 }
1914 /* OK, next line then */
Willy Tarreau572bf902012-07-02 17:01:20 +02001915 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916 }
1917}
1918
1919/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1920 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001921 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001922 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1924 * not enough data are available, the function does not change anything and
1925 * returns zero. If a parse error is encountered, the function returns < 0 and
1926 * does not change anything. Note: this function is designed to parse wrapped
1927 * CRLF at the end of the buffer.
1928 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001929int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001931 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 int bytes;
1934
1935 /* NB: we'll check data availabilty at the end. It's not a
1936 * problem because whatever we match first will be checked
1937 * against the correct length.
1938 */
1939 bytes = 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02001940 ptr = buf->buf.p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 if (*ptr == '\r') {
1942 bytes++;
1943 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001944 if (ptr >= buf->buf.data + buf->buf.size)
1945 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 }
1947
Willy Tarreau572bf902012-07-02 17:01:20 +02001948 if (bytes > buf->buf.i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 return 0;
1950
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001951 if (*ptr != '\n') {
Willy Tarreau572bf902012-07-02 17:01:20 +02001952 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001954 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955
1956 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001957 if (ptr >= buf->buf.data + buf->buf.size)
1958 ptr = buf->buf.data;
Willy Tarreau26927362012-05-18 23:22:52 +02001959 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1960 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001961 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1963 return 1;
1964}
Willy Tarreau5b154472009-12-21 20:11:07 +01001965
Willy Tarreaud787e662009-07-07 10:14:51 +02001966/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1967 * processing can continue on next analysers, or zero if it either needs more
1968 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1969 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1970 * when it has nothing left to do, and may remove any analyser when it wants to
1971 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001973int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974{
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 /*
1976 * We will parse the partial (or complete) lines.
1977 * We will check the request syntax, and also join multi-line
1978 * headers. An index of all the lines will be elaborated while
1979 * parsing.
1980 *
1981 * For the parsing, we use a 28 states FSM.
1982 *
1983 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02001984 * req->buf.p = beginning of request
1985 * req->buf.p + msg->eoh = end of processed headers / start of current one
1986 * req->buf.p + req->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001987 * msg->eol = end of current header or line (LF or CRLF)
1988 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001989 *
1990 * At end of parsing, we may perform a capture of the error (if any), and
1991 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1992 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1993 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001994 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001995
Willy Tarreau59234e92008-11-30 23:51:27 +01001996 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001997 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001998 struct http_txn *txn = &s->txn;
1999 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002000 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002001
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002002 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 +01002003 now_ms, __FUNCTION__,
2004 s,
2005 req,
2006 req->rex, req->wex,
2007 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002008 req->buf.i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002009 req->analysers);
2010
Willy Tarreau52a0c602009-08-16 22:45:38 +02002011 /* we're speaking HTTP here, so let's speak HTTP to the client */
2012 s->srv_error = http_return_srv_error;
2013
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014 /* There's a protected area at the end of the buffer for rewriting
2015 * purposes. We don't want to start to parse the request if the
2016 * protected area is affected, because we may have to move processed
2017 * data later, which is much more complicated.
2018 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002019 if (buffer_not_empty(&req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002020 if ((txn->flags & TX_NOT_FIRST) &&
2021 unlikely((req->flags & BF_FULL) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002022 bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2023 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)) {
2024 if (req->buf.o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002025 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2026 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002027 /* some data has still not left the buffer, wake us once that's done */
2028 buffer_dont_connect(req);
2029 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2030 return 0;
2031 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002032 if (bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2033 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)
2034 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002035 }
2036
Willy Tarreau065e8332010-01-08 00:30:20 +01002037 /* Note that we have the same problem with the response ; we
2038 * may want to send a redirect, error or anything which requires
2039 * some spare space. So we'll ensure that we have at least
2040 * maxrewrite bytes available in the response buffer before
2041 * processing that one. This will only affect pipelined
2042 * keep-alive requests.
2043 */
2044 if ((txn->flags & TX_NOT_FIRST) &&
2045 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002046 bi_end(&s->rep->buf) < b_ptr(&s->rep->buf, txn->rsp.next) ||
2047 bi_end(&s->rep->buf) > s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)) {
2048 if (s->rep->buf.o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002049 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2050 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002051 /* don't let a connection request be initiated */
2052 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002053 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002054 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002055 return 0;
2056 }
2057 }
2058
Willy Tarreau572bf902012-07-02 17:01:20 +02002059 if (likely(msg->next < req->buf.i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002060 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002061 }
2062
Willy Tarreau59234e92008-11-30 23:51:27 +01002063 /* 1: we might have to print this header in debug mode */
2064 if (unlikely((global.mode & MODE_DEBUG) &&
2065 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002066 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002067 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002068
Willy Tarreau572bf902012-07-02 17:01:20 +02002069 sol = req->buf.p;
Willy Tarreau59234e92008-11-30 23:51:27 +01002070 eol = sol + msg->sl.rq.l;
2071 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002072
Willy Tarreau59234e92008-11-30 23:51:27 +01002073 sol += hdr_idx_first_pos(&txn->hdr_idx);
2074 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002075
Willy Tarreau59234e92008-11-30 23:51:27 +01002076 while (cur_idx) {
2077 eol = sol + txn->hdr_idx.v[cur_idx].len;
2078 debug_hdr("clihdr", s, sol, eol);
2079 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2080 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002082 }
2083
Willy Tarreau58f10d72006-12-04 02:26:12 +01002084
Willy Tarreau59234e92008-11-30 23:51:27 +01002085 /*
2086 * Now we quickly check if we have found a full valid request.
2087 * If not so, we check the FD and buffer states before leaving.
2088 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002089 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002090 * requests are checked first. When waiting for a second request
2091 * on a keep-alive session, if we encounter and error, close, t/o,
2092 * we note the error in the session flags but don't set any state.
2093 * Since the error will be noted there, it will not be counted by
2094 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002095 * Last, we may increase some tracked counters' http request errors on
2096 * the cases that are deliberately the client's fault. For instance,
2097 * a timeout or connection reset is not counted as an error. However
2098 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002100
Willy Tarreau655dce92009-11-08 13:10:58 +01002101 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002102 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002104 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002105 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002106 session_inc_http_req_ctr(s);
2107 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002108 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002109 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau59234e92008-11-30 23:51:27 +01002112 /* 1: Since we are in header mode, if there's no space
2113 * left for headers, we won't be able to free more
2114 * later, so the session will never terminate. We
2115 * must terminate it now.
2116 */
2117 if (unlikely(req->flags & BF_FULL)) {
2118 /* FIXME: check if URI is set and return Status
2119 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002120 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002121 session_inc_http_req_ctr(s);
2122 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002123 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002124 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02002125 msg->err_pos = req->buf.i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 goto return_bad_req;
2127 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002128
Willy Tarreau59234e92008-11-30 23:51:27 +01002129 /* 2: have we encountered a read error ? */
2130 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002131 if (!(s->flags & SN_ERR_MASK))
2132 s->flags |= SN_ERR_CLICL;
2133
Willy Tarreaufcffa692010-01-10 14:21:19 +01002134 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002135 goto failed_keep_alive;
2136
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002138 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002139 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002140 session_inc_http_err_ctr(s);
2141 }
2142
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 msg->msg_state = HTTP_MSG_ERROR;
2144 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002145
Willy Tarreauda7ff642010-06-23 11:44:09 +02002146 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002147 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002148 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002149 if (s->listener->counters)
2150 s->listener->counters->failed_req++;
2151
Willy Tarreau59234e92008-11-30 23:51:27 +01002152 if (!(s->flags & SN_FINST_MASK))
2153 s->flags |= SN_FINST_R;
2154 return 0;
2155 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002156
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 /* 3: has the read timeout expired ? */
2158 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002159 if (!(s->flags & SN_ERR_MASK))
2160 s->flags |= SN_ERR_CLITO;
2161
Willy Tarreaufcffa692010-01-10 14:21:19 +01002162 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002163 goto failed_keep_alive;
2164
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002166 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002167 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002168 session_inc_http_err_ctr(s);
2169 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 txn->status = 408;
2171 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2172 msg->msg_state = HTTP_MSG_ERROR;
2173 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002174
Willy Tarreauda7ff642010-06-23 11:44:09 +02002175 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002176 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002177 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178 if (s->listener->counters)
2179 s->listener->counters->failed_req++;
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 if (!(s->flags & SN_FINST_MASK))
2182 s->flags |= SN_FINST_R;
2183 return 0;
2184 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* 4: have we encountered a close ? */
2187 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002188 if (!(s->flags & SN_ERR_MASK))
2189 s->flags |= SN_ERR_CLICL;
2190
Willy Tarreaufcffa692010-01-10 14:21:19 +01002191 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002192 goto failed_keep_alive;
2193
Willy Tarreau4076a152009-04-02 15:18:36 +02002194 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002195 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 txn->status = 400;
2197 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2198 msg->msg_state = HTTP_MSG_ERROR;
2199 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002200
Willy Tarreauda7ff642010-06-23 11:44:09 +02002201 session_inc_http_err_ctr(s);
2202 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002203 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002204 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002205 if (s->listener->counters)
2206 s->listener->counters->failed_req++;
2207
Willy Tarreau59234e92008-11-30 23:51:27 +01002208 if (!(s->flags & SN_FINST_MASK))
2209 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002210 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002211 }
2212
Willy Tarreau520d95e2009-09-19 21:04:57 +02002213 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002214 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002215 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002216#ifdef TCP_QUICKACK
Willy Tarreau572bf902012-07-02 17:01:20 +02002217 if (s->listener->options & LI_O_NOQUICKACK && req->buf.i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002218 /* We need more data, we have to re-enable quick-ack in case we
2219 * previously disabled it, otherwise we might cause the client
2220 * to delay next data.
2221 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002222 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002223 }
2224#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002225
Willy Tarreaufcffa692010-01-10 14:21:19 +01002226 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2227 /* If the client starts to talk, let's fall back to
2228 * request timeout processing.
2229 */
2230 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002231 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002232 }
2233
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002235 if (!tick_isset(req->analyse_exp)) {
2236 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2237 (txn->flags & TX_WAIT_NEXT_RQ) &&
2238 tick_isset(s->be->timeout.httpka))
2239 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2240 else
2241 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002243
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 /* we're not ready yet */
2245 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002246
2247 failed_keep_alive:
2248 /* Here we process low-level errors for keep-alive requests. In
2249 * short, if the request is not the first one and it experiences
2250 * a timeout, read error or shutdown, we just silently close so
2251 * that the client can try again.
2252 */
2253 txn->status = 0;
2254 msg->msg_state = HTTP_MSG_RQBEFORE;
2255 req->analysers = 0;
2256 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002257 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002258 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002259 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002260 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002261
Willy Tarreaud787e662009-07-07 10:14:51 +02002262 /* OK now we have a complete HTTP request with indexed headers. Let's
2263 * complete the request parsing by setting a few fields we will need
Willy Tarreau572bf902012-07-02 17:01:20 +02002264 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002265 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002266 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002267 * byte after the last LF. msg->sov points to the first byte of data.
2268 * msg->eol cannot be trusted because it may have been left uninitialized
2269 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002270 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002271
Willy Tarreauda7ff642010-06-23 11:44:09 +02002272 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002273 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2274
Willy Tarreaub16a5742010-01-10 14:46:16 +01002275 if (txn->flags & TX_WAIT_NEXT_RQ) {
2276 /* kill the pending keep-alive timeout */
2277 txn->flags &= ~TX_WAIT_NEXT_RQ;
2278 req->analyse_exp = TICK_ETERNITY;
2279 }
2280
2281
Willy Tarreaud787e662009-07-07 10:14:51 +02002282 /* Maybe we found in invalid header name while we were configured not
2283 * to block on that, so we have to capture it now.
2284 */
2285 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002286 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002287
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 /*
2289 * 1: identify the method
2290 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002291 txn->meth = find_http_meth(req->buf.p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002292
2293 /* we can make use of server redirect on GET and HEAD */
2294 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2295 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002296
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 /*
2298 * 2: check if the URI matches the monitor_uri.
2299 * We have to do this for every request which gets in, because
2300 * the monitor-uri is defined by the frontend.
2301 */
2302 if (unlikely((s->fe->monitor_uri_len != 0) &&
2303 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002304 !memcmp(req->buf.p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 s->fe->monitor_uri,
2306 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002309 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002311
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002313 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002316 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002317 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 ret = acl_pass(ret);
2320 if (cond->pol == ACL_COND_UNLESS)
2321 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 if (ret) {
2324 /* we fail this request, let's return 503 service unavail */
2325 txn->status = 503;
2326 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2327 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002328 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002329 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* nothing to fail, let's reply normaly */
2332 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002333 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 goto return_prx_cond;
2335 }
2336
2337 /*
2338 * 3: Maybe we have to copy the original REQURI for the logs ?
2339 * Note: we cannot log anymore if the request has been
2340 * classified as invalid.
2341 */
2342 if (unlikely(s->logs.logwait & LW_REQ)) {
2343 /* we have a complete HTTP request that we must log */
2344 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2345 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 if (urilen >= REQURI_LEN)
2348 urilen = REQURI_LEN - 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02002349 memcpy(txn->uri, req->buf.p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 if (!(s->logs.logwait &= ~LW_REQ))
2353 s->do_log(s);
2354 } else {
2355 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002356 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 }
Willy Tarreau06619262006-12-17 08:37:22 +01002358
William Lallemanda73203e2012-03-12 12:48:57 +01002359 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2360 s->unique_id = pool_alloc2(pool2_uniqueid);
2361 }
2362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002364 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002365 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002366
Willy Tarreau5b154472009-12-21 20:11:07 +01002367 /* ... and check if the request is HTTP/1.1 or above */
2368 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002369 ((req->buf.p[msg->sl.rq.v + 5] > '1') ||
2370 ((req->buf.p[msg->sl.rq.v + 5] == '1') &&
2371 (req->buf.p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002372 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002373
2374 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002375 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002376
Willy Tarreau88d349d2010-01-25 12:15:43 +01002377 /* if the frontend has "option http-use-proxy-header", we'll check if
2378 * we have what looks like a proxied connection instead of a connection,
2379 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2380 * Note that this is *not* RFC-compliant, however browsers and proxies
2381 * happen to do that despite being non-standard :-(
2382 * We consider that a request not beginning with either '/' or '*' is
2383 * a proxied connection, which covers both "scheme://location" and
2384 * CONNECT ip:port.
2385 */
2386 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002387 req->buf.p[msg->sl.rq.u] != '/' && req->buf.p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002388 txn->flags |= TX_USE_PX_CONN;
2389
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002390 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002391 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002394 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02002395 capture_headers(req->buf.p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002397
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002398 /* 6: determine the transfer-length.
2399 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2400 * the presence of a message-body in a REQUEST and its transfer length
2401 * must be determined that way (in order of precedence) :
2402 * 1. The presence of a message-body in a request is signaled by the
2403 * inclusion of a Content-Length or Transfer-Encoding header field
2404 * in the request's header fields. When a request message contains
2405 * both a message-body of non-zero length and a method that does
2406 * not define any semantics for that request message-body, then an
2407 * origin server SHOULD either ignore the message-body or respond
2408 * with an appropriate error message (e.g., 413). A proxy or
2409 * gateway, when presented the same request, SHOULD either forward
2410 * the request inbound with the message- body or ignore the
2411 * message-body when determining a response.
2412 *
2413 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2414 * and the "chunked" transfer-coding (Section 6.2) is used, the
2415 * transfer-length is defined by the use of this transfer-coding.
2416 * If a Transfer-Encoding header field is present and the "chunked"
2417 * transfer-coding is not present, the transfer-length is defined
2418 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002419 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002420 * 3. If a Content-Length header field is present, its decimal value in
2421 * OCTETs represents both the entity-length and the transfer-length.
2422 * If a message is received with both a Transfer-Encoding header
2423 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002424 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002425 * 4. By the server closing the connection. (Closing the connection
2426 * cannot be used to indicate the end of a request body, since that
2427 * would leave no possibility for the server to send back a response.)
2428 *
2429 * Whenever a transfer-coding is applied to a message-body, the set of
2430 * transfer-codings MUST include "chunked", unless the message indicates
2431 * it is terminated by closing the connection. When the "chunked"
2432 * transfer-coding is used, it MUST be the last transfer-coding applied
2433 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002434 */
2435
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002437 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002438 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002439 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002440 http_find_header2("Transfer-Encoding", 17, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002441 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002442 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2443 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002444 /* bad transfer-encoding (chunked followed by something else) */
2445 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002446 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002447 break;
2448 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 }
2450
Willy Tarreau32b47f42009-10-18 20:55:02 +02002451 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002452 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002453 http_find_header2("Content-Length", 14, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002454 signed long long cl;
2455
Willy Tarreauad14f752011-09-02 20:33:27 +02002456 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002457 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002459 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002460
Willy Tarreauad14f752011-09-02 20:33:27 +02002461 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002462 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002463 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002464 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002465
Willy Tarreauad14f752011-09-02 20:33:27 +02002466 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002467 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002468 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002469 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002471 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002472 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002473 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002474 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002475
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002476 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002477 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 }
2479
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 /* bodyless requests have a known length */
2481 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002482 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002483
Willy Tarreaud787e662009-07-07 10:14:51 +02002484 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002485 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002486 req->analyse_exp = TICK_ETERNITY;
2487 return 1;
2488
2489 return_bad_req:
2490 /* We centralize bad requests processing here */
2491 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2492 /* we detected a parsing error. We want to archive this request
2493 * in the dedicated proxy area for later troubleshooting.
2494 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002495 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002496 }
2497
2498 txn->req.msg_state = HTTP_MSG_ERROR;
2499 txn->status = 400;
2500 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002502 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002503 if (s->listener->counters)
2504 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002505
2506 return_prx_cond:
2507 if (!(s->flags & SN_ERR_MASK))
2508 s->flags |= SN_ERR_PRXCOND;
2509 if (!(s->flags & SN_FINST_MASK))
2510 s->flags |= SN_FINST_R;
2511
2512 req->analysers = 0;
2513 req->analyse_exp = TICK_ETERNITY;
2514 return 0;
2515}
2516
Cyril Bonté70be45d2010-10-12 00:14:35 +02002517/* We reached the stats page through a POST request.
2518 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002519 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002520 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002521int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002522{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002523 struct proxy *px = NULL;
2524 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002525
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002526 char key[LINESIZE];
2527 int action = ST_ADM_ACTION_NONE;
2528 int reprocess = 0;
2529
2530 int total_servers = 0;
2531 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002532
2533 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002534 char *st_cur_param = NULL;
2535 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002536
Willy Tarreau572bf902012-07-02 17:01:20 +02002537 first_param = req->buf.p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002538 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002539
2540 cur_param = next_param = end_params;
2541
Willy Tarreau572bf902012-07-02 17:01:20 +02002542 if (end_params >= req->buf.data + req->buf.size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002544 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002545 return 1;
2546 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002547 else if (end_params > req->buf.p + req->buf.i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002548 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002549 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002550 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002551 }
2552
2553 *end_params = '\0';
2554
Willy Tarreau295a8372011-03-10 11:25:07 +01002555 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002556
2557 /*
2558 * Parse the parameters in reverse order to only store the last value.
2559 * From the html form, the backend and the action are at the end.
2560 */
2561 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002562 char *value;
2563 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002564
2565 cur_param--;
2566 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002567 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002569 poffset = (cur_param != first_param ? 1 : 0);
2570 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2571 if ((plen > 0) && (plen <= sizeof(key))) {
2572 strncpy(key, cur_param + poffset, plen);
2573 key[plen - 1] = '\0';
2574 } else {
2575 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2576 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002577 }
2578
2579 /* Parse the value */
2580 value = key;
2581 while (*value != '\0' && *value != '=') {
2582 value++;
2583 }
2584 if (*value == '=') {
2585 /* Ok, a value is found, we can mark the end of the key */
2586 *value++ = '\0';
2587 }
2588
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002589 if (!url_decode(key) || !url_decode(value))
2590 break;
2591
Cyril Bonté70be45d2010-10-12 00:14:35 +02002592 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002593 if (!px && (strcmp(key, "b") == 0)) {
2594 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2595 /* the backend name is unknown or ambiguous (duplicate names) */
2596 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2597 goto out;
2598 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002600 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002601 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002602 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002603 }
2604 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002605 action = ST_ADM_ACTION_ENABLE;
2606 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002607 else if (strcmp(value, "stop") == 0) {
2608 action = ST_ADM_ACTION_STOP;
2609 }
2610 else if (strcmp(value, "start") == 0) {
2611 action = ST_ADM_ACTION_START;
2612 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002613 else if (strcmp(value, "shutdown") == 0) {
2614 action = ST_ADM_ACTION_SHUTDOWN;
2615 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002616 else {
2617 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2618 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002619 }
2620 }
2621 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002622 if (!(px && action)) {
2623 /*
2624 * Indicates that we'll need to reprocess the parameters
2625 * as soon as backend and action are known
2626 */
2627 if (!reprocess) {
2628 st_cur_param = cur_param;
2629 st_next_param = next_param;
2630 }
2631 reprocess = 1;
2632 }
2633 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002635 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002636 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002637 /* Not already in maintenance, we can change the server state */
2638 sv->state |= SRV_MAINTAIN;
2639 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002640 altered_servers++;
2641 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002642 }
2643 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002644 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002645 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646 /* Already in maintenance, we can change the server state */
2647 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002648 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002649 altered_servers++;
2650 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002651 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002652 break;
2653 case ST_ADM_ACTION_STOP:
2654 case ST_ADM_ACTION_START:
2655 if (action == ST_ADM_ACTION_START)
2656 sv->uweight = sv->iweight;
2657 else
2658 sv->uweight = 0;
2659
2660 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2661 /* we must take care of not pushing the server to full throttle during slow starts */
2662 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2663 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2664 else
2665 sv->eweight = BE_WEIGHT_SCALE;
2666 sv->eweight *= sv->uweight;
2667 } else {
2668 sv->eweight = sv->uweight;
2669 }
2670
2671 /* static LB algorithms are a bit harder to update */
2672 if (px->lbprm.update_server_eweight)
2673 px->lbprm.update_server_eweight(sv);
2674 else if (sv->eweight) {
2675 if (px->lbprm.set_server_status_up)
2676 px->lbprm.set_server_status_up(sv);
2677 }
2678 else {
2679 if (px->lbprm.set_server_status_down)
2680 px->lbprm.set_server_status_down(sv);
2681 }
2682 altered_servers++;
2683 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002684 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002685 case ST_ADM_ACTION_SHUTDOWN:
2686 if (px->state != PR_STSTOPPED) {
2687 struct session *sess, *sess_bck;
2688
2689 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2690 if (sess->srv_conn == sv)
2691 session_shutdown(sess, SN_ERR_KILLED);
2692
2693 altered_servers++;
2694 total_servers++;
2695 }
2696 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002697 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002698 } else {
2699 /* the server name is unknown or ambiguous (duplicate names) */
2700 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002701 }
2702 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002703 if (reprocess && px && action) {
2704 /* Now, we know the backend and the action chosen by the user.
2705 * We can safely restart from the first server parameter
2706 * to reprocess them
2707 */
2708 cur_param = st_cur_param;
2709 next_param = st_next_param;
2710 reprocess = 0;
2711 goto reprocess_servers;
2712 }
2713
Cyril Bonté70be45d2010-10-12 00:14:35 +02002714 next_param = cur_param;
2715 }
2716 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002717
2718 if (total_servers == 0) {
2719 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2720 }
2721 else if (altered_servers == 0) {
2722 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2723 }
2724 else if (altered_servers == total_servers) {
2725 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2726 }
2727 else {
2728 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2729 }
2730 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002731 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732}
2733
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734/* returns a pointer to the first rule which forbids access (deny or http_auth),
2735 * or NULL if everything's OK.
2736 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002737static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2739{
Willy Tarreauff011f22011-01-06 17:51:27 +01002740 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002741
Willy Tarreauff011f22011-01-06 17:51:27 +01002742 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002743 int ret = 1;
2744
Willy Tarreauff011f22011-01-06 17:51:27 +01002745 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002746 continue;
2747
2748 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002749 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002750 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002751 ret = acl_pass(ret);
2752
Willy Tarreauff011f22011-01-06 17:51:27 +01002753 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002754 ret = !ret;
2755 }
2756
2757 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002758 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002759 return NULL; /* no problem */
2760 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002761 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002762 }
2763 }
2764 return NULL;
2765}
2766
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767/* This stream analyser runs all HTTP request processing which is common to
2768 * frontends and backends, which means blocking ACLs, filters, connection-close,
2769 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002770 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771 * either needs more data or wants to immediately abort the request (eg: deny,
2772 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002773 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002774int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002775{
Willy Tarreaud787e662009-07-07 10:14:51 +02002776 struct http_txn *txn = &s->txn;
2777 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002778 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002779 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002781 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002782 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002783
Willy Tarreau655dce92009-11-08 13:10:58 +01002784 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002785 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002786 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002787 return 0;
2788 }
2789
Willy Tarreau3a816292009-07-07 10:55:49 +02002790 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002791 req->analyse_exp = TICK_ETERNITY;
2792
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002793 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 +02002794 now_ms, __FUNCTION__,
2795 s,
2796 req,
2797 req->rex, req->wex,
2798 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002799 req->buf.i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002800 req->analysers);
2801
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002802 /* first check whether we have some ACLs set to block this request */
2803 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002804 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002805
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 ret = acl_pass(ret);
2807 if (cond->pol == ACL_COND_UNLESS)
2808 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002809
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002810 if (ret) {
2811 txn->status = 403;
2812 /* let's log the request time */
2813 s->logs.tv_request = now;
2814 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002815 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002816 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002818 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002819
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002820 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002821 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002822
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002823 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002824 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002825 do_stats = stats_check_uri(s->rep->prod, txn, px);
2826 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002827 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 +01002828 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002829 else
2830 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002831
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002832 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002833 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002834 txn->status = 403;
2835 s->logs.tv_request = now;
2836 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002837 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002838 s->fe->fe_counters.denied_req++;
2839 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2840 s->be->be_counters.denied_req++;
2841 if (s->listener->counters)
2842 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002843 goto return_prx_cond;
2844 }
2845
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 /* try headers filters */
2847 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002848 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002849 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /* has the request been denied ? */
2852 if (txn->flags & TX_CLDENY) {
2853 /* no need to go further */
2854 txn->status = 403;
2855 /* let's log the request time */
2856 s->logs.tv_request = now;
2857 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002858 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 goto return_prx_cond;
2860 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002861
2862 /* When a connection is tarpitted, we use the tarpit timeout,
2863 * which may be the same as the connect timeout if unspecified.
2864 * If unset, then set it to zero because we really want it to
2865 * eventually expire. We build the tarpit as an analyser.
2866 */
2867 if (txn->flags & TX_CLTARPIT) {
2868 buffer_erase(s->req);
2869 /* wipe the request out so that we can drop the connection early
2870 * if the client closes first.
2871 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002872 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002873 req->analysers = 0; /* remove switching rules etc... */
2874 req->analysers |= AN_REQ_HTTP_TARPIT;
2875 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2876 if (!req->analyse_exp)
2877 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002878 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002879 return 1;
2880 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 }
Willy Tarreau06619262006-12-17 08:37:22 +01002882
Willy Tarreau5b154472009-12-21 20:11:07 +01002883 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2884 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002885 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2886 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002887 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2888 * avoid to redo the same work if FE and BE have the same settings (common).
2889 * The method consists in checking if options changed between the two calls
2890 * (implying that either one is non-null, or one of them is non-null and we
2891 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002892 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002893
Willy Tarreaudc008c52010-02-01 16:20:08 +01002894 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2895 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2896 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2897 (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 +01002898 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002899
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002900 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2901 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002902 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002903 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2904 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002905 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002906 tmp = TX_CON_WANT_CLO;
2907
Willy Tarreau5b154472009-12-21 20:11:07 +01002908 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2909 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002910
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002911 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2912 /* parse the Connection header and possibly clean it */
2913 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002914 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002915 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2916 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002917 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002918 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002919 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002920 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002921 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002922
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002923 /* check if client or config asks for explicit close in KAL/SCL */
2924 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2925 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2926 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002927 (!(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 +01002928 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002929 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002930 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002931 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2932 }
Willy Tarreau78599912009-10-17 20:12:21 +02002933
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002934 /* we can be blocked here because the request needs to be authenticated,
2935 * either to pass or to access stats.
2936 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002937 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002938 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002939 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002940
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002941 if (!realm)
2942 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2943
Willy Tarreau844a7e72010-01-31 21:46:18 +01002944 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002945 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002946 txn->status = 401;
2947 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002948 /* on 401 we still count one error, because normal browsing
2949 * won't significantly increase the counter but brute force
2950 * attempts will.
2951 */
2952 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002953 goto return_prx_cond;
2954 }
2955
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002956 /* add request headers from the rule sets in the same order */
2957 list_for_each_entry(wl, &px->req_add, list) {
2958 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002959 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002960 ret = acl_pass(ret);
2961 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2962 ret = !ret;
2963 if (!ret)
2964 continue;
2965 }
2966
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002967 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002968 goto return_bad_req;
2969 }
2970
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002971 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002972 struct stats_admin_rule *stats_admin_rule;
2973
2974 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002975 * FIXME!!! that one is rather dangerous, we want to
2976 * make it follow standard rules (eg: clear req->analysers).
2977 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002978
Cyril Bonté474be412010-10-12 00:14:36 +02002979 /* now check whether we have some admin rules for this request */
2980 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2981 int ret = 1;
2982
2983 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002984 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 +02002985 ret = acl_pass(ret);
2986 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2987 ret = !ret;
2988 }
2989
2990 if (ret) {
2991 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002992 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002993 break;
2994 }
2995 }
2996
Cyril Bonté70be45d2010-10-12 00:14:35 +02002997 /* Was the status page requested with a POST ? */
2998 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002999 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003000 if (msg->msg_state < HTTP_MSG_100_SENT) {
3001 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3002 * send an HTTP/1.1 100 Continue intermediate response.
3003 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003004 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003005 struct hdr_ctx ctx;
3006 ctx.idx = 0;
3007 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003008 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003009 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003010 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003011 }
3012 }
3013 msg->msg_state = HTTP_MSG_100_SENT;
3014 s->logs.tv_request = now; /* update the request timer to reflect full request */
3015 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003016 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003017 /* we need more data */
3018 req->analysers |= an_bit;
3019 buffer_dont_connect(req);
3020 return 0;
3021 }
Cyril Bonté474be412010-10-12 00:14:36 +02003022 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003023 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003024 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003025 }
3026
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003027 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003028 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003029 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003030 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +02003031 s->rep->prod->conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003032 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003033 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003034 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3035 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003036
3037 return 0;
3038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003040
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 /* check whether we have some ACLs set to redirect this request */
3042 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003043 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044
Willy Tarreauf285f542010-01-03 20:03:03 +01003045 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003046 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003047 ret = acl_pass(ret);
3048 if (rule->cond->pol == ACL_COND_UNLESS)
3049 ret = !ret;
3050 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003051
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003052 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003053 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003055
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003056 /* build redirect message */
3057 switch(rule->code) {
3058 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 msg_fmt = HTTP_303;
3060 break;
3061 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 msg_fmt = HTTP_301;
3063 break;
3064 case 302:
3065 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 msg_fmt = HTTP_302;
3067 break;
3068 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003069
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003070 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003071 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 switch(rule->type) {
3074 case REDIRECT_TYPE_PREFIX: {
3075 const char *path;
3076 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003077
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 path = http_get_path(txn);
3079 /* build message using path */
3080 if (path) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003081 pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003082 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3083 int qs = 0;
3084 while (qs < pathlen) {
3085 if (path[qs] == '?') {
3086 pathlen = qs;
3087 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003088 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092 } else {
3093 path = "/";
3094 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003095 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003096
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003097 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 goto return_bad_req;
3099
3100 /* add prefix. Note that if prefix == "/", we don't want to
3101 * add anything, otherwise it makes it hard for the user to
3102 * configure a self-redirection.
3103 */
3104 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003105 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3106 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003107 }
3108
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003109 /* add path */
3110 memcpy(rdr.str + rdr.len, path, pathlen);
3111 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003112
3113 /* append a slash at the end of the location is needed and missing */
3114 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3115 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3116 if (rdr.len > rdr.size - 5)
3117 goto return_bad_req;
3118 rdr.str[rdr.len] = '/';
3119 rdr.len++;
3120 }
3121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 break;
3123 }
3124 case REDIRECT_TYPE_LOCATION:
3125 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003126 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003127 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003128
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003129 /* add location */
3130 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3131 rdr.len += rule->rdr_len;
3132 break;
3133 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003134
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003135 if (rule->cookie_len) {
3136 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3137 rdr.len += 14;
3138 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3139 rdr.len += rule->cookie_len;
3140 memcpy(rdr.str + rdr.len, "\r\n", 2);
3141 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003142 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003143
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003144 /* add end of headers and the keep-alive/close status.
3145 * We may choose to set keep-alive if the Location begins
3146 * with a slash, because the client will come back to the
3147 * same server.
3148 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003149 txn->status = rule->code;
3150 /* let's log the request time */
3151 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003152
3153 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003154 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3155 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003156 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3157 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3158 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003159 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003160 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3161 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3162 rdr.len += 30;
3163 } else {
3164 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3165 rdr.len += 24;
3166 }
Willy Tarreau75661452010-01-10 10:35:01 +01003167 }
3168 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3169 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003170 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003171 /* "eat" the request */
Willy Tarreau572bf902012-07-02 17:01:20 +02003172 bi_fast_delete(&req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003173 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003174 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003175 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3176 txn->req.msg_state = HTTP_MSG_CLOSED;
3177 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003178 break;
3179 } else {
3180 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003181 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3182 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3183 rdr.len += 29;
3184 } else {
3185 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3186 rdr.len += 23;
3187 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003188 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003189 goto return_prx_cond;
3190 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003191 }
3192 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003193
Willy Tarreau2be39392010-01-03 17:24:51 +01003194 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3195 * If this happens, then the data will not come immediately, so we must
3196 * send all what we have without waiting. Note that due to the small gain
3197 * in waiting for the body of the request, it's easier to simply put the
3198 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3199 * itself once used.
3200 */
3201 req->flags |= BF_SEND_DONTWAIT;
3202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003203 /* that's OK for us now, let's move on to next analysers */
3204 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003206 return_bad_req:
3207 /* We centralize bad requests processing here */
3208 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3209 /* we detected a parsing error. We want to archive this request
3210 * in the dedicated proxy area for later troubleshooting.
3211 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003212 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003213 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003214
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003215 txn->req.msg_state = HTTP_MSG_ERROR;
3216 txn->status = 400;
3217 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003218
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003219 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003220 if (s->listener->counters)
3221 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003222
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003223 return_prx_cond:
3224 if (!(s->flags & SN_ERR_MASK))
3225 s->flags |= SN_ERR_PRXCOND;
3226 if (!(s->flags & SN_FINST_MASK))
3227 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003228
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229 req->analysers = 0;
3230 req->analyse_exp = TICK_ETERNITY;
3231 return 0;
3232}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003233
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003234/* This function performs all the processing enabled for the current request.
3235 * It returns 1 if the processing can continue on next analysers, or zero if it
3236 * needs more data, encounters an error, or wants to immediately abort the
3237 * request. It relies on buffers flags, and updates s->req->analysers.
3238 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003239int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003240{
3241 struct http_txn *txn = &s->txn;
3242 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003243
Willy Tarreau655dce92009-11-08 13:10:58 +01003244 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003245 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003246 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003247 return 0;
3248 }
3249
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003250 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 +02003251 now_ms, __FUNCTION__,
3252 s,
3253 req,
3254 req->rex, req->wex,
3255 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003256 req->buf.i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003257 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003258
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 /*
3260 * Right now, we know that we have processed the entire headers
3261 * and that unwanted requests have been filtered out. We can do
3262 * whatever we want with the remaining request. Also, now we
3263 * may have separate values for ->fe, ->be.
3264 */
Willy Tarreau06619262006-12-17 08:37:22 +01003265
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 /*
3267 * If HTTP PROXY is set we simply get remote server address
3268 * parsing incoming request.
3269 */
3270 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003271 url2sa(req->buf.p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003273
Willy Tarreau59234e92008-11-30 23:51:27 +01003274 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003275 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 * Note that doing so might move headers in the request, but
3277 * the fields will stay coherent and the URI will not move.
3278 * This should only be performed in the backend.
3279 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003280 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3282 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003283
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003285 * 8: the appsession cookie was looked up very early in 1.2,
3286 * so let's do the same now.
3287 */
3288
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003289 /* It needs to look into the URI unless persistence must be ignored */
3290 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003291 get_srv_from_appsession(s, req->buf.p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003292 }
3293
William Lallemanda73203e2012-03-12 12:48:57 +01003294 /* add unique-id if "header-unique-id" is specified */
3295
3296 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3297 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3298
3299 if (s->fe->header_unique_id && s->unique_id) {
3300 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3301 if (ret < 0 || ret > global.tune.bufsize)
3302 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003303 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003304 goto return_bad_req;
3305 }
3306
Cyril Bontéb21570a2009-11-29 20:04:48 +01003307 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 * 9: add X-Forwarded-For if either the frontend or the backend
3309 * asks for it.
3310 */
3311 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003312 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003313 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003314 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3315 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau572bf902012-07-02 17:01:20 +02003316 req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003317 /* The header is set to be added only if none is present
3318 * and we found it, so don't do anything.
3319 */
3320 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003321 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 /* Add an X-Forwarded-For header unless the source IP is
3323 * in the 'except' network range.
3324 */
3325 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003326 (((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 +01003327 != s->fe->except_net.s_addr) &&
3328 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003329 (((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 +01003330 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003331 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003332 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003333 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003334
3335 /* Note: we rely on the backend to get the header name to be used for
3336 * x-forwarded-for, because the header is really meant for the backends.
3337 * However, if the backend did not specify any option, we have to rely
3338 * on the frontend's header name.
3339 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003340 if (s->be->fwdfor_hdr_len) {
3341 len = s->be->fwdfor_hdr_len;
3342 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003343 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003344 len = s->fe->fwdfor_hdr_len;
3345 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003346 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003348
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003349 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003350 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003351 }
3352 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003353 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 /* FIXME: for the sake of completeness, we should also support
3355 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003356 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003357 int len;
3358 char pn[INET6_ADDRSTRLEN];
3359 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003360 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003361 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003362
Willy Tarreau59234e92008-11-30 23:51:27 +01003363 /* Note: we rely on the backend to get the header name to be used for
3364 * x-forwarded-for, because the header is really meant for the backends.
3365 * However, if the backend did not specify any option, we have to rely
3366 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003367 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003368 if (s->be->fwdfor_hdr_len) {
3369 len = s->be->fwdfor_hdr_len;
3370 memcpy(trash, s->be->fwdfor_hdr_name, len);
3371 } else {
3372 len = s->fe->fwdfor_hdr_len;
3373 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003374 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003375 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003376
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003377 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003378 goto return_bad_req;
3379 }
3380 }
3381
3382 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003383 * 10: add X-Original-To if either the frontend or the backend
3384 * asks for it.
3385 */
3386 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3387
3388 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003389 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003390 /* Add an X-Original-To header unless the destination IP is
3391 * in the 'except' network range.
3392 */
Willy Tarreau59b94792012-05-11 16:16:40 +02003393 si_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003394
Willy Tarreau6471afb2011-09-23 10:54:59 +02003395 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003396 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003397 (((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 +02003398 != s->fe->except_to.s_addr) &&
3399 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003400 (((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 +02003401 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003402 int len;
3403 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003404 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003405
3406 /* Note: we rely on the backend to get the header name to be used for
3407 * x-original-to, because the header is really meant for the backends.
3408 * However, if the backend did not specify any option, we have to rely
3409 * on the frontend's header name.
3410 */
3411 if (s->be->orgto_hdr_len) {
3412 len = s->be->orgto_hdr_len;
3413 memcpy(trash, s->be->orgto_hdr_name, len);
3414 } else {
3415 len = s->fe->orgto_hdr_len;
3416 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003417 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003418 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3419
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003420 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003421 goto return_bad_req;
3422 }
3423 }
3424 }
3425
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003426 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3427 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003428 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003429 unsigned int want_flags = 0;
3430
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003431 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003432 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3433 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3434 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003435 want_flags |= TX_CON_CLO_SET;
3436 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003437 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3438 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3439 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003440 want_flags |= TX_CON_KAL_SET;
3441 }
3442
3443 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003444 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003445 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003446
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003447
Willy Tarreau522d6c02009-12-06 18:49:18 +01003448 /* If we have no server assigned yet and we're balancing on url_param
3449 * with a POST request, we may be interested in checking the body for
3450 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003451 */
3452 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3453 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003454 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003455 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003456 buffer_dont_connect(req);
3457 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003460 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003461 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003462#ifdef TCP_QUICKACK
3463 /* We expect some data from the client. Unless we know for sure
3464 * we already have a full request, we have to re-enable quick-ack
3465 * in case we previously disabled it, otherwise we might cause
3466 * the client to delay further data.
3467 */
3468 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003469 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02003470 (msg->body_len > req->buf.i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003471 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003472#endif
3473 }
Willy Tarreau03945942009-12-22 16:50:27 +01003474
Willy Tarreau59234e92008-11-30 23:51:27 +01003475 /*************************************************************
3476 * OK, that's finished for the headers. We have done what we *
3477 * could. Let's switch to the DATA state. *
3478 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003479 req->analyse_exp = TICK_ETERNITY;
3480 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003481
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003482 /* if the server closes the connection, we want to immediately react
3483 * and close the socket to save packets and syscalls.
3484 */
3485 req->cons->flags |= SI_FL_NOHALF;
3486
Willy Tarreau59234e92008-11-30 23:51:27 +01003487 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003488 /* OK let's go on with the BODY now */
3489 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003490
Willy Tarreau59234e92008-11-30 23:51:27 +01003491 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003493 /* we detected a parsing error. We want to archive this request
3494 * in the dedicated proxy area for later troubleshooting.
3495 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003496 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003497 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003498
Willy Tarreau59234e92008-11-30 23:51:27 +01003499 txn->req.msg_state = HTTP_MSG_ERROR;
3500 txn->status = 400;
3501 req->analysers = 0;
3502 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003503
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003504 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003505 if (s->listener->counters)
3506 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003507
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 if (!(s->flags & SN_ERR_MASK))
3509 s->flags |= SN_ERR_PRXCOND;
3510 if (!(s->flags & SN_FINST_MASK))
3511 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003512 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003513}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003514
Willy Tarreau60b85b02008-11-30 23:28:40 +01003515/* This function is an analyser which processes the HTTP tarpit. It always
3516 * returns zero, at the beginning because it prevents any other processing
3517 * from occurring, and at the end because it terminates the request.
3518 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003519int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003520{
3521 struct http_txn *txn = &s->txn;
3522
3523 /* This connection is being tarpitted. The CLIENT side has
3524 * already set the connect expiration date to the right
3525 * timeout. We just have to check that the client is still
3526 * there and that the timeout has not expired.
3527 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003528 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003529 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3530 !tick_is_expired(req->analyse_exp, now_ms))
3531 return 0;
3532
3533 /* We will set the queue timer to the time spent, just for
3534 * logging purposes. We fake a 500 server error, so that the
3535 * attacker will not suspect his connection has been tarpitted.
3536 * It will not cause trouble to the logs because we can exclude
3537 * the tarpitted connections by filtering on the 'PT' status flags.
3538 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003539 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3540
3541 txn->status = 500;
Willy Tarreaudae2a8a2012-07-23 10:55:43 +02003542 if (!(req->flags & BF_READ_ERROR))
Willy Tarreau60b85b02008-11-30 23:28:40 +01003543 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3544
3545 req->analysers = 0;
3546 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003547
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003548 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003549 if (s->listener->counters)
3550 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003551
Willy Tarreau60b85b02008-11-30 23:28:40 +01003552 if (!(s->flags & SN_ERR_MASK))
3553 s->flags |= SN_ERR_PRXCOND;
3554 if (!(s->flags & SN_FINST_MASK))
3555 s->flags |= SN_FINST_T;
3556 return 0;
3557}
3558
Willy Tarreaud34af782008-11-30 23:36:37 +01003559/* This function is an analyser which processes the HTTP request body. It looks
3560 * for parameters to be used for the load balancing algorithm (url_param). It
3561 * must only be called after the standard HTTP request processing has occurred,
3562 * because it expects the request to be parsed. It returns zero if it needs to
3563 * read more data, or 1 once it has completed its analysis.
3564 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003565int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003566{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003567 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003568 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003569 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003570
3571 /* We have to parse the HTTP request body to find any required data.
3572 * "balance url_param check_post" should have been the only way to get
3573 * into this. We were brought here after HTTP header analysis, so all
3574 * related structures are ready.
3575 */
3576
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3578 goto missing_data;
3579
3580 if (msg->msg_state < HTTP_MSG_100_SENT) {
3581 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3582 * send an HTTP/1.1 100 Continue intermediate response.
3583 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003584 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585 struct hdr_ctx ctx;
3586 ctx.idx = 0;
3587 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003588 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003590 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591 }
3592 }
3593 msg->msg_state = HTTP_MSG_100_SENT;
3594 }
3595
3596 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003597 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02003598 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003599 * is still null. We must save the body in msg->next because it
3600 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003601 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003602 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003603
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003604 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003605 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3606 else
3607 msg->msg_state = HTTP_MSG_DATA;
3608 }
3609
3610 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003611 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003612 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003613 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003614 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003615 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003616
Willy Tarreau115acb92009-12-26 13:56:06 +01003617 if (!ret)
3618 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003619 else if (ret < 0) {
3620 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003622 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003623 }
3624
Willy Tarreaud98cf932009-12-27 22:54:55 +01003625 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003626 * We have the first data byte is in msg->sov. We're waiting for at
3627 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003628 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003629
Willy Tarreau124d9912011-03-01 20:30:48 +01003630 if (msg->body_len < limit)
3631 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003632
Willy Tarreau572bf902012-07-02 17:01:20 +02003633 if (req->buf.i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003634 goto http_end;
3635
3636 missing_data:
3637 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003638 if (req->flags & BF_FULL) {
3639 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003640 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003641 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003642
Willy Tarreau522d6c02009-12-06 18:49:18 +01003643 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3644 txn->status = 408;
3645 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003646
3647 if (!(s->flags & SN_ERR_MASK))
3648 s->flags |= SN_ERR_CLITO;
3649 if (!(s->flags & SN_FINST_MASK))
3650 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003651 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003652 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003653
3654 /* we get here if we need to wait for more data */
3655 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003656 /* Not enough data. We'll re-use the http-request
3657 * timeout here. Ideally, we should set the timeout
3658 * relative to the accept() date. We just set the
3659 * request timeout once at the beginning of the
3660 * request.
3661 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003662 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003663 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003664 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003665 return 0;
3666 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003667
3668 http_end:
3669 /* The situation will not evolve, so let's give up on the analysis. */
3670 s->logs.tv_request = now; /* update the request timer to reflect full request */
3671 req->analysers &= ~an_bit;
3672 req->analyse_exp = TICK_ETERNITY;
3673 return 1;
3674
3675 return_bad_req: /* let's centralize all bad requests */
3676 txn->req.msg_state = HTTP_MSG_ERROR;
3677 txn->status = 400;
3678 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3679
Willy Tarreau79ebac62010-06-07 13:47:49 +02003680 if (!(s->flags & SN_ERR_MASK))
3681 s->flags |= SN_ERR_PRXCOND;
3682 if (!(s->flags & SN_FINST_MASK))
3683 s->flags |= SN_FINST_R;
3684
Willy Tarreau522d6c02009-12-06 18:49:18 +01003685 return_err_msg:
3686 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003687 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003688 if (s->listener->counters)
3689 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003690 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003691}
3692
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003693/* send a server's name with an outgoing request over an established connection.
3694 * Note: this function is designed to be called once the request has been scheduled
3695 * for being forwarded. This is the reason why it rewinds the buffer before
3696 * proceeding.
3697 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003698int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003699
3700 struct hdr_ctx ctx;
3701
Mark Lamourinec2247f02012-01-04 13:02:01 -05003702 char *hdr_name = be->server_id_hdr_name;
3703 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau7421efb2012-07-02 15:11:27 +02003704 struct channel *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003705 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003706 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003707
William Lallemandd9e90662012-01-30 17:27:17 +01003708 ctx.idx = 0;
3709
Willy Tarreau572bf902012-07-02 17:01:20 +02003710 old_o = req->buf.o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003711 if (old_o) {
3712 /* The request was already skipped, let's restore it */
3713 b_rew(req, old_o);
3714 }
3715
Willy Tarreau572bf902012-07-02 17:01:20 +02003716 old_i = req->buf.i;
3717 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->buf.p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003718 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003719 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003720 }
3721
3722 /* Add the new header requested with the server value */
3723 hdr_val = trash;
3724 memcpy(hdr_val, hdr_name, hdr_name_len);
3725 hdr_val += hdr_name_len;
3726 *hdr_val++ = ':';
3727 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003728 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003729 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003730
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003731 if (old_o) {
3732 /* If this was a forwarded request, we must readjust the amount of
3733 * data to be forwarded in order to take into account the size
3734 * variations.
3735 */
Willy Tarreau572bf902012-07-02 17:01:20 +02003736 b_adv(req, old_o + req->buf.i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003737 }
3738
Mark Lamourinec2247f02012-01-04 13:02:01 -05003739 return 0;
3740}
3741
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742/* Terminate current transaction and prepare a new one. This is very tricky
3743 * right now but it works.
3744 */
3745void http_end_txn_clean_session(struct session *s)
3746{
3747 /* FIXME: We need a more portable way of releasing a backend's and a
3748 * server's connections. We need a safer way to reinitialize buffer
3749 * flags. We also need a more accurate method for computing per-request
3750 * data.
3751 */
3752 http_silent_debug(__LINE__, s);
3753
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003754 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003755 si_shutr(s->req->cons);
3756 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757
3758 http_silent_debug(__LINE__, s);
3759
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003760 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003762 if (unlikely(s->srv_conn))
3763 sess_change_server(s, NULL);
3764 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765
3766 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3767 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003768 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769
3770 if (s->txn.status) {
3771 int n;
3772
3773 n = s->txn.status / 100;
3774 if (n < 1 || n > 5)
3775 n = 0;
3776
3777 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003778 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003779
Willy Tarreau24657792010-02-26 10:30:28 +01003780 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003782 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 }
3784
3785 /* don't count other requests' data */
Willy Tarreau572bf902012-07-02 17:01:20 +02003786 s->logs.bytes_in -= s->req->buf.i;
3787 s->logs.bytes_out -= s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788
3789 /* let's do a final log if we need it */
3790 if (s->logs.logwait &&
3791 !(s->flags & SN_MONITOR) &&
3792 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3793 s->do_log(s);
3794 }
3795
3796 s->logs.accept_date = date; /* user-visible date for logging */
3797 s->logs.tv_accept = now; /* corrected date for internal use */
3798 tv_zero(&s->logs.tv_request);
3799 s->logs.t_queue = -1;
3800 s->logs.t_connect = -1;
3801 s->logs.t_data = -1;
3802 s->logs.t_close = 0;
3803 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3804 s->logs.srv_queue_size = 0; /* we will get this number soon */
3805
Willy Tarreau572bf902012-07-02 17:01:20 +02003806 s->logs.bytes_in = s->req->total = s->req->buf.i;
3807 s->logs.bytes_out = s->rep->total = s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003808
3809 if (s->pend_pos)
3810 pendconn_free(s->pend_pos);
3811
Willy Tarreau827aee92011-03-10 16:55:02 +01003812 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003813 if (s->flags & SN_CURR_SESS) {
3814 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003815 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003816 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003817 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3818 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 }
3820
Willy Tarreau9e000c62011-03-10 14:03:36 +01003821 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822
3823 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003824 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +02003825 s->req->cons->conn.flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003827 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003828 s->req->cons->err_loc = NULL;
3829 s->req->cons->exp = TICK_ETERNITY;
3830 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003831 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3832 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 +02003833 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 +01003834 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3835 s->txn.meth = 0;
3836 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003837 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003838 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 s->req->cons->flags |= SI_FL_INDEP_STR;
3840
Willy Tarreau96e31212011-05-30 18:10:30 +02003841 if (s->fe->options2 & PR_O2_NODELAY) {
3842 s->req->flags |= BF_NEVER_WAIT;
3843 s->rep->flags |= BF_NEVER_WAIT;
3844 }
3845
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846 /* if the request buffer is not empty, it means we're
3847 * about to process another request, so send pending
3848 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003849 * Just don't do this if the buffer is close to be full,
3850 * because the request will wait for it to flush a little
3851 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852 */
Willy Tarreau572bf902012-07-02 17:01:20 +02003853 if (s->req->buf.i) {
3854 if (s->rep->buf.o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003855 !(s->rep->flags & BF_FULL) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02003856 bi_end(&s->rep->buf) <= s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003857 s->rep->flags |= BF_EXPECT_MORE;
3858 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003859
3860 /* we're removing the analysers, we MUST re-enable events detection */
3861 buffer_auto_read(s->req);
3862 buffer_auto_close(s->req);
3863 buffer_auto_read(s->rep);
3864 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865
Willy Tarreau342b11c2010-11-24 16:22:09 +01003866 s->req->analysers = s->listener->analysers;
3867 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 s->rep->analysers = 0;
3869
3870 http_silent_debug(__LINE__, s);
3871}
3872
3873
3874/* This function updates the request state machine according to the response
3875 * state machine and buffer flags. It returns 1 if it changes anything (flag
3876 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3877 * it is only used to find when a request/response couple is complete. Both
3878 * this function and its equivalent should loop until both return zero. It
3879 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3880 */
3881int http_sync_req_state(struct session *s)
3882{
Willy Tarreau7421efb2012-07-02 15:11:27 +02003883 struct channel *buf = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884 struct http_txn *txn = &s->txn;
3885 unsigned int old_flags = buf->flags;
3886 unsigned int old_state = txn->req.msg_state;
3887
3888 http_silent_debug(__LINE__, s);
3889 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3890 return 0;
3891
3892 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003893 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003894 * We can shut the read side unless we want to abort_on_close,
3895 * or we have a POST request. The issue with POST requests is
3896 * that some browsers still send a CRLF after the request, and
3897 * this CRLF must be read so that it does not remain in the kernel
3898 * buffers, otherwise a close could cause an RST on some systems
3899 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003900 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003901 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003902 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903
3904 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3905 goto wait_other_side;
3906
3907 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3908 /* The server has not finished to respond, so we
3909 * don't want to move in order not to upset it.
3910 */
3911 goto wait_other_side;
3912 }
3913
3914 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3915 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003916 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917 txn->req.msg_state = HTTP_MSG_TUNNEL;
3918 goto wait_other_side;
3919 }
3920
3921 /* When we get here, it means that both the request and the
3922 * response have finished receiving. Depending on the connection
3923 * mode, we'll have to wait for the last bytes to leave in either
3924 * direction, and sometimes for a close to be effective.
3925 */
3926
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003927 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3928 /* Server-close mode : queue a connection close to the server */
3929 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003931 }
3932 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3933 /* Option forceclose is set, or either side wants to close,
3934 * let's enforce it now that we're not expecting any new
3935 * data to come. The caller knows the session is complete
3936 * once both states are CLOSED.
3937 */
3938 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003939 buffer_shutr_now(buf);
3940 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003941 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003942 }
3943 else {
3944 /* The last possible modes are keep-alive and tunnel. Since tunnel
3945 * mode does not set the body analyser, we can't reach this place
3946 * in tunnel mode, so we're left with keep-alive only.
3947 * This mode is currently not implemented, we switch to tunnel mode.
3948 */
3949 buffer_auto_read(buf);
3950 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003951 }
3952
3953 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3954 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003955 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3956
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 if (!(buf->flags & BF_OUT_EMPTY)) {
3958 txn->req.msg_state = HTTP_MSG_CLOSING;
3959 goto http_msg_closing;
3960 }
3961 else {
3962 txn->req.msg_state = HTTP_MSG_CLOSED;
3963 goto http_msg_closed;
3964 }
3965 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003966 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 }
3968
3969 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3970 http_msg_closing:
3971 /* nothing else to forward, just waiting for the output buffer
3972 * to be empty and for the shutw_now to take effect.
3973 */
3974 if (buf->flags & BF_OUT_EMPTY) {
3975 txn->req.msg_state = HTTP_MSG_CLOSED;
3976 goto http_msg_closed;
3977 }
3978 else if (buf->flags & BF_SHUTW) {
3979 txn->req.msg_state = HTTP_MSG_ERROR;
3980 goto wait_other_side;
3981 }
3982 }
3983
3984 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3985 http_msg_closed:
3986 goto wait_other_side;
3987 }
3988
3989 wait_other_side:
3990 http_silent_debug(__LINE__, s);
3991 return txn->req.msg_state != old_state || buf->flags != old_flags;
3992}
3993
3994
3995/* This function updates the response state machine according to the request
3996 * state machine and buffer flags. It returns 1 if it changes anything (flag
3997 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3998 * it is only used to find when a request/response couple is complete. Both
3999 * this function and its equivalent should loop until both return zero. It
4000 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4001 */
4002int http_sync_res_state(struct session *s)
4003{
Willy Tarreau7421efb2012-07-02 15:11:27 +02004004 struct channel *buf = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005 struct http_txn *txn = &s->txn;
4006 unsigned int old_flags = buf->flags;
4007 unsigned int old_state = txn->rsp.msg_state;
4008
4009 http_silent_debug(__LINE__, s);
4010 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4011 return 0;
4012
4013 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4014 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004015 * still monitor the server connection for a possible close
4016 * while the request is being uploaded, so we don't disable
4017 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004019 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020
4021 if (txn->req.msg_state == HTTP_MSG_ERROR)
4022 goto wait_other_side;
4023
4024 if (txn->req.msg_state < HTTP_MSG_DONE) {
4025 /* The client seems to still be sending data, probably
4026 * because we got an error response during an upload.
4027 * We have the choice of either breaking the connection
4028 * or letting it pass through. Let's do the later.
4029 */
4030 goto wait_other_side;
4031 }
4032
4033 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4034 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004035 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004036 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4037 goto wait_other_side;
4038 }
4039
4040 /* When we get here, it means that both the request and the
4041 * response have finished receiving. Depending on the connection
4042 * mode, we'll have to wait for the last bytes to leave in either
4043 * direction, and sometimes for a close to be effective.
4044 */
4045
4046 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4047 /* Server-close mode : shut read and wait for the request
4048 * side to close its output buffer. The caller will detect
4049 * when we're in DONE and the other is in CLOSED and will
4050 * catch that for the final cleanup.
4051 */
4052 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4053 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004055 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4056 /* Option forceclose is set, or either side wants to close,
4057 * let's enforce it now that we're not expecting any new
4058 * data to come. The caller knows the session is complete
4059 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004061 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4062 buffer_shutr_now(buf);
4063 buffer_shutw_now(buf);
4064 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 }
4066 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004067 /* The last possible modes are keep-alive and tunnel. Since tunnel
4068 * mode does not set the body analyser, we can't reach this place
4069 * in tunnel mode, so we're left with keep-alive only.
4070 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 }
4075
4076 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4077 /* if we've just closed an output, let's switch */
4078 if (!(buf->flags & BF_OUT_EMPTY)) {
4079 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4080 goto http_msg_closing;
4081 }
4082 else {
4083 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4084 goto http_msg_closed;
4085 }
4086 }
4087 goto wait_other_side;
4088 }
4089
4090 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4091 http_msg_closing:
4092 /* nothing else to forward, just waiting for the output buffer
4093 * to be empty and for the shutw_now to take effect.
4094 */
4095 if (buf->flags & BF_OUT_EMPTY) {
4096 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4097 goto http_msg_closed;
4098 }
4099 else if (buf->flags & BF_SHUTW) {
4100 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004101 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004102 if (target_srv(&s->target))
4103 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 goto wait_other_side;
4105 }
4106 }
4107
4108 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4109 http_msg_closed:
4110 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004111 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 goto wait_other_side;
4115 }
4116
4117 wait_other_side:
4118 http_silent_debug(__LINE__, s);
4119 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4120}
4121
4122
4123/* Resync the request and response state machines. Return 1 if either state
4124 * changes.
4125 */
4126int http_resync_states(struct session *s)
4127{
4128 struct http_txn *txn = &s->txn;
4129 int old_req_state = txn->req.msg_state;
4130 int old_res_state = txn->rsp.msg_state;
4131
4132 http_silent_debug(__LINE__, s);
4133 http_sync_req_state(s);
4134 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004135 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004136 if (!http_sync_res_state(s))
4137 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004138 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 if (!http_sync_req_state(s))
4140 break;
4141 }
4142 http_silent_debug(__LINE__, s);
4143 /* OK, both state machines agree on a compatible state.
4144 * There are a few cases we're interested in :
4145 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4146 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4147 * directions, so let's simply disable both analysers.
4148 * - HTTP_MSG_CLOSED on the response only means we must abort the
4149 * request.
4150 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4151 * with server-close mode means we've completed one request and we
4152 * must re-initialize the server connection.
4153 */
4154
4155 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4156 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4157 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4158 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4159 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004160 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004161 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004162 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004163 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004164 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004166 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4167 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004168 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004169 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004170 s->rep->analysers = 0;
4171 buffer_auto_close(s->rep);
4172 buffer_auto_read(s->rep);
4173 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 buffer_abort(s->req);
4175 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004176 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004177 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178 }
4179 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4180 txn->rsp.msg_state == HTTP_MSG_DONE &&
4181 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4182 /* server-close: terminate this server connection and
4183 * reinitialize a fresh-new transaction.
4184 */
4185 http_end_txn_clean_session(s);
4186 }
4187
4188 http_silent_debug(__LINE__, s);
4189 return txn->req.msg_state != old_req_state ||
4190 txn->rsp.msg_state != old_res_state;
4191}
4192
Willy Tarreaud98cf932009-12-27 22:54:55 +01004193/* This function is an analyser which forwards request body (including chunk
4194 * sizes if any). It is called as soon as we must forward, even if we forward
4195 * zero byte. The only situation where it must not be called is when we're in
4196 * tunnel mode and we want to forward till the close. It's used both to forward
4197 * remaining data and to resync after end of body. It expects the msg_state to
4198 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4199 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004200 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004201 * bytes of pending data + the headers if not already done (between sol and sov).
4202 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004203 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004204int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004205{
4206 struct http_txn *txn = &s->txn;
4207 struct http_msg *msg = &s->txn.req;
4208
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004209 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4210 return 0;
4211
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004212 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02004213 ((req->flags & BF_SHUTW) && (req->to_forward || req->buf.o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004214 /* Output closed while we were sending data. We must abort and
4215 * wake the other side up.
4216 */
4217 msg->msg_state = HTTP_MSG_ERROR;
4218 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004219 return 1;
4220 }
4221
Willy Tarreau4fe41902010-06-07 22:27:41 +02004222 /* in most states, we should abort in case of early close */
4223 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004224
4225 /* Note that we don't have to send 100-continue back because we don't
4226 * need the data to complete our job, and it's up to the server to
4227 * decide whether to return 100, 417 or anything else in return of
4228 * an "Expect: 100-continue" header.
4229 */
4230
4231 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004232 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02004233 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004234 * is still null. We must save the body in msg->next because it
4235 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004237 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004238
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004239 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004240 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4241 else {
4242 msg->msg_state = HTTP_MSG_DATA;
4243 }
4244 }
4245
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004247 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004248
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004250 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004251 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004252 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004253 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004254 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004255 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004256 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004257 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004258
Willy Tarreaucaabe412010-01-03 23:08:28 +01004259 if (msg->msg_state == HTTP_MSG_DATA) {
4260 /* must still forward */
4261 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004262 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004263
4264 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004265 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004266 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004268 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004269 }
4270 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004271 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004272 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004273 * TRAILERS state.
4274 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004275 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004276
4277 if (!ret)
4278 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004279 else if (ret < 0) {
4280 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004281 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004282 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004283 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004284 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004285 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004286 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004287 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4288 /* we want the CRLF after the data */
4289 int ret;
4290
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004291 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004292
4293 if (ret == 0)
4294 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004295 else if (ret < 0) {
4296 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004297 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004298 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004299 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004300 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004301 /* we're in MSG_CHUNK_SIZE now */
4302 }
4303 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004304 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004305
4306 if (ret == 0)
4307 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004308 else if (ret < 0) {
4309 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004310 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004311 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004313 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314 /* we're in HTTP_MSG_DONE now */
4315 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004317 int old_state = msg->msg_state;
4318
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004320 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004321 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4322 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4323 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004324 if (http_resync_states(s)) {
4325 /* some state changes occurred, maybe the analyser
4326 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004327 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004328 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4329 if (req->flags & BF_SHUTW) {
4330 /* request errors are most likely due to
4331 * the server aborting the transfer.
4332 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004333 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004334 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004335 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004336 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004338 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004340 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004341
4342 /* If "option abortonclose" is set on the backend, we
4343 * want to monitor the client's connection and forward
4344 * any shutdown notification to the server, which will
4345 * decide whether to close or to go on processing the
4346 * request.
4347 */
4348 if (s->be->options & PR_O_ABRT_CLOSE) {
4349 buffer_auto_read(req);
4350 buffer_auto_close(req);
4351 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004352 else if (s->txn.meth == HTTP_METH_POST) {
4353 /* POST requests may require to read extra CRLF
4354 * sent by broken browsers and which could cause
4355 * an RST to be sent upon close on some systems
4356 * (eg: Linux).
4357 */
4358 buffer_auto_read(req);
4359 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004360
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004362 }
4363 }
4364
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004366 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004367 if (req->flags & BF_SHUTR) {
4368 if (!(s->flags & SN_ERR_MASK))
4369 s->flags |= SN_ERR_CLICL;
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
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004377 s->fe->fe_counters.cli_aborts++;
4378 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004379 if (target_srv(&s->target))
4380 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004381
4382 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004383 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004384
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004385 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004386 if (req->flags & BF_SHUTW)
4387 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004389 /* When TE: chunked is used, we need to get there again to parse remaining
4390 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4391 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004392 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004393 buffer_dont_close(req);
4394
Willy Tarreau5c620922011-05-11 19:56:11 +02004395 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004396 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4397 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004398 * modes are already handled by the stream sock layer. We must not do
4399 * this in content-length mode because it could present the MSG_MORE
4400 * flag with the last block of forwarded data, which would cause an
4401 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004402 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004403 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004404 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004405
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004407 return 0;
4408
Willy Tarreaud98cf932009-12-27 22:54:55 +01004409 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004410 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004411 if (s->listener->counters)
4412 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004413 return_bad_req_stats_ok:
4414 txn->req.msg_state = HTTP_MSG_ERROR;
4415 if (txn->status) {
4416 /* Note: we don't send any error if some data were already sent */
4417 stream_int_retnclose(req->prod, NULL);
4418 } else {
4419 txn->status = 400;
4420 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4421 }
4422 req->analysers = 0;
4423 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004424
4425 if (!(s->flags & SN_ERR_MASK))
4426 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004427 if (!(s->flags & SN_FINST_MASK)) {
4428 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4429 s->flags |= SN_FINST_H;
4430 else
4431 s->flags |= SN_FINST_D;
4432 }
4433 return 0;
4434
4435 aborted_xfer:
4436 txn->req.msg_state = HTTP_MSG_ERROR;
4437 if (txn->status) {
4438 /* Note: we don't send any error if some data were already sent */
4439 stream_int_retnclose(req->prod, NULL);
4440 } else {
4441 txn->status = 502;
4442 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4443 }
4444 req->analysers = 0;
4445 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4446
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004447 s->fe->fe_counters.srv_aborts++;
4448 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004449 if (target_srv(&s->target))
4450 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004451
4452 if (!(s->flags & SN_ERR_MASK))
4453 s->flags |= SN_ERR_SRVCL;
4454 if (!(s->flags & SN_FINST_MASK)) {
4455 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4456 s->flags |= SN_FINST_H;
4457 else
4458 s->flags |= SN_FINST_D;
4459 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004460 return 0;
4461}
4462
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004463/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4464 * processing can continue on next analysers, or zero if it either needs more
4465 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4466 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4467 * when it has nothing left to do, and may remove any analyser when it wants to
4468 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004469 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004470int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004471{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004472 struct http_txn *txn = &s->txn;
4473 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004474 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004475 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004477 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004478
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004479 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 +02004480 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004481 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004482 rep,
4483 rep->rex, rep->wex,
4484 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004485 rep->buf.i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004486 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004487
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488 /*
4489 * Now parse the partial (or complete) lines.
4490 * We will check the response syntax, and also join multi-line
4491 * headers. An index of all the lines will be elaborated while
4492 * parsing.
4493 *
4494 * For the parsing, we use a 28 states FSM.
4495 *
4496 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02004497 * rep->buf.p = beginning of response
4498 * rep->buf.p + msg->eoh = end of processed headers / start of current one
4499 * rep->buf.p + rep->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004500 * msg->eol = end of current header or line (LF or CRLF)
4501 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 */
4503
Willy Tarreau83e3af02009-12-28 17:39:57 +01004504 /* There's a protected area at the end of the buffer for rewriting
4505 * purposes. We don't want to start to parse the request if the
4506 * protected area is affected, because we may have to move processed
4507 * data later, which is much more complicated.
4508 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004509 if (buffer_not_empty(&rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004510 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02004511 bi_end(&rep->buf) < b_ptr(&rep->buf, msg->next) ||
4512 bi_end(&rep->buf) > rep->buf.data + rep->buf.size - global.tune.maxrewrite)) {
4513 if (rep->buf.o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004514 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004515 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4516 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004517 buffer_dont_close(rep);
4518 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4519 return 0;
4520 }
Willy Tarreau572bf902012-07-02 17:01:20 +02004521 if (rep->buf.i <= rep->buf.size - global.tune.maxrewrite)
4522 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004523 }
4524
Willy Tarreau572bf902012-07-02 17:01:20 +02004525 if (likely(msg->next < rep->buf.i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004526 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004527 }
4528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 /* 1: we might have to print this header in debug mode */
4530 if (unlikely((global.mode & MODE_DEBUG) &&
4531 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004532 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004533 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004534
Willy Tarreau572bf902012-07-02 17:01:20 +02004535 sol = rep->buf.p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004536 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004538
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539 sol += hdr_idx_first_pos(&txn->hdr_idx);
4540 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004541
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542 while (cur_idx) {
4543 eol = sol + txn->hdr_idx.v[cur_idx].len;
4544 debug_hdr("srvhdr", s, sol, eol);
4545 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4546 cur_idx = txn->hdr_idx.v[cur_idx].next;
4547 }
4548 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004549
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 /*
4551 * Now we quickly check if we have found a full valid response.
4552 * If not so, we check the FD and buffer states before leaving.
4553 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004554 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 * responses are checked first.
4556 *
4557 * Depending on whether the client is still there or not, we
4558 * may send an error response back or not. Note that normally
4559 * we should only check for HTTP status there, and check I/O
4560 * errors somewhere else.
4561 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004562
Willy Tarreau655dce92009-11-08 13:10:58 +01004563 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 /* Invalid response */
4565 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4566 /* we detected a parsing error. We want to archive this response
4567 * in the dedicated proxy area for later troubleshooting.
4568 */
4569 hdr_response_bad:
4570 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004571 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004572
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004573 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004574 if (target_srv(&s->target)) {
4575 target_srv(&s->target)->counters.failed_resp++;
4576 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004577 }
Willy Tarreau64648412010-03-05 10:41:54 +01004578 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004579 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580 rep->analysers = 0;
4581 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004582 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004583 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004584 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4585
4586 if (!(s->flags & SN_ERR_MASK))
4587 s->flags |= SN_ERR_PRXCOND;
4588 if (!(s->flags & SN_FINST_MASK))
4589 s->flags |= SN_FINST_H;
4590
4591 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004592 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004593
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004594 /* too large response does not fit in buffer. */
4595 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004596 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02004597 msg->err_pos = rep->buf.i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 goto hdr_response_bad;
4599 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 /* read error */
4602 else if (rep->flags & BF_READ_ERROR) {
4603 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004604 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004605
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004606 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004607 if (target_srv(&s->target)) {
4608 target_srv(&s->target)->counters.failed_resp++;
4609 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004610 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004611
Willy Tarreau90deb182010-01-07 00:20:41 +01004612 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 rep->analysers = 0;
4614 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004615 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004616 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004618
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 if (!(s->flags & SN_ERR_MASK))
4620 s->flags |= SN_ERR_SRVCL;
4621 if (!(s->flags & SN_FINST_MASK))
4622 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004623 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004624 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004625
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 /* read timeout : return a 504 to the client. */
4627 else if (rep->flags & BF_READ_TIMEOUT) {
4628 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004629 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004630
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004631 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004632 if (target_srv(&s->target)) {
4633 target_srv(&s->target)->counters.failed_resp++;
4634 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004635 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004636
Willy Tarreau90deb182010-01-07 00:20:41 +01004637 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 rep->analysers = 0;
4639 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004640 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004641 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004643
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 if (!(s->flags & SN_ERR_MASK))
4645 s->flags |= SN_ERR_SRVTO;
4646 if (!(s->flags & SN_FINST_MASK))
4647 s->flags |= SN_FINST_H;
4648 return 0;
4649 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004650
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004651 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004653 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004654 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004655
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004656 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004657 if (target_srv(&s->target)) {
4658 target_srv(&s->target)->counters.failed_resp++;
4659 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004660 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004661
Willy Tarreau90deb182010-01-07 00:20:41 +01004662 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 rep->analysers = 0;
4664 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004665 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004666 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004668
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669 if (!(s->flags & SN_ERR_MASK))
4670 s->flags |= SN_ERR_SRVCL;
4671 if (!(s->flags & SN_FINST_MASK))
4672 s->flags |= SN_FINST_H;
4673 return 0;
4674 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004675
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676 /* write error to client (we don't send any message then) */
4677 else if (rep->flags & BF_WRITE_ERROR) {
4678 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004679 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004680
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004681 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004682 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004683 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684
4685 if (!(s->flags & SN_ERR_MASK))
4686 s->flags |= SN_ERR_CLICL;
4687 if (!(s->flags & SN_FINST_MASK))
4688 s->flags |= SN_FINST_H;
4689
4690 /* process_session() will take care of the error */
4691 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004692 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 buffer_dont_close(rep);
4695 return 0;
4696 }
4697
4698 /* More interesting part now : we know that we have a complete
4699 * response which at least looks like HTTP. We have an indicator
4700 * of each header's length, so we can parse them quickly.
4701 */
4702
4703 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004704 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706 /*
4707 * 1: get the status code
4708 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004709 n = rep->buf.p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004710 if (n < 1 || n > 5)
4711 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004712 /* when the client triggers a 4xx from the server, it's most often due
4713 * to a missing object or permission. These events should be tracked
4714 * because if they happen often, it may indicate a brute force or a
4715 * vulnerability scan.
4716 */
4717 if (n == 4)
4718 session_inc_http_err_ctr(s);
4719
Willy Tarreau827aee92011-03-10 16:55:02 +01004720 if (target_srv(&s->target))
4721 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722
Willy Tarreau5b154472009-12-21 20:11:07 +01004723 /* check if the response is HTTP/1.1 or above */
4724 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004725 ((rep->buf.p[5] > '1') ||
4726 ((rep->buf.p[5] == '1') && (rep->buf.p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004727 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004728
4729 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004730 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 +01004731
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004732 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004733 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004734
Willy Tarreau572bf902012-07-02 17:01:20 +02004735 txn->status = strl2ui(rep->buf.p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004736
Willy Tarreau39650402010-03-15 19:44:39 +01004737 /* Adjust server's health based on status code. Note: status codes 501
4738 * and 505 are triggered on demand by client request, so we must not
4739 * count them as server failures.
4740 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004741 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004742 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004743 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004744 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004745 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004746 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004747
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004748 /*
4749 * 2: check for cacheability.
4750 */
4751
4752 switch (txn->status) {
4753 case 200:
4754 case 203:
4755 case 206:
4756 case 300:
4757 case 301:
4758 case 410:
4759 /* RFC2616 @13.4:
4760 * "A response received with a status code of
4761 * 200, 203, 206, 300, 301 or 410 MAY be stored
4762 * by a cache (...) unless a cache-control
4763 * directive prohibits caching."
4764 *
4765 * RFC2616 @9.5: POST method :
4766 * "Responses to this method are not cacheable,
4767 * unless the response includes appropriate
4768 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004769 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02004771 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4773 break;
4774 default:
4775 break;
4776 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004777
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 /*
4779 * 3: we may need to capture headers
4780 */
4781 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004782 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02004783 capture_headers(rep->buf.p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 txn->rsp.cap, s->fe->rsp_cap);
4785
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004786 /* 4: determine the transfer-length.
4787 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4788 * the presence of a message-body in a RESPONSE and its transfer length
4789 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004790 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004791 * All responses to the HEAD request method MUST NOT include a
4792 * message-body, even though the presence of entity-header fields
4793 * might lead one to believe they do. All 1xx (informational), 204
4794 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4795 * message-body. All other responses do include a message-body,
4796 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004797 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004798 * 1. Any response which "MUST NOT" include a message-body (such as the
4799 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4800 * always terminated by the first empty line after the header fields,
4801 * regardless of the entity-header fields present in the message.
4802 *
4803 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4804 * the "chunked" transfer-coding (Section 6.2) is used, the
4805 * transfer-length is defined by the use of this transfer-coding.
4806 * If a Transfer-Encoding header field is present and the "chunked"
4807 * transfer-coding is not present, the transfer-length is defined by
4808 * the sender closing the connection.
4809 *
4810 * 3. If a Content-Length header field is present, its decimal value in
4811 * OCTETs represents both the entity-length and the transfer-length.
4812 * If a message is received with both a Transfer-Encoding header
4813 * field and a Content-Length header field, the latter MUST be ignored.
4814 *
4815 * 4. If the message uses the media type "multipart/byteranges", and
4816 * the transfer-length is not otherwise specified, then this self-
4817 * delimiting media type defines the transfer-length. This media
4818 * type MUST NOT be used unless the sender knows that the recipient
4819 * can parse it; the presence in a request of a Range header with
4820 * multiple byte-range specifiers from a 1.1 client implies that the
4821 * client can parse multipart/byteranges responses.
4822 *
4823 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004824 */
4825
4826 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004827 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004828 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004829 * FIXME: should we parse anyway and return an error on chunked encoding ?
4830 */
4831 if (txn->meth == HTTP_METH_HEAD ||
4832 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004833 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004834 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004835 goto skip_content_length;
4836 }
4837
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004838 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004839 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004840 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004841 http_find_header2("Transfer-Encoding", 17, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004842 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004843 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4844 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004845 /* bad transfer-encoding (chunked followed by something else) */
4846 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004847 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004848 break;
4849 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004850 }
4851
4852 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4853 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004854 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004855 http_find_header2("Content-Length", 14, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004856 signed long long cl;
4857
Willy Tarreauad14f752011-09-02 20:33:27 +02004858 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004859 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004860 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004861 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004862
Willy Tarreauad14f752011-09-02 20:33:27 +02004863 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004864 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004865 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004866 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004867
Willy Tarreauad14f752011-09-02 20:33:27 +02004868 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004869 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004870 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004871 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004872
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004873 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004874 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004875 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004876 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004877
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004878 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004879 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004880 }
4881
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004882 /* FIXME: we should also implement the multipart/byterange method.
4883 * For now on, we resort to close mode in this case (unknown length).
4884 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004885skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004886
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004887 /* end of job, return OK */
4888 rep->analysers &= ~an_bit;
4889 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004890 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004891 return 1;
4892}
4893
4894/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004895 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4896 * and updates t->rep->analysers. It might make sense to explode it into several
4897 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004898 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004899int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900{
4901 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004902 struct http_msg *msg = &txn->rsp;
4903 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004904 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004906 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 +02004907 now_ms, __FUNCTION__,
4908 t,
4909 rep,
4910 rep->rex, rep->wex,
4911 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004912 rep->buf.i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913 rep->analysers);
4914
Willy Tarreau655dce92009-11-08 13:10:58 +01004915 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004916 return 0;
4917
4918 rep->analysers &= ~an_bit;
4919 rep->analyse_exp = TICK_ETERNITY;
4920
Willy Tarreau5b154472009-12-21 20:11:07 +01004921 /* Now we have to check if we need to modify the Connection header.
4922 * This is more difficult on the response than it is on the request,
4923 * because we can have two different HTTP versions and we don't know
4924 * how the client will interprete a response. For instance, let's say
4925 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4926 * HTTP/1.1 response without any header. Maybe it will bound itself to
4927 * HTTP/1.0 because it only knows about it, and will consider the lack
4928 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4929 * the lack of header as a keep-alive. Thus we will use two flags
4930 * indicating how a request MAY be understood by the client. In case
4931 * of multiple possibilities, we'll fix the header to be explicit. If
4932 * ambiguous cases such as both close and keepalive are seen, then we
4933 * will fall back to explicit close. Note that we won't take risks with
4934 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004935 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004936 */
4937
Willy Tarreaudc008c52010-02-01 16:20:08 +01004938 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4939 txn->status == 101)) {
4940 /* Either we've established an explicit tunnel, or we're
4941 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004942 * to understand the next protocols. We have to switch to tunnel
4943 * mode, so that we transfer the request and responses then let
4944 * this protocol pass unmodified. When we later implement specific
4945 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004946 * header which contains information about that protocol for
4947 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004948 */
4949 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4950 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004951 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4952 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4953 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004954 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004955
Willy Tarreau60466522010-01-18 19:08:45 +01004956 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004957 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004958 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4959 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004960
Willy Tarreau60466522010-01-18 19:08:45 +01004961 /* now adjust header transformations depending on current state */
4962 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4963 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4964 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004965 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004966 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004967 }
Willy Tarreau60466522010-01-18 19:08:45 +01004968 else { /* SCL / KAL */
4969 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004970 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004971 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004972 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004973
Willy Tarreau60466522010-01-18 19:08:45 +01004974 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004975 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004976
Willy Tarreau60466522010-01-18 19:08:45 +01004977 /* Some keep-alive responses are converted to Server-close if
4978 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004979 */
Willy Tarreau60466522010-01-18 19:08:45 +01004980 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4981 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004982 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004983 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004984 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004985 }
4986
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004987 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 /*
4989 * 3: we will have to evaluate the filters.
4990 * As opposed to version 1.2, now they will be evaluated in the
4991 * filters order and not in the header order. This means that
4992 * each filter has to be validated among all headers.
4993 *
4994 * Filters are tried with ->be first, then with ->fe if it is
4995 * different from ->be.
4996 */
4997
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 cur_proxy = t->be;
4999 while (1) {
5000 struct proxy *rule_set = cur_proxy;
5001
5002 /* try headers filters */
5003 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005004 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005005 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005006 if (target_srv(&t->target)) {
5007 target_srv(&t->target)->counters.failed_resp++;
5008 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005009 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005010 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005011 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005012 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005013 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005014 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005015 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005016 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017 if (!(t->flags & SN_ERR_MASK))
5018 t->flags |= SN_ERR_PRXCOND;
5019 if (!(t->flags & SN_FINST_MASK))
5020 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005021 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005022 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005023 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005024
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005025 /* has the response been denied ? */
5026 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005027 if (target_srv(&t->target))
5028 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005029
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005030 t->be->be_counters.denied_resp++;
5031 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005032 if (t->listener->counters)
5033 t->listener->counters->denied_resp++;
5034
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005036 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005037
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005039 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005040 if (txn->status < 200)
5041 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005042 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005043 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005044 ret = acl_pass(ret);
5045 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5046 ret = !ret;
5047 if (!ret)
5048 continue;
5049 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005050 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005052 }
5053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005054 /* check whether we're already working on the frontend */
5055 if (cur_proxy == t->fe)
5056 break;
5057 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005059
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005060 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005061 * We may be facing a 100-continue response, in which case this
5062 * is not the right response, and we're waiting for the next one.
5063 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005064 * next one.
5065 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005066 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005067 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau09d1e252012-05-18 22:36:34 +02005068 msg->next -= buffer_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005069 msg->msg_state = HTTP_MSG_RPBEFORE;
5070 txn->status = 0;
5071 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5072 return 1;
5073 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005074 else if (unlikely(txn->status < 200))
5075 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005076
5077 /* we don't have any 1xx status code now */
5078
5079 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005080 * 4: check for server cookie.
5081 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005082 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5083 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005084 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005085
Willy Tarreaubaaee002006-06-26 02:48:02 +02005086
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005089 */
Willy Tarreau67402132012-05-31 20:40:20 +02005090 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005092
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 /*
5094 * 6: add server cookie in the response if needed
5095 */
Willy Tarreau67402132012-05-31 20:40:20 +02005096 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5097 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005098 (!(t->flags & SN_DIRECT) ||
5099 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5100 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5101 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5102 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005103 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005104 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005106 /* the server is known, it's not the one the client requested, or the
5107 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005108 * insert a set-cookie here, except if we want to insert only on POST
5109 * requests and this one isn't. Note that servers which don't have cookies
5110 * (eg: some backup servers) will return a full cookie removal request.
5111 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005112 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005113 len = sprintf(trash,
5114 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5115 t->be->cookie_name);
5116 }
5117 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005118 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005119
5120 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5121 /* emit last_date, which is mandatory */
5122 trash[len++] = COOKIE_DELIM_DATE;
5123 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5124 if (t->be->cookie_maxlife) {
5125 /* emit first_date, which is either the original one or
5126 * the current date.
5127 */
5128 trash[len++] = COOKIE_DELIM_DATE;
5129 s30tob64(txn->cookie_first_date ?
5130 txn->cookie_first_date >> 2 :
5131 (date.tv_sec+3) >> 2, trash + len);
5132 len += 5;
5133 }
5134 }
5135 len += sprintf(trash + len, "; path=/");
5136 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005137
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005138 if (t->be->cookie_domain)
5139 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005140
Willy Tarreau4992dd22012-05-31 21:02:17 +02005141 if (t->be->ck_opts & PR_CK_HTTPONLY)
5142 len += sprintf(trash+len, "; HttpOnly");
5143
5144 if (t->be->ck_opts & PR_CK_SECURE)
5145 len += sprintf(trash+len, "; Secure");
5146
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005147 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005149
Willy Tarreauf1348312010-10-07 15:54:11 +02005150 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005151 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005152 /* the server did not change, only the date was updated */
5153 txn->flags |= TX_SCK_UPDATED;
5154 else
5155 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005156
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005157 /* Here, we will tell an eventual cache on the client side that we don't
5158 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5159 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5160 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5161 */
Willy Tarreau67402132012-05-31 20:40:20 +02005162 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005163
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005164 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5165
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005166 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005167 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005169 }
5170 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005171
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005172 /*
5173 * 7: check if result will be cacheable with a cookie.
5174 * We'll block the response if security checks have caught
5175 * nasty things such as a cacheable cookie.
5176 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005177 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5178 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005179 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005180
5181 /* we're in presence of a cacheable response containing
5182 * a set-cookie header. We'll block it as requested by
5183 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005185 if (target_srv(&t->target))
5186 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005187
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005188 t->be->be_counters.denied_resp++;
5189 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005190 if (t->listener->counters)
5191 t->listener->counters->denied_resp++;
5192
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005194 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 send_log(t->be, LOG_ALERT,
5196 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005197 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005198 goto return_srv_prx_502;
5199 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005200
5201 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005202 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005203 */
Willy Tarreau60466522010-01-18 19:08:45 +01005204 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5205 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5206 unsigned int want_flags = 0;
5207
5208 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5209 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5210 /* we want a keep-alive response here. Keep-alive header
5211 * required if either side is not 1.1.
5212 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005213 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005214 want_flags |= TX_CON_KAL_SET;
5215 }
5216 else {
5217 /* we want a close response here. Close header required if
5218 * the server is 1.1, regardless of the client.
5219 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005220 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005221 want_flags |= TX_CON_CLO_SET;
5222 }
5223
5224 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005225 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005226 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005227
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005228 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005229 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005230 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005231 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005232
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005233 /*************************************************************
5234 * OK, that's finished for the headers. We have done what we *
5235 * could. Let's switch to the DATA state. *
5236 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005237
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005238 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 /* if the user wants to log as soon as possible, without counting
5241 * bytes from the server, then this is the right moment. We have
5242 * to temporarily assign bytes_out to log what we currently have.
5243 */
5244 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5245 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5246 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005247 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248 t->logs.bytes_out = 0;
5249 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005250
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005251 /* Note: we must not try to cheat by jumping directly to DATA,
5252 * otherwise we would not let the client side wake up.
5253 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005254
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005255 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005257 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005258}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005259
Willy Tarreaud98cf932009-12-27 22:54:55 +01005260/* This function is an analyser which forwards response body (including chunk
5261 * sizes if any). It is called as soon as we must forward, even if we forward
5262 * zero byte. The only situation where it must not be called is when we're in
5263 * tunnel mode and we want to forward till the close. It's used both to forward
5264 * remaining data and to resync after end of body. It expects the msg_state to
5265 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5266 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005267 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005268 * bytes of pending data + the headers if not already done (between sol and sov).
5269 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005270 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005271int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005272{
5273 struct http_txn *txn = &s->txn;
5274 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005275 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005277 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5278 return 0;
5279
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005280 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02005281 ((res->flags & BF_SHUTW) && (res->to_forward || res->buf.o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005282 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005283 /* Output closed while we were sending data. We must abort and
5284 * wake the other side up.
5285 */
5286 msg->msg_state = HTTP_MSG_ERROR;
5287 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005288 return 1;
5289 }
5290
Willy Tarreau4fe41902010-06-07 22:27:41 +02005291 /* in most states, we should abort in case of early close */
5292 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005293
Willy Tarreaud98cf932009-12-27 22:54:55 +01005294 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005295 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02005296 * rep->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005297 * is still null. We must save the body in msg->next because it
5298 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005300 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005301
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005302 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005303 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5304 else {
5305 msg->msg_state = HTTP_MSG_DATA;
5306 }
5307 }
5308
Willy Tarreaud98cf932009-12-27 22:54:55 +01005309 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005311 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005312 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005313 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005314 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005315 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005316 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005317 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005318 }
5319
Willy Tarreaucaabe412010-01-03 23:08:28 +01005320 if (msg->msg_state == HTTP_MSG_DATA) {
5321 /* must still forward */
5322 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005323 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005324
5325 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005326 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005327 msg->msg_state = HTTP_MSG_DATA_CRLF;
5328 else
5329 msg->msg_state = HTTP_MSG_DONE;
5330 }
5331 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005332 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005333 * set ->sov and ->next to point to the body and switch to DATA or
5334 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005335 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005336 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005337
5338 if (!ret)
5339 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005340 else if (ret < 0) {
5341 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005342 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005343 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005344 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005345 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005346 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5348 /* we want the CRLF after the data */
5349 int ret;
5350
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005351 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352
5353 if (!ret)
5354 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005355 else if (ret < 0) {
5356 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005357 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005359 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 /* we're in MSG_CHUNK_SIZE now */
5361 }
5362 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005363 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005364
Willy Tarreaud98cf932009-12-27 22:54:55 +01005365 if (ret == 0)
5366 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005367 else if (ret < 0) {
5368 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005369 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005370 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005371 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005372 /* we're in HTTP_MSG_DONE now */
5373 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005375 int old_state = msg->msg_state;
5376
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005378 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005379 if ((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);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 if (http_resync_states(s)) {
5383 http_silent_debug(__LINE__, s);
5384 /* some state changes occurred, maybe the analyser
5385 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005386 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005387 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5388 if (res->flags & BF_SHUTW) {
5389 /* response errors are most likely due to
5390 * the client aborting the transfer.
5391 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005392 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005393 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005394 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005395 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005397 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005399 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005401 }
5402 }
5403
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005405 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005406 if (res->flags & BF_SHUTR) {
5407 if (!(s->flags & SN_ERR_MASK))
5408 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005409 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005410 if (target_srv(&s->target))
5411 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005412 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005413 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005414
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005415 if (res->flags & BF_SHUTW)
5416 goto aborted_xfer;
5417
Willy Tarreau40dba092010-03-04 18:14:51 +01005418 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 if (!s->req->analysers)
5420 goto return_bad_res;
5421
Willy Tarreauea953162012-05-18 23:41:28 +02005422 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005423 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005424 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005425 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005426 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005427 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005428 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429 }
5430
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005431 /* When TE: chunked is used, we need to get there again to parse remaining
5432 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5433 * Similarly, with keep-alive on the client side, we don't want to forward a
5434 * close.
5435 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005436 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005437 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5438 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5439 buffer_dont_close(res);
5440
Willy Tarreau5c620922011-05-11 19:56:11 +02005441 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005442 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5443 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005444 * modes are already handled by the stream sock layer. We must not do
5445 * this in content-length mode because it could present the MSG_MORE
5446 * flag with the last block of forwarded data, which would cause an
5447 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005448 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005449 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005450 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005451
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 return 0;
5455
Willy Tarreau40dba092010-03-04 18:14:51 +01005456 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005457 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005458 if (target_srv(&s->target))
5459 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005460
5461 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005462 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005463 /* don't send any error message as we're in the body */
5464 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005466 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005467 if (target_srv(&s->target))
5468 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005469
5470 if (!(s->flags & SN_ERR_MASK))
5471 s->flags |= SN_ERR_PRXCOND;
5472 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005473 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005474 return 0;
5475
5476 aborted_xfer:
5477 txn->rsp.msg_state = HTTP_MSG_ERROR;
5478 /* don't send any error message as we're in the body */
5479 stream_int_retnclose(res->cons, NULL);
5480 res->analysers = 0;
5481 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5482
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005483 s->fe->fe_counters.cli_aborts++;
5484 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005485 if (target_srv(&s->target))
5486 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005487
5488 if (!(s->flags & SN_ERR_MASK))
5489 s->flags |= SN_ERR_CLICL;
5490 if (!(s->flags & SN_FINST_MASK))
5491 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492 return 0;
5493}
5494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005495/* Iterate the same filter through all request headers.
5496 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497 * Since it can manage the switch to another backend, it updates the per-proxy
5498 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005499 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005500int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005501{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502 char term;
5503 char *cur_ptr, *cur_end, *cur_next;
5504 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005505 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005506 struct hdr_idx_elem *cur_hdr;
5507 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005509 last_hdr = 0;
5510
Willy Tarreau572bf902012-07-02 17:01:20 +02005511 cur_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005512 old_idx = 0;
5513
5514 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005515 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005517 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005518 (exp->action == ACT_ALLOW ||
5519 exp->action == ACT_DENY ||
5520 exp->action == ACT_TARPIT))
5521 return 0;
5522
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005523 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005524 if (!cur_idx)
5525 break;
5526
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005527 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 cur_ptr = cur_next;
5529 cur_end = cur_ptr + cur_hdr->len;
5530 cur_next = cur_end + cur_hdr->cr + 1;
5531
5532 /* Now we have one header between cur_ptr and cur_end,
5533 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005534 */
5535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005536 /* The annoying part is that pattern matching needs
5537 * that we modify the contents to null-terminate all
5538 * strings before testing them.
5539 */
5540
5541 term = *cur_end;
5542 *cur_end = '\0';
5543
5544 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5545 switch (exp->action) {
5546 case ACT_SETBE:
5547 /* It is not possible to jump a second time.
5548 * FIXME: should we return an HTTP/500 here so that
5549 * the admin knows there's a problem ?
5550 */
5551 if (t->be != t->fe)
5552 break;
5553
5554 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005555 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005556 last_hdr = 1;
5557 break;
5558
5559 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005560 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 last_hdr = 1;
5562 break;
5563
5564 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005565 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005566 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005567
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005568 t->fe->fe_counters.denied_req++;
5569 if (t->fe != t->be)
5570 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005571 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005572 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 break;
5575
5576 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005577 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005578 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005579
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005580 t->fe->fe_counters.denied_req++;
5581 if (t->fe != t->be)
5582 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005583 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005584 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 break;
5587
5588 case ACT_REPLACE:
5589 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5590 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5591 /* FIXME: if the user adds a newline in the replacement, the
5592 * index will not be recalculated for now, and the new line
5593 * will not be counted as a new header.
5594 */
5595
5596 cur_end += delta;
5597 cur_next += delta;
5598 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005599 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 break;
5601
5602 case ACT_REMOVE:
5603 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5604 cur_next += delta;
5605
Willy Tarreaufa355d42009-11-29 18:12:29 +01005606 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005607 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5608 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609 cur_hdr->len = 0;
5610 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005611 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005612 break;
5613
5614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005615 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 if (cur_end)
5617 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005619 /* keep the link from this header to next one in case of later
5620 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005621 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 old_idx = cur_idx;
5623 }
5624 return 0;
5625}
5626
5627
5628/* Apply the filter to the request line.
5629 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5630 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005631 * Since it can manage the switch to another backend, it updates the per-proxy
5632 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005633 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005634int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635{
5636 char term;
5637 char *cur_ptr, *cur_end;
5638 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005639 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005641
Willy Tarreau58f10d72006-12-04 02:26:12 +01005642
Willy Tarreau3d300592007-03-18 18:34:41 +01005643 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005645 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005646 (exp->action == ACT_ALLOW ||
5647 exp->action == ACT_DENY ||
5648 exp->action == ACT_TARPIT))
5649 return 0;
5650 else if (exp->action == ACT_REMOVE)
5651 return 0;
5652
5653 done = 0;
5654
Willy Tarreau572bf902012-07-02 17:01:20 +02005655 cur_ptr = req->buf.p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005656 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657
5658 /* Now we have the request line between cur_ptr and cur_end */
5659
5660 /* The annoying part is that pattern matching needs
5661 * that we modify the contents to null-terminate all
5662 * strings before testing them.
5663 */
5664
5665 term = *cur_end;
5666 *cur_end = '\0';
5667
5668 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5669 switch (exp->action) {
5670 case ACT_SETBE:
5671 /* It is not possible to jump a second time.
5672 * FIXME: should we return an HTTP/500 here so that
5673 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005674 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005675 if (t->be != t->fe)
5676 break;
5677
5678 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005679 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 done = 1;
5681 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005684 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 done = 1;
5686 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005689 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005690
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005691 t->fe->fe_counters.denied_req++;
5692 if (t->fe != t->be)
5693 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005694 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005695 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 done = 1;
5698 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005701 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005702
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005703 t->fe->fe_counters.denied_req++;
5704 if (t->fe != t->be)
5705 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005706 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005707 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 done = 1;
5710 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005711
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 case ACT_REPLACE:
5713 *cur_end = term; /* restore the string terminator */
5714 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5715 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5716 /* FIXME: if the user adds a newline in the replacement, the
5717 * index will not be recalculated for now, and the new line
5718 * will not be counted as a new header.
5719 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005720
Willy Tarreaufa355d42009-11-29 18:12:29 +01005721 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005722 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005723 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 HTTP_MSG_RQMETH,
5725 cur_ptr, cur_end + 1,
5726 NULL, NULL);
5727 if (unlikely(!cur_end))
5728 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 /* we have a full request and we know that we have either a CR
5731 * or an LF at <ptr>.
5732 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005733 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5734 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 /* there is no point trying this regex on headers */
5736 return 1;
5737 }
5738 }
5739 *cur_end = term; /* restore the string terminator */
5740 return done;
5741}
Willy Tarreau97de6242006-12-27 17:18:38 +01005742
Willy Tarreau58f10d72006-12-04 02:26:12 +01005743
Willy Tarreau58f10d72006-12-04 02:26:12 +01005744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005746 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005747 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005748 * unparsable request. Since it can manage the switch to another backend, it
5749 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005751int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005753 struct http_txn *txn = &s->txn;
5754 struct hdr_exp *exp;
5755
5756 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005757 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005759 /*
5760 * The interleaving of transformations and verdicts
5761 * makes it difficult to decide to continue or stop
5762 * the evaluation.
5763 */
5764
Willy Tarreau6c123b12010-01-28 20:22:06 +01005765 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5766 break;
5767
Willy Tarreau3d300592007-03-18 18:34:41 +01005768 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005769 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005770 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005772
5773 /* if this filter had a condition, evaluate it now and skip to
5774 * next filter if the condition does not match.
5775 */
5776 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005777 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005778 ret = acl_pass(ret);
5779 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5780 ret = !ret;
5781
5782 if (!ret)
5783 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784 }
5785
5786 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005787 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005788 if (unlikely(ret < 0))
5789 return -1;
5790
5791 if (likely(ret == 0)) {
5792 /* The filter did not match the request, it can be
5793 * iterated through all headers.
5794 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005795 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005797 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005799}
5800
5801
Willy Tarreaua15645d2007-03-18 16:22:39 +01005802
Willy Tarreau58f10d72006-12-04 02:26:12 +01005803/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005804 * Try to retrieve the server associated to the appsession.
5805 * If the server is found, it's assigned to the session.
5806 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005807void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005808 struct http_txn *txn = &t->txn;
5809 appsess *asession = NULL;
5810 char *sessid_temp = NULL;
5811
Cyril Bontéb21570a2009-11-29 20:04:48 +01005812 if (len > t->be->appsession_len) {
5813 len = t->be->appsession_len;
5814 }
5815
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005816 if (t->be->options2 & PR_O2_AS_REQL) {
5817 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005818 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005819 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005820 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005821 }
5822
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005823 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005824 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5825 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5826 return;
5827 }
5828
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005829 memcpy(txn->sessid, buf, len);
5830 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005831 }
5832
5833 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5834 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5835 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5836 return;
5837 }
5838
Cyril Bontéb21570a2009-11-29 20:04:48 +01005839 memcpy(sessid_temp, buf, len);
5840 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005841
5842 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5843 /* free previously allocated memory */
5844 pool_free2(apools.sessid, sessid_temp);
5845
5846 if (asession != NULL) {
5847 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5848 if (!(t->be->options2 & PR_O2_AS_REQL))
5849 asession->request_count++;
5850
5851 if (asession->serverid != NULL) {
5852 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005853
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005854 while (srv) {
5855 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005856 if ((srv->state & SRV_RUNNING) ||
5857 (t->be->options & PR_O_PERSIST) ||
5858 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005859 /* we found the server and it's usable */
5860 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005861 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005862 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005863 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005864
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005865 break;
5866 } else {
5867 txn->flags &= ~TX_CK_MASK;
5868 txn->flags |= TX_CK_DOWN;
5869 }
5870 }
5871 srv = srv->next;
5872 }
5873 }
5874 }
5875}
5876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005877/* Find the end of a cookie value contained between <s> and <e>. It works the
5878 * same way as with headers above except that the semi-colon also ends a token.
5879 * See RFC2965 for more information. Note that it requires a valid header to
5880 * return a valid result.
5881 */
5882char *find_cookie_value_end(char *s, const char *e)
5883{
5884 int quoted, qdpair;
5885
5886 quoted = qdpair = 0;
5887 for (; s < e; s++) {
5888 if (qdpair) qdpair = 0;
5889 else if (quoted) {
5890 if (*s == '\\') qdpair = 1;
5891 else if (*s == '"') quoted = 0;
5892 }
5893 else if (*s == '"') quoted = 1;
5894 else if (*s == ',' || *s == ';') return s;
5895 }
5896 return s;
5897}
5898
5899/* Delete a value in a header between delimiters <from> and <next> in buffer
5900 * <buf>. The number of characters displaced is returned, and the pointer to
5901 * the first delimiter is updated if required. The function tries as much as
5902 * possible to respect the following principles :
5903 * - replace <from> delimiter by the <next> one unless <from> points to a
5904 * colon, in which case <next> is simply removed
5905 * - set exactly one space character after the new first delimiter, unless
5906 * there are not enough characters in the block being moved to do so.
5907 * - remove unneeded spaces before the previous delimiter and after the new
5908 * one.
5909 *
5910 * It is the caller's responsibility to ensure that :
5911 * - <from> points to a valid delimiter or the colon ;
5912 * - <next> points to a valid delimiter or the final CR/LF ;
5913 * - there are non-space chars before <from> ;
5914 * - there is a CR/LF at or after <next>.
5915 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005916int del_hdr_value(struct channel *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917{
5918 char *prev = *from;
5919
5920 if (*prev == ':') {
5921 /* We're removing the first value, preserve the colon and add a
5922 * space if possible.
5923 */
5924 if (!http_is_crlf[(unsigned char)*next])
5925 next++;
5926 prev++;
5927 if (prev < next)
5928 *prev++ = ' ';
5929
5930 while (http_is_spht[(unsigned char)*next])
5931 next++;
5932 } else {
5933 /* Remove useless spaces before the old delimiter. */
5934 while (http_is_spht[(unsigned char)*(prev-1)])
5935 prev--;
5936 *from = prev;
5937
5938 /* copy the delimiter and if possible a space if we're
5939 * not at the end of the line.
5940 */
5941 if (!http_is_crlf[(unsigned char)*next]) {
5942 *prev++ = *next++;
5943 if (prev + 1 < next)
5944 *prev++ = ' ';
5945 while (http_is_spht[(unsigned char)*next])
5946 next++;
5947 }
5948 }
5949 return buffer_replace2(buf, prev, next, NULL, 0);
5950}
5951
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005952/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005953 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005954 * desirable to call it only when needed. This code is quite complex because
5955 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5956 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005958void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005960 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005961 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005963 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5964 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005966 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005967 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02005968 hdr_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005970 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005971 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005972 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005973
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005974 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005975 hdr_beg = hdr_next;
5976 hdr_end = hdr_beg + cur_hdr->len;
5977 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005978
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005979 /* We have one full header between hdr_beg and hdr_end, and the
5980 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005981 * "Cookie:" headers.
5982 */
5983
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005984 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005985 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986 old_idx = cur_idx;
5987 continue;
5988 }
5989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 del_from = NULL; /* nothing to be deleted */
5991 preserve_hdr = 0; /* assume we may kill the whole header */
5992
Willy Tarreau58f10d72006-12-04 02:26:12 +01005993 /* Now look for cookies. Conforming to RFC2109, we have to support
5994 * attributes whose name begin with a '$', and associate them with
5995 * the right cookie, if we want to delete this cookie.
5996 * So there are 3 cases for each cookie read :
5997 * 1) it's a special attribute, beginning with a '$' : ignore it.
5998 * 2) it's a server id cookie that we *MAY* want to delete : save
5999 * some pointers on it (last semi-colon, beginning of cookie...)
6000 * 3) it's an application cookie : we *MAY* have to delete a previous
6001 * "special" cookie.
6002 * At the end of loop, if a "special" cookie remains, we may have to
6003 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006004 * *MUST* delete it.
6005 *
6006 * Note: RFC2965 is unclear about the processing of spaces around
6007 * the equal sign in the ATTR=VALUE form. A careful inspection of
6008 * the RFC explicitly allows spaces before it, and not within the
6009 * tokens (attrs or values). An inspection of RFC2109 allows that
6010 * too but section 10.1.3 lets one think that spaces may be allowed
6011 * after the equal sign too, resulting in some (rare) buggy
6012 * implementations trying to do that. So let's do what servers do.
6013 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6014 * allowed quoted strings in values, with any possible character
6015 * after a backslash, including control chars and delimitors, which
6016 * causes parsing to become ambiguous. Browsers also allow spaces
6017 * within values even without quotes.
6018 *
6019 * We have to keep multiple pointers in order to support cookie
6020 * removal at the beginning, middle or end of header without
6021 * corrupting the header. All of these headers are valid :
6022 *
6023 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6024 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6025 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6026 * | | | | | | | | |
6027 * | | | | | | | | hdr_end <--+
6028 * | | | | | | | +--> next
6029 * | | | | | | +----> val_end
6030 * | | | | | +-----------> val_beg
6031 * | | | | +--------------> equal
6032 * | | | +----------------> att_end
6033 * | | +---------------------> att_beg
6034 * | +--------------------------> prev
6035 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036 */
6037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006038 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6039 /* Iterate through all cookies on this line */
6040
6041 /* find att_beg */
6042 att_beg = prev + 1;
6043 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6044 att_beg++;
6045
6046 /* find att_end : this is the first character after the last non
6047 * space before the equal. It may be equal to hdr_end.
6048 */
6049 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006050
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 while (equal < hdr_end) {
6052 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006053 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006054 if (http_is_spht[(unsigned char)*equal++])
6055 continue;
6056 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006057 }
6058
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6060 * is between <att_beg> and <equal>, both may be identical.
6061 */
6062
6063 /* look for end of cookie if there is an equal sign */
6064 if (equal < hdr_end && *equal == '=') {
6065 /* look for the beginning of the value */
6066 val_beg = equal + 1;
6067 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6068 val_beg++;
6069
6070 /* find the end of the value, respecting quotes */
6071 next = find_cookie_value_end(val_beg, hdr_end);
6072
6073 /* make val_end point to the first white space or delimitor after the value */
6074 val_end = next;
6075 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6076 val_end--;
6077 } else {
6078 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006081 /* We have nothing to do with attributes beginning with '$'. However,
6082 * they will automatically be removed if a header before them is removed,
6083 * since they're supposed to be linked together.
6084 */
6085 if (*att_beg == '$')
6086 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006088 /* Ignore cookies with no equal sign */
6089 if (equal == next) {
6090 /* This is not our cookie, so we must preserve it. But if we already
6091 * scheduled another cookie for removal, we cannot remove the
6092 * complete header, but we can remove the previous block itself.
6093 */
6094 preserve_hdr = 1;
6095 if (del_from != NULL) {
6096 int delta = del_hdr_value(req, &del_from, prev);
6097 val_end += delta;
6098 next += delta;
6099 hdr_end += delta;
6100 hdr_next += delta;
6101 cur_hdr->len += delta;
6102 http_msg_move_end(&txn->req, delta);
6103 prev = del_from;
6104 del_from = NULL;
6105 }
6106 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 /* if there are spaces around the equal sign, we need to
6110 * strip them otherwise we'll get trouble for cookie captures,
6111 * or even for rewrites. Since this happens extremely rarely,
6112 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6115 int stripped_before = 0;
6116 int stripped_after = 0;
6117
6118 if (att_end != equal) {
6119 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6120 equal += stripped_before;
6121 val_beg += stripped_before;
6122 }
6123
6124 if (val_beg > equal + 1) {
6125 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6126 val_beg += stripped_after;
6127 stripped_before += stripped_after;
6128 }
6129
6130 val_end += stripped_before;
6131 next += stripped_before;
6132 hdr_end += stripped_before;
6133 hdr_next += stripped_before;
6134 cur_hdr->len += stripped_before;
6135 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 /* First, let's see if we want to capture this cookie. We check
6140 * that we don't already have a client side cookie, because we
6141 * can only capture one. Also as an optimisation, we ignore
6142 * cookies shorter than the declared name.
6143 */
6144 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6145 (val_end - att_beg >= t->fe->capture_namelen) &&
6146 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6147 int log_len = val_end - att_beg;
6148
6149 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6150 Alert("HTTP logging : out of memory.\n");
6151 } else {
6152 if (log_len > t->fe->capture_len)
6153 log_len = t->fe->capture_len;
6154 memcpy(txn->cli_cookie, att_beg, log_len);
6155 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006156 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006157 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006158
Willy Tarreaubca99692010-10-06 19:25:55 +02006159 /* Persistence cookies in passive, rewrite or insert mode have the
6160 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006162 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006163 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006164 * For cookies in prefix mode, the form is :
6165 *
6166 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006167 */
6168 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6169 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6170 struct server *srv = t->be->srv;
6171 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006172
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6174 * have the server ID between val_beg and delim, and the original cookie between
6175 * delim+1 and val_end. Otherwise, delim==val_end :
6176 *
6177 * Cookie: NAME=SRV; # in all but prefix modes
6178 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6179 * | || || | |+-> next
6180 * | || || | +--> val_end
6181 * | || || +---------> delim
6182 * | || |+------------> val_beg
6183 * | || +-------------> att_end = equal
6184 * | |+-----------------> att_beg
6185 * | +------------------> prev
6186 * +-------------------------> hdr_beg
6187 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006188
Willy Tarreau67402132012-05-31 20:40:20 +02006189 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 for (delim = val_beg; delim < val_end; delim++)
6191 if (*delim == COOKIE_DELIM)
6192 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006193 } else {
6194 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006196 /* Now check if the cookie contains a date field, which would
6197 * appear after a vertical bar ('|') just after the server name
6198 * and before the delimiter.
6199 */
6200 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6201 if (vbar1) {
6202 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006203 * right is the last seen date. It is a base64 encoded
6204 * 30-bit value representing the UNIX date since the
6205 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006206 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006207 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006208 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006209 if (val_end - vbar1 >= 5) {
6210 val = b64tos30(vbar1);
6211 if (val > 0)
6212 txn->cookie_last_date = val << 2;
6213 }
6214 /* look for a second vertical bar */
6215 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6216 if (vbar1 && (val_end - vbar1 > 5)) {
6217 val = b64tos30(vbar1 + 1);
6218 if (val > 0)
6219 txn->cookie_first_date = val << 2;
6220 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006221 }
6222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006223
Willy Tarreauf64d1412010-10-07 20:06:11 +02006224 /* if the cookie has an expiration date and the proxy wants to check
6225 * it, then we do that now. We first check if the cookie is too old,
6226 * then only if it has expired. We detect strict overflow because the
6227 * time resolution here is not great (4 seconds). Cookies with dates
6228 * in the future are ignored if their offset is beyond one day. This
6229 * allows an admin to fix timezone issues without expiring everyone
6230 * and at the same time avoids keeping unwanted side effects for too
6231 * long.
6232 */
6233 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006234 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6235 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006236 txn->flags &= ~TX_CK_MASK;
6237 txn->flags |= TX_CK_OLD;
6238 delim = val_beg; // let's pretend we have not found the cookie
6239 txn->cookie_first_date = 0;
6240 txn->cookie_last_date = 0;
6241 }
6242 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006243 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6244 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006245 txn->flags &= ~TX_CK_MASK;
6246 txn->flags |= TX_CK_EXPIRED;
6247 delim = val_beg; // let's pretend we have not found the cookie
6248 txn->cookie_first_date = 0;
6249 txn->cookie_last_date = 0;
6250 }
6251
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252 /* Here, we'll look for the first running server which supports the cookie.
6253 * This allows to share a same cookie between several servers, for example
6254 * to dedicate backup servers to specific servers only.
6255 * However, to prevent clients from sticking to cookie-less backup server
6256 * when they have incidentely learned an empty cookie, we simply ignore
6257 * empty cookies and mark them as invalid.
6258 * The same behaviour is applied when persistence must be ignored.
6259 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006260 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 while (srv) {
6264 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6265 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6266 if ((srv->state & SRV_RUNNING) ||
6267 (t->be->options & PR_O_PERSIST) ||
6268 (t->flags & SN_FORCE_PRST)) {
6269 /* we found the server and we can use it */
6270 txn->flags &= ~TX_CK_MASK;
6271 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6272 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006273 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 break;
6275 } else {
6276 /* we found a server, but it's down,
6277 * mark it as such and go on in case
6278 * another one is available.
6279 */
6280 txn->flags &= ~TX_CK_MASK;
6281 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 srv = srv->next;
6285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286
Willy Tarreauf64d1412010-10-07 20:06:11 +02006287 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006288 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006290 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6291 txn->flags |= TX_CK_UNUSED;
6292 else
6293 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006294 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 /* depending on the cookie mode, we may have to either :
6297 * - delete the complete cookie if we're in insert+indirect mode, so that
6298 * the server never sees it ;
6299 * - remove the server id from the cookie value, and tag the cookie as an
6300 * application cookie so that it does not get accidentely removed later,
6301 * if we're in cookie prefix mode
6302 */
Willy Tarreau67402132012-05-31 20:40:20 +02006303 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6307 val_end += delta;
6308 next += delta;
6309 hdr_end += delta;
6310 hdr_next += delta;
6311 cur_hdr->len += delta;
6312 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006314 del_from = NULL;
6315 preserve_hdr = 1; /* we want to keep this cookie */
6316 }
6317 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006318 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006319 del_from = prev;
6320 }
6321 } else {
6322 /* This is not our cookie, so we must preserve it. But if we already
6323 * scheduled another cookie for removal, we cannot remove the
6324 * complete header, but we can remove the previous block itself.
6325 */
6326 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 if (del_from != NULL) {
6329 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006330 if (att_beg >= del_from)
6331 att_beg += delta;
6332 if (att_end >= del_from)
6333 att_end += delta;
6334 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 val_end += delta;
6336 next += delta;
6337 hdr_end += delta;
6338 hdr_next += delta;
6339 cur_hdr->len += delta;
6340 http_msg_move_end(&txn->req, delta);
6341 prev = del_from;
6342 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 /* Look for the appsession cookie unless persistence must be ignored */
6347 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6348 int cmp_len, value_len;
6349 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006350
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 if (t->be->options2 & PR_O2_AS_PFX) {
6352 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6353 value_begin = att_beg + t->be->appsession_name_len;
6354 value_len = val_end - att_beg - t->be->appsession_name_len;
6355 } else {
6356 cmp_len = att_end - att_beg;
6357 value_begin = val_beg;
6358 value_len = val_end - val_beg;
6359 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006360
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 /* let's see if the cookie is our appcookie */
6362 if (cmp_len == t->be->appsession_name_len &&
6363 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6364 manage_client_side_appsession(t, value_begin, value_len);
6365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366 }
6367
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006368 /* continue with next cookie on this header line */
6369 att_beg = next;
6370 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006371
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 /* There are no more cookies on this line.
6373 * We may still have one (or several) marked for deletion at the
6374 * end of the line. We must do this now in two ways :
6375 * - if some cookies must be preserved, we only delete from the
6376 * mark to the end of line ;
6377 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006379 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 if (preserve_hdr) {
6382 delta = del_hdr_value(req, &del_from, hdr_end);
6383 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384 cur_hdr->len += delta;
6385 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006387
6388 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006389 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6390 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006392 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006395 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396 }
6397
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006399 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401}
6402
6403
Willy Tarreaua15645d2007-03-18 16:22:39 +01006404/* Iterate the same filter through all response headers contained in <rtr>.
6405 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6406 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006407int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006408{
6409 char term;
6410 char *cur_ptr, *cur_end, *cur_next;
6411 int cur_idx, old_idx, last_hdr;
6412 struct http_txn *txn = &t->txn;
6413 struct hdr_idx_elem *cur_hdr;
6414 int len, delta;
6415
6416 last_hdr = 0;
6417
Willy Tarreau572bf902012-07-02 17:01:20 +02006418 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006419 old_idx = 0;
6420
6421 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006422 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006424 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006425 (exp->action == ACT_ALLOW ||
6426 exp->action == ACT_DENY))
6427 return 0;
6428
6429 cur_idx = txn->hdr_idx.v[old_idx].next;
6430 if (!cur_idx)
6431 break;
6432
6433 cur_hdr = &txn->hdr_idx.v[cur_idx];
6434 cur_ptr = cur_next;
6435 cur_end = cur_ptr + cur_hdr->len;
6436 cur_next = cur_end + cur_hdr->cr + 1;
6437
6438 /* Now we have one header between cur_ptr and cur_end,
6439 * and the next header starts at cur_next.
6440 */
6441
6442 /* The annoying part is that pattern matching needs
6443 * that we modify the contents to null-terminate all
6444 * strings before testing them.
6445 */
6446
6447 term = *cur_end;
6448 *cur_end = '\0';
6449
6450 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6451 switch (exp->action) {
6452 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006453 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 last_hdr = 1;
6455 break;
6456
6457 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006458 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459 last_hdr = 1;
6460 break;
6461
6462 case ACT_REPLACE:
6463 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6464 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6465 /* FIXME: if the user adds a newline in the replacement, the
6466 * index will not be recalculated for now, and the new line
6467 * will not be counted as a new header.
6468 */
6469
6470 cur_end += delta;
6471 cur_next += delta;
6472 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006473 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 break;
6475
6476 case ACT_REMOVE:
6477 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6478 cur_next += delta;
6479
Willy Tarreaufa355d42009-11-29 18:12:29 +01006480 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006481 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6482 txn->hdr_idx.used--;
6483 cur_hdr->len = 0;
6484 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006485 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006486 break;
6487
6488 }
6489 }
6490 if (cur_end)
6491 *cur_end = term; /* restore the string terminator */
6492
6493 /* keep the link from this header to next one in case of later
6494 * removal of next header.
6495 */
6496 old_idx = cur_idx;
6497 }
6498 return 0;
6499}
6500
6501
6502/* Apply the filter to the status line in the response buffer <rtr>.
6503 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6504 * or -1 if a replacement resulted in an invalid status line.
6505 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006506int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507{
6508 char term;
6509 char *cur_ptr, *cur_end;
6510 int done;
6511 struct http_txn *txn = &t->txn;
6512 int len, delta;
6513
6514
Willy Tarreau3d300592007-03-18 18:34:41 +01006515 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006517 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006518 (exp->action == ACT_ALLOW ||
6519 exp->action == ACT_DENY))
6520 return 0;
6521 else if (exp->action == ACT_REMOVE)
6522 return 0;
6523
6524 done = 0;
6525
Willy Tarreau572bf902012-07-02 17:01:20 +02006526 cur_ptr = rtr->buf.p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006527 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528
6529 /* Now we have the status line between cur_ptr and cur_end */
6530
6531 /* The annoying part is that pattern matching needs
6532 * that we modify the contents to null-terminate all
6533 * strings before testing them.
6534 */
6535
6536 term = *cur_end;
6537 *cur_end = '\0';
6538
6539 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6540 switch (exp->action) {
6541 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006542 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543 done = 1;
6544 break;
6545
6546 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006547 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548 done = 1;
6549 break;
6550
6551 case ACT_REPLACE:
6552 *cur_end = term; /* restore the string terminator */
6553 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6554 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6555 /* FIXME: if the user adds a newline in the replacement, the
6556 * index will not be recalculated for now, and the new line
6557 * will not be counted as a new header.
6558 */
6559
Willy Tarreaufa355d42009-11-29 18:12:29 +01006560 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006562 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006563 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564 cur_ptr, cur_end + 1,
6565 NULL, NULL);
6566 if (unlikely(!cur_end))
6567 return -1;
6568
6569 /* we have a full respnse and we know that we have either a CR
6570 * or an LF at <ptr>.
6571 */
Willy Tarreau572bf902012-07-02 17:01:20 +02006572 txn->status = strl2ui(rtr->buf.p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006573 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006574 /* there is no point trying this regex on headers */
6575 return 1;
6576 }
6577 }
6578 *cur_end = term; /* restore the string terminator */
6579 return done;
6580}
6581
6582
6583
6584/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006585 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6587 * unparsable response.
6588 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006589int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006591 struct http_txn *txn = &s->txn;
6592 struct hdr_exp *exp;
6593
6594 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 int ret;
6596
6597 /*
6598 * The interleaving of transformations and verdicts
6599 * makes it difficult to decide to continue or stop
6600 * the evaluation.
6601 */
6602
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006603 if (txn->flags & TX_SVDENY)
6604 break;
6605
Willy Tarreau3d300592007-03-18 18:34:41 +01006606 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6608 exp->action == ACT_PASS)) {
6609 exp = exp->next;
6610 continue;
6611 }
6612
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006613 /* if this filter had a condition, evaluate it now and skip to
6614 * next filter if the condition does not match.
6615 */
6616 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006617 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006618 ret = acl_pass(ret);
6619 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6620 ret = !ret;
6621 if (!ret)
6622 continue;
6623 }
6624
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006626 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 if (unlikely(ret < 0))
6628 return -1;
6629
6630 if (likely(ret == 0)) {
6631 /* The filter did not match the response, it can be
6632 * iterated through all headers.
6633 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006634 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 }
6637 return 0;
6638}
6639
6640
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006642 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006643 * desirable to call it only when needed. This function is also used when we
6644 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006646void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647{
6648 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006649 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006651 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006652 char *hdr_beg, *hdr_end, *hdr_next;
6653 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 /* Iterate through the headers.
6656 * we start with the start line.
6657 */
6658 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02006659 hdr_next = res->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660
6661 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6662 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006663 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664
6665 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006666 hdr_beg = hdr_next;
6667 hdr_end = hdr_beg + cur_hdr->len;
6668 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669
Willy Tarreau24581ba2010-08-31 22:39:35 +02006670 /* We have one full header between hdr_beg and hdr_end, and the
6671 * next header starts at hdr_next. We're only interested in
6672 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673 */
6674
Willy Tarreau24581ba2010-08-31 22:39:35 +02006675 is_cookie2 = 0;
6676 prev = hdr_beg + 10;
6677 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006678 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006679 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6680 if (!val) {
6681 old_idx = cur_idx;
6682 continue;
6683 }
6684 is_cookie2 = 1;
6685 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686 }
6687
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6689 * <prev> points to the colon.
6690 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006691 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692
Willy Tarreau24581ba2010-08-31 22:39:35 +02006693 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6694 * check-cache is enabled) and we are not interested in checking
6695 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006696 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006697 if (t->be->cookie_name == NULL &&
6698 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006699 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700 return;
6701
Willy Tarreau24581ba2010-08-31 22:39:35 +02006702 /* OK so now we know we have to process this response cookie.
6703 * The format of the Set-Cookie header is slightly different
6704 * from the format of the Cookie header in that it does not
6705 * support the comma as a cookie delimiter (thus the header
6706 * cannot be folded) because the Expires attribute described in
6707 * the original Netscape's spec may contain an unquoted date
6708 * with a comma inside. We have to live with this because
6709 * many browsers don't support Max-Age and some browsers don't
6710 * support quoted strings. However the Set-Cookie2 header is
6711 * clean.
6712 *
6713 * We have to keep multiple pointers in order to support cookie
6714 * removal at the beginning, middle or end of header without
6715 * corrupting the header (in case of set-cookie2). A special
6716 * pointer, <scav> points to the beginning of the set-cookie-av
6717 * fields after the first semi-colon. The <next> pointer points
6718 * either to the end of line (set-cookie) or next unquoted comma
6719 * (set-cookie2). All of these headers are valid :
6720 *
6721 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6722 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6723 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6724 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6725 * | | | | | | | | | |
6726 * | | | | | | | | +-> next hdr_end <--+
6727 * | | | | | | | +------------> scav
6728 * | | | | | | +--------------> val_end
6729 * | | | | | +--------------------> val_beg
6730 * | | | | +----------------------> equal
6731 * | | | +------------------------> att_end
6732 * | | +----------------------------> att_beg
6733 * | +------------------------------> prev
6734 * +-----------------------------------------> hdr_beg
6735 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736
Willy Tarreau24581ba2010-08-31 22:39:35 +02006737 for (; prev < hdr_end; prev = next) {
6738 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
Willy Tarreau24581ba2010-08-31 22:39:35 +02006740 /* find att_beg */
6741 att_beg = prev + 1;
6742 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6743 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744
Willy Tarreau24581ba2010-08-31 22:39:35 +02006745 /* find att_end : this is the first character after the last non
6746 * space before the equal. It may be equal to hdr_end.
6747 */
6748 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 while (equal < hdr_end) {
6751 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6752 break;
6753 if (http_is_spht[(unsigned char)*equal++])
6754 continue;
6755 att_end = equal;
6756 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006757
Willy Tarreau24581ba2010-08-31 22:39:35 +02006758 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6759 * is between <att_beg> and <equal>, both may be identical.
6760 */
6761
6762 /* look for end of cookie if there is an equal sign */
6763 if (equal < hdr_end && *equal == '=') {
6764 /* look for the beginning of the value */
6765 val_beg = equal + 1;
6766 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6767 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768
Willy Tarreau24581ba2010-08-31 22:39:35 +02006769 /* find the end of the value, respecting quotes */
6770 next = find_cookie_value_end(val_beg, hdr_end);
6771
6772 /* make val_end point to the first white space or delimitor after the value */
6773 val_end = next;
6774 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6775 val_end--;
6776 } else {
6777 /* <equal> points to next comma, semi-colon or EOL */
6778 val_beg = val_end = next = equal;
6779 }
6780
6781 if (next < hdr_end) {
6782 /* Set-Cookie2 supports multiple cookies, and <next> points to
6783 * a colon or semi-colon before the end. So skip all attr-value
6784 * pairs and look for the next comma. For Set-Cookie, since
6785 * commas are permitted in values, skip to the end.
6786 */
6787 if (is_cookie2)
6788 next = find_hdr_value_end(next, hdr_end);
6789 else
6790 next = hdr_end;
6791 }
6792
6793 /* Now everything is as on the diagram above */
6794
6795 /* Ignore cookies with no equal sign */
6796 if (equal == val_end)
6797 continue;
6798
6799 /* If there are spaces around the equal sign, we need to
6800 * strip them otherwise we'll get trouble for cookie captures,
6801 * or even for rewrites. Since this happens extremely rarely,
6802 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006804 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6805 int stripped_before = 0;
6806 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807
Willy Tarreau24581ba2010-08-31 22:39:35 +02006808 if (att_end != equal) {
6809 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6810 equal += stripped_before;
6811 val_beg += stripped_before;
6812 }
6813
6814 if (val_beg > equal + 1) {
6815 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6816 val_beg += stripped_after;
6817 stripped_before += stripped_after;
6818 }
6819
6820 val_end += stripped_before;
6821 next += stripped_before;
6822 hdr_end += stripped_before;
6823 hdr_next += stripped_before;
6824 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006825 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 }
6827
6828 /* First, let's see if we want to capture this cookie. We check
6829 * that we don't already have a server side cookie, because we
6830 * can only capture one. Also as an optimisation, we ignore
6831 * cookies shorter than the declared name.
6832 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006833 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006834 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 (val_end - att_beg >= t->fe->capture_namelen) &&
6836 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6837 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006838 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 Alert("HTTP logging : out of memory.\n");
6840 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006841 else {
6842 if (log_len > t->fe->capture_len)
6843 log_len = t->fe->capture_len;
6844 memcpy(txn->srv_cookie, att_beg, log_len);
6845 txn->srv_cookie[log_len] = 0;
6846 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 }
6848
Willy Tarreau827aee92011-03-10 16:55:02 +01006849 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 if (!(t->flags & SN_IGNORE_PRST) &&
6852 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6853 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006854 /* assume passive cookie by default */
6855 txn->flags &= ~TX_SCK_MASK;
6856 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857
6858 /* If the cookie is in insert mode on a known server, we'll delete
6859 * this occurrence because we'll insert another one later.
6860 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 * a direct access.
6862 */
Willy Tarreau67402132012-05-31 20:40:20 +02006863 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006864 /* The "preserve" flag was set, we don't want to touch the
6865 * server's cookie.
6866 */
6867 }
Willy Tarreau67402132012-05-31 20:40:20 +02006868 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
6869 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006870 /* this cookie must be deleted */
6871 if (*prev == ':' && next == hdr_end) {
6872 /* whole header */
6873 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6874 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6875 txn->hdr_idx.used--;
6876 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006877 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006878 hdr_next += delta;
6879 http_msg_move_end(&txn->rsp, delta);
6880 /* note: while both invalid now, <next> and <hdr_end>
6881 * are still equal, so the for() will stop as expected.
6882 */
6883 } else {
6884 /* just remove the value */
6885 int delta = del_hdr_value(res, &prev, next);
6886 next = prev;
6887 hdr_end += delta;
6888 hdr_next += delta;
6889 cur_hdr->len += delta;
6890 http_msg_move_end(&txn->rsp, delta);
6891 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006892 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006893 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 }
Willy Tarreau67402132012-05-31 20:40:20 +02006896 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 * with this server since we know it.
6899 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006900 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006901 next += delta;
6902 hdr_end += delta;
6903 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006905 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906
Willy Tarreauf1348312010-10-07 15:54:11 +02006907 txn->flags &= ~TX_SCK_MASK;
6908 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909 }
Willy Tarreaua0590312012-06-06 16:07:00 +02006910 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006912 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006914 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006915 next += delta;
6916 hdr_end += delta;
6917 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006919 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920
Willy Tarreau827aee92011-03-10 16:55:02 +01006921 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006922 txn->flags &= ~TX_SCK_MASK;
6923 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006924 }
6925 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006926 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6927 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006928 int cmp_len, value_len;
6929 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930
Cyril Bontéb21570a2009-11-29 20:04:48 +01006931 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006932 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6933 value_begin = att_beg + t->be->appsession_name_len;
6934 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006935 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006936 cmp_len = att_end - att_beg;
6937 value_begin = val_beg;
6938 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006940
Cyril Bonté17530c32010-04-06 21:11:10 +02006941 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6943 /* free a possibly previously allocated memory */
6944 pool_free2(apools.sessid, txn->sessid);
6945
Cyril Bontéb21570a2009-11-29 20:04:48 +01006946 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006947 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006948 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6949 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6950 return;
6951 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006952 memcpy(txn->sessid, value_begin, value_len);
6953 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 }
6956 /* that's done for this cookie, check the next one on the same
6957 * line when next != hdr_end (only if is_cookie2).
6958 */
6959 }
6960 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006963
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006964 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965 appsess *asession = NULL;
6966 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006967 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006968 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006969 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006970 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6971 Alert("Not enough Memory process_srv():asession:calloc().\n");
6972 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6973 return;
6974 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006975 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6976
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006977 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6978 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6979 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006980 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006981 return;
6982 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006983 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006984 asession->sessid[t->be->appsession_len] = 0;
6985
Willy Tarreau827aee92011-03-10 16:55:02 +01006986 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006987 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006988 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006989 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006990 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006991 return;
6992 }
6993 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006994 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006995
6996 asession->request_count = 0;
6997 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6998 }
6999
7000 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7001 asession->request_count++;
7002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003}
7004
7005
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006/*
7007 * Check if response is cacheable or not. Updates t->flags.
7008 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007009void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010{
7011 struct http_txn *txn = &t->txn;
7012 char *p1, *p2;
7013
7014 char *cur_ptr, *cur_end, *cur_next;
7015 int cur_idx;
7016
Willy Tarreau5df51872007-11-25 16:20:08 +01007017 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018 return;
7019
7020 /* Iterate through the headers.
7021 * we start with the start line.
7022 */
7023 cur_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007024 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025
7026 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7027 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007028 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029
7030 cur_hdr = &txn->hdr_idx.v[cur_idx];
7031 cur_ptr = cur_next;
7032 cur_end = cur_ptr + cur_hdr->len;
7033 cur_next = cur_end + cur_hdr->cr + 1;
7034
7035 /* We have one full header between cur_ptr and cur_end, and the
7036 * next header starts at cur_next. We're only interested in
7037 * "Cookie:" headers.
7038 */
7039
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007040 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7041 if (val) {
7042 if ((cur_end - (cur_ptr + val) >= 8) &&
7043 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7044 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7045 return;
7046 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 }
7048
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007049 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7050 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051 continue;
7052
7053 /* OK, right now we know we have a cache-control header at cur_ptr */
7054
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007055 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056
7057 if (p1 >= cur_end) /* no more info */
7058 continue;
7059
7060 /* p1 is at the beginning of the value */
7061 p2 = p1;
7062
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007063 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064 p2++;
7065
7066 /* we have a complete value between p1 and p2 */
7067 if (p2 < cur_end && *p2 == '=') {
7068 /* we have something of the form no-cache="set-cookie" */
7069 if ((cur_end - p1 >= 21) &&
7070 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7071 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007072 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 continue;
7074 }
7075
7076 /* OK, so we know that either p2 points to the end of string or to a comma */
7077 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7078 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7079 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7080 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007081 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 return;
7083 }
7084
7085 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007086 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007087 continue;
7088 }
7089 }
7090}
7091
7092
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093/*
7094 * Try to retrieve a known appsession in the URI, then the associated server.
7095 * If the server is found, it's assigned to the session.
7096 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007099 char *end_params, *first_param, *cur_param, *next_param;
7100 char separator;
7101 int value_len;
7102
7103 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007105 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007106 (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 +01007107 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
Cyril Bontéb21570a2009-11-29 20:04:48 +01007110 first_param = NULL;
7111 switch (mode) {
7112 case PR_O2_AS_M_PP:
7113 first_param = memchr(begin, ';', len);
7114 break;
7115 case PR_O2_AS_M_QS:
7116 first_param = memchr(begin, '?', len);
7117 break;
7118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119
Cyril Bontéb21570a2009-11-29 20:04:48 +01007120 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123
Cyril Bontéb21570a2009-11-29 20:04:48 +01007124 switch (mode) {
7125 case PR_O2_AS_M_PP:
7126 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7127 end_params = (char *) begin + len;
7128 }
7129 separator = ';';
7130 break;
7131 case PR_O2_AS_M_QS:
7132 end_params = (char *) begin + len;
7133 separator = '&';
7134 break;
7135 default:
7136 /* unknown mode, shouldn't happen */
7137 return;
7138 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007139
Cyril Bontéb21570a2009-11-29 20:04:48 +01007140 cur_param = next_param = end_params;
7141 while (cur_param > first_param) {
7142 cur_param--;
7143 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7144 /* let's see if this is the appsession parameter */
7145 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7146 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7147 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7148 /* Cool... it's the right one */
7149 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7150 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7151 if (value_len > 0) {
7152 manage_client_side_appsession(t, cur_param, value_len);
7153 }
7154 break;
7155 }
7156 next_param = cur_param;
7157 }
7158 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007160 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007161 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163}
7164
Willy Tarreaub2513902006-12-17 14:52:38 +01007165/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007166 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007167 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007168 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007169 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007170 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007171 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007172 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007173 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007174int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007175{
7176 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007177 struct http_msg *msg = &txn->req;
Willy Tarreau572bf902012-07-02 17:01:20 +02007178 const char *uri = msg->buf->buf.p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007179 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007180
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007181 if (!uri_auth)
7182 return 0;
7183
Cyril Bonté70be45d2010-10-12 00:14:35 +02007184 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007185 return 0;
7186
Willy Tarreau295a8372011-03-10 11:25:07 +01007187 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007188 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007190 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007191 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007192 return 0;
7193
Willy Tarreau3a215be2012-03-09 21:39:51 +01007194 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007195 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007196 return 0;
7197
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007198 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007199 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007200 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007201 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007202 break;
7203 }
7204 h++;
7205 }
7206
7207 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007208 h = uri + uri_auth->uri_len;
7209 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007210 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007211 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007212 break;
7213 }
7214 h++;
7215 }
7216 }
7217
Willy Tarreau3a215be2012-03-09 21:39:51 +01007218 h = uri + uri_auth->uri_len;
7219 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007220 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007221 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007222 break;
7223 }
7224 h++;
7225 }
7226
Willy Tarreau3a215be2012-03-09 21:39:51 +01007227 h = uri + uri_auth->uri_len;
7228 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007229 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007230 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007231 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007232 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007233 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7234 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7235 si->applet.ctx.stats.st_code = i;
7236 break;
7237 }
7238 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007239 break;
7240 }
7241 h++;
7242 }
7243
Willy Tarreau295a8372011-03-10 11:25:07 +01007244 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007245
Willy Tarreaub2513902006-12-17 14:52:38 +01007246 return 1;
7247}
7248
Willy Tarreau4076a152009-04-02 15:18:36 +02007249/*
7250 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007251 * By default it tries to report the error position as msg->err_pos. However if
7252 * this one is not set, it will then report msg->next, which is the last known
7253 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreau572bf902012-07-02 17:01:20 +02007254 * displays buffers as a contiguous area starting at buf->buf.p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007255 */
7256void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007257 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007258 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007259{
Willy Tarreau7421efb2012-07-02 15:11:27 +02007260 struct channel *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007261 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007262
Willy Tarreau572bf902012-07-02 17:01:20 +02007263 es->len = MIN(buf->buf.i, sizeof(es->buf));
7264 len1 = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007265 len1 = MIN(len1, es->len);
7266 len2 = es->len - len1; /* remaining data if buffer wraps */
7267
Willy Tarreau572bf902012-07-02 17:01:20 +02007268 memcpy(es->buf, buf->buf.p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007269 if (len2)
Willy Tarreau572bf902012-07-02 17:01:20 +02007270 memcpy(es->buf + len1, buf->buf.data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007271
Willy Tarreau4076a152009-04-02 15:18:36 +02007272 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007273 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007274 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007275 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007276
Willy Tarreau4076a152009-04-02 15:18:36 +02007277 es->when = date; // user-visible date
7278 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007279 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007280 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007281 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007282 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007283 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007284 es->b_flags = buf->flags;
7285 es->s_flags = s->flags;
7286 es->t_flags = s->txn.flags;
7287 es->m_flags = msg->flags;
Willy Tarreau572bf902012-07-02 17:01:20 +02007288 es->b_out = buf->buf.o;
7289 es->b_wrap = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007290 es->b_tot = buf->total;
7291 es->m_clen = msg->chunk_len;
7292 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007293}
Willy Tarreaub2513902006-12-17 14:52:38 +01007294
Willy Tarreau294c4732011-12-16 21:35:50 +01007295/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7296 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7297 * performed over the whole headers. Otherwise it must contain a valid header
7298 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7299 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7300 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7301 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7302 * -1.
7303 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007304 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007305unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007306 struct hdr_idx *idx, int occ,
7307 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007308{
Willy Tarreau294c4732011-12-16 21:35:50 +01007309 struct hdr_ctx local_ctx;
7310 char *ptr_hist[MAX_HDR_HISTORY];
7311 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007312 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007313 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007314
Willy Tarreau294c4732011-12-16 21:35:50 +01007315 if (!ctx) {
7316 local_ctx.idx = 0;
7317 ctx = &local_ctx;
7318 }
7319
Willy Tarreaubce70882009-09-07 11:51:47 +02007320 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007321 /* search from the beginning */
Willy Tarreau572bf902012-07-02 17:01:20 +02007322 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007323 occ--;
7324 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007325 *vptr = ctx->line + ctx->val;
7326 *vlen = ctx->vlen;
7327 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007328 }
7329 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007330 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007331 }
7332
7333 /* negative occurrence, we scan all the list then walk back */
7334 if (-occ > MAX_HDR_HISTORY)
7335 return 0;
7336
Willy Tarreau294c4732011-12-16 21:35:50 +01007337 found = hist_ptr = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007338 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007339 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7340 len_hist[hist_ptr] = ctx->vlen;
7341 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007342 hist_ptr = 0;
7343 found++;
7344 }
7345 if (-occ > found)
7346 return 0;
7347 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7348 * find occurrence -occ, so we have to check [hist_ptr+occ].
7349 */
7350 hist_ptr += occ;
7351 if (hist_ptr >= MAX_HDR_HISTORY)
7352 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007353 *vptr = ptr_hist[hist_ptr];
7354 *vlen = len_hist[hist_ptr];
7355 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007356}
7357
Willy Tarreaubaaee002006-06-26 02:48:02 +02007358/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359 * Print a debug line with a header
7360 */
7361void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7362{
7363 int len, max;
7364 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007365 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366 max = end - start;
David du Colombier7af46052012-05-16 14:16:48 +02007367 UBOUND(max, trashlen - len - 1);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368 len += strlcpy2(trash + len, start, max + 1);
7369 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007370 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007371}
7372
Willy Tarreau0937bc42009-12-22 15:03:09 +01007373/*
7374 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7375 * the required fields are properly allocated and that we only need to (re)init
7376 * them. This should be used before processing any new request.
7377 */
7378void http_init_txn(struct session *s)
7379{
7380 struct http_txn *txn = &s->txn;
7381 struct proxy *fe = s->fe;
7382
7383 txn->flags = 0;
7384 txn->status = -1;
7385
William Lallemand5f232402012-04-05 18:02:55 +02007386 global.req_count++;
7387
Willy Tarreauf64d1412010-10-07 20:06:11 +02007388 txn->cookie_first_date = 0;
7389 txn->cookie_last_date = 0;
7390
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007391 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007392 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007393 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007394 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007395 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007396 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007397 txn->req.chunk_len = 0LL;
7398 txn->req.body_len = 0LL;
7399 txn->rsp.chunk_len = 0LL;
7400 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007401 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7402 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007403 txn->req.buf = s->req;
7404 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007405
7406 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007407
7408 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7409 if (fe->options2 & PR_O2_REQBUG_OK)
7410 txn->req.err_pos = -1; /* let buggy requests pass */
7411
Willy Tarreau46023632010-01-07 22:51:47 +01007412 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007413 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7414
Willy Tarreau46023632010-01-07 22:51:47 +01007415 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007416 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7417
7418 if (txn->hdr_idx.v)
7419 hdr_idx_init(&txn->hdr_idx);
7420}
7421
7422/* to be used at the end of a transaction */
7423void http_end_txn(struct session *s)
7424{
7425 struct http_txn *txn = &s->txn;
7426
7427 /* these ones will have been dynamically allocated */
7428 pool_free2(pool2_requri, txn->uri);
7429 pool_free2(pool2_capture, txn->cli_cookie);
7430 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007431 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007432 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007433
William Lallemanda73203e2012-03-12 12:48:57 +01007434 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007435 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436 txn->uri = NULL;
7437 txn->srv_cookie = NULL;
7438 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007439
7440 if (txn->req.cap) {
7441 struct cap_hdr *h;
7442 for (h = s->fe->req_cap; h; h = h->next)
7443 pool_free2(h->pool, txn->req.cap[h->index]);
7444 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7445 }
7446
7447 if (txn->rsp.cap) {
7448 struct cap_hdr *h;
7449 for (h = s->fe->rsp_cap; h; h = h->next)
7450 pool_free2(h->pool, txn->rsp.cap[h->index]);
7451 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7452 }
7453
Willy Tarreau0937bc42009-12-22 15:03:09 +01007454}
7455
7456/* to be used at the end of a transaction to prepare a new one */
7457void http_reset_txn(struct session *s)
7458{
7459 http_end_txn(s);
7460 http_init_txn(s);
7461
7462 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007463 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007464 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007465 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007466 /* re-init store persistence */
7467 s->store_count = 0;
7468
Willy Tarreau0937bc42009-12-22 15:03:09 +01007469 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007470
7471 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7472
Willy Tarreau739cfba2010-01-25 23:11:14 +01007473 /* We must trim any excess data from the response buffer, because we
7474 * may have blocked an invalid response from a server that we don't
7475 * want to accidentely forward once we disable the analysers, nor do
7476 * we want those data to come along with next response. A typical
7477 * example of such data would be from a buggy server responding to
7478 * a HEAD with some data, or sending more than the advertised
7479 * content-length.
7480 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007481 if (unlikely(s->rep->buf.i))
7482 s->rep->buf.i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007483
Willy Tarreau0937bc42009-12-22 15:03:09 +01007484 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007485 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007486
Willy Tarreaud04e8582010-05-31 12:31:35 +02007487 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007488 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007489
7490 s->req->rex = TICK_ETERNITY;
7491 s->req->wex = TICK_ETERNITY;
7492 s->req->analyse_exp = TICK_ETERNITY;
7493 s->rep->rex = TICK_ETERNITY;
7494 s->rep->wex = TICK_ETERNITY;
7495 s->rep->analyse_exp = TICK_ETERNITY;
7496}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreauff011f22011-01-06 17:51:27 +01007498void free_http_req_rules(struct list *r) {
7499 struct http_req_rule *tr, *pr;
7500
7501 list_for_each_entry_safe(pr, tr, r, list) {
7502 LIST_DEL(&pr->list);
7503 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7504 free(pr->http_auth.realm);
7505
7506 free(pr);
7507 }
7508}
7509
7510struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7511{
7512 struct http_req_rule *rule;
7513 int cur_arg;
7514
7515 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7516 if (!rule) {
7517 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7518 return NULL;
7519 }
7520
7521 if (!*args[0]) {
7522 goto req_error_parsing;
7523 } else if (!strcmp(args[0], "allow")) {
7524 rule->action = HTTP_REQ_ACT_ALLOW;
7525 cur_arg = 1;
7526 } else if (!strcmp(args[0], "deny")) {
7527 rule->action = HTTP_REQ_ACT_DENY;
7528 cur_arg = 1;
7529 } else if (!strcmp(args[0], "auth")) {
7530 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7531 cur_arg = 1;
7532
7533 while(*args[cur_arg]) {
7534 if (!strcmp(args[cur_arg], "realm")) {
7535 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7536 cur_arg+=2;
7537 continue;
7538 } else
7539 break;
7540 }
7541 } else {
7542req_error_parsing:
7543 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7544 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7545 return NULL;
7546 }
7547
7548 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7549 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007550 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007551
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007552 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7553 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7554 file, linenum, args[0], errmsg);
7555 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007556 return NULL;
7557 }
7558 rule->cond = cond;
7559 }
7560 else if (*args[cur_arg]) {
7561 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7562 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7563 file, linenum, args[0], args[cur_arg]);
7564 return NULL;
7565 }
7566
7567 return rule;
7568}
7569
Willy Tarreau8797c062007-05-07 00:55:35 +02007570/************************************************************************/
7571/* The code below is dedicated to ACL parsing and matching */
7572/************************************************************************/
7573
7574
Willy Tarreau14174bc2012-04-16 14:34:04 +02007575/* This function ensures that the prerequisites for an L7 fetch are ready,
7576 * which means that a request or response is ready. If some data is missing,
7577 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007578 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7579 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007580 *
7581 * The function returns :
7582 * 0 if some data is missing or if the requested data cannot be fetched
7583 * -1 if it is certain that we'll never have any HTTP message there
7584 * 1 if an HTTP message is ready
7585 */
7586static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007587acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007588 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007589{
7590 struct http_txn *txn = l7;
7591 struct http_msg *msg = &txn->req;
7592
7593 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7594 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7595 */
7596
7597 if (unlikely(!s || !txn))
7598 return 0;
7599
7600 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007601 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007602
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007603 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007604 if (unlikely(!s->req))
7605 return 0;
7606
7607 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
7608 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007609 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007610 return -1;
7611 }
7612
7613 /* Try to decode HTTP request */
Willy Tarreau572bf902012-07-02 17:01:20 +02007614 if (likely(msg->next < s->req->buf.i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007615 http_msg_analyzer(msg, &txn->hdr_idx);
7616
7617 /* Still no valid request ? */
7618 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7619 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007620 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007621 return -1;
7622 }
7623 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007624 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007625 return 0;
7626 }
7627
7628 /* OK we just got a valid HTTP request. We have some minor
7629 * preparation to perform so that further checks can rely
7630 * on HTTP tests.
7631 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007632 txn->meth = find_http_meth(msg->buf->buf.p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007633 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7634 s->flags |= SN_REDIRECTABLE;
7635
7636 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007637 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007638 return -1;
7639 }
7640 }
7641
Willy Tarreau24e32d82012-04-23 23:55:44 +02007642 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007643 return 0; /* data might have moved and indexes changed */
7644
7645 /* otherwise everything's ready for the request */
7646 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007647 else {
7648 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007649 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7650 return 0;
7651 }
7652
7653 /* everything's OK */
7654 return 1;
7655}
Willy Tarreau8797c062007-05-07 00:55:35 +02007656
Willy Tarreauc0239e02012-04-16 14:42:55 +02007657#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007658 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 +02007659
Willy Tarreau24e32d82012-04-23 23:55:44 +02007660#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007661 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 +02007662
Willy Tarreau8797c062007-05-07 00:55:35 +02007663
7664/* 1. Check on METHOD
7665 * We use the pre-parsed method if it is known, and store its number as an
7666 * integer. If it is unknown, we use the pointer and the length.
7667 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007668static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007669{
7670 int len, meth;
7671
Willy Tarreauae8b7962007-06-09 23:10:04 +02007672 len = strlen(*text);
7673 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007674
7675 pattern->val.i = meth;
7676 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007677 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007678 if (!pattern->ptr.str) {
7679 if (err)
7680 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007681 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007682 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007683 pattern->len = len;
7684 }
7685 return 1;
7686}
7687
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007688/* This function fetches the method of current HTTP request and stores
7689 * it in the global pattern struct as a chunk. There are two possibilities :
7690 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7691 * in <len> and <ptr> is NULL ;
7692 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7693 * <len> to its length.
7694 * This is intended to be used with acl_match_meth() only.
7695 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007696static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007697acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007698 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007699{
7700 int meth;
7701 struct http_txn *txn = l7;
7702
Willy Tarreau24e32d82012-04-23 23:55:44 +02007703 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007704
Willy Tarreau8797c062007-05-07 00:55:35 +02007705 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007706 smp->type = SMP_T_UINT;
7707 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007708 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007709 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7710 /* ensure the indexes are not affected */
7711 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007712 smp->type = SMP_T_CSTR;
7713 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007714 smp->data.str.str = txn->req.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007715 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007716 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007717 return 1;
7718}
7719
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007720/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007721static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007722{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007723 int icase;
7724
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007725
Willy Tarreauf853c462012-04-23 18:53:56 +02007726 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007727 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007728 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007729 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007730 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007731 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007732
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007733 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7734 if (pattern->val.i != HTTP_METH_OTHER)
7735 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007736
7737 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007738 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007739 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007740
7741 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007742 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7743 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007744 return ACL_PAT_FAIL;
7745 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007746}
7747
7748/* 2. Check on Request/Status Version
7749 * We simply compare strings here.
7750 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007751static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007752{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007753 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007754 if (!pattern->ptr.str) {
7755 if (err)
7756 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007757 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007758 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007759 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007760 return 1;
7761}
7762
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007763static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007764acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007765 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007766{
7767 struct http_txn *txn = l7;
7768 char *ptr;
7769 int len;
7770
Willy Tarreauc0239e02012-04-16 14:42:55 +02007771 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007772
Willy Tarreau8797c062007-05-07 00:55:35 +02007773 len = txn->req.sl.rq.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007774 ptr = txn->req.buf->buf.p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007775
7776 while ((len-- > 0) && (*ptr++ != '/'));
7777 if (len <= 0)
7778 return 0;
7779
Willy Tarreauf853c462012-04-23 18:53:56 +02007780 smp->type = SMP_T_CSTR;
7781 smp->data.str.str = ptr;
7782 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007783
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007784 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007785 return 1;
7786}
7787
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007788static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007789acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007790 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007791{
7792 struct http_txn *txn = l7;
7793 char *ptr;
7794 int len;
7795
Willy Tarreauc0239e02012-04-16 14:42:55 +02007796 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007797
Willy Tarreau8797c062007-05-07 00:55:35 +02007798 len = txn->rsp.sl.st.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007799 ptr = txn->rsp.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007800
7801 while ((len-- > 0) && (*ptr++ != '/'));
7802 if (len <= 0)
7803 return 0;
7804
Willy Tarreauf853c462012-04-23 18:53:56 +02007805 smp->type = SMP_T_CSTR;
7806 smp->data.str.str = ptr;
7807 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007808
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007809 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007810 return 1;
7811}
7812
7813/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007814static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007815acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007816 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007817{
7818 struct http_txn *txn = l7;
7819 char *ptr;
7820 int len;
7821
Willy Tarreauc0239e02012-04-16 14:42:55 +02007822 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007823
Willy Tarreau8797c062007-05-07 00:55:35 +02007824 len = txn->rsp.sl.st.c_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007825 ptr = txn->rsp.buf->buf.p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007826
Willy Tarreauf853c462012-04-23 18:53:56 +02007827 smp->type = SMP_T_UINT;
7828 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007829 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007830 return 1;
7831}
7832
7833/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007834static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007835smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007836 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007837{
7838 struct http_txn *txn = l7;
7839
Willy Tarreauc0239e02012-04-16 14:42:55 +02007840 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007841
Willy Tarreauf853c462012-04-23 18:53:56 +02007842 smp->type = SMP_T_CSTR;
7843 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007844 smp->data.str.str = txn->req.buf->buf.p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007845 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007846 return 1;
7847}
7848
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007849static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007850smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007851 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007852{
7853 struct http_txn *txn = l7;
7854
Willy Tarreauc0239e02012-04-16 14:42:55 +02007855 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007856
7857 /* Parse HTTP request */
Willy Tarreau572bf902012-07-02 17:01:20 +02007858 url2sa(txn->req.buf->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 +01007859 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7860 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007861 smp->type = SMP_T_IPV4;
7862 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007863
7864 /*
7865 * If we are parsing url in frontend space, we prepare backend stage
7866 * to not parse again the same url ! optimization lazyness...
7867 */
7868 if (px->options & PR_O_HTTP_PROXY)
7869 l4->flags |= SN_ADDR_SET;
7870
Willy Tarreau37406352012-04-23 16:16:37 +02007871 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007872 return 1;
7873}
7874
7875static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007876smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007877 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007878{
7879 struct http_txn *txn = l7;
7880
Willy Tarreauc0239e02012-04-16 14:42:55 +02007881 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007882
7883 /* Same optimization as url_ip */
Willy Tarreau572bf902012-07-02 17:01:20 +02007884 url2sa(txn->req.buf->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 +02007885 smp->type = SMP_T_UINT;
7886 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007887
7888 if (px->options & PR_O_HTTP_PROXY)
7889 l4->flags |= SN_ADDR_SET;
7890
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007891 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007892 return 1;
7893}
7894
Willy Tarreau185b5c42012-04-26 15:11:51 +02007895/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7896 * Accepts an optional argument of type string containing the header field name,
7897 * and an optional argument of type signed or unsigned integer to request an
7898 * explicit occurrence of the header. Note that in the event of a missing name,
7899 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007900 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007901static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007902smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007903 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007904{
7905 struct http_txn *txn = l7;
7906 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007907 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007908 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007909 int occ = 0;
7910 const char *name_str = NULL;
7911 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007912
Willy Tarreau185b5c42012-04-26 15:11:51 +02007913 if (args) {
7914 if (args[0].type != ARGT_STR)
7915 return 0;
7916 name_str = args[0].data.str.str;
7917 name_len = args[0].data.str.len;
7918
7919 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7920 occ = args[1].data.uint;
7921 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007922
Willy Tarreaue333ec92012-04-16 16:26:40 +02007923 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924
Willy Tarreau185b5c42012-04-26 15:11:51 +02007925 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007926 /* search for header from the beginning */
7927 ctx->idx = 0;
7928
Willy Tarreau185b5c42012-04-26 15:11:51 +02007929 if (!occ && !(opt & SMP_OPT_ITERATE))
7930 /* no explicit occurrence and single fetch => last header by default */
7931 occ = -1;
7932
7933 if (!occ)
7934 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007935 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007936
Willy Tarreau185b5c42012-04-26 15:11:51 +02007937 smp->type = SMP_T_CSTR;
7938 smp->flags |= SMP_F_VOL_HDR;
7939 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 +02007940 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007941
Willy Tarreau37406352012-04-23 16:16:37 +02007942 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007943 return 0;
7944}
7945
Willy Tarreauc11416f2007-06-17 16:58:38 +02007946/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007947 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948 */
7949static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007950smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007951 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007952{
7953 struct http_txn *txn = l7;
7954 struct hdr_idx *idx = &txn->hdr_idx;
7955 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007956 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007957 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007958
Willy Tarreau24e32d82012-04-23 23:55:44 +02007959 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007960 return 0;
7961
Willy Tarreaue333ec92012-04-16 16:26:40 +02007962 CHECK_HTTP_MESSAGE_FIRST();
7963
Willy Tarreau33a7e692007-06-10 19:45:56 +02007964 ctx.idx = 0;
7965 cnt = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007966 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->buf.p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007967 cnt++;
7968
Willy Tarreauf853c462012-04-23 18:53:56 +02007969 smp->type = SMP_T_UINT;
7970 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007971 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007972 return 1;
7973}
7974
Willy Tarreau185b5c42012-04-26 15:11:51 +02007975/* Fetch an HTTP header's integer value. The integer value is returned. It
7976 * takes a mandatory argument of type string and an optional one of type int
7977 * to designate a specific occurrence. It returns an unsigned integer, which
7978 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007979 */
7980static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007981smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007982 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007983{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007984 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007985
Willy Tarreauf853c462012-04-23 18:53:56 +02007986 if (ret > 0) {
7987 smp->type = SMP_T_UINT;
7988 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7989 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007990
Willy Tarreaud53e2422012-04-16 17:21:11 +02007991 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007992}
7993
Willy Tarreau185b5c42012-04-26 15:11:51 +02007994/* Fetch an HTTP header's integer value. The integer value is returned. It
7995 * takes a mandatory argument of type string and an optional one of type int
7996 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02007997 */
7998static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007999smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008000 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008001{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008002 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008003
Willy Tarreau185b5c42012-04-26 15:11:51 +02008004 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02008005 smp->type = SMP_T_IPV4;
8006 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02008007 break;
8008 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008009 if (!(smp->flags & SMP_F_NOT_LAST))
8010 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008011 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008012 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008013}
8014
Willy Tarreau737b0c12007-06-10 21:28:46 +02008015/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8016 * the first '/' after the possible hostname, and ends before the possible '?'.
8017 */
8018static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008019smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008020 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008021{
8022 struct http_txn *txn = l7;
8023 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008024
Willy Tarreauc0239e02012-04-16 14:42:55 +02008025 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008026
Willy Tarreau572bf902012-07-02 17:01:20 +02008027 end = txn->req.buf->buf.p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008028 ptr = http_get_path(txn);
8029 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008030 return 0;
8031
8032 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008033 smp->type = SMP_T_CSTR;
8034 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008035
8036 while (ptr < end && *ptr != '?')
8037 ptr++;
8038
Willy Tarreauf853c462012-04-23 18:53:56 +02008039 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008040 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008041 return 1;
8042}
8043
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008044/* This produces a concatenation of the first occurrence of the Host header
8045 * followed by the path component if it begins with a slash ('/'). This means
8046 * that '*' will not be added, resulting in exactly the first Host entry.
8047 * If no Host header is found, then the path is returned as-is. The returned
8048 * value is stored in the trash so it does not need to be marked constant.
8049 */
8050static int
8051smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8052 const struct arg *args, struct sample *smp)
8053{
8054 struct http_txn *txn = l7;
8055 char *ptr, *end, *beg;
8056 struct hdr_ctx ctx;
8057
8058 CHECK_HTTP_MESSAGE_FIRST();
8059
8060 ctx.idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02008061 if (!http_find_header2("Host", 4, txn->req.buf->buf.p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008062 !ctx.vlen)
8063 return smp_fetch_path(px, l4, l7, opt, args, smp);
8064
8065 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8066 memcpy(trash, ctx.line + ctx.val, ctx.vlen);
8067 smp->type = SMP_T_STR;
8068 smp->data.str.str = trash;
8069 smp->data.str.len = ctx.vlen;
8070
8071 /* now retrieve the path */
Willy Tarreau572bf902012-07-02 17:01:20 +02008072 end = txn->req.buf->buf.p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008073 beg = http_get_path(txn);
8074 if (!beg)
8075 beg = end;
8076
8077 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8078
8079 if (beg < ptr && *beg == '/') {
8080 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8081 smp->data.str.len += ptr - beg;
8082 }
8083
8084 smp->flags = SMP_F_VOL_1ST;
8085 return 1;
8086}
8087
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008088static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008089acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008090 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008091{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008092 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8093 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8094 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008095
Willy Tarreau24e32d82012-04-23 23:55:44 +02008096 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008097
Willy Tarreauf853c462012-04-23 18:53:56 +02008098 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008099 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008100 return 1;
8101}
8102
Willy Tarreau7f18e522010-10-22 20:04:13 +02008103/* return a valid test if the current request is the first one on the connection */
8104static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008105acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008106 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008107{
8108 if (!s)
8109 return 0;
8110
Willy Tarreauf853c462012-04-23 18:53:56 +02008111 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008112 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008113 return 1;
8114}
8115
Willy Tarreau34db1082012-04-19 17:16:54 +02008116/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008117static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008118acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008119 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008120{
8121
Willy Tarreau24e32d82012-04-23 23:55:44 +02008122 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008123 return 0;
8124
Willy Tarreauc0239e02012-04-16 14:42:55 +02008125 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008126
Willy Tarreauc0239e02012-04-16 14:42:55 +02008127 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008128 return 0;
8129
Willy Tarreauf853c462012-04-23 18:53:56 +02008130 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008131 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 +01008132 return 1;
8133}
Willy Tarreau8797c062007-05-07 00:55:35 +02008134
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008135/* Accepts exactly 1 argument of type userlist */
8136static int
8137acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8138 const struct arg *args, struct sample *smp)
8139{
8140
8141 if (!args || args->type != ARGT_USR)
8142 return 0;
8143
8144 CHECK_HTTP_MESSAGE_FIRST();
8145
8146 if (!get_http_auth(l4))
8147 return 0;
8148
8149 /* acl_match_auth() will need several information at once */
8150 smp->ctx.a[0] = args->data.usr; /* user list */
8151 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8152 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8153
8154 /* if the user does not belong to the userlist or has a wrong password,
8155 * report that it unconditionally does not match. Otherwise we return
8156 * a non-zero integer which will be ignored anyway since all the params
8157 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8158 */
8159 smp->type = SMP_T_BOOL;
8160 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8161 if (smp->data.uint)
8162 smp->type = SMP_T_UINT;
8163
8164 return 1;
8165}
8166
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008167/* Try to find the next occurrence of a cookie name in a cookie header value.
8168 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8169 * the cookie value is returned into *value and *value_l, and the function
8170 * returns a pointer to the next pointer to search from if the value was found.
8171 * Otherwise if the cookie was not found, NULL is returned and neither value
8172 * nor value_l are touched. The input <hdr> string should first point to the
8173 * header's value, and the <hdr_end> pointer must point to the first character
8174 * not part of the value. <list> must be non-zero if value may represent a list
8175 * of values (cookie headers). This makes it faster to abort parsing when no
8176 * list is expected.
8177 */
8178static char *
8179extract_cookie_value(char *hdr, const char *hdr_end,
8180 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008181 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008182{
8183 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8184 char *next;
8185
8186 /* we search at least a cookie name followed by an equal, and more
8187 * generally something like this :
8188 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8189 */
8190 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8191 /* Iterate through all cookies on this line */
8192
8193 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8194 att_beg++;
8195
8196 /* find att_end : this is the first character after the last non
8197 * space before the equal. It may be equal to hdr_end.
8198 */
8199 equal = att_end = att_beg;
8200
8201 while (equal < hdr_end) {
8202 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8203 break;
8204 if (http_is_spht[(unsigned char)*equal++])
8205 continue;
8206 att_end = equal;
8207 }
8208
8209 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8210 * is between <att_beg> and <equal>, both may be identical.
8211 */
8212
8213 /* look for end of cookie if there is an equal sign */
8214 if (equal < hdr_end && *equal == '=') {
8215 /* look for the beginning of the value */
8216 val_beg = equal + 1;
8217 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8218 val_beg++;
8219
8220 /* find the end of the value, respecting quotes */
8221 next = find_cookie_value_end(val_beg, hdr_end);
8222
8223 /* make val_end point to the first white space or delimitor after the value */
8224 val_end = next;
8225 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8226 val_end--;
8227 } else {
8228 val_beg = val_end = next = equal;
8229 }
8230
8231 /* We have nothing to do with attributes beginning with '$'. However,
8232 * they will automatically be removed if a header before them is removed,
8233 * since they're supposed to be linked together.
8234 */
8235 if (*att_beg == '$')
8236 continue;
8237
8238 /* Ignore cookies with no equal sign */
8239 if (equal == next)
8240 continue;
8241
8242 /* Now we have the cookie name between att_beg and att_end, and
8243 * its value between val_beg and val_end.
8244 */
8245
8246 if (att_end - att_beg == cookie_name_l &&
8247 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8248 /* let's return this value and indicate where to go on from */
8249 *value = val_beg;
8250 *value_l = val_end - val_beg;
8251 return next + 1;
8252 }
8253
8254 /* Set-Cookie headers only have the name in the first attr=value part */
8255 if (!list)
8256 break;
8257 }
8258
8259 return NULL;
8260}
8261
Willy Tarreaue333ec92012-04-16 16:26:40 +02008262/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008263 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8264 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008265 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008266 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008267 * Accepts exactly 1 argument of type string. If the input options indicate
8268 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008269 */
8270static int
Willy Tarreau51539362012-05-08 12:46:28 +02008271smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8272 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008273{
8274 struct http_txn *txn = l7;
8275 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008276 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008277 const struct http_msg *msg;
8278 const char *hdr_name;
8279 int hdr_name_len;
8280 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008281 int occ = 0;
8282 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008283
Willy Tarreau24e32d82012-04-23 23:55:44 +02008284 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008285 return 0;
8286
Willy Tarreaue333ec92012-04-16 16:26:40 +02008287 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008288
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008289 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008290 msg = &txn->req;
8291 hdr_name = "Cookie";
8292 hdr_name_len = 6;
8293 } else {
8294 msg = &txn->rsp;
8295 hdr_name = "Set-Cookie";
8296 hdr_name_len = 10;
8297 }
8298
Willy Tarreau28376d62012-04-26 21:26:10 +02008299 if (!occ && !(opt & SMP_OPT_ITERATE))
8300 /* no explicit occurrence and single fetch => last cookie by default */
8301 occ = -1;
8302
8303 /* OK so basically here, either we want only one value and it's the
8304 * last one, or we want to iterate over all of them and we fetch the
8305 * next one.
8306 */
8307
Willy Tarreau572bf902012-07-02 17:01:20 +02008308 sol = msg->buf->buf.p;
Willy Tarreau37406352012-04-23 16:16:37 +02008309 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008310 /* search for the header from the beginning, we must first initialize
8311 * the search parameters.
8312 */
Willy Tarreau37406352012-04-23 16:16:37 +02008313 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008314 ctx->idx = 0;
8315 }
8316
Willy Tarreau28376d62012-04-26 21:26:10 +02008317 smp->flags |= SMP_F_VOL_HDR;
8318
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008319 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008320 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8321 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008322 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8323 goto out;
8324
Willy Tarreau24e32d82012-04-23 23:55:44 +02008325 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008326 continue;
8327
Willy Tarreau37406352012-04-23 16:16:37 +02008328 smp->ctx.a[0] = ctx->line + ctx->val;
8329 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008330 }
8331
Willy Tarreauf853c462012-04-23 18:53:56 +02008332 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008333 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008334 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008335 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008336 &smp->data.str.str,
8337 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008338 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008339 found = 1;
8340 if (occ >= 0) {
8341 /* one value was returned into smp->data.str.{str,len} */
8342 smp->flags |= SMP_F_NOT_LAST;
8343 return 1;
8344 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008345 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008346 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008347 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008348 /* all cookie headers and values were scanned. If we're looking for the
8349 * last occurrence, we may return it now.
8350 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008351 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008352 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008353 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008354}
8355
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008356/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008357 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008358 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008359 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008360 */
8361static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008362acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008363 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008364{
8365 struct http_txn *txn = l7;
8366 struct hdr_idx *idx = &txn->hdr_idx;
8367 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008368 const struct http_msg *msg;
8369 const char *hdr_name;
8370 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008371 int cnt;
8372 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008373 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008374
Willy Tarreau24e32d82012-04-23 23:55:44 +02008375 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008376 return 0;
8377
Willy Tarreaue333ec92012-04-16 16:26:40 +02008378 CHECK_HTTP_MESSAGE_FIRST();
8379
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008380 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008381 msg = &txn->req;
8382 hdr_name = "Cookie";
8383 hdr_name_len = 6;
8384 } else {
8385 msg = &txn->rsp;
8386 hdr_name = "Set-Cookie";
8387 hdr_name_len = 10;
8388 }
8389
Willy Tarreau572bf902012-07-02 17:01:20 +02008390 sol = msg->buf->buf.p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008391 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008392 ctx.idx = 0;
8393 cnt = 0;
8394
8395 while (1) {
8396 /* Note: val_beg == NULL every time we need to fetch a new header */
8397 if (!val_beg) {
8398 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8399 break;
8400
Willy Tarreau24e32d82012-04-23 23:55:44 +02008401 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008402 continue;
8403
8404 val_beg = ctx.line + ctx.val;
8405 val_end = val_beg + ctx.vlen;
8406 }
8407
Willy Tarreauf853c462012-04-23 18:53:56 +02008408 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008409 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008410 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008411 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008412 &smp->data.str.str,
8413 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008414 cnt++;
8415 }
8416 }
8417
Willy Tarreauf853c462012-04-23 18:53:56 +02008418 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008419 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008420 return 1;
8421}
8422
Willy Tarreau51539362012-05-08 12:46:28 +02008423/* Fetch an cookie's integer value. The integer value is returned. It
8424 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8425 */
8426static int
8427smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8428 const struct arg *args, struct sample *smp)
8429{
8430 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8431
8432 if (ret > 0) {
8433 smp->type = SMP_T_UINT;
8434 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8435 }
8436
8437 return ret;
8438}
8439
Willy Tarreau8797c062007-05-07 00:55:35 +02008440/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008441/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008442/************************************************************************/
8443
David Cournapeau16023ee2010-12-23 20:55:41 +09008444/*
8445 * Given a path string and its length, find the position of beginning of the
8446 * query string. Returns NULL if no query string is found in the path.
8447 *
8448 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8449 *
8450 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8451 */
8452static inline char *find_query_string(char *path, size_t path_l)
8453{
8454 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008455
David Cournapeau16023ee2010-12-23 20:55:41 +09008456 p = memchr(path, '?', path_l);
8457 return p ? p + 1 : NULL;
8458}
8459
8460static inline int is_param_delimiter(char c)
8461{
8462 return c == '&' || c == ';';
8463}
8464
8465/*
8466 * Given a url parameter, find the starting position of the first occurence,
8467 * or NULL if the parameter is not found.
8468 *
8469 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8470 * the function will return query_string+8.
8471 */
8472static char*
8473find_url_param_pos(char* query_string, size_t query_string_l,
8474 char* url_param_name, size_t url_param_name_l)
8475{
8476 char *pos, *last;
8477
8478 pos = query_string;
8479 last = query_string + query_string_l - url_param_name_l - 1;
8480
8481 while (pos <= last) {
8482 if (pos[url_param_name_l] == '=') {
8483 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8484 return pos;
8485 pos += url_param_name_l + 1;
8486 }
8487 while (pos <= last && !is_param_delimiter(*pos))
8488 pos++;
8489 pos++;
8490 }
8491 return NULL;
8492}
8493
8494/*
8495 * Given a url parameter name, returns its value and size into *value and
8496 * *value_l respectively, and returns non-zero. If the parameter is not found,
8497 * zero is returned and value/value_l are not touched.
8498 */
8499static int
8500find_url_param_value(char* path, size_t path_l,
8501 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008502 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008503{
8504 char *query_string, *qs_end;
8505 char *arg_start;
8506 char *value_start, *value_end;
8507
8508 query_string = find_query_string(path, path_l);
8509 if (!query_string)
8510 return 0;
8511
8512 qs_end = path + path_l;
8513 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8514 url_param_name, url_param_name_l);
8515 if (!arg_start)
8516 return 0;
8517
8518 value_start = arg_start + url_param_name_l + 1;
8519 value_end = value_start;
8520
8521 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8522 value_end++;
8523
8524 *value = value_start;
8525 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008526 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008527}
8528
8529static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008530smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8531 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008532{
8533 struct http_txn *txn = l7;
8534 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008535
8536 if (!args || args->type != ARGT_STR)
8537 return 0;
8538
8539 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008540
Willy Tarreau572bf902012-07-02 17:01:20 +02008541 if (!find_url_param_value(msg->buf->buf.p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008542 args->data.str.str, args->data.str.len,
8543 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008544 return 0;
8545
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008546 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008547 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008548 return 1;
8549}
8550
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008551/* Return the signed integer value for the specified url parameter (see url_param
8552 * above).
8553 */
8554static int
8555smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8556 const struct arg *args, struct sample *smp)
8557{
8558 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8559
8560 if (ret > 0) {
8561 smp->type = SMP_T_UINT;
8562 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8563 }
8564
8565 return ret;
8566}
8567
Willy Tarreau185b5c42012-04-26 15:11:51 +02008568/* This function is used to validate the arguments passed to any "hdr" fetch
8569 * keyword. These keywords support an optional positive or negative occurrence
8570 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8571 * is assumed that the types are already the correct ones. Returns 0 on error,
8572 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8573 * error message in case of error, that the caller is responsible for freeing.
8574 * The initial location must either be freeable or NULL.
8575 */
8576static int val_hdr(struct arg *arg, char **err_msg)
8577{
8578 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8579 if (err_msg)
8580 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8581 return 0;
8582 }
8583 return 1;
8584}
8585
Willy Tarreau4a568972010-05-12 08:08:50 +02008586/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008587/* All supported ACL keywords must be declared here. */
8588/************************************************************************/
8589
8590/* Note: must not be declared <const> as its list will be overwritten.
8591 * Please take care of keeping this list alphabetically sorted.
8592 */
8593static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008594 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8595 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8596 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8597 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8598 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8599 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8600 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8601 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8602
Willy Tarreau51539362012-05-08 12:46:28 +02008603 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8604 { "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 +02008605 { "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 +02008606 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8607 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8608 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8609 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8610 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8611 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8612 { "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 +02008613
Willy Tarreau185b5c42012-04-26 15:11:51 +02008614 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8615 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8616 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8617 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8618 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8619 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8620 { "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 },
8621 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8622 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8623 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8624 { "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 +02008625
8626 { "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 +02008627 { "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 +02008628 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8629
8630 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8631
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008632 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8633 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8634 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8635 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8636 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8637 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8638 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8639 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008640
8641 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8642 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8643 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8644
Willy Tarreau51539362012-05-08 12:46:28 +02008645 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8646 { "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 +02008647 { "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 +02008648 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8649 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8650 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8651 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8652 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8653 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8654 { "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 +02008655
Willy Tarreau185b5c42012-04-26 15:11:51 +02008656 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8657 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8658 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8659 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8660 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8661 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8662 { "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 },
8663 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8664 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8665 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8666 { "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 +02008667
8668 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8669
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008670 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8671 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8672 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8673 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8674 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8675 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8676 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8677 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8678 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8679 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008680
8681 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8682 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8683 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8684 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8685 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8686 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8687 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8688 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8689 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008690 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008691
8692 { NULL, NULL, NULL, NULL },
8693}};
8694
8695/************************************************************************/
8696/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008697/************************************************************************/
8698/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008699static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008700 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008701 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008702 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8703 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8704 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ },
8705 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008706 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008707 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8708 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008709 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008710}};
8711
Willy Tarreau8797c062007-05-07 00:55:35 +02008712
8713__attribute__((constructor))
8714static void __http_protocol_init(void)
8715{
8716 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008717 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008718}
8719
8720
Willy Tarreau58f10d72006-12-04 02:26:12 +01008721/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008722 * Local variables:
8723 * c-indent-level: 8
8724 * c-basic-offset: 8
8725 * End:
8726 */