blob: 61cef2621cf4a9026fcb934cbf98e90bde82206e [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>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010045#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/backend.h>
47#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010048#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020051#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010053#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020054#include <proto/pattern.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 Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010061#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020062#include <proto/stream_sock.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;
392 size += snprintf(trash + size, sizeof(trash) - 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 Tarreaua458b672012-03-05 11:17:50 +0100396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - 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 Tarreaua458b672012-03-05 11:17:50 +0100403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +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 Tarreau6acf7c92012-03-09 13:30:45 +0100423 bytes = buffer_insert_line2(msg->buf, msg->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 Tarreau6acf7c92012-03-09 13:30:45 +0100443 bytes = buffer_insert_line2(msg->buf, msg->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 Tarreau2d3d94c2008-11-30 20:20:08 +0100659 buffer_write(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 Tarreau3a215be2012-03-09 21:39:51 +0100714 ptr = txn->req.buf->p + txn->req.sol + 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.
756 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758{
759 struct http_txn *txn;
760 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
763 int len;
764
765 /* 1: create the response header */
766 rdr.len = strlen(HTTP_302);
767 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100768 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769 memcpy(rdr.str, HTTP_302, rdr.len);
770
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 srv = target_srv(&s->target);
772
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 return;
776
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100777 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
779 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
780 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100781 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* 3: add the request URI */
784 txn = &s->txn;
785 path = http_get_path(txn);
786 if (!path)
787 return;
788
Willy Tarreau3a215be2012-03-09 21:39:51 +0100789 len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200790 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100791 return;
792
793 memcpy(rdr.str + rdr.len, path, len);
794 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100795
796 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
797 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
798 rdr.len += 29;
799 } else {
800 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
801 rdr.len += 23;
802 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803
804 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100805 si->shutr(si);
806 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 si->err_type = SI_ET_NONE;
808 si->err_loc = NULL;
809 si->state = SI_ST_CLO;
810
811 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100812 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (srv)
816 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817}
818
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 * that the server side is closed. Note that err_type is actually a
821 * bitmask, where almost only aborts may be cumulated with other
822 * values. We consider that aborted operations are more important
823 * than timeouts or errors due to the fact that nobody else in the
824 * logs might explain incomplete retries. All others should avoid
825 * being cumulated. It should normally not be possible to have multiple
826 * aborts at once, but just in case, the first one in sequence is reported.
827 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831
832 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
834 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100836 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
837 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100838 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
840 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_SRVCL, 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_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_SRVTO, 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_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
849 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
852 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853}
854
Willy Tarreau42250582007-04-01 01:30:43 +0200855extern const char sess_term_cond[8];
856extern const char sess_fin_state[8];
857extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200858struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200859struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100860struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862/*
863 * Capture headers from message starting at <som> according to header list
864 * <cap_hdr>, and fill the <idx> structure appropriately.
865 */
866void capture_headers(char *som, struct hdr_idx *idx,
867 char **cap, struct cap_hdr *cap_hdr)
868{
869 char *eol, *sol, *col, *sov;
870 int cur_idx;
871 struct cap_hdr *h;
872 int len;
873
874 sol = som + hdr_idx_first_pos(idx);
875 cur_idx = hdr_idx_first_idx(idx);
876
877 while (cur_idx) {
878 eol = sol + idx->v[cur_idx].len;
879
880 col = sol;
881 while (col < eol && *col != ':')
882 col++;
883
884 sov = col + 1;
885 while (sov < eol && http_is_lws[(unsigned char)*sov])
886 sov++;
887
888 for (h = cap_hdr; h; h = h->next) {
889 if ((h->namelen == col - sol) &&
890 (strncasecmp(sol, h->name, h->namelen) == 0)) {
891 if (cap[h->index] == NULL)
892 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200893 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100894
895 if (cap[h->index] == NULL) {
896 Alert("HTTP capture : out of memory.\n");
897 continue;
898 }
899
900 len = eol - sov;
901 if (len > h->len)
902 len = h->len;
903
904 memcpy(cap[h->index], sov, len);
905 cap[h->index][len]=0;
906 }
907 }
908 sol = eol + idx->v[cur_idx].cr + 1;
909 cur_idx = idx->v[cur_idx].next;
910 }
911}
912
913
Willy Tarreau42250582007-04-01 01:30:43 +0200914/* either we find an LF at <ptr> or we jump to <bad>.
915 */
916#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
917
918/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
919 * otherwise to <http_msg_ood> with <state> set to <st>.
920 */
921#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
922 ptr++; \
923 if (likely(ptr < end)) \
924 goto good; \
925 else { \
926 state = (st); \
927 goto http_msg_ood; \
928 } \
929 } while (0)
930
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100933 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100934 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
935 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
936 * will give undefined results.
937 * Note that it is upon the caller's responsibility to ensure that ptr < end,
938 * and that msg->sol points to the beginning of the response.
939 * If a complete line is found (which implies that at least one CR or LF is
940 * found before <end>, the updated <ptr> is returned, otherwise NULL is
941 * returned indicating an incomplete line (which does not mean that parts have
942 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
943 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
944 * upon next call.
945 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
948 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100950 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100951const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
952 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100953 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100954{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100955 const char *msg_start = msg->buf->p;
956
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200959 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100960 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
962
963 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100964 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
966 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100967 state = HTTP_MSG_ERROR;
968 break;
969
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200971 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100973 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 goto http_msg_rpcode;
975 }
976 if (likely(HTTP_IS_SPHT(*ptr)))
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
978 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100979 state = HTTP_MSG_ERROR;
980 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100981
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200983 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 if (likely(!HTTP_IS_LWS(*ptr)))
985 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
986
987 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100988 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
990 }
991
992 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 http_msg_rsp_reason:
995 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100996 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 msg->sl.st.r_l = 0;
998 goto http_msg_rpline_eol;
999
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001001 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 goto http_msg_rpreason;
1005 }
1006 if (likely(HTTP_IS_SPHT(*ptr)))
1007 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1008 /* so it's a CR/LF, so there is no reason phrase */
1009 goto http_msg_rsp_reason;
1010
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001012 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 if (likely(!HTTP_IS_CRLF(*ptr)))
1014 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001015 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 http_msg_rpline_eol:
1017 /* We have seen the end of line. Note that we do not
1018 * necessarily have the \n yet, but at least we know that we
1019 * have EITHER \r OR \n, otherwise the response would not be
1020 * complete. We can then record the response length and return
1021 * to the caller which will be able to register it.
1022 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001023 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 return ptr;
1025
1026#ifdef DEBUG_FULL
1027 default:
1028 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1029 exit(1);
1030#endif
1031 }
1032
1033 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001034 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 if (ret_state)
1036 *ret_state = state;
1037 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001038 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040}
1041
Willy Tarreau8973c702007-01-21 23:58:29 +01001042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001063 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001065 const char *msg_start = msg->buf->p;
1066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001069 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 if (likely(HTTP_IS_TOKEN(*ptr)))
1071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001074 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_CRLF(*ptr))) {
1079 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 msg->sl.rq.v_l = 0;
1088 goto http_msg_rqline_eol;
1089 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001090 state = HTTP_MSG_ERROR;
1091 break;
1092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001094 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 goto http_msg_rquri;
1098 }
1099 if (likely(HTTP_IS_SPHT(*ptr)))
1100 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1101 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1102 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001105 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001106 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001110 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001114 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001115 /* non-ASCII chars are forbidden unless option
1116 * accept-invalid-http-request is enabled in the frontend.
1117 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001119 if (msg->err_pos < -1)
1120 goto invalid_char;
1121 if (msg->err_pos == -1)
1122 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1124 }
1125
1126 if (likely(HTTP_IS_CRLF(*ptr))) {
1127 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1128 goto http_msg_req09_uri_e;
1129 }
1130
1131 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001132 invalid_char:
1133 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 goto http_msg_rqver;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1145 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1146 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001152
1153 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 http_msg_rqline_eol:
1156 /* We have seen the end of line. Note that we do not
1157 * necessarily have the \n yet, but at least we know that we
1158 * have EITHER \r OR \n, otherwise the request would not be
1159 * complete. We can then record the request length and return
1160 * to the caller which will be able to register it.
1161 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001162 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163 return ptr;
1164 }
1165
1166 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001167 state = HTTP_MSG_ERROR;
1168 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170#ifdef DEBUG_FULL
1171 default:
1172 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1173 exit(1);
1174#endif
1175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001178 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 if (ret_state)
1180 *ret_state = state;
1181 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001182 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001186/*
1187 * Returns the data from Authorization header. Function may be called more
1188 * than once so data is stored in txn->auth_data. When no header is found
1189 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1190 * searching again for something we are unable to find anyway.
1191 */
1192
1193char get_http_auth_buff[BUFSIZE];
1194
1195int
1196get_http_auth(struct session *s)
1197{
1198
1199 struct http_txn *txn = &s->txn;
1200 struct chunk auth_method;
1201 struct hdr_ctx ctx;
1202 char *h, *p;
1203 int len;
1204
1205#ifdef DEBUG_AUTH
1206 printf("Auth for session %p: %d\n", s, txn->auth.method);
1207#endif
1208
1209 if (txn->auth.method == HTTP_AUTH_WRONG)
1210 return 0;
1211
1212 if (txn->auth.method)
1213 return 1;
1214
1215 txn->auth.method = HTTP_AUTH_WRONG;
1216
1217 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001218
1219 if (txn->flags & TX_USE_PX_CONN) {
1220 h = "Proxy-Authorization";
1221 len = strlen(h);
1222 } else {
1223 h = "Authorization";
1224 len = strlen(h);
1225 }
1226
Willy Tarreau3a215be2012-03-09 21:39:51 +01001227 if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001228 return 0;
1229
1230 h = ctx.line + ctx.val;
1231
1232 p = memchr(h, ' ', ctx.vlen);
1233 if (!p || p == h)
1234 return 0;
1235
1236 chunk_initlen(&auth_method, h, 0, p-h);
1237 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1238
1239 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1240
1241 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1242 get_http_auth_buff, BUFSIZE - 1);
1243
1244 if (len < 0)
1245 return 0;
1246
1247
1248 get_http_auth_buff[len] = '\0';
1249
1250 p = strchr(get_http_auth_buff, ':');
1251
1252 if (!p)
1253 return 0;
1254
1255 txn->auth.user = get_http_auth_buff;
1256 *p = '\0';
1257 txn->auth.pass = p+1;
1258
1259 txn->auth.method = HTTP_AUTH_BASIC;
1260 return 1;
1261 }
1262
1263 return 0;
1264}
1265
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8973c702007-01-21 23:58:29 +01001267/*
1268 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001269 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001270 * when data are missing and recalled at the exact same location with no
1271 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001273 * fields. Note that msg->som and msg->sol will be initialized after completing
1274 * the first state, so that none of the msg pointers has to be initialized
1275 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001277void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001279 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001281 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001283 state = msg->msg_state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001284 ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001285 end = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 if (unlikely(ptr >= end))
1288 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 /*
1292 * First, states that are specific to the response only.
1293 * We check them first so that request and headers are
1294 * closer to each other (accessed more often).
1295 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001297 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001299 /* we have a start of message, but we have to check
1300 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001301 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001302 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001303 char *beg = buf->p;
1304
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001306 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001308 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001311 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 hdr_idx_init(idx);
1313 state = HTTP_MSG_RPVER;
1314 goto http_msg_rpver;
1315 }
1316
1317 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1318 goto http_msg_invalid;
1319
1320 if (unlikely(*ptr == '\n'))
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1323 /* stop here */
1324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EXPECT_LF_HERE(ptr, http_msg_invalid);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 /* stop here */
1330
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001332 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPVER_SP:
1334 case HTTP_MSG_RPCODE:
1335 case HTTP_MSG_RPCODE_SP:
1336 case HTTP_MSG_RPREASON:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001337 ptr = (char *)http_parse_stsline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001338 state, ptr, end,
1339 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (unlikely(!ptr))
1341 return;
1342
1343 /* we have a full response and we know that we have either a CR
1344 * or an LF at <ptr>.
1345 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1347
Willy Tarreau3a215be2012-03-09 21:39:51 +01001348 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 if (likely(*ptr == '\r'))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1351 goto http_msg_rpline_end;
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 /* msg->sol must point to the first of CR or LF. */
1356 EXPECT_LF_HERE(ptr, http_msg_invalid);
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1358 /* stop here */
1359
1360 /*
1361 * Second, states that are specific to the request only
1362 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001366 /* we have a start of message, but we have to check
1367 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001368 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001370 char *beg = buf->p;
1371
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001373 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001375 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001378 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001379 /* we will need this when keep-alive will be supported
1380 hdr_idx_init(idx);
1381 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 state = HTTP_MSG_RQMETH;
1383 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1387 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(*ptr == '\n'))
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001392 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001395 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 EXPECT_LF_HERE(ptr, http_msg_invalid);
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001401 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQMETH_SP:
1403 case HTTP_MSG_RQURI:
1404 case HTTP_MSG_RQURI_SP:
1405 case HTTP_MSG_RQVER:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001406 ptr = (char *)http_parse_reqline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001407 state, ptr, end,
1408 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (unlikely(!ptr))
1410 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 /* we have a full request and we know that we have either a CR
1413 * or an LF at <ptr>.
1414 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau3a215be2012-03-09 21:39:51 +01001417 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 if (likely(*ptr == '\r'))
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 /* check for HTTP/0.9 request : no version information available.
1425 * msg->sol must point to the first of CR or LF.
1426 */
1427 if (unlikely(msg->sl.rq.v_l == 0))
1428 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 EXPECT_LF_HERE(ptr, http_msg_invalid);
1431 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /*
1435 * Common states below
1436 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001439 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(!HTTP_IS_CRLF(*ptr))) {
1441 goto http_msg_hdr_name;
1442 }
1443
1444 if (likely(*ptr == '\r'))
1445 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 /* assumes msg->sol points to the first char */
1451 if (likely(HTTP_IS_TOKEN(*ptr)))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001454 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001457 if (likely(msg->err_pos < -1) || *ptr == '\n')
1458 goto http_msg_invalid;
1459
1460 if (msg->err_pos == -1) /* capture error pointer */
1461 msg->err_pos = ptr - buf->data; /* >= 0 now */
1462
1463 /* and we still accept this non-token character */
1464 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001468 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 if (likely(HTTP_IS_SPHT(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001473 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 if (likely(!HTTP_IS_CRLF(*ptr))) {
1476 goto http_msg_hdr_val;
1477 }
1478
1479 if (likely(*ptr == '\r'))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1481 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001484 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 EXPECT_LF_HERE(ptr, http_msg_invalid);
1486 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001489 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 if (likely(HTTP_IS_SPHT(*ptr))) {
1491 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 for (; buf->p + msg->sov < ptr; msg->sov++)
1493 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 goto http_msg_hdr_l1_sp;
1495 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001496 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001497 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 goto http_msg_complete_header;
1499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001502 /* assumes msg->sol points to the first char, and msg->sov
1503 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 */
1505 if (likely(!HTTP_IS_CRLF(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau12e48b32012-03-05 16:57:34 +01001508 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 /* Note: we could also copy eol into ->eoh so that we have the
1510 * real header end in case it ends with lots of LWS, but is this
1511 * really needed ?
1512 */
1513 if (likely(*ptr == '\r'))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1515 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001518 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 EXPECT_LF_HERE(ptr, http_msg_invalid);
1520 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001523 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1525 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001526 for (; buf->p + msg->eol < ptr; msg->eol++)
1527 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 goto http_msg_hdr_val;
1529 }
1530 http_msg_complete_header:
1531 /*
1532 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001533 * Assumes msg->sol points to the first char, msg->sov points
1534 * to the first character of the value and msg->eol to the
1535 * first CR or LF so we know how the line ends. We insert last
1536 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001538 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 idx, idx->tail) < 0))
1540 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001541
Willy Tarreau3a215be2012-03-09 21:39:51 +01001542 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (likely(!HTTP_IS_CRLF(*ptr))) {
1544 goto http_msg_hdr_name;
1545 }
1546
1547 if (likely(*ptr == '\r'))
1548 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1549 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 /* Assumes msg->sol points to the first of either CR or LF */
1554 EXPECT_LF_HERE(ptr, http_msg_invalid);
1555 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001556 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001557 msg->eoh = msg->sol;
1558 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001559 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001561
1562 case HTTP_MSG_ERROR:
1563 /* this may only happen if we call http_msg_analyser() twice with an error */
1564 break;
1565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566#ifdef DEBUG_FULL
1567 default:
1568 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1569 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001570#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 }
1572 http_msg_ood:
1573 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001574 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001575 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 http_msg_invalid:
1579 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001581 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 return;
1583}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001584
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001585/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1586 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1587 * nothing is done and 1 is returned.
1588 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001589static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001590{
1591 int delta;
1592 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001593 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594
1595 if (msg->sl.rq.v_l != 0)
1596 return 1;
1597
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001598 cur_end = msg->buf->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001599 delta = 0;
1600
1601 if (msg->sl.rq.u_l == 0) {
1602 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001603 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001604 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001605 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 }
1607 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001608 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001609 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 cur_end += delta;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 cur_end = (char *)http_parse_reqline(msg, msg->buf->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 HTTP_MSG_RQMETH,
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001613 msg->buf->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 NULL, NULL);
1615 if (unlikely(!cur_end))
1616 return 0;
1617
1618 /* we have a full HTTP/1.0 request now and we know that
1619 * we have either a CR or an LF at <ptr>.
1620 */
1621 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1622 return 1;
1623}
1624
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001625/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001626 * and "keep-alive" values. If we already know that some headers may safely
1627 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1629 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1630 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1631 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1632 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001634 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001635void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001636{
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001638 const char *hdr_val = "Connection";
1639 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001640
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001641 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 return;
1643
Willy Tarreau88d349d2010-01-25 12:15:43 +01001644 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1645 hdr_val = "Proxy-Connection";
1646 hdr_len = 16;
1647 }
1648
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001650 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001651 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001652 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1653 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654 if (to_del & 2)
1655 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 else
1657 txn->flags |= TX_CON_KAL_SET;
1658 }
1659 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1660 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001661 if (to_del & 1)
1662 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 else
1664 txn->flags |= TX_CON_CLO_SET;
1665 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001666 }
1667
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001668 txn->flags |= TX_HDR_CONN_PRS;
1669 return;
1670}
Willy Tarreau5b154472009-12-21 20:11:07 +01001671
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672/* Apply desired changes on the Connection: header. Values may be removed and/or
1673 * added depending on the <wanted> flags, which are exclusively composed of
1674 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1675 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1676 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001677void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001678{
1679 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001680 const char *hdr_val = "Connection";
1681 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682
1683 ctx.idx = 0;
1684
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685
1686 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1687 hdr_val = "Proxy-Connection";
1688 hdr_len = 16;
1689 }
1690
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001692 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001693 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1694 if (wanted & TX_CON_KAL_SET)
1695 txn->flags |= TX_CON_KAL_SET;
1696 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001697 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001698 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1700 if (wanted & TX_CON_CLO_SET)
1701 txn->flags |= TX_CON_CLO_SET;
1702 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001703 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001704 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706
1707 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1708 return;
1709
1710 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1711 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001712 hdr_val = "Connection: close";
1713 hdr_len = 17;
1714 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1715 hdr_val = "Proxy-Connection: close";
1716 hdr_len = 23;
1717 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001718 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001719 }
1720
1721 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1722 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001723 hdr_val = "Connection: keep-alive";
1724 hdr_len = 22;
1725 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1726 hdr_val = "Proxy-Connection: keep-alive";
1727 hdr_len = 28;
1728 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 }
1731 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001732}
1733
Willy Tarreaua458b672012-03-05 11:17:50 +01001734/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735 * first byte of body, and increments msg->sov by the number of bytes parsed,
1736 * so that we know we can forward between ->som and ->sov. Note that due to
1737 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1738 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001741 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001743{
Willy Tarreaua458b672012-03-05 11:17:50 +01001744 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1745 char *ptr_old = ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 char *end = buf->data + buf->size;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001747 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 unsigned int chunk = 0;
1749
1750 /* The chunk size is in the following form, though we are only
1751 * interested in the size and CRLF :
1752 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1753 */
1754 while (1) {
1755 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001756 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001757 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001758 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 if (c < 0) /* not a hex digit anymore */
1760 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 if (++ptr >= end)
1762 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001763 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001764 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 chunk = (chunk << 4) + c;
1766 }
1767
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001769 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771
1772 while (http_is_spht[(unsigned char)*ptr]) {
1773 if (++ptr >= end)
1774 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001775 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001776 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001777 }
1778
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 /* Up to there, we know that at least one byte is present at *ptr. Check
1780 * for the end of chunk size.
1781 */
1782 while (1) {
1783 if (likely(HTTP_IS_CRLF(*ptr))) {
1784 /* we now have a CR or an LF at ptr */
1785 if (likely(*ptr == '\r')) {
1786 if (++ptr >= end)
1787 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001788 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 return 0;
1790 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001791
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001793 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001794 if (++ptr >= end)
1795 ptr = buf->data;
1796 /* done */
1797 break;
1798 }
1799 else if (*ptr == ';') {
1800 /* chunk extension, ends at next CRLF */
1801 if (++ptr >= end)
1802 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001803 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001804 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001805
1806 while (!HTTP_IS_CRLF(*ptr)) {
1807 if (++ptr >= end)
1808 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 return 0;
1811 }
1812 /* we have a CRLF now, loop above */
1813 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001814 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001816 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001817 }
1818
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001820 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001823 msg->sov += ptr - ptr_old;
1824 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001825 msg->chunk_len = chunk;
1826 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001829 error:
1830 msg->err_pos = ptr - buf->data;
1831 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001832}
1833
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834/* This function skips trailers in the buffer <buf> associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001835 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 * the trailers is found, it is automatically scheduled to be forwarded,
1837 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1838 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001840 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001842 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1843 * which implies that all non-trailers data have already been scheduled for
1844 * forwarding, and that the difference between msg->som and msg->sov exactly
1845 * matches the length of trailers already parsed and not forwarded. It is also
1846 * important to note that this function is designed to be able to parse wrapped
1847 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 */
1849int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1850{
Willy Tarreaua458b672012-03-05 11:17:50 +01001851 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 while (1) {
1853 char *p1 = NULL, *p2 = NULL;
Willy Tarreaua458b672012-03-05 11:17:50 +01001854 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001855 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001856 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857
1858 /* scan current line and stop at LF or CRLF */
1859 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001860 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 return 0;
1862
1863 if (*ptr == '\n') {
1864 if (!p1)
1865 p1 = ptr;
1866 p2 = ptr;
1867 break;
1868 }
1869
1870 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001871 if (p1) {
1872 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 p1 = ptr;
1876 }
1877
1878 ptr++;
1879 if (ptr >= buf->data + buf->size)
1880 ptr = buf->data;
1881 }
1882
1883 /* after LF; point to beginning of next line */
1884 p2++;
1885 if (p2 >= buf->data + buf->size)
1886 p2 = buf->data;
1887
Willy Tarreaua458b672012-03-05 11:17:50 +01001888 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001889 if (bytes < 0)
1890 bytes += buf->size;
1891
1892 /* schedule this line for forwarding */
1893 msg->sov += bytes;
1894 if (msg->sov >= buf->size)
1895 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
Willy Tarreaua458b672012-03-05 11:17:50 +01001897 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 /* LF/CRLF at beginning of line => end of trailers at p2.
1899 * Everything was scheduled for forwarding, there's nothing
1900 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001901 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001902 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001903 msg->msg_state = HTTP_MSG_DONE;
1904 return 1;
1905 }
1906 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001907 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 }
1909}
1910
1911/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1912 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001913 * ->som, ->next in order to include this part into the next forwarding phase.
1914 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1916 * not enough data are available, the function does not change anything and
1917 * returns zero. If a parse error is encountered, the function returns < 0 and
1918 * does not change anything. Note: this function is designed to parse wrapped
1919 * CRLF at the end of the buffer.
1920 */
1921int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1922{
1923 char *ptr;
1924 int bytes;
1925
1926 /* NB: we'll check data availabilty at the end. It's not a
1927 * problem because whatever we match first will be checked
1928 * against the correct length.
1929 */
1930 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001931 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932 if (*ptr == '\r') {
1933 bytes++;
1934 ptr++;
1935 if (ptr >= buf->data + buf->size)
1936 ptr = buf->data;
1937 }
1938
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001939 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 return 0;
1941
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001942 if (*ptr != '\n') {
1943 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001945 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946
1947 ptr++;
1948 if (ptr >= buf->data + buf->size)
1949 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001950 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1951 msg->som = 0;
1952 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1954 return 1;
1955}
Willy Tarreau5b154472009-12-21 20:11:07 +01001956
Willy Tarreau21710ff2012-03-09 13:58:04 +01001957/* This function realigns a possibly wrapping http message at the beginning
1958 * of its buffer. The function may only be used when the buffer's tail is
1959 * empty.
1960 */
1961void http_message_realign(struct http_msg *msg)
Willy Tarreau83e3af02009-12-28 17:39:57 +01001962{
Willy Tarreau21710ff2012-03-09 13:58:04 +01001963 struct buffer *buf = msg->buf;
Willy Tarreau89fa7062012-03-02 16:13:16 +01001964 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001965
1966 /* two possible cases :
1967 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001968 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001970 if (buf->i) {
1971 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001972 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001973 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001974 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001975 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001976 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001977 }
1978 if (block2)
1979 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001980 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001981 if (block2)
1982 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001983 }
1984
1985 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001986 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001987
Willy Tarreau83e3af02009-12-28 17:39:57 +01001988 if (msg->err_pos >= 0) {
1989 msg->err_pos += off;
1990 if (msg->err_pos >= buf->size)
1991 msg->err_pos -= buf->size;
1992 }
1993
1994 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001995 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01001996 buf->flags |= BF_FULL;
1997}
1998
Willy Tarreaud787e662009-07-07 10:14:51 +02001999/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2000 * processing can continue on next analysers, or zero if it either needs more
2001 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2002 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2003 * when it has nothing left to do, and may remove any analyser when it wants to
2004 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002006int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002007{
Willy Tarreau59234e92008-11-30 23:51:27 +01002008 /*
2009 * We will parse the partial (or complete) lines.
2010 * We will check the request syntax, and also join multi-line
2011 * headers. An index of all the lines will be elaborated while
2012 * parsing.
2013 *
2014 * For the parsing, we use a 28 states FSM.
2015 *
2016 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002017 * req->p + msg->som = beginning of request
2018 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002019 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002020 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002021 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002022 *
2023 * At end of parsing, we may perform a capture of the error (if any), and
2024 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2025 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2026 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002028
Willy Tarreau59234e92008-11-30 23:51:27 +01002029 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002030 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 struct http_txn *txn = &s->txn;
2032 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002033 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002034
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002035 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 +01002036 now_ms, __FUNCTION__,
2037 s,
2038 req,
2039 req->rex, req->wex,
2040 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002041 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002042 req->analysers);
2043
Willy Tarreau52a0c602009-08-16 22:45:38 +02002044 /* we're speaking HTTP here, so let's speak HTTP to the client */
2045 s->srv_error = http_return_srv_error;
2046
Willy Tarreau83e3af02009-12-28 17:39:57 +01002047 /* There's a protected area at the end of the buffer for rewriting
2048 * purposes. We don't want to start to parse the request if the
2049 * protected area is affected, because we may have to move processed
2050 * data later, which is much more complicated.
2051 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002052 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002053 if ((txn->flags & TX_NOT_FIRST) &&
2054 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002055 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002056 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002057 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002058 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2059 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002060 /* some data has still not left the buffer, wake us once that's done */
2061 buffer_dont_connect(req);
2062 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2063 return 0;
2064 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002065 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002066 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01002067 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002068 }
2069
Willy Tarreau065e8332010-01-08 00:30:20 +01002070 /* Note that we have the same problem with the response ; we
2071 * may want to send a redirect, error or anything which requires
2072 * some spare space. So we'll ensure that we have at least
2073 * maxrewrite bytes available in the response buffer before
2074 * processing that one. This will only affect pipelined
2075 * keep-alive requests.
2076 */
2077 if ((txn->flags & TX_NOT_FIRST) &&
2078 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002079 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) < buffer_wrap_add(s->rep, s->rep->p + txn->rsp.next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002080 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002081 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002082 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2083 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002084 /* don't let a connection request be initiated */
2085 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002086 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002087 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002088 return 0;
2089 }
2090 }
2091
Willy Tarreaua458b672012-03-05 11:17:50 +01002092 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002093 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002094 }
2095
Willy Tarreau59234e92008-11-30 23:51:27 +01002096 /* 1: we might have to print this header in debug mode */
2097 if (unlikely((global.mode & MODE_DEBUG) &&
2098 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002099 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002100 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002102
Willy Tarreauea1175a2012-03-05 15:52:30 +01002103 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 eol = sol + msg->sl.rq.l;
2105 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002106
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 sol += hdr_idx_first_pos(&txn->hdr_idx);
2108 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 while (cur_idx) {
2111 eol = sol + txn->hdr_idx.v[cur_idx].len;
2112 debug_hdr("clihdr", s, sol, eol);
2113 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2114 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002115 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 }
2117
Willy Tarreau58f10d72006-12-04 02:26:12 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 /*
2120 * Now we quickly check if we have found a full valid request.
2121 * If not so, we check the FD and buffer states before leaving.
2122 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002123 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002124 * requests are checked first. When waiting for a second request
2125 * on a keep-alive session, if we encounter and error, close, t/o,
2126 * we note the error in the session flags but don't set any state.
2127 * Since the error will be noted there, it will not be counted by
2128 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002129 * Last, we may increase some tracked counters' http request errors on
2130 * the cases that are deliberately the client's fault. For instance,
2131 * a timeout or connection reset is not counted as an error. However
2132 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002134
Willy Tarreau655dce92009-11-08 13:10:58 +01002135 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002136 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002139 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002140 session_inc_http_req_ctr(s);
2141 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002142 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002144 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002145
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 /* 1: Since we are in header mode, if there's no space
2147 * left for headers, we won't be able to free more
2148 * later, so the session will never terminate. We
2149 * must terminate it now.
2150 */
2151 if (unlikely(req->flags & BF_FULL)) {
2152 /* FIXME: check if URI is set and return Status
2153 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002154 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002155 session_inc_http_req_ctr(s);
2156 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002157 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002158 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002159 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 goto return_bad_req;
2161 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002162
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 /* 2: have we encountered a read error ? */
2164 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002165 if (!(s->flags & SN_ERR_MASK))
2166 s->flags |= SN_ERR_CLICL;
2167
Willy Tarreaufcffa692010-01-10 14:21:19 +01002168 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002169 goto failed_keep_alive;
2170
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002172 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002173 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002174 session_inc_http_err_ctr(s);
2175 }
2176
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 msg->msg_state = HTTP_MSG_ERROR;
2178 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179
Willy Tarreauda7ff642010-06-23 11:44:09 +02002180 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002181 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002182 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002183 if (s->listener->counters)
2184 s->listener->counters->failed_req++;
2185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 if (!(s->flags & SN_FINST_MASK))
2187 s->flags |= SN_FINST_R;
2188 return 0;
2189 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 /* 3: has the read timeout expired ? */
2192 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002193 if (!(s->flags & SN_ERR_MASK))
2194 s->flags |= SN_ERR_CLITO;
2195
Willy Tarreaufcffa692010-01-10 14:21:19 +01002196 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002197 goto failed_keep_alive;
2198
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002200 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002201 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002202 session_inc_http_err_ctr(s);
2203 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 txn->status = 408;
2205 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2206 msg->msg_state = HTTP_MSG_ERROR;
2207 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002208
Willy Tarreauda7ff642010-06-23 11:44:09 +02002209 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002210 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002211 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002212 if (s->listener->counters)
2213 s->listener->counters->failed_req++;
2214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 if (!(s->flags & SN_FINST_MASK))
2216 s->flags |= SN_FINST_R;
2217 return 0;
2218 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002219
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 /* 4: have we encountered a close ? */
2221 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002222 if (!(s->flags & SN_ERR_MASK))
2223 s->flags |= SN_ERR_CLICL;
2224
Willy Tarreaufcffa692010-01-10 14:21:19 +01002225 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002226 goto failed_keep_alive;
2227
Willy Tarreau4076a152009-04-02 15:18:36 +02002228 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002229 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002230 txn->status = 400;
2231 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2232 msg->msg_state = HTTP_MSG_ERROR;
2233 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002234
Willy Tarreauda7ff642010-06-23 11:44:09 +02002235 session_inc_http_err_ctr(s);
2236 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002237 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002238 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002239 if (s->listener->counters)
2240 s->listener->counters->failed_req++;
2241
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 if (!(s->flags & SN_FINST_MASK))
2243 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002244 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002245 }
2246
Willy Tarreau520d95e2009-09-19 21:04:57 +02002247 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002248 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002249 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002250#ifdef TCP_QUICKACK
2251 if (s->listener->options & LI_O_NOQUICKACK) {
2252 /* We need more data, we have to re-enable quick-ack in case we
2253 * previously disabled it, otherwise we might cause the client
2254 * to delay next data.
2255 */
2256 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2257 }
2258#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002259
Willy Tarreaufcffa692010-01-10 14:21:19 +01002260 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2261 /* If the client starts to talk, let's fall back to
2262 * request timeout processing.
2263 */
2264 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002265 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002266 }
2267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002269 if (!tick_isset(req->analyse_exp)) {
2270 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2271 (txn->flags & TX_WAIT_NEXT_RQ) &&
2272 tick_isset(s->be->timeout.httpka))
2273 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2274 else
2275 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2276 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 /* we're not ready yet */
2279 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002280
2281 failed_keep_alive:
2282 /* Here we process low-level errors for keep-alive requests. In
2283 * short, if the request is not the first one and it experiences
2284 * a timeout, read error or shutdown, we just silently close so
2285 * that the client can try again.
2286 */
2287 txn->status = 0;
2288 msg->msg_state = HTTP_MSG_RQBEFORE;
2289 req->analysers = 0;
2290 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002291 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002292 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002293 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002294 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002295
Willy Tarreaud787e662009-07-07 10:14:51 +02002296 /* OK now we have a complete HTTP request with indexed headers. Let's
2297 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002298 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2299 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002300 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002301 * byte after the last LF. msg->sov points to the first byte of data.
2302 * msg->eol cannot be trusted because it may have been left uninitialized
2303 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002304 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002305
Willy Tarreauda7ff642010-06-23 11:44:09 +02002306 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002307 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2308
Willy Tarreaub16a5742010-01-10 14:46:16 +01002309 if (txn->flags & TX_WAIT_NEXT_RQ) {
2310 /* kill the pending keep-alive timeout */
2311 txn->flags &= ~TX_WAIT_NEXT_RQ;
2312 req->analyse_exp = TICK_ETERNITY;
2313 }
2314
2315
Willy Tarreaud787e662009-07-07 10:14:51 +02002316 /* Maybe we found in invalid header name while we were configured not
2317 * to block on that, so we have to capture it now.
2318 */
2319 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002320 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002321
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 /*
2323 * 1: identify the method
2324 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002325 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002326
2327 /* we can make use of server redirect on GET and HEAD */
2328 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2329 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /*
2332 * 2: check if the URI matches the monitor_uri.
2333 * We have to do this for every request which gets in, because
2334 * the monitor-uri is defined by the frontend.
2335 */
2336 if (unlikely((s->fe->monitor_uri_len != 0) &&
2337 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002338 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 s->fe->monitor_uri,
2340 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002341 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002343 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002347 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2351 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 ret = acl_pass(ret);
2354 if (cond->pol == ACL_COND_UNLESS)
2355 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 if (ret) {
2358 /* we fail this request, let's return 503 service unavail */
2359 txn->status = 503;
2360 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2361 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002362 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /* nothing to fail, let's reply normaly */
2366 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002367 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 goto return_prx_cond;
2369 }
2370
2371 /*
2372 * 3: Maybe we have to copy the original REQURI for the logs ?
2373 * Note: we cannot log anymore if the request has been
2374 * classified as invalid.
2375 */
2376 if (unlikely(s->logs.logwait & LW_REQ)) {
2377 /* we have a complete HTTP request that we must log */
2378 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2379 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (urilen >= REQURI_LEN)
2382 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002383 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 if (!(s->logs.logwait &= ~LW_REQ))
2387 s->do_log(s);
2388 } else {
2389 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 }
Willy Tarreau06619262006-12-17 08:37:22 +01002392
William Lallemanda73203e2012-03-12 12:48:57 +01002393 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2394 s->unique_id = pool_alloc2(pool2_uniqueid);
2395 }
2396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002398 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002399 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau5b154472009-12-21 20:11:07 +01002401 /* ... and check if the request is HTTP/1.1 or above */
2402 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002403 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2404 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2405 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002406 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002407
2408 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002409 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002410
Willy Tarreau88d349d2010-01-25 12:15:43 +01002411 /* if the frontend has "option http-use-proxy-header", we'll check if
2412 * we have what looks like a proxied connection instead of a connection,
2413 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2414 * Note that this is *not* RFC-compliant, however browsers and proxies
2415 * happen to do that despite being non-standard :-(
2416 * We consider that a request not beginning with either '/' or '*' is
2417 * a proxied connection, which covers both "scheme://location" and
2418 * CONNECT ip:port.
2419 */
2420 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002421 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002422 txn->flags |= TX_USE_PX_CONN;
2423
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002424 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002425 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002428 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002429 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002431
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002432 /* 6: determine the transfer-length.
2433 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2434 * the presence of a message-body in a REQUEST and its transfer length
2435 * must be determined that way (in order of precedence) :
2436 * 1. The presence of a message-body in a request is signaled by the
2437 * inclusion of a Content-Length or Transfer-Encoding header field
2438 * in the request's header fields. When a request message contains
2439 * both a message-body of non-zero length and a method that does
2440 * not define any semantics for that request message-body, then an
2441 * origin server SHOULD either ignore the message-body or respond
2442 * with an appropriate error message (e.g., 413). A proxy or
2443 * gateway, when presented the same request, SHOULD either forward
2444 * the request inbound with the message- body or ignore the
2445 * message-body when determining a response.
2446 *
2447 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2448 * and the "chunked" transfer-coding (Section 6.2) is used, the
2449 * transfer-length is defined by the use of this transfer-coding.
2450 * If a Transfer-Encoding header field is present and the "chunked"
2451 * transfer-coding is not present, the transfer-length is defined
2452 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002453 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002454 * 3. If a Content-Length header field is present, its decimal value in
2455 * OCTETs represents both the entity-length and the transfer-length.
2456 * If a message is received with both a Transfer-Encoding header
2457 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002459 * 4. By the server closing the connection. (Closing the connection
2460 * cannot be used to indicate the end of a request body, since that
2461 * would leave no possibility for the server to send back a response.)
2462 *
2463 * Whenever a transfer-coding is applied to a message-body, the set of
2464 * transfer-codings MUST include "chunked", unless the message indicates
2465 * it is terminated by closing the connection. When the "chunked"
2466 * transfer-coding is used, it MUST be the last transfer-coding applied
2467 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002468 */
2469
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002470 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002471 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002472 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002473 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002474 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002475 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002476 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2477 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002478 /* bad transfer-encoding (chunked followed by something else) */
2479 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002480 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 break;
2482 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 }
2484
Willy Tarreau32b47f42009-10-18 20:55:02 +02002485 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002486 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002487 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 signed long long cl;
2489
Willy Tarreauad14f752011-09-02 20:33:27 +02002490 if (!ctx.vlen) {
2491 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002492 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002493 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494
Willy Tarreauad14f752011-09-02 20:33:27 +02002495 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2496 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002498 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002499
Willy Tarreauad14f752011-09-02 20:33:27 +02002500 if (cl < 0) {
2501 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002502 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002503 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002504
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002505 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002506 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002507 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002508 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002509
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002510 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002511 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002512 }
2513
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002514 /* bodyless requests have a known length */
2515 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002516 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002517
Willy Tarreaud787e662009-07-07 10:14:51 +02002518 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002519 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002520 req->analyse_exp = TICK_ETERNITY;
2521 return 1;
2522
2523 return_bad_req:
2524 /* We centralize bad requests processing here */
2525 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2526 /* we detected a parsing error. We want to archive this request
2527 * in the dedicated proxy area for later troubleshooting.
2528 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002529 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002530 }
2531
2532 txn->req.msg_state = HTTP_MSG_ERROR;
2533 txn->status = 400;
2534 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002535
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002536 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002537 if (s->listener->counters)
2538 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002539
2540 return_prx_cond:
2541 if (!(s->flags & SN_ERR_MASK))
2542 s->flags |= SN_ERR_PRXCOND;
2543 if (!(s->flags & SN_FINST_MASK))
2544 s->flags |= SN_FINST_R;
2545
2546 req->analysers = 0;
2547 req->analyse_exp = TICK_ETERNITY;
2548 return 0;
2549}
2550
Cyril Bonté70be45d2010-10-12 00:14:35 +02002551/* We reached the stats page through a POST request.
2552 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002553 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002554 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002555int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002556{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002557 struct proxy *px = NULL;
2558 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002559
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002560 char key[LINESIZE];
2561 int action = ST_ADM_ACTION_NONE;
2562 int reprocess = 0;
2563
2564 int total_servers = 0;
2565 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002566
2567 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002568 char *st_cur_param = NULL;
2569 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002570
Willy Tarreauea1175a2012-03-05 15:52:30 +01002571 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002572 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002573
2574 cur_param = next_param = end_params;
2575
Cyril Bonté23b39d92011-02-10 22:54:44 +01002576 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002577 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002578 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579 return 1;
2580 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002581 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002582 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002583 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002584 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002585 }
2586
2587 *end_params = '\0';
2588
Willy Tarreau295a8372011-03-10 11:25:07 +01002589 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002590
2591 /*
2592 * Parse the parameters in reverse order to only store the last value.
2593 * From the html form, the backend and the action are at the end.
2594 */
2595 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002596 char *value;
2597 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002598
2599 cur_param--;
2600 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002601 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002602 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002603 poffset = (cur_param != first_param ? 1 : 0);
2604 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2605 if ((plen > 0) && (plen <= sizeof(key))) {
2606 strncpy(key, cur_param + poffset, plen);
2607 key[plen - 1] = '\0';
2608 } else {
2609 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2610 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002611 }
2612
2613 /* Parse the value */
2614 value = key;
2615 while (*value != '\0' && *value != '=') {
2616 value++;
2617 }
2618 if (*value == '=') {
2619 /* Ok, a value is found, we can mark the end of the key */
2620 *value++ = '\0';
2621 }
2622
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002623 if (!url_decode(key) || !url_decode(value))
2624 break;
2625
Cyril Bonté70be45d2010-10-12 00:14:35 +02002626 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002627 if (!px && (strcmp(key, "b") == 0)) {
2628 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2629 /* the backend name is unknown or ambiguous (duplicate names) */
2630 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2631 goto out;
2632 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002633 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002634 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002635 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002636 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002637 }
2638 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002639 action = ST_ADM_ACTION_ENABLE;
2640 }
2641 else {
2642 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2643 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002644 }
2645 }
2646 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002647 if (!(px && action)) {
2648 /*
2649 * Indicates that we'll need to reprocess the parameters
2650 * as soon as backend and action are known
2651 */
2652 if (!reprocess) {
2653 st_cur_param = cur_param;
2654 st_next_param = next_param;
2655 }
2656 reprocess = 1;
2657 }
2658 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002659 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002660 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002661 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002662 /* Not already in maintenance, we can change the server state */
2663 sv->state |= SRV_MAINTAIN;
2664 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002665 altered_servers++;
2666 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002667 }
2668 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002669 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002670 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002671 /* Already in maintenance, we can change the server state */
2672 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002673 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002674 altered_servers++;
2675 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002676 }
2677 break;
2678 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002679 } else {
2680 /* the server name is unknown or ambiguous (duplicate names) */
2681 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002682 }
2683 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002684 if (reprocess && px && action) {
2685 /* Now, we know the backend and the action chosen by the user.
2686 * We can safely restart from the first server parameter
2687 * to reprocess them
2688 */
2689 cur_param = st_cur_param;
2690 next_param = st_next_param;
2691 reprocess = 0;
2692 goto reprocess_servers;
2693 }
2694
Cyril Bonté70be45d2010-10-12 00:14:35 +02002695 next_param = cur_param;
2696 }
2697 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002698
2699 if (total_servers == 0) {
2700 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2701 }
2702 else if (altered_servers == 0) {
2703 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2704 }
2705 else if (altered_servers == total_servers) {
2706 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2707 }
2708 else {
2709 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2710 }
2711 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002712 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002713}
2714
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002715/* returns a pointer to the first rule which forbids access (deny or http_auth),
2716 * or NULL if everything's OK.
2717 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002718static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002719http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2720{
Willy Tarreauff011f22011-01-06 17:51:27 +01002721 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002722
Willy Tarreauff011f22011-01-06 17:51:27 +01002723 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002724 int ret = 1;
2725
Willy Tarreauff011f22011-01-06 17:51:27 +01002726 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002727 continue;
2728
2729 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002730 if (rule->cond) {
2731 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002732 ret = acl_pass(ret);
2733
Willy Tarreauff011f22011-01-06 17:51:27 +01002734 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002735 ret = !ret;
2736 }
2737
2738 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002739 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002740 return NULL; /* no problem */
2741 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002742 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002743 }
2744 }
2745 return NULL;
2746}
2747
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748/* This stream analyser runs all HTTP request processing which is common to
2749 * frontends and backends, which means blocking ACLs, filters, connection-close,
2750 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 * either needs more data or wants to immediately abort the request (eg: deny,
2753 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002754 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002756{
Willy Tarreaud787e662009-07-07 10:14:51 +02002757 struct http_txn *txn = &s->txn;
2758 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002759 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002760 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002762 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002763 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002764
Willy Tarreau655dce92009-11-08 13:10:58 +01002765 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002766 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002767 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002768 return 0;
2769 }
2770
Willy Tarreau3a816292009-07-07 10:55:49 +02002771 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 req->analyse_exp = TICK_ETERNITY;
2773
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002774 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 +02002775 now_ms, __FUNCTION__,
2776 s,
2777 req,
2778 req->rex, req->wex,
2779 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002780 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002781 req->analysers);
2782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783 /* first check whether we have some ACLs set to block this request */
2784 list_for_each_entry(cond, &px->block_cond, list) {
2785 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002786
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 ret = acl_pass(ret);
2788 if (cond->pol == ACL_COND_UNLESS)
2789 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 if (ret) {
2792 txn->status = 403;
2793 /* let's log the request time */
2794 s->logs.tv_request = now;
2795 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002796 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002799 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002800
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002801 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002802 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002803
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002804 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002805 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002806 do_stats = stats_check_uri(s->rep->prod, txn, px);
2807 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002808 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 +01002809 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002810 else
2811 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002812
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002813 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002814 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002815 txn->status = 403;
2816 s->logs.tv_request = now;
2817 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002818 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002819 s->fe->fe_counters.denied_req++;
2820 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2821 s->be->be_counters.denied_req++;
2822 if (s->listener->counters)
2823 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002824 goto return_prx_cond;
2825 }
2826
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002827 /* try headers filters */
2828 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002829 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002831
Willy Tarreau59234e92008-11-30 23:51:27 +01002832 /* has the request been denied ? */
2833 if (txn->flags & TX_CLDENY) {
2834 /* no need to go further */
2835 txn->status = 403;
2836 /* let's log the request time */
2837 s->logs.tv_request = now;
2838 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002839 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 goto return_prx_cond;
2841 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002842
2843 /* When a connection is tarpitted, we use the tarpit timeout,
2844 * which may be the same as the connect timeout if unspecified.
2845 * If unset, then set it to zero because we really want it to
2846 * eventually expire. We build the tarpit as an analyser.
2847 */
2848 if (txn->flags & TX_CLTARPIT) {
2849 buffer_erase(s->req);
2850 /* wipe the request out so that we can drop the connection early
2851 * if the client closes first.
2852 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002853 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002854 req->analysers = 0; /* remove switching rules etc... */
2855 req->analysers |= AN_REQ_HTTP_TARPIT;
2856 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2857 if (!req->analyse_exp)
2858 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002859 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002860 return 1;
2861 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002862 }
Willy Tarreau06619262006-12-17 08:37:22 +01002863
Willy Tarreau5b154472009-12-21 20:11:07 +01002864 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2865 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002866 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2867 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002868 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2869 * avoid to redo the same work if FE and BE have the same settings (common).
2870 * The method consists in checking if options changed between the two calls
2871 * (implying that either one is non-null, or one of them is non-null and we
2872 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002873 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002874
Willy Tarreaudc008c52010-02-01 16:20:08 +01002875 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2876 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2877 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2878 (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 +01002879 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002880
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002881 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2882 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002883 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002884 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2885 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002886 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002887 tmp = TX_CON_WANT_CLO;
2888
Willy Tarreau5b154472009-12-21 20:11:07 +01002889 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2890 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002891
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002892 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2893 /* parse the Connection header and possibly clean it */
2894 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002895 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002896 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2897 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002898 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002899 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002900 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002901 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002902 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002903
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002904 /* check if client or config asks for explicit close in KAL/SCL */
2905 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2906 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2907 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002908 (!(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 +01002909 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002910 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002911 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002912 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2913 }
Willy Tarreau78599912009-10-17 20:12:21 +02002914
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002915 /* we can be blocked here because the request needs to be authenticated,
2916 * either to pass or to access stats.
2917 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002918 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002919 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002920 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002921
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002922 if (!realm)
2923 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2924
Willy Tarreau844a7e72010-01-31 21:46:18 +01002925 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002926 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2927 txn->status = 401;
2928 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002929 /* on 401 we still count one error, because normal browsing
2930 * won't significantly increase the counter but brute force
2931 * attempts will.
2932 */
2933 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002934 goto return_prx_cond;
2935 }
2936
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002937 /* add request headers from the rule sets in the same order */
2938 list_for_each_entry(wl, &px->req_add, list) {
2939 if (wl->cond) {
2940 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2941 ret = acl_pass(ret);
2942 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2943 ret = !ret;
2944 if (!ret)
2945 continue;
2946 }
2947
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002948 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002949 goto return_bad_req;
2950 }
2951
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002952 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002953 struct stats_admin_rule *stats_admin_rule;
2954
2955 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002956 * FIXME!!! that one is rather dangerous, we want to
2957 * make it follow standard rules (eg: clear req->analysers).
2958 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002959
Cyril Bonté474be412010-10-12 00:14:36 +02002960 /* now check whether we have some admin rules for this request */
2961 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2962 int ret = 1;
2963
2964 if (stats_admin_rule->cond) {
2965 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2966 ret = acl_pass(ret);
2967 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2968 ret = !ret;
2969 }
2970
2971 if (ret) {
2972 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002973 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002974 break;
2975 }
2976 }
2977
Cyril Bonté70be45d2010-10-12 00:14:35 +02002978 /* Was the status page requested with a POST ? */
2979 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002980 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002981 if (msg->msg_state < HTTP_MSG_100_SENT) {
2982 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2983 * send an HTTP/1.1 100 Continue intermediate response.
2984 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002985 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002986 struct hdr_ctx ctx;
2987 ctx.idx = 0;
2988 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002989 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002990 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2991 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2992 }
2993 }
2994 msg->msg_state = HTTP_MSG_100_SENT;
2995 s->logs.tv_request = now; /* update the request timer to reflect full request */
2996 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002997 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002998 /* we need more data */
2999 req->analysers |= an_bit;
3000 buffer_dont_connect(req);
3001 return 0;
3002 }
Cyril Bonté474be412010-10-12 00:14:36 +02003003 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003004 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003005 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003006 }
3007
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003008 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003009 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003010 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003011 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003012 s->rep->prod->applet.private = s;
3013 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003014 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003015 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3016 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003017
3018 return 0;
3019
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003021
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003022 /* check whether we have some ACLs set to redirect this request */
3023 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003024 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003025
Willy Tarreauf285f542010-01-03 20:03:03 +01003026 if (rule->cond) {
3027 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3028 ret = acl_pass(ret);
3029 if (rule->cond->pol == ACL_COND_UNLESS)
3030 ret = !ret;
3031 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003033 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003034 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003036
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 /* build redirect message */
3038 switch(rule->code) {
3039 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003040 msg_fmt = HTTP_303;
3041 break;
3042 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 msg_fmt = HTTP_301;
3044 break;
3045 case 302:
3046 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 msg_fmt = HTTP_302;
3048 break;
3049 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003050
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003051 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003052 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003053
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 switch(rule->type) {
3055 case REDIRECT_TYPE_PREFIX: {
3056 const char *path;
3057 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 path = http_get_path(txn);
3060 /* build message using path */
3061 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003062 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3064 int qs = 0;
3065 while (qs < pathlen) {
3066 if (path[qs] == '?') {
3067 pathlen = qs;
3068 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003069 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003070 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 } else {
3074 path = "/";
3075 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003077
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003078 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 goto return_bad_req;
3080
3081 /* add prefix. Note that if prefix == "/", we don't want to
3082 * add anything, otherwise it makes it hard for the user to
3083 * configure a self-redirection.
3084 */
3085 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003086 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3087 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003088 }
3089
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090 /* add path */
3091 memcpy(rdr.str + rdr.len, path, pathlen);
3092 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003093
3094 /* append a slash at the end of the location is needed and missing */
3095 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3096 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3097 if (rdr.len > rdr.size - 5)
3098 goto return_bad_req;
3099 rdr.str[rdr.len] = '/';
3100 rdr.len++;
3101 }
3102
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003103 break;
3104 }
3105 case REDIRECT_TYPE_LOCATION:
3106 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003107 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003108 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003109
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 /* add location */
3111 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3112 rdr.len += rule->rdr_len;
3113 break;
3114 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003115
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 if (rule->cookie_len) {
3117 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3118 rdr.len += 14;
3119 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3120 rdr.len += rule->cookie_len;
3121 memcpy(rdr.str + rdr.len, "\r\n", 2);
3122 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003124
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003125 /* add end of headers and the keep-alive/close status.
3126 * We may choose to set keep-alive if the Location begins
3127 * with a slash, because the client will come back to the
3128 * same server.
3129 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 txn->status = rule->code;
3131 /* let's log the request time */
3132 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003133
3134 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003135 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3136 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003137 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3138 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3139 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003140 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003141 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3142 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3143 rdr.len += 30;
3144 } else {
3145 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3146 rdr.len += 24;
3147 }
Willy Tarreau75661452010-01-10 10:35:01 +01003148 }
3149 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3150 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003151 buffer_write(req->prod->ob, rdr.str, rdr.len);
3152 /* "eat" the request */
3153 buffer_ignore(req, msg->sov - msg->som);
3154 msg->som = msg->sov;
3155 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003156 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3157 txn->req.msg_state = HTTP_MSG_CLOSED;
3158 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003159 break;
3160 } else {
3161 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003162 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3163 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3164 rdr.len += 29;
3165 } else {
3166 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3167 rdr.len += 23;
3168 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003169 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003170 goto return_prx_cond;
3171 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003172 }
3173 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003174
Willy Tarreau2be39392010-01-03 17:24:51 +01003175 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3176 * If this happens, then the data will not come immediately, so we must
3177 * send all what we have without waiting. Note that due to the small gain
3178 * in waiting for the body of the request, it's easier to simply put the
3179 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3180 * itself once used.
3181 */
3182 req->flags |= BF_SEND_DONTWAIT;
3183
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003184 /* that's OK for us now, let's move on to next analysers */
3185 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003186
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003187 return_bad_req:
3188 /* We centralize bad requests processing here */
3189 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3190 /* we detected a parsing error. We want to archive this request
3191 * in the dedicated proxy area for later troubleshooting.
3192 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003193 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003194 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003195
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003196 txn->req.msg_state = HTTP_MSG_ERROR;
3197 txn->status = 400;
3198 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003199
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003200 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003201 if (s->listener->counters)
3202 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 return_prx_cond:
3205 if (!(s->flags & SN_ERR_MASK))
3206 s->flags |= SN_ERR_PRXCOND;
3207 if (!(s->flags & SN_FINST_MASK))
3208 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 req->analysers = 0;
3211 req->analyse_exp = TICK_ETERNITY;
3212 return 0;
3213}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003214
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003215/* This function performs all the processing enabled for the current request.
3216 * It returns 1 if the processing can continue on next analysers, or zero if it
3217 * needs more data, encounters an error, or wants to immediately abort the
3218 * request. It relies on buffers flags, and updates s->req->analysers.
3219 */
3220int http_process_request(struct session *s, struct buffer *req, int an_bit)
3221{
3222 struct http_txn *txn = &s->txn;
3223 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003224
Willy Tarreau655dce92009-11-08 13:10:58 +01003225 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003226 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003227 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003228 return 0;
3229 }
3230
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003231 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 +02003232 now_ms, __FUNCTION__,
3233 s,
3234 req,
3235 req->rex, req->wex,
3236 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003237 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003238 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003239
Willy Tarreau59234e92008-11-30 23:51:27 +01003240 /*
3241 * Right now, we know that we have processed the entire headers
3242 * and that unwanted requests have been filtered out. We can do
3243 * whatever we want with the remaining request. Also, now we
3244 * may have separate values for ->fe, ->be.
3245 */
Willy Tarreau06619262006-12-17 08:37:22 +01003246
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 /*
3248 * If HTTP PROXY is set we simply get remote server address
3249 * parsing incoming request.
3250 */
3251 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003252 url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003253 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003254
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003256 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003257 * Note that doing so might move headers in the request, but
3258 * the fields will stay coherent and the URI will not move.
3259 * This should only be performed in the backend.
3260 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003261 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3263 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003264
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003266 * 8: the appsession cookie was looked up very early in 1.2,
3267 * so let's do the same now.
3268 */
3269
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003270 /* It needs to look into the URI unless persistence must be ignored */
3271 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003272 get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003273 }
3274
William Lallemanda73203e2012-03-12 12:48:57 +01003275 /* add unique-id if "header-unique-id" is specified */
3276
3277 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3278 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3279
3280 if (s->fe->header_unique_id && s->unique_id) {
3281 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3282 if (ret < 0 || ret > global.tune.bufsize)
3283 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003284 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003285 goto return_bad_req;
3286 }
3287
Cyril Bontéb21570a2009-11-29 20:04:48 +01003288 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003289 * 9: add X-Forwarded-For if either the frontend or the backend
3290 * asks for it.
3291 */
3292 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003293 struct hdr_ctx ctx = { .idx = 0 };
3294
3295 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003296 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003297 /* The header is set to be added only if none is present
3298 * and we found it, so don't do anything.
3299 */
3300 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003301 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 /* Add an X-Forwarded-For header unless the source IP is
3303 * in the 'except' network range.
3304 */
3305 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003306 (((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 +01003307 != s->fe->except_net.s_addr) &&
3308 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003309 (((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 +01003310 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003311 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003313 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003314
3315 /* Note: we rely on the backend to get the header name to be used for
3316 * x-forwarded-for, because the header is really meant for the backends.
3317 * However, if the backend did not specify any option, we have to rely
3318 * on the frontend's header name.
3319 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 if (s->be->fwdfor_hdr_len) {
3321 len = s->be->fwdfor_hdr_len;
3322 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003323 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 len = s->fe->fwdfor_hdr_len;
3325 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003326 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003327 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003328
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003329 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003330 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003331 }
3332 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003333 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003334 /* FIXME: for the sake of completeness, we should also support
3335 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003336 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 int len;
3338 char pn[INET6_ADDRSTRLEN];
3339 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003340 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003341 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003342
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 /* Note: we rely on the backend to get the header name to be used for
3344 * x-forwarded-for, because the header is really meant for the backends.
3345 * However, if the backend did not specify any option, we have to rely
3346 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003347 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003348 if (s->be->fwdfor_hdr_len) {
3349 len = s->be->fwdfor_hdr_len;
3350 memcpy(trash, s->be->fwdfor_hdr_name, len);
3351 } else {
3352 len = s->fe->fwdfor_hdr_len;
3353 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003354 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003355 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003356
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003357 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003358 goto return_bad_req;
3359 }
3360 }
3361
3362 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003363 * 10: add X-Original-To if either the frontend or the backend
3364 * asks for it.
3365 */
3366 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3367
3368 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003369 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003370 /* Add an X-Original-To header unless the destination IP is
3371 * in the 'except' network range.
3372 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003373 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003374
Willy Tarreau6471afb2011-09-23 10:54:59 +02003375 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003376 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003377 (((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 +02003378 != s->fe->except_to.s_addr) &&
3379 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003380 (((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 +02003381 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003382 int len;
3383 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003384 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003385
3386 /* Note: we rely on the backend to get the header name to be used for
3387 * x-original-to, because the header is really meant for the backends.
3388 * However, if the backend did not specify any option, we have to rely
3389 * on the frontend's header name.
3390 */
3391 if (s->be->orgto_hdr_len) {
3392 len = s->be->orgto_hdr_len;
3393 memcpy(trash, s->be->orgto_hdr_name, len);
3394 } else {
3395 len = s->fe->orgto_hdr_len;
3396 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003397 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003398 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3399
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003400 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003401 goto return_bad_req;
3402 }
3403 }
3404 }
3405
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003406 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3407 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003408 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003409 unsigned int want_flags = 0;
3410
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003411 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003412 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3413 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3414 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003415 want_flags |= TX_CON_CLO_SET;
3416 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003417 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3418 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3419 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003420 want_flags |= TX_CON_KAL_SET;
3421 }
3422
3423 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003424 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003425 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003426
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003427
Willy Tarreau522d6c02009-12-06 18:49:18 +01003428 /* If we have no server assigned yet and we're balancing on url_param
3429 * with a POST request, we may be interested in checking the body for
3430 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003431 */
3432 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3433 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003434 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003435 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003436 buffer_dont_connect(req);
3437 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003440 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003441 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003442#ifdef TCP_QUICKACK
3443 /* We expect some data from the client. Unless we know for sure
3444 * we already have a full request, we have to re-enable quick-ack
3445 * in case we previously disabled it, otherwise we might cause
3446 * the client to delay further data.
3447 */
3448 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003449 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003450 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003451 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3452#endif
3453 }
Willy Tarreau03945942009-12-22 16:50:27 +01003454
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 /*************************************************************
3456 * OK, that's finished for the headers. We have done what we *
3457 * could. Let's switch to the DATA state. *
3458 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003459 req->analyse_exp = TICK_ETERNITY;
3460 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003461
Willy Tarreau59234e92008-11-30 23:51:27 +01003462 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003463 /* OK let's go on with the BODY now */
3464 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003465
Willy Tarreau59234e92008-11-30 23:51:27 +01003466 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003467 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003468 /* we detected a parsing error. We want to archive this request
3469 * in the dedicated proxy area for later troubleshooting.
3470 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003471 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003472 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003473
Willy Tarreau59234e92008-11-30 23:51:27 +01003474 txn->req.msg_state = HTTP_MSG_ERROR;
3475 txn->status = 400;
3476 req->analysers = 0;
3477 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003478
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003479 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003480 if (s->listener->counters)
3481 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003482
Willy Tarreau59234e92008-11-30 23:51:27 +01003483 if (!(s->flags & SN_ERR_MASK))
3484 s->flags |= SN_ERR_PRXCOND;
3485 if (!(s->flags & SN_FINST_MASK))
3486 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003487 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003488}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003489
Willy Tarreau60b85b02008-11-30 23:28:40 +01003490/* This function is an analyser which processes the HTTP tarpit. It always
3491 * returns zero, at the beginning because it prevents any other processing
3492 * from occurring, and at the end because it terminates the request.
3493 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003494int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003495{
3496 struct http_txn *txn = &s->txn;
3497
3498 /* This connection is being tarpitted. The CLIENT side has
3499 * already set the connect expiration date to the right
3500 * timeout. We just have to check that the client is still
3501 * there and that the timeout has not expired.
3502 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003503 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003504 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3505 !tick_is_expired(req->analyse_exp, now_ms))
3506 return 0;
3507
3508 /* We will set the queue timer to the time spent, just for
3509 * logging purposes. We fake a 500 server error, so that the
3510 * attacker will not suspect his connection has been tarpitted.
3511 * It will not cause trouble to the logs because we can exclude
3512 * the tarpitted connections by filtering on the 'PT' status flags.
3513 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003514 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3515
3516 txn->status = 500;
3517 if (req->flags != BF_READ_ERROR)
3518 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3519
3520 req->analysers = 0;
3521 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003522
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003523 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003524 if (s->listener->counters)
3525 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003526
Willy Tarreau60b85b02008-11-30 23:28:40 +01003527 if (!(s->flags & SN_ERR_MASK))
3528 s->flags |= SN_ERR_PRXCOND;
3529 if (!(s->flags & SN_FINST_MASK))
3530 s->flags |= SN_FINST_T;
3531 return 0;
3532}
3533
Willy Tarreaud34af782008-11-30 23:36:37 +01003534/* This function is an analyser which processes the HTTP request body. It looks
3535 * for parameters to be used for the load balancing algorithm (url_param). It
3536 * must only be called after the standard HTTP request processing has occurred,
3537 * because it expects the request to be parsed. It returns zero if it needs to
3538 * read more data, or 1 once it has completed its analysis.
3539 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003540int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003541{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003542 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003543 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003544 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003545
3546 /* We have to parse the HTTP request body to find any required data.
3547 * "balance url_param check_post" should have been the only way to get
3548 * into this. We were brought here after HTTP header analysis, so all
3549 * related structures are ready.
3550 */
3551
Willy Tarreau522d6c02009-12-06 18:49:18 +01003552 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3553 goto missing_data;
3554
3555 if (msg->msg_state < HTTP_MSG_100_SENT) {
3556 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3557 * send an HTTP/1.1 100 Continue intermediate response.
3558 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003559 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003560 struct hdr_ctx ctx;
3561 ctx.idx = 0;
3562 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003563 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3565 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3566 }
3567 }
3568 msg->msg_state = HTTP_MSG_100_SENT;
3569 }
3570
3571 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003572 /* we have msg->sov which points to the first byte of message body.
3573 * msg->som still points to the beginning of the message. We must
3574 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003575 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003576 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003577
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003578 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003579 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3580 else
3581 msg->msg_state = HTTP_MSG_DATA;
3582 }
3583
3584 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003585 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003586 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003587 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003588 */
3589 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003590
Willy Tarreau115acb92009-12-26 13:56:06 +01003591 if (!ret)
3592 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003593 else if (ret < 0) {
3594 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003595 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003596 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003597 }
3598
Willy Tarreaud98cf932009-12-27 22:54:55 +01003599 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003600 * We have the first data byte is in msg->sov. We're waiting for at
3601 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003602 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603
Willy Tarreau124d9912011-03-01 20:30:48 +01003604 if (msg->body_len < limit)
3605 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003606
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003607 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003608 goto http_end;
3609
3610 missing_data:
3611 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003612 if (req->flags & BF_FULL) {
3613 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003614 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003615 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003616
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3618 txn->status = 408;
3619 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003620
3621 if (!(s->flags & SN_ERR_MASK))
3622 s->flags |= SN_ERR_CLITO;
3623 if (!(s->flags & SN_FINST_MASK))
3624 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003625 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003626 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627
3628 /* we get here if we need to wait for more data */
3629 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003630 /* Not enough data. We'll re-use the http-request
3631 * timeout here. Ideally, we should set the timeout
3632 * relative to the accept() date. We just set the
3633 * request timeout once at the beginning of the
3634 * request.
3635 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003636 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003637 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003638 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003639 return 0;
3640 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641
3642 http_end:
3643 /* The situation will not evolve, so let's give up on the analysis. */
3644 s->logs.tv_request = now; /* update the request timer to reflect full request */
3645 req->analysers &= ~an_bit;
3646 req->analyse_exp = TICK_ETERNITY;
3647 return 1;
3648
3649 return_bad_req: /* let's centralize all bad requests */
3650 txn->req.msg_state = HTTP_MSG_ERROR;
3651 txn->status = 400;
3652 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3653
Willy Tarreau79ebac62010-06-07 13:47:49 +02003654 if (!(s->flags & SN_ERR_MASK))
3655 s->flags |= SN_ERR_PRXCOND;
3656 if (!(s->flags & SN_FINST_MASK))
3657 s->flags |= SN_FINST_R;
3658
Willy Tarreau522d6c02009-12-06 18:49:18 +01003659 return_err_msg:
3660 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003661 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003662 if (s->listener->counters)
3663 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003664 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003665}
3666
Willy Tarreau45c0d982012-03-09 12:11:57 +01003667/* send a server's name with an outgoing request over an established connection */
3668int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003669
3670 struct hdr_ctx ctx;
3671
Mark Lamourinec2247f02012-01-04 13:02:01 -05003672 char *hdr_name = be->server_id_hdr_name;
3673 int hdr_name_len = be->server_id_hdr_len;
3674
3675 char *hdr_val;
3676
William Lallemandd9e90662012-01-30 17:27:17 +01003677 ctx.idx = 0;
3678
Willy Tarreau45c0d982012-03-09 12:11:57 +01003679 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003680 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003681 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003682 }
3683
3684 /* Add the new header requested with the server value */
3685 hdr_val = trash;
3686 memcpy(hdr_val, hdr_name, hdr_name_len);
3687 hdr_val += hdr_name_len;
3688 *hdr_val++ = ':';
3689 *hdr_val++ = ' ';
3690 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003691 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003692
3693 return 0;
3694}
3695
Willy Tarreau610ecce2010-01-04 21:15:02 +01003696/* Terminate current transaction and prepare a new one. This is very tricky
3697 * right now but it works.
3698 */
3699void http_end_txn_clean_session(struct session *s)
3700{
3701 /* FIXME: We need a more portable way of releasing a backend's and a
3702 * server's connections. We need a safer way to reinitialize buffer
3703 * flags. We also need a more accurate method for computing per-request
3704 * data.
3705 */
3706 http_silent_debug(__LINE__, s);
3707
3708 s->req->cons->flags |= SI_FL_NOLINGER;
3709 s->req->cons->shutr(s->req->cons);
3710 s->req->cons->shutw(s->req->cons);
3711
3712 http_silent_debug(__LINE__, s);
3713
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003714 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003716 if (unlikely(s->srv_conn))
3717 sess_change_server(s, NULL);
3718 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719
3720 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3721 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003722 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003723
3724 if (s->txn.status) {
3725 int n;
3726
3727 n = s->txn.status / 100;
3728 if (n < 1 || n > 5)
3729 n = 0;
3730
3731 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003732 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733
Willy Tarreau24657792010-02-26 10:30:28 +01003734 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003736 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737 }
3738
3739 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003740 s->logs.bytes_in -= s->req->i;
3741 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742
3743 /* let's do a final log if we need it */
3744 if (s->logs.logwait &&
3745 !(s->flags & SN_MONITOR) &&
3746 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3747 s->do_log(s);
3748 }
3749
3750 s->logs.accept_date = date; /* user-visible date for logging */
3751 s->logs.tv_accept = now; /* corrected date for internal use */
3752 tv_zero(&s->logs.tv_request);
3753 s->logs.t_queue = -1;
3754 s->logs.t_connect = -1;
3755 s->logs.t_data = -1;
3756 s->logs.t_close = 0;
3757 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3758 s->logs.srv_queue_size = 0; /* we will get this number soon */
3759
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003760 s->logs.bytes_in = s->req->total = s->req->i;
3761 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762
3763 if (s->pend_pos)
3764 pendconn_free(s->pend_pos);
3765
Willy Tarreau827aee92011-03-10 16:55:02 +01003766 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 if (s->flags & SN_CURR_SESS) {
3768 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003769 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003771 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3772 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 }
3774
Willy Tarreau9e000c62011-03-10 14:03:36 +01003775 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776
3777 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3778 s->req->cons->fd = -1; /* just to help with debugging */
3779 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003780 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 s->req->cons->err_loc = NULL;
3782 s->req->cons->exp = TICK_ETERNITY;
3783 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003784 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3785 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 +02003786 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 +01003787 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3788 s->txn.meth = 0;
3789 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003790 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003791 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003792 s->req->cons->flags |= SI_FL_INDEP_STR;
3793
Willy Tarreau96e31212011-05-30 18:10:30 +02003794 if (s->fe->options2 & PR_O2_NODELAY) {
3795 s->req->flags |= BF_NEVER_WAIT;
3796 s->rep->flags |= BF_NEVER_WAIT;
3797 }
3798
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 /* if the request buffer is not empty, it means we're
3800 * about to process another request, so send pending
3801 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003802 * Just don't do this if the buffer is close to be full,
3803 * because the request will wait for it to flush a little
3804 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003806 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003807 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003808 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003809 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003810 s->rep->flags |= BF_EXPECT_MORE;
3811 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003812
3813 /* we're removing the analysers, we MUST re-enable events detection */
3814 buffer_auto_read(s->req);
3815 buffer_auto_close(s->req);
3816 buffer_auto_read(s->rep);
3817 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818
Willy Tarreau342b11c2010-11-24 16:22:09 +01003819 s->req->analysers = s->listener->analysers;
3820 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821 s->rep->analysers = 0;
3822
3823 http_silent_debug(__LINE__, s);
3824}
3825
3826
3827/* This function updates the request state machine according to the response
3828 * state machine and buffer flags. It returns 1 if it changes anything (flag
3829 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3830 * it is only used to find when a request/response couple is complete. Both
3831 * this function and its equivalent should loop until both return zero. It
3832 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3833 */
3834int http_sync_req_state(struct session *s)
3835{
3836 struct buffer *buf = s->req;
3837 struct http_txn *txn = &s->txn;
3838 unsigned int old_flags = buf->flags;
3839 unsigned int old_state = txn->req.msg_state;
3840
3841 http_silent_debug(__LINE__, s);
3842 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3843 return 0;
3844
3845 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003846 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003847 * We can shut the read side unless we want to abort_on_close,
3848 * or we have a POST request. The issue with POST requests is
3849 * that some browsers still send a CRLF after the request, and
3850 * this CRLF must be read so that it does not remain in the kernel
3851 * buffers, otherwise a close could cause an RST on some systems
3852 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003853 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003854 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003855 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003856
3857 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3858 goto wait_other_side;
3859
3860 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3861 /* The server has not finished to respond, so we
3862 * don't want to move in order not to upset it.
3863 */
3864 goto wait_other_side;
3865 }
3866
3867 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3868 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003869 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870 txn->req.msg_state = HTTP_MSG_TUNNEL;
3871 goto wait_other_side;
3872 }
3873
3874 /* When we get here, it means that both the request and the
3875 * response have finished receiving. Depending on the connection
3876 * mode, we'll have to wait for the last bytes to leave in either
3877 * direction, and sometimes for a close to be effective.
3878 */
3879
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003880 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3881 /* Server-close mode : queue a connection close to the server */
3882 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003883 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003884 }
3885 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3886 /* Option forceclose is set, or either side wants to close,
3887 * let's enforce it now that we're not expecting any new
3888 * data to come. The caller knows the session is complete
3889 * once both states are CLOSED.
3890 */
3891 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892 buffer_shutr_now(buf);
3893 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003895 }
3896 else {
3897 /* The last possible modes are keep-alive and tunnel. Since tunnel
3898 * mode does not set the body analyser, we can't reach this place
3899 * in tunnel mode, so we're left with keep-alive only.
3900 * This mode is currently not implemented, we switch to tunnel mode.
3901 */
3902 buffer_auto_read(buf);
3903 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 }
3905
3906 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3907 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003908 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3909
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 if (!(buf->flags & BF_OUT_EMPTY)) {
3911 txn->req.msg_state = HTTP_MSG_CLOSING;
3912 goto http_msg_closing;
3913 }
3914 else {
3915 txn->req.msg_state = HTTP_MSG_CLOSED;
3916 goto http_msg_closed;
3917 }
3918 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003919 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003920 }
3921
3922 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3923 http_msg_closing:
3924 /* nothing else to forward, just waiting for the output buffer
3925 * to be empty and for the shutw_now to take effect.
3926 */
3927 if (buf->flags & BF_OUT_EMPTY) {
3928 txn->req.msg_state = HTTP_MSG_CLOSED;
3929 goto http_msg_closed;
3930 }
3931 else if (buf->flags & BF_SHUTW) {
3932 txn->req.msg_state = HTTP_MSG_ERROR;
3933 goto wait_other_side;
3934 }
3935 }
3936
3937 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3938 http_msg_closed:
3939 goto wait_other_side;
3940 }
3941
3942 wait_other_side:
3943 http_silent_debug(__LINE__, s);
3944 return txn->req.msg_state != old_state || buf->flags != old_flags;
3945}
3946
3947
3948/* This function updates the response state machine according to the request
3949 * state machine and buffer flags. It returns 1 if it changes anything (flag
3950 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3951 * it is only used to find when a request/response couple is complete. Both
3952 * this function and its equivalent should loop until both return zero. It
3953 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3954 */
3955int http_sync_res_state(struct session *s)
3956{
3957 struct buffer *buf = s->rep;
3958 struct http_txn *txn = &s->txn;
3959 unsigned int old_flags = buf->flags;
3960 unsigned int old_state = txn->rsp.msg_state;
3961
3962 http_silent_debug(__LINE__, s);
3963 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3964 return 0;
3965
3966 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3967 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003968 * still monitor the server connection for a possible close
3969 * while the request is being uploaded, so we don't disable
3970 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003972 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973
3974 if (txn->req.msg_state == HTTP_MSG_ERROR)
3975 goto wait_other_side;
3976
3977 if (txn->req.msg_state < HTTP_MSG_DONE) {
3978 /* The client seems to still be sending data, probably
3979 * because we got an error response during an upload.
3980 * We have the choice of either breaking the connection
3981 * or letting it pass through. Let's do the later.
3982 */
3983 goto wait_other_side;
3984 }
3985
3986 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3987 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003988 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003989 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3990 goto wait_other_side;
3991 }
3992
3993 /* When we get here, it means that both the request and the
3994 * response have finished receiving. Depending on the connection
3995 * mode, we'll have to wait for the last bytes to leave in either
3996 * direction, and sometimes for a close to be effective.
3997 */
3998
3999 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4000 /* Server-close mode : shut read and wait for the request
4001 * side to close its output buffer. The caller will detect
4002 * when we're in DONE and the other is in CLOSED and will
4003 * catch that for the final cleanup.
4004 */
4005 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4006 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004008 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4009 /* Option forceclose is set, or either side wants to close,
4010 * let's enforce it now that we're not expecting any new
4011 * data to come. The caller knows the session is complete
4012 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4015 buffer_shutr_now(buf);
4016 buffer_shutw_now(buf);
4017 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 }
4019 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004020 /* The last possible modes are keep-alive and tunnel. Since tunnel
4021 * mode does not set the body analyser, we can't reach this place
4022 * in tunnel mode, so we're left with keep-alive only.
4023 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004025 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027 }
4028
4029 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4030 /* if we've just closed an output, let's switch */
4031 if (!(buf->flags & BF_OUT_EMPTY)) {
4032 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4033 goto http_msg_closing;
4034 }
4035 else {
4036 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4037 goto http_msg_closed;
4038 }
4039 }
4040 goto wait_other_side;
4041 }
4042
4043 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4044 http_msg_closing:
4045 /* nothing else to forward, just waiting for the output buffer
4046 * to be empty and for the shutw_now to take effect.
4047 */
4048 if (buf->flags & BF_OUT_EMPTY) {
4049 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4050 goto http_msg_closed;
4051 }
4052 else if (buf->flags & BF_SHUTW) {
4053 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004054 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004055 if (target_srv(&s->target))
4056 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 goto wait_other_side;
4058 }
4059 }
4060
4061 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4062 http_msg_closed:
4063 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004064 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004066 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004067 goto wait_other_side;
4068 }
4069
4070 wait_other_side:
4071 http_silent_debug(__LINE__, s);
4072 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4073}
4074
4075
4076/* Resync the request and response state machines. Return 1 if either state
4077 * changes.
4078 */
4079int http_resync_states(struct session *s)
4080{
4081 struct http_txn *txn = &s->txn;
4082 int old_req_state = txn->req.msg_state;
4083 int old_res_state = txn->rsp.msg_state;
4084
4085 http_silent_debug(__LINE__, s);
4086 http_sync_req_state(s);
4087 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004088 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004089 if (!http_sync_res_state(s))
4090 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004091 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004092 if (!http_sync_req_state(s))
4093 break;
4094 }
4095 http_silent_debug(__LINE__, s);
4096 /* OK, both state machines agree on a compatible state.
4097 * There are a few cases we're interested in :
4098 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4099 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4100 * directions, so let's simply disable both analysers.
4101 * - HTTP_MSG_CLOSED on the response only means we must abort the
4102 * request.
4103 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4104 * with server-close mode means we've completed one request and we
4105 * must re-initialize the server connection.
4106 */
4107
4108 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4109 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4110 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4111 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4112 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004113 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004114 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004116 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004117 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004119 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4120 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004121 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004122 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 s->rep->analysers = 0;
4124 buffer_auto_close(s->rep);
4125 buffer_auto_read(s->rep);
4126 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004127 buffer_abort(s->req);
4128 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004130 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004131 }
4132 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4133 txn->rsp.msg_state == HTTP_MSG_DONE &&
4134 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4135 /* server-close: terminate this server connection and
4136 * reinitialize a fresh-new transaction.
4137 */
4138 http_end_txn_clean_session(s);
4139 }
4140
4141 http_silent_debug(__LINE__, s);
4142 return txn->req.msg_state != old_req_state ||
4143 txn->rsp.msg_state != old_res_state;
4144}
4145
Willy Tarreaud98cf932009-12-27 22:54:55 +01004146/* This function is an analyser which forwards request body (including chunk
4147 * sizes if any). It is called as soon as we must forward, even if we forward
4148 * zero byte. The only situation where it must not be called is when we're in
4149 * tunnel mode and we want to forward till the close. It's used both to forward
4150 * remaining data and to resync after end of body. It expects the msg_state to
4151 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4152 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004153 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004154 * bytes of pending data + the headers if not already done (between som and sov).
4155 * It eventually adjusts som to match sov after the data in between have been sent.
4156 */
4157int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4158{
4159 struct http_txn *txn = &s->txn;
4160 struct http_msg *msg = &s->txn.req;
4161
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004162 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4163 return 0;
4164
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004165 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004166 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004167 /* Output closed while we were sending data. We must abort and
4168 * wake the other side up.
4169 */
4170 msg->msg_state = HTTP_MSG_ERROR;
4171 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004172 return 1;
4173 }
4174
Willy Tarreau4fe41902010-06-07 22:27:41 +02004175 /* in most states, we should abort in case of early close */
4176 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004177
4178 /* Note that we don't have to send 100-continue back because we don't
4179 * need the data to complete our job, and it's up to the server to
4180 * decide whether to return 100, 417 or anything else in return of
4181 * an "Expect: 100-continue" header.
4182 */
4183
4184 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004185 /* we have msg->sov which points to the first byte of message body.
4186 * msg->som still points to the beginning of the message. We must
4187 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004189 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004190
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004191 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004192 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4193 else {
4194 msg->msg_state = HTTP_MSG_DATA;
4195 }
4196 }
4197
Willy Tarreaud98cf932009-12-27 22:54:55 +01004198 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004199 int bytes;
4200
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004202 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004203 bytes = msg->sov - msg->som;
4204 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004205 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004206 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4207 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004208 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004209 msg->chunk_len += (unsigned int)bytes;
4210 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004211 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004212
Willy Tarreaucaabe412010-01-03 23:08:28 +01004213 if (msg->msg_state == HTTP_MSG_DATA) {
4214 /* must still forward */
4215 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004216 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004217
4218 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004219 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004220 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004222 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004223 }
4224 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004225 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004226 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004227 * TRAILERS state.
4228 */
4229 int ret = http_parse_chunk_size(req, msg);
4230
4231 if (!ret)
4232 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004233 else if (ret < 0) {
4234 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004235 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004236 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004238 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004240 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4242 /* we want the CRLF after the data */
4243 int ret;
4244
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245 ret = http_skip_chunk_crlf(req, msg);
4246
4247 if (ret == 0)
4248 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004249 else if (ret < 0) {
4250 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004251 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004252 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004253 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004254 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004255 /* we're in MSG_CHUNK_SIZE now */
4256 }
4257 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4258 int ret = http_forward_trailers(req, msg);
4259
4260 if (ret == 0)
4261 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004262 else if (ret < 0) {
4263 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004264 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004265 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004266 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004267 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004268 /* we're in HTTP_MSG_DONE now */
4269 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004271 int old_state = msg->msg_state;
4272
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004274 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004275 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4276 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4277 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 if (http_resync_states(s)) {
4279 /* some state changes occurred, maybe the analyser
4280 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004281 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004282 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4283 if (req->flags & BF_SHUTW) {
4284 /* request errors are most likely due to
4285 * the server aborting the transfer.
4286 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004287 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004288 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004289 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004290 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004294 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004295
4296 /* If "option abortonclose" is set on the backend, we
4297 * want to monitor the client's connection and forward
4298 * any shutdown notification to the server, which will
4299 * decide whether to close or to go on processing the
4300 * request.
4301 */
4302 if (s->be->options & PR_O_ABRT_CLOSE) {
4303 buffer_auto_read(req);
4304 buffer_auto_close(req);
4305 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004306 else if (s->txn.meth == HTTP_METH_POST) {
4307 /* POST requests may require to read extra CRLF
4308 * sent by broken browsers and which could cause
4309 * an RST to be sent upon close on some systems
4310 * (eg: Linux).
4311 */
4312 buffer_auto_read(req);
4313 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004314
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004316 }
4317 }
4318
Willy Tarreaud98cf932009-12-27 22:54:55 +01004319 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004320 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004321 if (req->flags & BF_SHUTR) {
4322 if (!(s->flags & SN_ERR_MASK))
4323 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004324 if (!(s->flags & SN_FINST_MASK)) {
4325 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4326 s->flags |= SN_FINST_H;
4327 else
4328 s->flags |= SN_FINST_D;
4329 }
4330
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004331 s->fe->fe_counters.cli_aborts++;
4332 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004333 if (target_srv(&s->target))
4334 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004335
4336 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004337 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004338
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004339 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004340 if (req->flags & BF_SHUTW)
4341 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004343 /* When TE: chunked is used, we need to get there again to parse remaining
4344 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4345 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004346 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004347 buffer_dont_close(req);
4348
Willy Tarreau5c620922011-05-11 19:56:11 +02004349 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004350 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4351 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004352 * modes are already handled by the stream sock layer. We must not do
4353 * this in content-length mode because it could present the MSG_MORE
4354 * flag with the last block of forwarded data, which would cause an
4355 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004356 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004357 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004358 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004359
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004361 return 0;
4362
Willy Tarreaud98cf932009-12-27 22:54:55 +01004363 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004364 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365 if (s->listener->counters)
4366 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004367 return_bad_req_stats_ok:
4368 txn->req.msg_state = HTTP_MSG_ERROR;
4369 if (txn->status) {
4370 /* Note: we don't send any error if some data were already sent */
4371 stream_int_retnclose(req->prod, NULL);
4372 } else {
4373 txn->status = 400;
4374 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4375 }
4376 req->analysers = 0;
4377 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004378
4379 if (!(s->flags & SN_ERR_MASK))
4380 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004381 if (!(s->flags & SN_FINST_MASK)) {
4382 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4383 s->flags |= SN_FINST_H;
4384 else
4385 s->flags |= SN_FINST_D;
4386 }
4387 return 0;
4388
4389 aborted_xfer:
4390 txn->req.msg_state = HTTP_MSG_ERROR;
4391 if (txn->status) {
4392 /* Note: we don't send any error if some data were already sent */
4393 stream_int_retnclose(req->prod, NULL);
4394 } else {
4395 txn->status = 502;
4396 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4397 }
4398 req->analysers = 0;
4399 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4400
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004401 s->fe->fe_counters.srv_aborts++;
4402 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004403 if (target_srv(&s->target))
4404 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004405
4406 if (!(s->flags & SN_ERR_MASK))
4407 s->flags |= SN_ERR_SRVCL;
4408 if (!(s->flags & SN_FINST_MASK)) {
4409 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4410 s->flags |= SN_FINST_H;
4411 else
4412 s->flags |= SN_FINST_D;
4413 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004414 return 0;
4415}
4416
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004417/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4418 * processing can continue on next analysers, or zero if it either needs more
4419 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4420 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4421 * when it has nothing left to do, and may remove any analyser when it wants to
4422 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004423 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004424int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004425{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004426 struct http_txn *txn = &s->txn;
4427 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004428 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004429 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004431 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004433 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 +02004434 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004436 rep,
4437 rep->rex, rep->wex,
4438 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004439 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004440 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004441
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004442 /*
4443 * Now parse the partial (or complete) lines.
4444 * We will check the response syntax, and also join multi-line
4445 * headers. An index of all the lines will be elaborated while
4446 * parsing.
4447 *
4448 * For the parsing, we use a 28 states FSM.
4449 *
4450 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004451 * rep->data + msg->som = beginning of response
4452 * rep->data + msg->eoh = end of processed headers / start of current one
4453 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004454 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004455 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004456 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 */
4458
Willy Tarreau83e3af02009-12-28 17:39:57 +01004459 /* There's a protected area at the end of the buffer for rewriting
4460 * purposes. We don't want to start to parse the request if the
4461 * protected area is affected, because we may have to move processed
4462 * data later, which is much more complicated.
4463 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004464 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004465 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004466 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004467 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004468 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004469 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004470 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4471 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004472 buffer_dont_close(rep);
4473 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4474 return 0;
4475 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004476 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01004477 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004478 }
4479
Willy Tarreaua458b672012-03-05 11:17:50 +01004480 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004481 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004482 }
4483
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484 /* 1: we might have to print this header in debug mode */
4485 if (unlikely((global.mode & MODE_DEBUG) &&
4486 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004487 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004488 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004490
Willy Tarreauea1175a2012-03-05 15:52:30 +01004491 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004492 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004494
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495 sol += hdr_idx_first_pos(&txn->hdr_idx);
4496 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004497
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498 while (cur_idx) {
4499 eol = sol + txn->hdr_idx.v[cur_idx].len;
4500 debug_hdr("srvhdr", s, sol, eol);
4501 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4502 cur_idx = txn->hdr_idx.v[cur_idx].next;
4503 }
4504 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004505
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004506 /*
4507 * Now we quickly check if we have found a full valid response.
4508 * If not so, we check the FD and buffer states before leaving.
4509 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004510 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 * responses are checked first.
4512 *
4513 * Depending on whether the client is still there or not, we
4514 * may send an error response back or not. Note that normally
4515 * we should only check for HTTP status there, and check I/O
4516 * errors somewhere else.
4517 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004518
Willy Tarreau655dce92009-11-08 13:10:58 +01004519 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004520 /* Invalid response */
4521 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4522 /* we detected a parsing error. We want to archive this response
4523 * in the dedicated proxy area for later troubleshooting.
4524 */
4525 hdr_response_bad:
4526 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004527 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004528
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004529 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004530 if (target_srv(&s->target)) {
4531 target_srv(&s->target)->counters.failed_resp++;
4532 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004533 }
Willy Tarreau64648412010-03-05 10:41:54 +01004534 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004535 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004536 rep->analysers = 0;
4537 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004538 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004539 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4541
4542 if (!(s->flags & SN_ERR_MASK))
4543 s->flags |= SN_ERR_PRXCOND;
4544 if (!(s->flags & SN_FINST_MASK))
4545 s->flags |= SN_FINST_H;
4546
4547 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004548 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004549
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 /* too large response does not fit in buffer. */
4551 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004552 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004553 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004554 goto hdr_response_bad;
4555 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004556
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557 /* read error */
4558 else if (rep->flags & BF_READ_ERROR) {
4559 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004560 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004561
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004562 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004563 if (target_srv(&s->target)) {
4564 target_srv(&s->target)->counters.failed_resp++;
4565 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004566 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004567
Willy Tarreau90deb182010-01-07 00:20:41 +01004568 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 rep->analysers = 0;
4570 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004571 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004572 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004574
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 if (!(s->flags & SN_ERR_MASK))
4576 s->flags |= SN_ERR_SRVCL;
4577 if (!(s->flags & SN_FINST_MASK))
4578 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004579 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004580 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004581
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004582 /* read timeout : return a 504 to the client. */
4583 else if (rep->flags & BF_READ_TIMEOUT) {
4584 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004585 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004586
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004587 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004588 if (target_srv(&s->target)) {
4589 target_srv(&s->target)->counters.failed_resp++;
4590 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004591 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004592
Willy Tarreau90deb182010-01-07 00:20:41 +01004593 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004594 rep->analysers = 0;
4595 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004596 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004597 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004599
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600 if (!(s->flags & SN_ERR_MASK))
4601 s->flags |= SN_ERR_SRVTO;
4602 if (!(s->flags & SN_FINST_MASK))
4603 s->flags |= SN_FINST_H;
4604 return 0;
4605 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004606
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004607 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004609 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004610 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004611
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004612 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004613 if (target_srv(&s->target)) {
4614 target_srv(&s->target)->counters.failed_resp++;
4615 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004616 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004617
Willy Tarreau90deb182010-01-07 00:20:41 +01004618 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 rep->analysers = 0;
4620 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004621 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004622 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625 if (!(s->flags & SN_ERR_MASK))
4626 s->flags |= SN_ERR_SRVCL;
4627 if (!(s->flags & SN_FINST_MASK))
4628 s->flags |= SN_FINST_H;
4629 return 0;
4630 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004631
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 /* write error to client (we don't send any message then) */
4633 else if (rep->flags & BF_WRITE_ERROR) {
4634 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004635 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004636
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004637 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004639 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640
4641 if (!(s->flags & SN_ERR_MASK))
4642 s->flags |= SN_ERR_CLICL;
4643 if (!(s->flags & SN_FINST_MASK))
4644 s->flags |= SN_FINST_H;
4645
4646 /* process_session() will take care of the error */
4647 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004649
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 buffer_dont_close(rep);
4651 return 0;
4652 }
4653
4654 /* More interesting part now : we know that we have a complete
4655 * response which at least looks like HTTP. We have an indicator
4656 * of each header's length, so we can parse them quickly.
4657 */
4658
4659 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004660 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 /*
4663 * 1: get the status code
4664 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004665 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 if (n < 1 || n > 5)
4667 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004668 /* when the client triggers a 4xx from the server, it's most often due
4669 * to a missing object or permission. These events should be tracked
4670 * because if they happen often, it may indicate a brute force or a
4671 * vulnerability scan.
4672 */
4673 if (n == 4)
4674 session_inc_http_err_ctr(s);
4675
Willy Tarreau827aee92011-03-10 16:55:02 +01004676 if (target_srv(&s->target))
4677 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678
Willy Tarreau5b154472009-12-21 20:11:07 +01004679 /* check if the response is HTTP/1.1 or above */
4680 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004681 ((rep->p[msg->sol + 5] > '1') ||
4682 ((rep->p[msg->sol + 5] == '1') &&
4683 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004684 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004685
4686 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004687 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 +01004688
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004689 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004690 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004691
Willy Tarreau3a215be2012-03-09 21:39:51 +01004692 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004693
Willy Tarreau39650402010-03-15 19:44:39 +01004694 /* Adjust server's health based on status code. Note: status codes 501
4695 * and 505 are triggered on demand by client request, so we must not
4696 * count them as server failures.
4697 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004698 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004699 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004700 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004701 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004702 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004703 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004704
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 /*
4706 * 2: check for cacheability.
4707 */
4708
4709 switch (txn->status) {
4710 case 200:
4711 case 203:
4712 case 206:
4713 case 300:
4714 case 301:
4715 case 410:
4716 /* RFC2616 @13.4:
4717 * "A response received with a status code of
4718 * 200, 203, 206, 300, 301 or 410 MAY be stored
4719 * by a cache (...) unless a cache-control
4720 * directive prohibits caching."
4721 *
4722 * RFC2616 @9.5: POST method :
4723 * "Responses to this method are not cacheable,
4724 * unless the response includes appropriate
4725 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004726 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 if (likely(txn->meth != HTTP_METH_POST) &&
4728 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4729 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4730 break;
4731 default:
4732 break;
4733 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004734
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004735 /*
4736 * 3: we may need to capture headers
4737 */
4738 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004739 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004740 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 txn->rsp.cap, s->fe->rsp_cap);
4742
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004743 /* 4: determine the transfer-length.
4744 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4745 * the presence of a message-body in a RESPONSE and its transfer length
4746 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004747 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004748 * All responses to the HEAD request method MUST NOT include a
4749 * message-body, even though the presence of entity-header fields
4750 * might lead one to believe they do. All 1xx (informational), 204
4751 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4752 * message-body. All other responses do include a message-body,
4753 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004754 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004755 * 1. Any response which "MUST NOT" include a message-body (such as the
4756 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4757 * always terminated by the first empty line after the header fields,
4758 * regardless of the entity-header fields present in the message.
4759 *
4760 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4761 * the "chunked" transfer-coding (Section 6.2) is used, the
4762 * transfer-length is defined by the use of this transfer-coding.
4763 * If a Transfer-Encoding header field is present and the "chunked"
4764 * transfer-coding is not present, the transfer-length is defined by
4765 * the sender closing the connection.
4766 *
4767 * 3. If a Content-Length header field is present, its decimal value in
4768 * OCTETs represents both the entity-length and the transfer-length.
4769 * If a message is received with both a Transfer-Encoding header
4770 * field and a Content-Length header field, the latter MUST be ignored.
4771 *
4772 * 4. If the message uses the media type "multipart/byteranges", and
4773 * the transfer-length is not otherwise specified, then this self-
4774 * delimiting media type defines the transfer-length. This media
4775 * type MUST NOT be used unless the sender knows that the recipient
4776 * can parse it; the presence in a request of a Range header with
4777 * multiple byte-range specifiers from a 1.1 client implies that the
4778 * client can parse multipart/byteranges responses.
4779 *
4780 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004781 */
4782
4783 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004784 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004785 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004786 * FIXME: should we parse anyway and return an error on chunked encoding ?
4787 */
4788 if (txn->meth == HTTP_METH_HEAD ||
4789 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004790 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004791 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004792 goto skip_content_length;
4793 }
4794
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004795 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004796 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004798 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004799 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004800 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4801 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004802 /* bad transfer-encoding (chunked followed by something else) */
4803 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004804 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004805 break;
4806 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004807 }
4808
4809 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4810 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004811 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004812 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004813 signed long long cl;
4814
Willy Tarreauad14f752011-09-02 20:33:27 +02004815 if (!ctx.vlen) {
4816 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004818 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819
Willy Tarreauad14f752011-09-02 20:33:27 +02004820 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4821 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004823 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004824
Willy Tarreauad14f752011-09-02 20:33:27 +02004825 if (cl < 0) {
4826 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004827 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004828 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004829
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004830 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004831 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004832 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004833 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004834
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004835 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004836 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004837 }
4838
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004839 /* FIXME: we should also implement the multipart/byterange method.
4840 * For now on, we resort to close mode in this case (unknown length).
4841 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004842skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004843
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844 /* end of job, return OK */
4845 rep->analysers &= ~an_bit;
4846 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004847 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 return 1;
4849}
4850
4851/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004852 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4853 * and updates t->rep->analysers. It might make sense to explode it into several
4854 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 */
4856int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4857{
4858 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 struct http_msg *msg = &txn->rsp;
4860 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004861 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004862
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004863 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 +02004864 now_ms, __FUNCTION__,
4865 t,
4866 rep,
4867 rep->rex, rep->wex,
4868 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004869 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 rep->analysers);
4871
Willy Tarreau655dce92009-11-08 13:10:58 +01004872 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 return 0;
4874
4875 rep->analysers &= ~an_bit;
4876 rep->analyse_exp = TICK_ETERNITY;
4877
Willy Tarreau5b154472009-12-21 20:11:07 +01004878 /* Now we have to check if we need to modify the Connection header.
4879 * This is more difficult on the response than it is on the request,
4880 * because we can have two different HTTP versions and we don't know
4881 * how the client will interprete a response. For instance, let's say
4882 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4883 * HTTP/1.1 response without any header. Maybe it will bound itself to
4884 * HTTP/1.0 because it only knows about it, and will consider the lack
4885 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4886 * the lack of header as a keep-alive. Thus we will use two flags
4887 * indicating how a request MAY be understood by the client. In case
4888 * of multiple possibilities, we'll fix the header to be explicit. If
4889 * ambiguous cases such as both close and keepalive are seen, then we
4890 * will fall back to explicit close. Note that we won't take risks with
4891 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004892 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004893 */
4894
Willy Tarreaudc008c52010-02-01 16:20:08 +01004895 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4896 txn->status == 101)) {
4897 /* Either we've established an explicit tunnel, or we're
4898 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004899 * to understand the next protocols. We have to switch to tunnel
4900 * mode, so that we transfer the request and responses then let
4901 * this protocol pass unmodified. When we later implement specific
4902 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004903 * header which contains information about that protocol for
4904 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004905 */
4906 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4907 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004908 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4909 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4910 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004911 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004912
Willy Tarreau60466522010-01-18 19:08:45 +01004913 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004914 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004915 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4916 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004917
Willy Tarreau60466522010-01-18 19:08:45 +01004918 /* now adjust header transformations depending on current state */
4919 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4920 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4921 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004922 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004923 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004924 }
Willy Tarreau60466522010-01-18 19:08:45 +01004925 else { /* SCL / KAL */
4926 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004927 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004928 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004929 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004930
Willy Tarreau60466522010-01-18 19:08:45 +01004931 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004932 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004933
Willy Tarreau60466522010-01-18 19:08:45 +01004934 /* Some keep-alive responses are converted to Server-close if
4935 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004936 */
Willy Tarreau60466522010-01-18 19:08:45 +01004937 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4938 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004939 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004940 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004941 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004942 }
4943
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004944 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004945 /*
4946 * 3: we will have to evaluate the filters.
4947 * As opposed to version 1.2, now they will be evaluated in the
4948 * filters order and not in the header order. This means that
4949 * each filter has to be validated among all headers.
4950 *
4951 * Filters are tried with ->be first, then with ->fe if it is
4952 * different from ->be.
4953 */
4954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004955 cur_proxy = t->be;
4956 while (1) {
4957 struct proxy *rule_set = cur_proxy;
4958
4959 /* try headers filters */
4960 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004961 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004962 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004963 if (target_srv(&t->target)) {
4964 target_srv(&t->target)->counters.failed_resp++;
4965 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004966 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004967 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004969 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004970 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004971 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004972 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004973 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 if (!(t->flags & SN_ERR_MASK))
4975 t->flags |= SN_ERR_PRXCOND;
4976 if (!(t->flags & SN_FINST_MASK))
4977 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004978 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004979 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004980 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004981
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004982 /* has the response been denied ? */
4983 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004984 if (target_srv(&t->target))
4985 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004986
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004987 t->be->be_counters.denied_resp++;
4988 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004989 if (t->listener->counters)
4990 t->listener->counters->denied_resp++;
4991
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004992 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004993 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004994
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004995 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004996 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004997 if (txn->status < 200)
4998 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004999 if (wl->cond) {
5000 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5001 ret = acl_pass(ret);
5002 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5003 ret = !ret;
5004 if (!ret)
5005 continue;
5006 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005007 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005008 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005009 }
5010
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005011 /* check whether we're already working on the frontend */
5012 if (cur_proxy == t->fe)
5013 break;
5014 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005015 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005016
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005018 * We may be facing a 100-continue response, in which case this
5019 * is not the right response, and we're waiting for the next one.
5020 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005021 * next one.
5022 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005023 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005024 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005025 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005026 msg->msg_state = HTTP_MSG_RPBEFORE;
5027 txn->status = 0;
5028 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5029 return 1;
5030 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005031 else if (unlikely(txn->status < 200))
5032 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005033
5034 /* we don't have any 1xx status code now */
5035
5036 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005037 * 4: check for server cookie.
5038 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005039 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5040 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005042
Willy Tarreaubaaee002006-06-26 02:48:02 +02005043
Willy Tarreaua15645d2007-03-18 16:22:39 +01005044 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005046 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005047 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005049
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005050 /*
5051 * 6: add server cookie in the response if needed
5052 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005053 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005054 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005055 (!(t->flags & SN_DIRECT) ||
5056 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5057 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5058 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5059 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005060 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5061 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005062 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005063 /* the server is known, it's not the one the client requested, or the
5064 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005065 * insert a set-cookie here, except if we want to insert only on POST
5066 * requests and this one isn't. Note that servers which don't have cookies
5067 * (eg: some backup servers) will return a full cookie removal request.
5068 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005069 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005070 len = sprintf(trash,
5071 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5072 t->be->cookie_name);
5073 }
5074 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005075 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005076
5077 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5078 /* emit last_date, which is mandatory */
5079 trash[len++] = COOKIE_DELIM_DATE;
5080 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5081 if (t->be->cookie_maxlife) {
5082 /* emit first_date, which is either the original one or
5083 * the current date.
5084 */
5085 trash[len++] = COOKIE_DELIM_DATE;
5086 s30tob64(txn->cookie_first_date ?
5087 txn->cookie_first_date >> 2 :
5088 (date.tv_sec+3) >> 2, trash + len);
5089 len += 5;
5090 }
5091 }
5092 len += sprintf(trash + len, "; path=/");
5093 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005094
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005095 if (t->be->cookie_domain)
5096 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005097
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005098 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005099 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005100
Willy Tarreauf1348312010-10-07 15:54:11 +02005101 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005102 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005103 /* the server did not change, only the date was updated */
5104 txn->flags |= TX_SCK_UPDATED;
5105 else
5106 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005107
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005108 /* Here, we will tell an eventual cache on the client side that we don't
5109 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5110 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5111 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5112 */
5113 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5116
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005117 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005118 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005120 }
5121 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005122
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005123 /*
5124 * 7: check if result will be cacheable with a cookie.
5125 * We'll block the response if security checks have caught
5126 * nasty things such as a cacheable cookie.
5127 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005128 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5129 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005130 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005131
5132 /* we're in presence of a cacheable response containing
5133 * a set-cookie header. We'll block it as requested by
5134 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005135 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005136 if (target_srv(&t->target))
5137 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005138
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005139 t->be->be_counters.denied_resp++;
5140 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005141 if (t->listener->counters)
5142 t->listener->counters->denied_resp++;
5143
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005145 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005146 send_log(t->be, LOG_ALERT,
5147 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005148 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149 goto return_srv_prx_502;
5150 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005151
5152 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005153 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005154 */
Willy Tarreau60466522010-01-18 19:08:45 +01005155 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5156 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5157 unsigned int want_flags = 0;
5158
5159 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5160 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5161 /* we want a keep-alive response here. Keep-alive header
5162 * required if either side is not 1.1.
5163 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005164 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005165 want_flags |= TX_CON_KAL_SET;
5166 }
5167 else {
5168 /* we want a close response here. Close header required if
5169 * the server is 1.1, regardless of the client.
5170 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005171 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005172 want_flags |= TX_CON_CLO_SET;
5173 }
5174
5175 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005176 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005177 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005178
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005179 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005180 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005181 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005182 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005183
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005184 /*************************************************************
5185 * OK, that's finished for the headers. We have done what we *
5186 * could. Let's switch to the DATA state. *
5187 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005188
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005189 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005190
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005191 /* if the user wants to log as soon as possible, without counting
5192 * bytes from the server, then this is the right moment. We have
5193 * to temporarily assign bytes_out to log what we currently have.
5194 */
5195 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5196 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5197 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005198 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199 t->logs.bytes_out = 0;
5200 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005201
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202 /* Note: we must not try to cheat by jumping directly to DATA,
5203 * otherwise we would not let the client side wake up.
5204 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005205
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005206 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005208 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005209}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005210
Willy Tarreaud98cf932009-12-27 22:54:55 +01005211/* This function is an analyser which forwards response body (including chunk
5212 * sizes if any). It is called as soon as we must forward, even if we forward
5213 * zero byte. The only situation where it must not be called is when we're in
5214 * tunnel mode and we want to forward till the close. It's used both to forward
5215 * remaining data and to resync after end of body. It expects the msg_state to
5216 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5217 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005218 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005219 * bytes of pending data + the headers if not already done (between som and sov).
5220 * It eventually adjusts som to match sov after the data in between have been sent.
5221 */
5222int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5223{
5224 struct http_txn *txn = &s->txn;
5225 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005226 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005227
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005228 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5229 return 0;
5230
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005231 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005232 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005233 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005234 /* Output closed while we were sending data. We must abort and
5235 * wake the other side up.
5236 */
5237 msg->msg_state = HTTP_MSG_ERROR;
5238 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005239 return 1;
5240 }
5241
Willy Tarreau4fe41902010-06-07 22:27:41 +02005242 /* in most states, we should abort in case of early close */
5243 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005244
Willy Tarreaud98cf932009-12-27 22:54:55 +01005245 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005246 /* we have msg->sov which points to the first byte of message body.
5247 * msg->som still points to the beginning of the message. We must
5248 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005250 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005251
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005252 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5254 else {
5255 msg->msg_state = HTTP_MSG_DATA;
5256 }
5257 }
5258
Willy Tarreaud98cf932009-12-27 22:54:55 +01005259 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005260 int bytes;
5261
Willy Tarreau610ecce2010-01-04 21:15:02 +01005262 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005263 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005264 bytes = msg->sov - msg->som;
5265 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005266 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005267 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5268 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005269 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005270 msg->chunk_len += (unsigned int)bytes;
5271 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005272 }
5273
Willy Tarreaucaabe412010-01-03 23:08:28 +01005274 if (msg->msg_state == HTTP_MSG_DATA) {
5275 /* must still forward */
5276 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005277 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005278
5279 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005280 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005281 msg->msg_state = HTTP_MSG_DATA_CRLF;
5282 else
5283 msg->msg_state = HTTP_MSG_DONE;
5284 }
5285 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005286 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005287 * set ->sov and ->next to point to the body and switch to DATA or
5288 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 */
5290 int ret = http_parse_chunk_size(res, msg);
5291
5292 if (!ret)
5293 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005294 else if (ret < 0) {
5295 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005296 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005297 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005298 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005301 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5302 /* we want the CRLF after the data */
5303 int ret;
5304
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 ret = http_skip_chunk_crlf(res, msg);
5306
5307 if (!ret)
5308 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005309 else if (ret < 0) {
5310 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005311 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005312 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005313 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005314 /* we're in MSG_CHUNK_SIZE now */
5315 }
5316 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5317 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005318
Willy Tarreaud98cf932009-12-27 22:54:55 +01005319 if (ret == 0)
5320 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005321 else if (ret < 0) {
5322 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005323 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005324 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005325 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005326 /* we're in HTTP_MSG_DONE now */
5327 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005328 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005329 int old_state = msg->msg_state;
5330
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005332 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005333 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5334 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5335 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 if (http_resync_states(s)) {
5337 http_silent_debug(__LINE__, s);
5338 /* some state changes occurred, maybe the analyser
5339 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005340 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005341 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5342 if (res->flags & BF_SHUTW) {
5343 /* response errors are most likely due to
5344 * the client aborting the transfer.
5345 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005346 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005347 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005348 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005349 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005351 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005353 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005355 }
5356 }
5357
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005359 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005360 if (res->flags & BF_SHUTR) {
5361 if (!(s->flags & SN_ERR_MASK))
5362 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005363 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005364 if (target_srv(&s->target))
5365 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005366 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005367 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005368
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005369 if (res->flags & BF_SHUTW)
5370 goto aborted_xfer;
5371
Willy Tarreau40dba092010-03-04 18:14:51 +01005372 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005373 if (!s->req->analysers)
5374 goto return_bad_res;
5375
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005376 /* forward any pending data */
5377 bytes = msg->sov - msg->som;
5378 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005379 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005380 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5381 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005382 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005383 msg->chunk_len += (unsigned int)bytes;
5384 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005385 }
5386
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005387 /* When TE: chunked is used, we need to get there again to parse remaining
5388 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5389 * Similarly, with keep-alive on the client side, we don't want to forward a
5390 * close.
5391 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005392 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005393 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5394 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5395 buffer_dont_close(res);
5396
Willy Tarreau5c620922011-05-11 19:56:11 +02005397 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005398 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5399 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005400 * modes are already handled by the stream sock layer. We must not do
5401 * this in content-length mode because it could present the MSG_MORE
5402 * flag with the last block of forwarded data, which would cause an
5403 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005404 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005405 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005406 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005407
Willy Tarreaud98cf932009-12-27 22:54:55 +01005408 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 return 0;
5411
Willy Tarreau40dba092010-03-04 18:14:51 +01005412 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005413 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005414 if (target_srv(&s->target))
5415 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005416
5417 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005419 /* don't send any error message as we're in the body */
5420 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005422 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005423 if (target_srv(&s->target))
5424 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425
5426 if (!(s->flags & SN_ERR_MASK))
5427 s->flags |= SN_ERR_PRXCOND;
5428 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005429 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005430 return 0;
5431
5432 aborted_xfer:
5433 txn->rsp.msg_state = HTTP_MSG_ERROR;
5434 /* don't send any error message as we're in the body */
5435 stream_int_retnclose(res->cons, NULL);
5436 res->analysers = 0;
5437 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5438
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005439 s->fe->fe_counters.cli_aborts++;
5440 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005441 if (target_srv(&s->target))
5442 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005443
5444 if (!(s->flags & SN_ERR_MASK))
5445 s->flags |= SN_ERR_CLICL;
5446 if (!(s->flags & SN_FINST_MASK))
5447 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 return 0;
5449}
5450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451/* Iterate the same filter through all request headers.
5452 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005453 * Since it can manage the switch to another backend, it updates the per-proxy
5454 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005455 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005456int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005457{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005458 char term;
5459 char *cur_ptr, *cur_end, *cur_next;
5460 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005461 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462 struct hdr_idx_elem *cur_hdr;
5463 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005465 last_hdr = 0;
5466
Willy Tarreau3a215be2012-03-09 21:39:51 +01005467 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 old_idx = 0;
5469
5470 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005471 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005472 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005473 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 (exp->action == ACT_ALLOW ||
5475 exp->action == ACT_DENY ||
5476 exp->action == ACT_TARPIT))
5477 return 0;
5478
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 if (!cur_idx)
5481 break;
5482
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005483 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005484 cur_ptr = cur_next;
5485 cur_end = cur_ptr + cur_hdr->len;
5486 cur_next = cur_end + cur_hdr->cr + 1;
5487
5488 /* Now we have one header between cur_ptr and cur_end,
5489 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005490 */
5491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 /* The annoying part is that pattern matching needs
5493 * that we modify the contents to null-terminate all
5494 * strings before testing them.
5495 */
5496
5497 term = *cur_end;
5498 *cur_end = '\0';
5499
5500 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5501 switch (exp->action) {
5502 case ACT_SETBE:
5503 /* It is not possible to jump a second time.
5504 * FIXME: should we return an HTTP/500 here so that
5505 * the admin knows there's a problem ?
5506 */
5507 if (t->be != t->fe)
5508 break;
5509
5510 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005511 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005512 last_hdr = 1;
5513 break;
5514
5515 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005516 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 last_hdr = 1;
5518 break;
5519
5520 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005521 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005522 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005523
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005524 t->fe->fe_counters.denied_req++;
5525 if (t->fe != t->be)
5526 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005527 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005528 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 break;
5531
5532 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005533 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005534 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005536 t->fe->fe_counters.denied_req++;
5537 if (t->fe != t->be)
5538 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005539 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005540 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005542 break;
5543
5544 case ACT_REPLACE:
5545 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5546 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5547 /* FIXME: if the user adds a newline in the replacement, the
5548 * index will not be recalculated for now, and the new line
5549 * will not be counted as a new header.
5550 */
5551
5552 cur_end += delta;
5553 cur_next += delta;
5554 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005555 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005556 break;
5557
5558 case ACT_REMOVE:
5559 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5560 cur_next += delta;
5561
Willy Tarreaufa355d42009-11-29 18:12:29 +01005562 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005563 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5564 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005565 cur_hdr->len = 0;
5566 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005567 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 break;
5569
5570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005571 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005572 if (cur_end)
5573 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 /* keep the link from this header to next one in case of later
5576 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005577 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005578 old_idx = cur_idx;
5579 }
5580 return 0;
5581}
5582
5583
5584/* Apply the filter to the request line.
5585 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5586 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005587 * Since it can manage the switch to another backend, it updates the per-proxy
5588 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005589 */
5590int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5591{
5592 char term;
5593 char *cur_ptr, *cur_end;
5594 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005595 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005597
Willy Tarreau58f10d72006-12-04 02:26:12 +01005598
Willy Tarreau3d300592007-03-18 18:34:41 +01005599 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005601 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005602 (exp->action == ACT_ALLOW ||
5603 exp->action == ACT_DENY ||
5604 exp->action == ACT_TARPIT))
5605 return 0;
5606 else if (exp->action == ACT_REMOVE)
5607 return 0;
5608
5609 done = 0;
5610
Willy Tarreau3a215be2012-03-09 21:39:51 +01005611 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005612 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005613
5614 /* Now we have the request line between cur_ptr and cur_end */
5615
5616 /* The annoying part is that pattern matching needs
5617 * that we modify the contents to null-terminate all
5618 * strings before testing them.
5619 */
5620
5621 term = *cur_end;
5622 *cur_end = '\0';
5623
5624 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5625 switch (exp->action) {
5626 case ACT_SETBE:
5627 /* It is not possible to jump a second time.
5628 * FIXME: should we return an HTTP/500 here so that
5629 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005630 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005631 if (t->be != t->fe)
5632 break;
5633
5634 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005635 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636 done = 1;
5637 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005640 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005641 done = 1;
5642 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005645 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005647 t->fe->fe_counters.denied_req++;
5648 if (t->fe != t->be)
5649 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005650 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005651 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653 done = 1;
5654 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005657 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005659 t->fe->fe_counters.denied_req++;
5660 if (t->fe != t->be)
5661 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005662 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005663 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005665 done = 1;
5666 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 case ACT_REPLACE:
5669 *cur_end = term; /* restore the string terminator */
5670 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5671 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5672 /* FIXME: if the user adds a newline in the replacement, the
5673 * index will not be recalculated for now, and the new line
5674 * will not be counted as a new header.
5675 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005676
Willy Tarreaufa355d42009-11-29 18:12:29 +01005677 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005679 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 HTTP_MSG_RQMETH,
5681 cur_ptr, cur_end + 1,
5682 NULL, NULL);
5683 if (unlikely(!cur_end))
5684 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 /* we have a full request and we know that we have either a CR
5687 * or an LF at <ptr>.
5688 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005689 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5690 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 /* there is no point trying this regex on headers */
5692 return 1;
5693 }
5694 }
5695 *cur_end = term; /* restore the string terminator */
5696 return done;
5697}
Willy Tarreau97de6242006-12-27 17:18:38 +01005698
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699
Willy Tarreau58f10d72006-12-04 02:26:12 +01005700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005702 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704 * unparsable request. Since it can manage the switch to another backend, it
5705 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005706 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005707int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005709 struct http_txn *txn = &s->txn;
5710 struct hdr_exp *exp;
5711
5712 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005714
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715 /*
5716 * The interleaving of transformations and verdicts
5717 * makes it difficult to decide to continue or stop
5718 * the evaluation.
5719 */
5720
Willy Tarreau6c123b12010-01-28 20:22:06 +01005721 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5722 break;
5723
Willy Tarreau3d300592007-03-18 18:34:41 +01005724 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005726 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005727 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005728
5729 /* if this filter had a condition, evaluate it now and skip to
5730 * next filter if the condition does not match.
5731 */
5732 if (exp->cond) {
5733 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5734 ret = acl_pass(ret);
5735 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5736 ret = !ret;
5737
5738 if (!ret)
5739 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 }
5741
5742 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005743 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 if (unlikely(ret < 0))
5745 return -1;
5746
5747 if (likely(ret == 0)) {
5748 /* The filter did not match the request, it can be
5749 * iterated through all headers.
5750 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005751 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005752 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005753 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005754 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005755}
5756
5757
Willy Tarreaua15645d2007-03-18 16:22:39 +01005758
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005760 * Try to retrieve the server associated to the appsession.
5761 * If the server is found, it's assigned to the session.
5762 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005763void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005764 struct http_txn *txn = &t->txn;
5765 appsess *asession = NULL;
5766 char *sessid_temp = NULL;
5767
Cyril Bontéb21570a2009-11-29 20:04:48 +01005768 if (len > t->be->appsession_len) {
5769 len = t->be->appsession_len;
5770 }
5771
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772 if (t->be->options2 & PR_O2_AS_REQL) {
5773 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005774 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005775 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005776 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005777 }
5778
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005779 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005780 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5781 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5782 return;
5783 }
5784
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005785 memcpy(txn->sessid, buf, len);
5786 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005787 }
5788
5789 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5790 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5791 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5792 return;
5793 }
5794
Cyril Bontéb21570a2009-11-29 20:04:48 +01005795 memcpy(sessid_temp, buf, len);
5796 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005797
5798 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5799 /* free previously allocated memory */
5800 pool_free2(apools.sessid, sessid_temp);
5801
5802 if (asession != NULL) {
5803 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5804 if (!(t->be->options2 & PR_O2_AS_REQL))
5805 asession->request_count++;
5806
5807 if (asession->serverid != NULL) {
5808 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005809
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005810 while (srv) {
5811 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005812 if ((srv->state & SRV_RUNNING) ||
5813 (t->be->options & PR_O_PERSIST) ||
5814 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005815 /* we found the server and it's usable */
5816 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005817 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005818 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005819 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005820
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005821 break;
5822 } else {
5823 txn->flags &= ~TX_CK_MASK;
5824 txn->flags |= TX_CK_DOWN;
5825 }
5826 }
5827 srv = srv->next;
5828 }
5829 }
5830 }
5831}
5832
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005833/* Find the end of a cookie value contained between <s> and <e>. It works the
5834 * same way as with headers above except that the semi-colon also ends a token.
5835 * See RFC2965 for more information. Note that it requires a valid header to
5836 * return a valid result.
5837 */
5838char *find_cookie_value_end(char *s, const char *e)
5839{
5840 int quoted, qdpair;
5841
5842 quoted = qdpair = 0;
5843 for (; s < e; s++) {
5844 if (qdpair) qdpair = 0;
5845 else if (quoted) {
5846 if (*s == '\\') qdpair = 1;
5847 else if (*s == '"') quoted = 0;
5848 }
5849 else if (*s == '"') quoted = 1;
5850 else if (*s == ',' || *s == ';') return s;
5851 }
5852 return s;
5853}
5854
5855/* Delete a value in a header between delimiters <from> and <next> in buffer
5856 * <buf>. The number of characters displaced is returned, and the pointer to
5857 * the first delimiter is updated if required. The function tries as much as
5858 * possible to respect the following principles :
5859 * - replace <from> delimiter by the <next> one unless <from> points to a
5860 * colon, in which case <next> is simply removed
5861 * - set exactly one space character after the new first delimiter, unless
5862 * there are not enough characters in the block being moved to do so.
5863 * - remove unneeded spaces before the previous delimiter and after the new
5864 * one.
5865 *
5866 * It is the caller's responsibility to ensure that :
5867 * - <from> points to a valid delimiter or the colon ;
5868 * - <next> points to a valid delimiter or the final CR/LF ;
5869 * - there are non-space chars before <from> ;
5870 * - there is a CR/LF at or after <next>.
5871 */
5872int del_hdr_value(struct buffer *buf, char **from, char *next)
5873{
5874 char *prev = *from;
5875
5876 if (*prev == ':') {
5877 /* We're removing the first value, preserve the colon and add a
5878 * space if possible.
5879 */
5880 if (!http_is_crlf[(unsigned char)*next])
5881 next++;
5882 prev++;
5883 if (prev < next)
5884 *prev++ = ' ';
5885
5886 while (http_is_spht[(unsigned char)*next])
5887 next++;
5888 } else {
5889 /* Remove useless spaces before the old delimiter. */
5890 while (http_is_spht[(unsigned char)*(prev-1)])
5891 prev--;
5892 *from = prev;
5893
5894 /* copy the delimiter and if possible a space if we're
5895 * not at the end of the line.
5896 */
5897 if (!http_is_crlf[(unsigned char)*next]) {
5898 *prev++ = *next++;
5899 if (prev + 1 < next)
5900 *prev++ = ' ';
5901 while (http_is_spht[(unsigned char)*next])
5902 next++;
5903 }
5904 }
5905 return buffer_replace2(buf, prev, next, NULL, 0);
5906}
5907
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005908/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005909 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005910 * desirable to call it only when needed. This code is quite complex because
5911 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5912 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005913 */
5914void manage_client_side_cookies(struct session *t, struct buffer *req)
5915{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005916 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005918 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005919 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5920 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005922 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005923 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005924 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005928 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005930 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 hdr_beg = hdr_next;
5932 hdr_end = hdr_beg + cur_hdr->len;
5933 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935 /* We have one full header between hdr_beg and hdr_end, and the
5936 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005937 * "Cookie:" headers.
5938 */
5939
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005940 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005941 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005942 old_idx = cur_idx;
5943 continue;
5944 }
5945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005946 del_from = NULL; /* nothing to be deleted */
5947 preserve_hdr = 0; /* assume we may kill the whole header */
5948
Willy Tarreau58f10d72006-12-04 02:26:12 +01005949 /* Now look for cookies. Conforming to RFC2109, we have to support
5950 * attributes whose name begin with a '$', and associate them with
5951 * the right cookie, if we want to delete this cookie.
5952 * So there are 3 cases for each cookie read :
5953 * 1) it's a special attribute, beginning with a '$' : ignore it.
5954 * 2) it's a server id cookie that we *MAY* want to delete : save
5955 * some pointers on it (last semi-colon, beginning of cookie...)
5956 * 3) it's an application cookie : we *MAY* have to delete a previous
5957 * "special" cookie.
5958 * At the end of loop, if a "special" cookie remains, we may have to
5959 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005960 * *MUST* delete it.
5961 *
5962 * Note: RFC2965 is unclear about the processing of spaces around
5963 * the equal sign in the ATTR=VALUE form. A careful inspection of
5964 * the RFC explicitly allows spaces before it, and not within the
5965 * tokens (attrs or values). An inspection of RFC2109 allows that
5966 * too but section 10.1.3 lets one think that spaces may be allowed
5967 * after the equal sign too, resulting in some (rare) buggy
5968 * implementations trying to do that. So let's do what servers do.
5969 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5970 * allowed quoted strings in values, with any possible character
5971 * after a backslash, including control chars and delimitors, which
5972 * causes parsing to become ambiguous. Browsers also allow spaces
5973 * within values even without quotes.
5974 *
5975 * We have to keep multiple pointers in order to support cookie
5976 * removal at the beginning, middle or end of header without
5977 * corrupting the header. All of these headers are valid :
5978 *
5979 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5980 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5981 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5982 * | | | | | | | | |
5983 * | | | | | | | | hdr_end <--+
5984 * | | | | | | | +--> next
5985 * | | | | | | +----> val_end
5986 * | | | | | +-----------> val_beg
5987 * | | | | +--------------> equal
5988 * | | | +----------------> att_end
5989 * | | +---------------------> att_beg
5990 * | +--------------------------> prev
5991 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005992 */
5993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005994 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5995 /* Iterate through all cookies on this line */
5996
5997 /* find att_beg */
5998 att_beg = prev + 1;
5999 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6000 att_beg++;
6001
6002 /* find att_end : this is the first character after the last non
6003 * space before the equal. It may be equal to hdr_end.
6004 */
6005 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006006
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006007 while (equal < hdr_end) {
6008 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006010 if (http_is_spht[(unsigned char)*equal++])
6011 continue;
6012 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006013 }
6014
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006015 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6016 * is between <att_beg> and <equal>, both may be identical.
6017 */
6018
6019 /* look for end of cookie if there is an equal sign */
6020 if (equal < hdr_end && *equal == '=') {
6021 /* look for the beginning of the value */
6022 val_beg = equal + 1;
6023 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6024 val_beg++;
6025
6026 /* find the end of the value, respecting quotes */
6027 next = find_cookie_value_end(val_beg, hdr_end);
6028
6029 /* make val_end point to the first white space or delimitor after the value */
6030 val_end = next;
6031 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6032 val_end--;
6033 } else {
6034 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006037 /* We have nothing to do with attributes beginning with '$'. However,
6038 * they will automatically be removed if a header before them is removed,
6039 * since they're supposed to be linked together.
6040 */
6041 if (*att_beg == '$')
6042 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006043
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006044 /* Ignore cookies with no equal sign */
6045 if (equal == next) {
6046 /* This is not our cookie, so we must preserve it. But if we already
6047 * scheduled another cookie for removal, we cannot remove the
6048 * complete header, but we can remove the previous block itself.
6049 */
6050 preserve_hdr = 1;
6051 if (del_from != NULL) {
6052 int delta = del_hdr_value(req, &del_from, prev);
6053 val_end += delta;
6054 next += delta;
6055 hdr_end += delta;
6056 hdr_next += delta;
6057 cur_hdr->len += delta;
6058 http_msg_move_end(&txn->req, delta);
6059 prev = del_from;
6060 del_from = NULL;
6061 }
6062 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006064
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006065 /* if there are spaces around the equal sign, we need to
6066 * strip them otherwise we'll get trouble for cookie captures,
6067 * or even for rewrites. Since this happens extremely rarely,
6068 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6071 int stripped_before = 0;
6072 int stripped_after = 0;
6073
6074 if (att_end != equal) {
6075 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6076 equal += stripped_before;
6077 val_beg += stripped_before;
6078 }
6079
6080 if (val_beg > equal + 1) {
6081 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6082 val_beg += stripped_after;
6083 stripped_before += stripped_after;
6084 }
6085
6086 val_end += stripped_before;
6087 next += stripped_before;
6088 hdr_end += stripped_before;
6089 hdr_next += stripped_before;
6090 cur_hdr->len += stripped_before;
6091 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006093 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006095 /* First, let's see if we want to capture this cookie. We check
6096 * that we don't already have a client side cookie, because we
6097 * can only capture one. Also as an optimisation, we ignore
6098 * cookies shorter than the declared name.
6099 */
6100 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6101 (val_end - att_beg >= t->fe->capture_namelen) &&
6102 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6103 int log_len = val_end - att_beg;
6104
6105 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6106 Alert("HTTP logging : out of memory.\n");
6107 } else {
6108 if (log_len > t->fe->capture_len)
6109 log_len = t->fe->capture_len;
6110 memcpy(txn->cli_cookie, att_beg, log_len);
6111 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114
Willy Tarreaubca99692010-10-06 19:25:55 +02006115 /* Persistence cookies in passive, rewrite or insert mode have the
6116 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006118 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006120 * For cookies in prefix mode, the form is :
6121 *
6122 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 */
6124 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6125 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6126 struct server *srv = t->be->srv;
6127 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6130 * have the server ID between val_beg and delim, and the original cookie between
6131 * delim+1 and val_end. Otherwise, delim==val_end :
6132 *
6133 * Cookie: NAME=SRV; # in all but prefix modes
6134 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6135 * | || || | |+-> next
6136 * | || || | +--> val_end
6137 * | || || +---------> delim
6138 * | || |+------------> val_beg
6139 * | || +-------------> att_end = equal
6140 * | |+-----------------> att_beg
6141 * | +------------------> prev
6142 * +-------------------------> hdr_beg
6143 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006145 if (t->be->options & PR_O_COOK_PFX) {
6146 for (delim = val_beg; delim < val_end; delim++)
6147 if (*delim == COOKIE_DELIM)
6148 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006149 } else {
6150 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006151 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006152 /* Now check if the cookie contains a date field, which would
6153 * appear after a vertical bar ('|') just after the server name
6154 * and before the delimiter.
6155 */
6156 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6157 if (vbar1) {
6158 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006159 * right is the last seen date. It is a base64 encoded
6160 * 30-bit value representing the UNIX date since the
6161 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006162 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006163 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006164 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006165 if (val_end - vbar1 >= 5) {
6166 val = b64tos30(vbar1);
6167 if (val > 0)
6168 txn->cookie_last_date = val << 2;
6169 }
6170 /* look for a second vertical bar */
6171 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6172 if (vbar1 && (val_end - vbar1 > 5)) {
6173 val = b64tos30(vbar1 + 1);
6174 if (val > 0)
6175 txn->cookie_first_date = val << 2;
6176 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006177 }
6178 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006179
Willy Tarreauf64d1412010-10-07 20:06:11 +02006180 /* if the cookie has an expiration date and the proxy wants to check
6181 * it, then we do that now. We first check if the cookie is too old,
6182 * then only if it has expired. We detect strict overflow because the
6183 * time resolution here is not great (4 seconds). Cookies with dates
6184 * in the future are ignored if their offset is beyond one day. This
6185 * allows an admin to fix timezone issues without expiring everyone
6186 * and at the same time avoids keeping unwanted side effects for too
6187 * long.
6188 */
6189 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006190 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6191 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006192 txn->flags &= ~TX_CK_MASK;
6193 txn->flags |= TX_CK_OLD;
6194 delim = val_beg; // let's pretend we have not found the cookie
6195 txn->cookie_first_date = 0;
6196 txn->cookie_last_date = 0;
6197 }
6198 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006199 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6200 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006201 txn->flags &= ~TX_CK_MASK;
6202 txn->flags |= TX_CK_EXPIRED;
6203 delim = val_beg; // let's pretend we have not found the cookie
6204 txn->cookie_first_date = 0;
6205 txn->cookie_last_date = 0;
6206 }
6207
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 /* Here, we'll look for the first running server which supports the cookie.
6209 * This allows to share a same cookie between several servers, for example
6210 * to dedicate backup servers to specific servers only.
6211 * However, to prevent clients from sticking to cookie-less backup server
6212 * when they have incidentely learned an empty cookie, we simply ignore
6213 * empty cookies and mark them as invalid.
6214 * The same behaviour is applied when persistence must be ignored.
6215 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006216 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 while (srv) {
6220 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6221 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6222 if ((srv->state & SRV_RUNNING) ||
6223 (t->be->options & PR_O_PERSIST) ||
6224 (t->flags & SN_FORCE_PRST)) {
6225 /* we found the server and we can use it */
6226 txn->flags &= ~TX_CK_MASK;
6227 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6228 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006229 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 break;
6231 } else {
6232 /* we found a server, but it's down,
6233 * mark it as such and go on in case
6234 * another one is available.
6235 */
6236 txn->flags &= ~TX_CK_MASK;
6237 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 srv = srv->next;
6241 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242
Willy Tarreauf64d1412010-10-07 20:06:11 +02006243 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006244 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006246 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6247 txn->flags |= TX_CK_UNUSED;
6248 else
6249 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252 /* depending on the cookie mode, we may have to either :
6253 * - delete the complete cookie if we're in insert+indirect mode, so that
6254 * the server never sees it ;
6255 * - remove the server id from the cookie value, and tag the cookie as an
6256 * application cookie so that it does not get accidentely removed later,
6257 * if we're in cookie prefix mode
6258 */
6259 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6260 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6263 val_end += delta;
6264 next += delta;
6265 hdr_end += delta;
6266 hdr_next += delta;
6267 cur_hdr->len += delta;
6268 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 del_from = NULL;
6271 preserve_hdr = 1; /* we want to keep this cookie */
6272 }
6273 else if (del_from == NULL &&
6274 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6275 del_from = prev;
6276 }
6277 } else {
6278 /* This is not our cookie, so we must preserve it. But if we already
6279 * scheduled another cookie for removal, we cannot remove the
6280 * complete header, but we can remove the previous block itself.
6281 */
6282 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 if (del_from != NULL) {
6285 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006286 if (att_beg >= del_from)
6287 att_beg += delta;
6288 if (att_end >= del_from)
6289 att_end += delta;
6290 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006291 val_end += delta;
6292 next += delta;
6293 hdr_end += delta;
6294 hdr_next += delta;
6295 cur_hdr->len += delta;
6296 http_msg_move_end(&txn->req, delta);
6297 prev = del_from;
6298 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006299 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 /* Look for the appsession cookie unless persistence must be ignored */
6303 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6304 int cmp_len, value_len;
6305 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006306
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 if (t->be->options2 & PR_O2_AS_PFX) {
6308 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6309 value_begin = att_beg + t->be->appsession_name_len;
6310 value_len = val_end - att_beg - t->be->appsession_name_len;
6311 } else {
6312 cmp_len = att_end - att_beg;
6313 value_begin = val_beg;
6314 value_len = val_end - val_beg;
6315 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 /* let's see if the cookie is our appcookie */
6318 if (cmp_len == t->be->appsession_name_len &&
6319 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6320 manage_client_side_appsession(t, value_begin, value_len);
6321 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322 }
6323
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 /* continue with next cookie on this header line */
6325 att_beg = next;
6326 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 /* There are no more cookies on this line.
6329 * We may still have one (or several) marked for deletion at the
6330 * end of the line. We must do this now in two ways :
6331 * - if some cookies must be preserved, we only delete from the
6332 * mark to the end of line ;
6333 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006334 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 if (preserve_hdr) {
6338 delta = del_hdr_value(req, &del_from, hdr_end);
6339 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 cur_hdr->len += delta;
6341 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343
6344 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006345 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6346 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006348 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006351 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352 }
6353
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006357}
6358
6359
Willy Tarreaua15645d2007-03-18 16:22:39 +01006360/* Iterate the same filter through all response headers contained in <rtr>.
6361 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6362 */
6363int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6364{
6365 char term;
6366 char *cur_ptr, *cur_end, *cur_next;
6367 int cur_idx, old_idx, last_hdr;
6368 struct http_txn *txn = &t->txn;
6369 struct hdr_idx_elem *cur_hdr;
6370 int len, delta;
6371
6372 last_hdr = 0;
6373
Willy Tarreau3a215be2012-03-09 21:39:51 +01006374 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006375 old_idx = 0;
6376
6377 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006378 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006379 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006380 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006381 (exp->action == ACT_ALLOW ||
6382 exp->action == ACT_DENY))
6383 return 0;
6384
6385 cur_idx = txn->hdr_idx.v[old_idx].next;
6386 if (!cur_idx)
6387 break;
6388
6389 cur_hdr = &txn->hdr_idx.v[cur_idx];
6390 cur_ptr = cur_next;
6391 cur_end = cur_ptr + cur_hdr->len;
6392 cur_next = cur_end + cur_hdr->cr + 1;
6393
6394 /* Now we have one header between cur_ptr and cur_end,
6395 * and the next header starts at cur_next.
6396 */
6397
6398 /* The annoying part is that pattern matching needs
6399 * that we modify the contents to null-terminate all
6400 * strings before testing them.
6401 */
6402
6403 term = *cur_end;
6404 *cur_end = '\0';
6405
6406 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6407 switch (exp->action) {
6408 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006410 last_hdr = 1;
6411 break;
6412
6413 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006414 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006415 last_hdr = 1;
6416 break;
6417
6418 case ACT_REPLACE:
6419 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6420 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6421 /* FIXME: if the user adds a newline in the replacement, the
6422 * index will not be recalculated for now, and the new line
6423 * will not be counted as a new header.
6424 */
6425
6426 cur_end += delta;
6427 cur_next += delta;
6428 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006429 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006430 break;
6431
6432 case ACT_REMOVE:
6433 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6434 cur_next += delta;
6435
Willy Tarreaufa355d42009-11-29 18:12:29 +01006436 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6438 txn->hdr_idx.used--;
6439 cur_hdr->len = 0;
6440 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006441 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442 break;
6443
6444 }
6445 }
6446 if (cur_end)
6447 *cur_end = term; /* restore the string terminator */
6448
6449 /* keep the link from this header to next one in case of later
6450 * removal of next header.
6451 */
6452 old_idx = cur_idx;
6453 }
6454 return 0;
6455}
6456
6457
6458/* Apply the filter to the status line in the response buffer <rtr>.
6459 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6460 * or -1 if a replacement resulted in an invalid status line.
6461 */
6462int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6463{
6464 char term;
6465 char *cur_ptr, *cur_end;
6466 int done;
6467 struct http_txn *txn = &t->txn;
6468 int len, delta;
6469
6470
Willy Tarreau3d300592007-03-18 18:34:41 +01006471 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006472 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 (exp->action == ACT_ALLOW ||
6475 exp->action == ACT_DENY))
6476 return 0;
6477 else if (exp->action == ACT_REMOVE)
6478 return 0;
6479
6480 done = 0;
6481
Willy Tarreau3a215be2012-03-09 21:39:51 +01006482 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006483 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484
6485 /* Now we have the status line between cur_ptr and cur_end */
6486
6487 /* The annoying part is that pattern matching needs
6488 * that we modify the contents to null-terminate all
6489 * strings before testing them.
6490 */
6491
6492 term = *cur_end;
6493 *cur_end = '\0';
6494
6495 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6496 switch (exp->action) {
6497 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006498 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 done = 1;
6500 break;
6501
6502 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006503 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 done = 1;
6505 break;
6506
6507 case ACT_REPLACE:
6508 *cur_end = term; /* restore the string terminator */
6509 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6510 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6511 /* FIXME: if the user adds a newline in the replacement, the
6512 * index will not be recalculated for now, and the new line
6513 * will not be counted as a new header.
6514 */
6515
Willy Tarreaufa355d42009-11-29 18:12:29 +01006516 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006518 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006519 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006520 cur_ptr, cur_end + 1,
6521 NULL, NULL);
6522 if (unlikely(!cur_end))
6523 return -1;
6524
6525 /* we have a full respnse and we know that we have either a CR
6526 * or an LF at <ptr>.
6527 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006528 txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006529 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 /* there is no point trying this regex on headers */
6531 return 1;
6532 }
6533 }
6534 *cur_end = term; /* restore the string terminator */
6535 return done;
6536}
6537
6538
6539
6540/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006541 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6543 * unparsable response.
6544 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006545int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006547 struct http_txn *txn = &s->txn;
6548 struct hdr_exp *exp;
6549
6550 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 int ret;
6552
6553 /*
6554 * The interleaving of transformations and verdicts
6555 * makes it difficult to decide to continue or stop
6556 * the evaluation.
6557 */
6558
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006559 if (txn->flags & TX_SVDENY)
6560 break;
6561
Willy Tarreau3d300592007-03-18 18:34:41 +01006562 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006563 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6564 exp->action == ACT_PASS)) {
6565 exp = exp->next;
6566 continue;
6567 }
6568
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006569 /* if this filter had a condition, evaluate it now and skip to
6570 * next filter if the condition does not match.
6571 */
6572 if (exp->cond) {
6573 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6574 ret = acl_pass(ret);
6575 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6576 ret = !ret;
6577 if (!ret)
6578 continue;
6579 }
6580
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006582 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583 if (unlikely(ret < 0))
6584 return -1;
6585
6586 if (likely(ret == 0)) {
6587 /* The filter did not match the response, it can be
6588 * iterated through all headers.
6589 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006590 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592 }
6593 return 0;
6594}
6595
6596
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006598 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006599 * desirable to call it only when needed. This function is also used when we
6600 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006602void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603{
6604 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006605 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006606 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006608 char *hdr_beg, *hdr_end, *hdr_next;
6609 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611 /* Iterate through the headers.
6612 * we start with the start line.
6613 */
6614 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006615 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
6617 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6618 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006619 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620
6621 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006622 hdr_beg = hdr_next;
6623 hdr_end = hdr_beg + cur_hdr->len;
6624 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625
Willy Tarreau24581ba2010-08-31 22:39:35 +02006626 /* We have one full header between hdr_beg and hdr_end, and the
6627 * next header starts at hdr_next. We're only interested in
6628 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 */
6630
Willy Tarreau24581ba2010-08-31 22:39:35 +02006631 is_cookie2 = 0;
6632 prev = hdr_beg + 10;
6633 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006634 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006635 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6636 if (!val) {
6637 old_idx = cur_idx;
6638 continue;
6639 }
6640 is_cookie2 = 1;
6641 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 }
6643
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6645 * <prev> points to the colon.
6646 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006647 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648
Willy Tarreau24581ba2010-08-31 22:39:35 +02006649 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6650 * check-cache is enabled) and we are not interested in checking
6651 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006652 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006653 if (t->be->cookie_name == NULL &&
6654 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006655 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 return;
6657
Willy Tarreau24581ba2010-08-31 22:39:35 +02006658 /* OK so now we know we have to process this response cookie.
6659 * The format of the Set-Cookie header is slightly different
6660 * from the format of the Cookie header in that it does not
6661 * support the comma as a cookie delimiter (thus the header
6662 * cannot be folded) because the Expires attribute described in
6663 * the original Netscape's spec may contain an unquoted date
6664 * with a comma inside. We have to live with this because
6665 * many browsers don't support Max-Age and some browsers don't
6666 * support quoted strings. However the Set-Cookie2 header is
6667 * clean.
6668 *
6669 * We have to keep multiple pointers in order to support cookie
6670 * removal at the beginning, middle or end of header without
6671 * corrupting the header (in case of set-cookie2). A special
6672 * pointer, <scav> points to the beginning of the set-cookie-av
6673 * fields after the first semi-colon. The <next> pointer points
6674 * either to the end of line (set-cookie) or next unquoted comma
6675 * (set-cookie2). All of these headers are valid :
6676 *
6677 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6678 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6679 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6680 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6681 * | | | | | | | | | |
6682 * | | | | | | | | +-> next hdr_end <--+
6683 * | | | | | | | +------------> scav
6684 * | | | | | | +--------------> val_end
6685 * | | | | | +--------------------> val_beg
6686 * | | | | +----------------------> equal
6687 * | | | +------------------------> att_end
6688 * | | +----------------------------> att_beg
6689 * | +------------------------------> prev
6690 * +-----------------------------------------> hdr_beg
6691 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692
Willy Tarreau24581ba2010-08-31 22:39:35 +02006693 for (; prev < hdr_end; prev = next) {
6694 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695
Willy Tarreau24581ba2010-08-31 22:39:35 +02006696 /* find att_beg */
6697 att_beg = prev + 1;
6698 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6699 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700
Willy Tarreau24581ba2010-08-31 22:39:35 +02006701 /* find att_end : this is the first character after the last non
6702 * space before the equal. It may be equal to hdr_end.
6703 */
6704 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705
Willy Tarreau24581ba2010-08-31 22:39:35 +02006706 while (equal < hdr_end) {
6707 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6708 break;
6709 if (http_is_spht[(unsigned char)*equal++])
6710 continue;
6711 att_end = equal;
6712 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau24581ba2010-08-31 22:39:35 +02006714 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6715 * is between <att_beg> and <equal>, both may be identical.
6716 */
6717
6718 /* look for end of cookie if there is an equal sign */
6719 if (equal < hdr_end && *equal == '=') {
6720 /* look for the beginning of the value */
6721 val_beg = equal + 1;
6722 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6723 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006724
Willy Tarreau24581ba2010-08-31 22:39:35 +02006725 /* find the end of the value, respecting quotes */
6726 next = find_cookie_value_end(val_beg, hdr_end);
6727
6728 /* make val_end point to the first white space or delimitor after the value */
6729 val_end = next;
6730 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6731 val_end--;
6732 } else {
6733 /* <equal> points to next comma, semi-colon or EOL */
6734 val_beg = val_end = next = equal;
6735 }
6736
6737 if (next < hdr_end) {
6738 /* Set-Cookie2 supports multiple cookies, and <next> points to
6739 * a colon or semi-colon before the end. So skip all attr-value
6740 * pairs and look for the next comma. For Set-Cookie, since
6741 * commas are permitted in values, skip to the end.
6742 */
6743 if (is_cookie2)
6744 next = find_hdr_value_end(next, hdr_end);
6745 else
6746 next = hdr_end;
6747 }
6748
6749 /* Now everything is as on the diagram above */
6750
6751 /* Ignore cookies with no equal sign */
6752 if (equal == val_end)
6753 continue;
6754
6755 /* If there are spaces around the equal sign, we need to
6756 * strip them otherwise we'll get trouble for cookie captures,
6757 * or even for rewrites. Since this happens extremely rarely,
6758 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006760 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6761 int stripped_before = 0;
6762 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763
Willy Tarreau24581ba2010-08-31 22:39:35 +02006764 if (att_end != equal) {
6765 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6766 equal += stripped_before;
6767 val_beg += stripped_before;
6768 }
6769
6770 if (val_beg > equal + 1) {
6771 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6772 val_beg += stripped_after;
6773 stripped_before += stripped_after;
6774 }
6775
6776 val_end += stripped_before;
6777 next += stripped_before;
6778 hdr_end += stripped_before;
6779 hdr_next += stripped_before;
6780 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006781 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006782 }
6783
6784 /* First, let's see if we want to capture this cookie. We check
6785 * that we don't already have a server side cookie, because we
6786 * can only capture one. Also as an optimisation, we ignore
6787 * cookies shorter than the declared name.
6788 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006789 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006790 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 (val_end - att_beg >= t->fe->capture_namelen) &&
6792 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6793 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006794 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795 Alert("HTTP logging : out of memory.\n");
6796 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006797 else {
6798 if (log_len > t->fe->capture_len)
6799 log_len = t->fe->capture_len;
6800 memcpy(txn->srv_cookie, att_beg, log_len);
6801 txn->srv_cookie[log_len] = 0;
6802 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 }
6804
Willy Tarreau827aee92011-03-10 16:55:02 +01006805 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006807 if (!(t->flags & SN_IGNORE_PRST) &&
6808 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6809 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006810 /* assume passive cookie by default */
6811 txn->flags &= ~TX_SCK_MASK;
6812 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813
6814 /* If the cookie is in insert mode on a known server, we'll delete
6815 * this occurrence because we'll insert another one later.
6816 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 * a direct access.
6818 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006819 if (t->be->options2 & PR_O2_COOK_PSV) {
6820 /* The "preserve" flag was set, we don't want to touch the
6821 * server's cookie.
6822 */
6823 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006824 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006825 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 /* this cookie must be deleted */
6827 if (*prev == ':' && next == hdr_end) {
6828 /* whole header */
6829 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6830 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6831 txn->hdr_idx.used--;
6832 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006833 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 hdr_next += delta;
6835 http_msg_move_end(&txn->rsp, delta);
6836 /* note: while both invalid now, <next> and <hdr_end>
6837 * are still equal, so the for() will stop as expected.
6838 */
6839 } else {
6840 /* just remove the value */
6841 int delta = del_hdr_value(res, &prev, next);
6842 next = prev;
6843 hdr_end += delta;
6844 hdr_next += delta;
6845 cur_hdr->len += delta;
6846 http_msg_move_end(&txn->rsp, delta);
6847 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006848 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006849 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006850 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006852 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854 * with this server since we know it.
6855 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006856 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006857 next += delta;
6858 hdr_end += delta;
6859 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006861 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862
Willy Tarreauf1348312010-10-07 15:54:11 +02006863 txn->flags &= ~TX_SCK_MASK;
6864 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006866 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006868 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006870 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 next += delta;
6872 hdr_end += delta;
6873 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006875 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876
Willy Tarreau827aee92011-03-10 16:55:02 +01006877 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006878 txn->flags &= ~TX_SCK_MASK;
6879 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 }
6881 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006882 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6883 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006884 int cmp_len, value_len;
6885 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Cyril Bontéb21570a2009-11-29 20:04:48 +01006887 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006888 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6889 value_begin = att_beg + t->be->appsession_name_len;
6890 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006891 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 cmp_len = att_end - att_beg;
6893 value_begin = val_beg;
6894 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006895 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006896
Cyril Bonté17530c32010-04-06 21:11:10 +02006897 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6899 /* free a possibly previously allocated memory */
6900 pool_free2(apools.sessid, txn->sessid);
6901
Cyril Bontéb21570a2009-11-29 20:04:48 +01006902 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006903 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006904 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6905 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6906 return;
6907 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006908 memcpy(txn->sessid, value_begin, value_len);
6909 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006911 }
6912 /* that's done for this cookie, check the next one on the same
6913 * line when next != hdr_end (only if is_cookie2).
6914 */
6915 }
6916 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006919
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006920 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006921 appsess *asession = NULL;
6922 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006923 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006924 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006925 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006926 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6927 Alert("Not enough Memory process_srv():asession:calloc().\n");
6928 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6929 return;
6930 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006931 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6932
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6934 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6935 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006936 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937 return;
6938 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006939 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006940 asession->sessid[t->be->appsession_len] = 0;
6941
Willy Tarreau827aee92011-03-10 16:55:02 +01006942 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006944 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006946 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947 return;
6948 }
6949 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006950 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951
6952 asession->request_count = 0;
6953 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6954 }
6955
6956 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6957 asession->request_count++;
6958 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006959}
6960
6961
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962/*
6963 * Check if response is cacheable or not. Updates t->flags.
6964 */
6965void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6966{
6967 struct http_txn *txn = &t->txn;
6968 char *p1, *p2;
6969
6970 char *cur_ptr, *cur_end, *cur_next;
6971 int cur_idx;
6972
Willy Tarreau5df51872007-11-25 16:20:08 +01006973 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 return;
6975
6976 /* Iterate through the headers.
6977 * we start with the start line.
6978 */
6979 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006980 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981
6982 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6983 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006984 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
6986 cur_hdr = &txn->hdr_idx.v[cur_idx];
6987 cur_ptr = cur_next;
6988 cur_end = cur_ptr + cur_hdr->len;
6989 cur_next = cur_end + cur_hdr->cr + 1;
6990
6991 /* We have one full header between cur_ptr and cur_end, and the
6992 * next header starts at cur_next. We're only interested in
6993 * "Cookie:" headers.
6994 */
6995
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006996 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6997 if (val) {
6998 if ((cur_end - (cur_ptr + val) >= 8) &&
6999 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7000 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7001 return;
7002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 }
7004
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007005 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7006 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 continue;
7008
7009 /* OK, right now we know we have a cache-control header at cur_ptr */
7010
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007011 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012
7013 if (p1 >= cur_end) /* no more info */
7014 continue;
7015
7016 /* p1 is at the beginning of the value */
7017 p2 = p1;
7018
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007019 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 p2++;
7021
7022 /* we have a complete value between p1 and p2 */
7023 if (p2 < cur_end && *p2 == '=') {
7024 /* we have something of the form no-cache="set-cookie" */
7025 if ((cur_end - p1 >= 21) &&
7026 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7027 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 continue;
7030 }
7031
7032 /* OK, so we know that either p2 points to the end of string or to a comma */
7033 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7034 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7035 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7036 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007037 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007038 return;
7039 }
7040
7041 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007042 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 continue;
7044 }
7045 }
7046}
7047
7048
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049/*
7050 * Try to retrieve a known appsession in the URI, then the associated server.
7051 * If the server is found, it's assigned to the session.
7052 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007055 char *end_params, *first_param, *cur_param, *next_param;
7056 char separator;
7057 int value_len;
7058
7059 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007061 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007062 (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 +01007063 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007064 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065
Cyril Bontéb21570a2009-11-29 20:04:48 +01007066 first_param = NULL;
7067 switch (mode) {
7068 case PR_O2_AS_M_PP:
7069 first_param = memchr(begin, ';', len);
7070 break;
7071 case PR_O2_AS_M_QS:
7072 first_param = memchr(begin, '?', len);
7073 break;
7074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
Cyril Bontéb21570a2009-11-29 20:04:48 +01007076 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007077 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007078 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079
Cyril Bontéb21570a2009-11-29 20:04:48 +01007080 switch (mode) {
7081 case PR_O2_AS_M_PP:
7082 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7083 end_params = (char *) begin + len;
7084 }
7085 separator = ';';
7086 break;
7087 case PR_O2_AS_M_QS:
7088 end_params = (char *) begin + len;
7089 separator = '&';
7090 break;
7091 default:
7092 /* unknown mode, shouldn't happen */
7093 return;
7094 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095
Cyril Bontéb21570a2009-11-29 20:04:48 +01007096 cur_param = next_param = end_params;
7097 while (cur_param > first_param) {
7098 cur_param--;
7099 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7100 /* let's see if this is the appsession parameter */
7101 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7102 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7103 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7104 /* Cool... it's the right one */
7105 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7106 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7107 if (value_len > 0) {
7108 manage_client_side_appsession(t, cur_param, value_len);
7109 }
7110 break;
7111 }
7112 next_param = cur_param;
7113 }
7114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007116 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007117 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119}
7120
Willy Tarreaub2513902006-12-17 14:52:38 +01007121/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007122 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007123 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007124 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007125 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007126 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007127 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007128 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007129 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007130int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007131{
7132 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007133 struct http_msg *msg = &txn->req;
7134 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7135 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007136
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007137 if (!uri_auth)
7138 return 0;
7139
Cyril Bonté70be45d2010-10-12 00:14:35 +02007140 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007141 return 0;
7142
Willy Tarreau295a8372011-03-10 11:25:07 +01007143 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007144 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007146 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007147 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007148 return 0;
7149
Willy Tarreau3a215be2012-03-09 21:39:51 +01007150 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007151 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007152 return 0;
7153
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007154 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007155 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007156 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007157 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007158 break;
7159 }
7160 h++;
7161 }
7162
7163 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007164 h = uri + uri_auth->uri_len;
7165 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007166 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007167 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007168 break;
7169 }
7170 h++;
7171 }
7172 }
7173
Willy Tarreau3a215be2012-03-09 21:39:51 +01007174 h = uri + uri_auth->uri_len;
7175 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007176 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007177 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007178 break;
7179 }
7180 h++;
7181 }
7182
Willy Tarreau3a215be2012-03-09 21:39:51 +01007183 h = uri + uri_auth->uri_len;
7184 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007185 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007186 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007187 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007188 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7189 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7190 si->applet.ctx.stats.st_code = i;
7191 break;
7192 }
7193 }
7194 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007195 break;
7196 }
7197 h++;
7198 }
7199
Willy Tarreau295a8372011-03-10 11:25:07 +01007200 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007201
Willy Tarreaub2513902006-12-17 14:52:38 +01007202 return 1;
7203}
7204
Willy Tarreau4076a152009-04-02 15:18:36 +02007205/*
7206 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007207 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007208 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7209 * or response messages cannot wrap, this function may also be used with chunks
7210 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007211 */
7212void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007213 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007214 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007215{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007216 struct buffer *buf = msg->buf;
7217
Willy Tarreauea1175a2012-03-05 15:52:30 +01007218 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7219 int len1 = buf->size - msg->som - (buf->p - buf->data);
7220 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7221 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007222 if (es->len > len1 && len1 < sizeof(es->buf))
7223 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7224 }
7225 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007226 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7227 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007228 }
7229
Willy Tarreau4076a152009-04-02 15:18:36 +02007230 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007231 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7232 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7233 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007234 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007235 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007236
Willy Tarreau4076a152009-04-02 15:18:36 +02007237 es->when = date; // user-visible date
7238 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007239 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007240 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007241 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007242 es->state = state;
7243 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007244 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007245}
Willy Tarreaub2513902006-12-17 14:52:38 +01007246
Willy Tarreau294c4732011-12-16 21:35:50 +01007247/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7248 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7249 * performed over the whole headers. Otherwise it must contain a valid header
7250 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7251 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7252 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7253 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7254 * -1.
7255 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007256 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007257unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7258 struct hdr_idx *idx, int occ,
7259 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007260{
Willy Tarreau294c4732011-12-16 21:35:50 +01007261 struct hdr_ctx local_ctx;
7262 char *ptr_hist[MAX_HDR_HISTORY];
7263 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007264 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007265 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007266
Willy Tarreau294c4732011-12-16 21:35:50 +01007267 if (!ctx) {
7268 local_ctx.idx = 0;
7269 ctx = &local_ctx;
7270 }
7271
Willy Tarreaubce70882009-09-07 11:51:47 +02007272 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007273 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007274 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007275 occ--;
7276 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007277 *vptr = ctx->line + ctx->val;
7278 *vlen = ctx->vlen;
7279 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007280 }
7281 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007282 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007283 }
7284
7285 /* negative occurrence, we scan all the list then walk back */
7286 if (-occ > MAX_HDR_HISTORY)
7287 return 0;
7288
Willy Tarreau294c4732011-12-16 21:35:50 +01007289 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007290 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007291 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7292 len_hist[hist_ptr] = ctx->vlen;
7293 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007294 hist_ptr = 0;
7295 found++;
7296 }
7297 if (-occ > found)
7298 return 0;
7299 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7300 * find occurrence -occ, so we have to check [hist_ptr+occ].
7301 */
7302 hist_ptr += occ;
7303 if (hist_ptr >= MAX_HDR_HISTORY)
7304 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007305 *vptr = ptr_hist[hist_ptr];
7306 *vlen = len_hist[hist_ptr];
7307 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007308}
7309
Willy Tarreaubaaee002006-06-26 02:48:02 +02007310/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311 * Print a debug line with a header
7312 */
7313void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7314{
7315 int len, max;
7316 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007317 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318 max = end - start;
7319 UBOUND(max, sizeof(trash) - len - 1);
7320 len += strlcpy2(trash + len, start, max + 1);
7321 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007322 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323}
7324
Willy Tarreau0937bc42009-12-22 15:03:09 +01007325/*
7326 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7327 * the required fields are properly allocated and that we only need to (re)init
7328 * them. This should be used before processing any new request.
7329 */
7330void http_init_txn(struct session *s)
7331{
7332 struct http_txn *txn = &s->txn;
7333 struct proxy *fe = s->fe;
7334
7335 txn->flags = 0;
7336 txn->status = -1;
7337
William Lallemand5f232402012-04-05 18:02:55 +02007338 global.req_count++;
7339
Willy Tarreauf64d1412010-10-07 20:06:11 +02007340 txn->cookie_first_date = 0;
7341 txn->cookie_last_date = 0;
7342
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007343 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007344 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007345 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007346 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007347 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007348 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007349 txn->req.chunk_len = 0LL;
7350 txn->req.body_len = 0LL;
7351 txn->rsp.chunk_len = 0LL;
7352 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007353 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7354 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007355 txn->req.buf = s->req;
7356 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007357
7358 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007359
7360 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7361 if (fe->options2 & PR_O2_REQBUG_OK)
7362 txn->req.err_pos = -1; /* let buggy requests pass */
7363
Willy Tarreau46023632010-01-07 22:51:47 +01007364 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007365 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7366
Willy Tarreau46023632010-01-07 22:51:47 +01007367 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007368 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7369
7370 if (txn->hdr_idx.v)
7371 hdr_idx_init(&txn->hdr_idx);
7372}
7373
7374/* to be used at the end of a transaction */
7375void http_end_txn(struct session *s)
7376{
7377 struct http_txn *txn = &s->txn;
7378
7379 /* these ones will have been dynamically allocated */
7380 pool_free2(pool2_requri, txn->uri);
7381 pool_free2(pool2_capture, txn->cli_cookie);
7382 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007383 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007384 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007385
William Lallemanda73203e2012-03-12 12:48:57 +01007386 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007387 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007388 txn->uri = NULL;
7389 txn->srv_cookie = NULL;
7390 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007391
7392 if (txn->req.cap) {
7393 struct cap_hdr *h;
7394 for (h = s->fe->req_cap; h; h = h->next)
7395 pool_free2(h->pool, txn->req.cap[h->index]);
7396 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7397 }
7398
7399 if (txn->rsp.cap) {
7400 struct cap_hdr *h;
7401 for (h = s->fe->rsp_cap; h; h = h->next)
7402 pool_free2(h->pool, txn->rsp.cap[h->index]);
7403 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7404 }
7405
Willy Tarreau0937bc42009-12-22 15:03:09 +01007406}
7407
7408/* to be used at the end of a transaction to prepare a new one */
7409void http_reset_txn(struct session *s)
7410{
7411 http_end_txn(s);
7412 http_init_txn(s);
7413
7414 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007415 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007416 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007417 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007418 /* re-init store persistence */
7419 s->store_count = 0;
7420
Willy Tarreau0937bc42009-12-22 15:03:09 +01007421 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007422
7423 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7424
Willy Tarreau739cfba2010-01-25 23:11:14 +01007425 /* We must trim any excess data from the response buffer, because we
7426 * may have blocked an invalid response from a server that we don't
7427 * want to accidentely forward once we disable the analysers, nor do
7428 * we want those data to come along with next response. A typical
7429 * example of such data would be from a buggy server responding to
7430 * a HEAD with some data, or sending more than the advertised
7431 * content-length.
7432 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007433 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007434 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007435
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007437 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007438
Willy Tarreaud04e8582010-05-31 12:31:35 +02007439 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007440 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007441
7442 s->req->rex = TICK_ETERNITY;
7443 s->req->wex = TICK_ETERNITY;
7444 s->req->analyse_exp = TICK_ETERNITY;
7445 s->rep->rex = TICK_ETERNITY;
7446 s->rep->wex = TICK_ETERNITY;
7447 s->rep->analyse_exp = TICK_ETERNITY;
7448}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007449
Willy Tarreauff011f22011-01-06 17:51:27 +01007450void free_http_req_rules(struct list *r) {
7451 struct http_req_rule *tr, *pr;
7452
7453 list_for_each_entry_safe(pr, tr, r, list) {
7454 LIST_DEL(&pr->list);
7455 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7456 free(pr->http_auth.realm);
7457
7458 free(pr);
7459 }
7460}
7461
7462struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7463{
7464 struct http_req_rule *rule;
7465 int cur_arg;
7466
7467 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7468 if (!rule) {
7469 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7470 return NULL;
7471 }
7472
7473 if (!*args[0]) {
7474 goto req_error_parsing;
7475 } else if (!strcmp(args[0], "allow")) {
7476 rule->action = HTTP_REQ_ACT_ALLOW;
7477 cur_arg = 1;
7478 } else if (!strcmp(args[0], "deny")) {
7479 rule->action = HTTP_REQ_ACT_DENY;
7480 cur_arg = 1;
7481 } else if (!strcmp(args[0], "auth")) {
7482 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7483 cur_arg = 1;
7484
7485 while(*args[cur_arg]) {
7486 if (!strcmp(args[cur_arg], "realm")) {
7487 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7488 cur_arg+=2;
7489 continue;
7490 } else
7491 break;
7492 }
7493 } else {
7494req_error_parsing:
7495 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7496 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7497 return NULL;
7498 }
7499
7500 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7501 struct acl_cond *cond;
7502
7503 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7504 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7505 file, linenum, args[0]);
7506 return NULL;
7507 }
7508 rule->cond = cond;
7509 }
7510 else if (*args[cur_arg]) {
7511 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7512 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7513 file, linenum, args[0], args[cur_arg]);
7514 return NULL;
7515 }
7516
7517 return rule;
7518}
7519
Willy Tarreau8797c062007-05-07 00:55:35 +02007520/************************************************************************/
7521/* The code below is dedicated to ACL parsing and matching */
7522/************************************************************************/
7523
7524
7525
7526
7527/* 1. Check on METHOD
7528 * We use the pre-parsed method if it is known, and store its number as an
7529 * integer. If it is unknown, we use the pointer and the length.
7530 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007531static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007532{
7533 int len, meth;
7534
Willy Tarreauae8b7962007-06-09 23:10:04 +02007535 len = strlen(*text);
7536 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007537
7538 pattern->val.i = meth;
7539 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007540 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007541 if (!pattern->ptr.str)
7542 return 0;
7543 pattern->len = len;
7544 }
7545 return 1;
7546}
7547
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007548/* This function fetches the method of current HTTP request and stores
7549 * it in the global pattern struct as a chunk. There are two possibilities :
7550 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7551 * in <len> and <ptr> is NULL ;
7552 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7553 * <len> to its length.
7554 * This is intended to be used with acl_match_meth() only.
7555 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007556static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007557acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7558 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007559{
7560 int meth;
7561 struct http_txn *txn = l7;
7562
Willy Tarreaub6866442008-07-14 23:54:42 +02007563 if (!txn)
7564 return 0;
7565
Willy Tarreau655dce92009-11-08 13:10:58 +01007566 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007567 return 0;
7568
Willy Tarreau8797c062007-05-07 00:55:35 +02007569 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007570 temp_pattern.data.str.len = meth;
7571 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007572 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007573 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7574 /* ensure the indexes are not affected */
7575 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007576 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007577 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007578 }
7579 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7580 return 1;
7581}
7582
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007583/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007584static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7585{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007586 int icase;
7587
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007588
7589 if (temp_pattern.data.str.str == NULL) {
7590 /* well-known method */
7591 if (temp_pattern.data.str.len == pattern->val.i)
7592 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007593 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007594 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007595
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007596 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7597 if (pattern->val.i != HTTP_METH_OTHER)
7598 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007599
7600 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007601 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007602 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007603
7604 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007605 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7606 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007607 return ACL_PAT_FAIL;
7608 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007609}
7610
7611/* 2. Check on Request/Status Version
7612 * We simply compare strings here.
7613 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007614static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007615{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007616 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007617 if (!pattern->ptr.str)
7618 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007619 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007620 return 1;
7621}
7622
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007623static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007624acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7625 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007626{
7627 struct http_txn *txn = l7;
7628 char *ptr;
7629 int len;
7630
Willy Tarreaub6866442008-07-14 23:54:42 +02007631 if (!txn)
7632 return 0;
7633
Willy Tarreau655dce92009-11-08 13:10:58 +01007634 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007635 return 0;
7636
Willy Tarreau8797c062007-05-07 00:55:35 +02007637 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007638 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007639
7640 while ((len-- > 0) && (*ptr++ != '/'));
7641 if (len <= 0)
7642 return 0;
7643
Willy Tarreau664092c2011-12-16 19:11:42 +01007644 temp_pattern.data.str.str = ptr;
7645 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007646
7647 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7648 return 1;
7649}
7650
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007651static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007652acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7653 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007654{
7655 struct http_txn *txn = l7;
7656 char *ptr;
7657 int len;
7658
Willy Tarreaub6866442008-07-14 23:54:42 +02007659 if (!txn)
7660 return 0;
7661
Willy Tarreau655dce92009-11-08 13:10:58 +01007662 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007663 return 0;
7664
Willy Tarreau8797c062007-05-07 00:55:35 +02007665 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007666 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007667
7668 while ((len-- > 0) && (*ptr++ != '/'));
7669 if (len <= 0)
7670 return 0;
7671
Willy Tarreau664092c2011-12-16 19:11:42 +01007672 temp_pattern.data.str.str = ptr;
7673 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007674
7675 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7676 return 1;
7677}
7678
7679/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007680static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007681acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7682 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007683{
7684 struct http_txn *txn = l7;
7685 char *ptr;
7686 int len;
7687
Willy Tarreaub6866442008-07-14 23:54:42 +02007688 if (!txn)
7689 return 0;
7690
Willy Tarreau655dce92009-11-08 13:10:58 +01007691 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007692 return 0;
7693
Willy Tarreau8797c062007-05-07 00:55:35 +02007694 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007695 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007696
Willy Tarreaua5e37562011-12-16 17:06:15 +01007697 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007698 test->flags = ACL_TEST_F_VOL_1ST;
7699 return 1;
7700}
7701
7702/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007703static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007704acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7705 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007706{
7707 struct http_txn *txn = l7;
7708
Willy Tarreaub6866442008-07-14 23:54:42 +02007709 if (!txn)
7710 return 0;
7711
Willy Tarreau655dce92009-11-08 13:10:58 +01007712 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007713 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007714
Willy Tarreauc11416f2007-06-17 16:58:38 +02007715 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7716 /* ensure the indexes are not affected */
7717 return 0;
7718
Willy Tarreau664092c2011-12-16 19:11:42 +01007719 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007720 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007721
Willy Tarreauf3d25982007-05-08 22:45:09 +02007722 /* we do not need to set READ_ONLY because the data is in a buffer */
7723 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007724 return 1;
7725}
7726
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007727static int
7728acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7729 struct acl_expr *expr, struct acl_test *test)
7730{
7731 struct http_txn *txn = l7;
7732
Willy Tarreaub6866442008-07-14 23:54:42 +02007733 if (!txn)
7734 return 0;
7735
Willy Tarreau655dce92009-11-08 13:10:58 +01007736 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007737 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007738
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007739 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7740 /* ensure the indexes are not affected */
7741 return 0;
7742
7743 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007744 url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007745 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7746 return 0;
7747 temp_pattern.type = PATTERN_TYPE_IP;
7748 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007749
7750 /*
7751 * If we are parsing url in frontend space, we prepare backend stage
7752 * to not parse again the same url ! optimization lazyness...
7753 */
7754 if (px->options & PR_O_HTTP_PROXY)
7755 l4->flags |= SN_ADDR_SET;
7756
Willy Tarreauf4362b32011-12-16 17:49:52 +01007757 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007758 return 1;
7759}
7760
7761static int
7762acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7763 struct acl_expr *expr, struct acl_test *test)
7764{
7765 struct http_txn *txn = l7;
7766
Willy Tarreaub6866442008-07-14 23:54:42 +02007767 if (!txn)
7768 return 0;
7769
Willy Tarreau655dce92009-11-08 13:10:58 +01007770 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007771 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007772
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007773 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7774 /* ensure the indexes are not affected */
7775 return 0;
7776
7777 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007778 url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreaua5e37562011-12-16 17:06:15 +01007779 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007780
7781 if (px->options & PR_O_HTTP_PROXY)
7782 l4->flags |= SN_ADDR_SET;
7783
7784 test->flags = ACL_TEST_F_READ_ONLY;
7785 return 1;
7786}
7787
Willy Tarreauc11416f2007-06-17 16:58:38 +02007788/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7789 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7790 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007791static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007792acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007793 struct acl_expr *expr, struct acl_test *test)
7794{
7795 struct http_txn *txn = l7;
7796 struct hdr_idx *idx = &txn->hdr_idx;
7797 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007798
Willy Tarreaub6866442008-07-14 23:54:42 +02007799 if (!txn)
7800 return 0;
7801
Willy Tarreau33a7e692007-06-10 19:45:56 +02007802 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7803 /* search for header from the beginning */
7804 ctx->idx = 0;
7805
Willy Tarreau33a7e692007-06-10 19:45:56 +02007806 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7807 test->flags |= ACL_TEST_F_FETCH_MORE;
7808 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007809 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7810 temp_pattern.data.str.len = ctx->vlen;
7811
Willy Tarreau33a7e692007-06-10 19:45:56 +02007812 return 1;
7813 }
7814
7815 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7816 test->flags |= ACL_TEST_F_VOL_HDR;
7817 return 0;
7818}
7819
Willy Tarreau33a7e692007-06-10 19:45:56 +02007820static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7822 struct acl_expr *expr, struct acl_test *test)
7823{
7824 struct http_txn *txn = l7;
7825
Willy Tarreaub6866442008-07-14 23:54:42 +02007826 if (!txn)
7827 return 0;
7828
Willy Tarreau655dce92009-11-08 13:10:58 +01007829 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007830 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007831
Willy Tarreauc11416f2007-06-17 16:58:38 +02007832 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7833 /* ensure the indexes are not affected */
7834 return 0;
7835
Willy Tarreau3a215be2012-03-09 21:39:51 +01007836 return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007837}
7838
7839static int
7840acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7841 struct acl_expr *expr, struct acl_test *test)
7842{
7843 struct http_txn *txn = l7;
7844
Willy Tarreaub6866442008-07-14 23:54:42 +02007845 if (!txn)
7846 return 0;
7847
Willy Tarreau655dce92009-11-08 13:10:58 +01007848 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007849 return 0;
7850
Willy Tarreau3a215be2012-03-09 21:39:51 +01007851 return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007852}
7853
7854/* 6. Check on HTTP header count. The number of occurrences is returned.
7855 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7856 */
7857static int
7858acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007859 struct acl_expr *expr, struct acl_test *test)
7860{
7861 struct http_txn *txn = l7;
7862 struct hdr_idx *idx = &txn->hdr_idx;
7863 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007864 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007865
Willy Tarreaub6866442008-07-14 23:54:42 +02007866 if (!txn)
7867 return 0;
7868
Willy Tarreau33a7e692007-06-10 19:45:56 +02007869 ctx.idx = 0;
7870 cnt = 0;
7871 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7872 cnt++;
7873
Willy Tarreaua5e37562011-12-16 17:06:15 +01007874 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007875 test->flags = ACL_TEST_F_VOL_HDR;
7876 return 1;
7877}
7878
Willy Tarreauc11416f2007-06-17 16:58:38 +02007879static int
7880acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7881 struct acl_expr *expr, struct acl_test *test)
7882{
7883 struct http_txn *txn = l7;
7884
Willy Tarreaub6866442008-07-14 23:54:42 +02007885 if (!txn)
7886 return 0;
7887
Willy Tarreau655dce92009-11-08 13:10:58 +01007888 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007889 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007890
Willy Tarreauc11416f2007-06-17 16:58:38 +02007891 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7892 /* ensure the indexes are not affected */
7893 return 0;
7894
Willy Tarreau3a215be2012-03-09 21:39:51 +01007895 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007896}
7897
7898static int
7899acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7900 struct acl_expr *expr, struct acl_test *test)
7901{
7902 struct http_txn *txn = l7;
7903
Willy Tarreaub6866442008-07-14 23:54:42 +02007904 if (!txn)
7905 return 0;
7906
Willy Tarreau655dce92009-11-08 13:10:58 +01007907 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007908 return 0;
7909
Willy Tarreau3a215be2012-03-09 21:39:51 +01007910 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007911}
7912
Willy Tarreau33a7e692007-06-10 19:45:56 +02007913/* 7. Check on HTTP header's integer value. The integer value is returned.
7914 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007915 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007916 */
7917static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007918acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007919 struct acl_expr *expr, struct acl_test *test)
7920{
7921 struct http_txn *txn = l7;
7922 struct hdr_idx *idx = &txn->hdr_idx;
7923 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924
Willy Tarreaub6866442008-07-14 23:54:42 +02007925 if (!txn)
7926 return 0;
7927
Willy Tarreau33a7e692007-06-10 19:45:56 +02007928 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7929 /* search for header from the beginning */
7930 ctx->idx = 0;
7931
Willy Tarreau33a7e692007-06-10 19:45:56 +02007932 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7933 test->flags |= ACL_TEST_F_FETCH_MORE;
7934 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007935 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007936 return 1;
7937 }
7938
7939 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7940 test->flags |= ACL_TEST_F_VOL_HDR;
7941 return 0;
7942}
7943
Willy Tarreauc11416f2007-06-17 16:58:38 +02007944static int
7945acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7946 struct acl_expr *expr, struct acl_test *test)
7947{
7948 struct http_txn *txn = l7;
7949
Willy Tarreaub6866442008-07-14 23:54:42 +02007950 if (!txn)
7951 return 0;
7952
Willy Tarreau655dce92009-11-08 13:10:58 +01007953 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007954 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007955
Willy Tarreauc11416f2007-06-17 16:58:38 +02007956 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7957 /* ensure the indexes are not affected */
7958 return 0;
7959
Willy Tarreau3a215be2012-03-09 21:39:51 +01007960 return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007961}
7962
7963static int
7964acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7965 struct acl_expr *expr, struct acl_test *test)
7966{
7967 struct http_txn *txn = l7;
7968
Willy Tarreaub6866442008-07-14 23:54:42 +02007969 if (!txn)
7970 return 0;
7971
Willy Tarreau655dce92009-11-08 13:10:58 +01007972 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007973 return 0;
7974
Willy Tarreau3a215be2012-03-09 21:39:51 +01007975 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007976}
7977
Willy Tarreau106f9792009-09-19 07:54:16 +02007978/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7979 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7980 */
7981static int
7982acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7983 struct acl_expr *expr, struct acl_test *test)
7984{
7985 struct http_txn *txn = l7;
7986 struct hdr_idx *idx = &txn->hdr_idx;
7987 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7988
7989 if (!txn)
7990 return 0;
7991
7992 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7993 /* search for header from the beginning */
7994 ctx->idx = 0;
7995
Willy Tarreauf4362b32011-12-16 17:49:52 +01007996 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007997 test->flags |= ACL_TEST_F_FETCH_MORE;
7998 test->flags |= ACL_TEST_F_VOL_HDR;
7999 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008000 temp_pattern.type = PATTERN_TYPE_IP;
8001 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8002 return 1;
8003 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008004 }
8005
8006 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8007 test->flags |= ACL_TEST_F_VOL_HDR;
8008 return 0;
8009}
8010
8011static int
8012acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8013 struct acl_expr *expr, struct acl_test *test)
8014{
8015 struct http_txn *txn = l7;
8016
8017 if (!txn)
8018 return 0;
8019
Willy Tarreau655dce92009-11-08 13:10:58 +01008020 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008021 return 0;
8022
8023 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8024 /* ensure the indexes are not affected */
8025 return 0;
8026
Willy Tarreau3a215be2012-03-09 21:39:51 +01008027 return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008028}
8029
8030static int
8031acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8032 struct acl_expr *expr, struct acl_test *test)
8033{
8034 struct http_txn *txn = l7;
8035
8036 if (!txn)
8037 return 0;
8038
Willy Tarreau655dce92009-11-08 13:10:58 +01008039 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008040 return 0;
8041
Willy Tarreau3a215be2012-03-09 21:39:51 +01008042 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008043}
8044
Willy Tarreau737b0c12007-06-10 21:28:46 +02008045/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8046 * the first '/' after the possible hostname, and ends before the possible '?'.
8047 */
8048static int
8049acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8050 struct acl_expr *expr, struct acl_test *test)
8051{
8052 struct http_txn *txn = l7;
8053 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008054
Willy Tarreaub6866442008-07-14 23:54:42 +02008055 if (!txn)
8056 return 0;
8057
Willy Tarreau655dce92009-11-08 13:10:58 +01008058 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008059 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008060
Willy Tarreauc11416f2007-06-17 16:58:38 +02008061 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8062 /* ensure the indexes are not affected */
8063 return 0;
8064
Willy Tarreau3a215be2012-03-09 21:39:51 +01008065 end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008066 ptr = http_get_path(txn);
8067 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008068 return 0;
8069
8070 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008071 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008072
8073 while (ptr < end && *ptr != '?')
8074 ptr++;
8075
Willy Tarreau664092c2011-12-16 19:11:42 +01008076 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008077
8078 /* we do not need to set READ_ONLY because the data is in a buffer */
8079 test->flags = ACL_TEST_F_VOL_1ST;
8080 return 1;
8081}
8082
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008083static int
8084acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8085 struct acl_expr *expr, struct acl_test *test)
8086{
8087 struct buffer *req = s->req;
8088 struct http_txn *txn = &s->txn;
8089 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008090
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008091 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8092 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8093 */
8094
8095 if (!s || !req)
8096 return 0;
8097
Willy Tarreau655dce92009-11-08 13:10:58 +01008098 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008099 /* Already decoded as OK */
8100 test->flags |= ACL_TEST_F_SET_RES_PASS;
8101 return 1;
8102 }
8103
8104 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008105 if (likely(msg->next < req->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01008106 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008107
Willy Tarreau655dce92009-11-08 13:10:58 +01008108 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008109 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8110 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8111 return 1;
8112 }
8113 /* wait for final state */
8114 test->flags |= ACL_TEST_F_MAY_CHANGE;
8115 return 0;
8116 }
8117
8118 /* OK we got a valid HTTP request. We have some minor preparation to
8119 * perform so that further checks can rely on HTTP tests.
8120 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008121 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008122 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8123 s->flags |= SN_REDIRECTABLE;
8124
Willy Tarreau418bfcc2012-03-09 13:56:20 +01008125 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008126 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8127 return 1;
8128 }
8129
8130 test->flags |= ACL_TEST_F_SET_RES_PASS;
8131 return 1;
8132}
8133
Willy Tarreau7f18e522010-10-22 20:04:13 +02008134/* return a valid test if the current request is the first one on the connection */
8135static int
8136acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8137 struct acl_expr *expr, struct acl_test *test)
8138{
8139 if (!s)
8140 return 0;
8141
8142 if (s->txn.flags & TX_NOT_FIRST)
8143 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8144 else
8145 test->flags |= ACL_TEST_F_SET_RES_PASS;
8146
8147 return 1;
8148}
8149
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008150static int
8151acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8152 struct acl_expr *expr, struct acl_test *test)
8153{
8154
8155 if (!s)
8156 return 0;
8157
8158 if (!get_http_auth(s))
8159 return 0;
8160
8161 test->ctx.a[0] = expr->arg.ul;
8162 test->ctx.a[1] = s->txn.auth.user;
8163 test->ctx.a[2] = s->txn.auth.pass;
8164
8165 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8166
8167 return 1;
8168}
Willy Tarreau8797c062007-05-07 00:55:35 +02008169
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008170/* Try to find the next occurrence of a cookie name in a cookie header value.
8171 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8172 * the cookie value is returned into *value and *value_l, and the function
8173 * returns a pointer to the next pointer to search from if the value was found.
8174 * Otherwise if the cookie was not found, NULL is returned and neither value
8175 * nor value_l are touched. The input <hdr> string should first point to the
8176 * header's value, and the <hdr_end> pointer must point to the first character
8177 * not part of the value. <list> must be non-zero if value may represent a list
8178 * of values (cookie headers). This makes it faster to abort parsing when no
8179 * list is expected.
8180 */
8181static char *
8182extract_cookie_value(char *hdr, const char *hdr_end,
8183 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008184 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008185{
8186 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8187 char *next;
8188
8189 /* we search at least a cookie name followed by an equal, and more
8190 * generally something like this :
8191 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8192 */
8193 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8194 /* Iterate through all cookies on this line */
8195
8196 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8197 att_beg++;
8198
8199 /* find att_end : this is the first character after the last non
8200 * space before the equal. It may be equal to hdr_end.
8201 */
8202 equal = att_end = att_beg;
8203
8204 while (equal < hdr_end) {
8205 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8206 break;
8207 if (http_is_spht[(unsigned char)*equal++])
8208 continue;
8209 att_end = equal;
8210 }
8211
8212 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8213 * is between <att_beg> and <equal>, both may be identical.
8214 */
8215
8216 /* look for end of cookie if there is an equal sign */
8217 if (equal < hdr_end && *equal == '=') {
8218 /* look for the beginning of the value */
8219 val_beg = equal + 1;
8220 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8221 val_beg++;
8222
8223 /* find the end of the value, respecting quotes */
8224 next = find_cookie_value_end(val_beg, hdr_end);
8225
8226 /* make val_end point to the first white space or delimitor after the value */
8227 val_end = next;
8228 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8229 val_end--;
8230 } else {
8231 val_beg = val_end = next = equal;
8232 }
8233
8234 /* We have nothing to do with attributes beginning with '$'. However,
8235 * they will automatically be removed if a header before them is removed,
8236 * since they're supposed to be linked together.
8237 */
8238 if (*att_beg == '$')
8239 continue;
8240
8241 /* Ignore cookies with no equal sign */
8242 if (equal == next)
8243 continue;
8244
8245 /* Now we have the cookie name between att_beg and att_end, and
8246 * its value between val_beg and val_end.
8247 */
8248
8249 if (att_end - att_beg == cookie_name_l &&
8250 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8251 /* let's return this value and indicate where to go on from */
8252 *value = val_beg;
8253 *value_l = val_end - val_beg;
8254 return next + 1;
8255 }
8256
8257 /* Set-Cookie headers only have the name in the first attr=value part */
8258 if (!list)
8259 break;
8260 }
8261
8262 return NULL;
8263}
8264
8265/* Iterate over all cookies present in a request. The context is stored in
8266 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8267 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8268 * non-null, then multiple cookies may be parsed on the same line.
8269 * The cookie name is in expr->arg and the name length in expr->arg_len.
8270 */
8271static int
8272acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8273 const char *hdr_name, int hdr_name_len, int multi,
8274 struct acl_expr *expr, struct acl_test *test)
8275{
8276 struct http_txn *txn = l7;
8277 struct hdr_idx *idx = &txn->hdr_idx;
8278 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8279
8280 if (!txn)
8281 return 0;
8282
8283 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8284 /* search for the header from the beginning, we must first initialize
8285 * the search parameters.
8286 */
8287 test->ctx.a[0] = NULL;
8288 ctx->idx = 0;
8289 }
8290
8291 while (1) {
8292 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8293 if (!test->ctx.a[0]) {
8294 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8295 goto out;
8296
8297 if (ctx->vlen < expr->arg_len + 1)
8298 continue;
8299
8300 test->ctx.a[0] = ctx->line + ctx->val;
8301 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8302 }
8303
8304 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8305 expr->arg.str, expr->arg_len, multi,
8306 &temp_pattern.data.str.str,
8307 &temp_pattern.data.str.len);
8308 if (test->ctx.a[0]) {
8309 /* one value was returned into temp_pattern.data.str.{str,len} */
8310 test->flags |= ACL_TEST_F_FETCH_MORE;
8311 test->flags |= ACL_TEST_F_VOL_HDR;
8312 return 1;
8313 }
8314 }
8315
8316 out:
8317 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8318 test->flags |= ACL_TEST_F_VOL_HDR;
8319 return 0;
8320}
8321
8322static int
8323acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8324 struct acl_expr *expr, struct acl_test *test)
8325{
8326 struct http_txn *txn = l7;
8327
8328 if (!txn)
8329 return 0;
8330
8331 if (txn->req.msg_state < HTTP_MSG_BODY)
8332 return 0;
8333
8334 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8335 /* ensure the indexes are not affected */
8336 return 0;
8337
8338 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008339 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008340}
8341
8342static int
8343acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8344 struct acl_expr *expr, struct acl_test *test)
8345{
8346 struct http_txn *txn = l7;
8347
8348 if (!txn)
8349 return 0;
8350
8351 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8352 return 0;
8353
8354 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008355 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008356}
8357
8358/* Iterate over all cookies present in a request to count how many occurrences
8359 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8360 * multiple cookies may be parsed on the same line.
8361 */
8362static int
8363acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8364 const char *hdr_name, int hdr_name_len, int multi,
8365 struct acl_expr *expr, struct acl_test *test)
8366{
8367 struct http_txn *txn = l7;
8368 struct hdr_idx *idx = &txn->hdr_idx;
8369 struct hdr_ctx ctx;
8370 int cnt;
8371 char *val_beg, *val_end;
8372
8373 if (!txn)
8374 return 0;
8375
Willy Tarreau46787ed2012-04-11 17:28:40 +02008376 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008377 ctx.idx = 0;
8378 cnt = 0;
8379
8380 while (1) {
8381 /* Note: val_beg == NULL every time we need to fetch a new header */
8382 if (!val_beg) {
8383 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8384 break;
8385
8386 if (ctx.vlen < expr->arg_len + 1)
8387 continue;
8388
8389 val_beg = ctx.line + ctx.val;
8390 val_end = val_beg + ctx.vlen;
8391 }
8392
8393 while ((val_beg = extract_cookie_value(val_beg, val_end,
8394 expr->arg.str, expr->arg_len, multi,
8395 &temp_pattern.data.str.str,
8396 &temp_pattern.data.str.len))) {
8397 cnt++;
8398 }
8399 }
8400
8401 temp_pattern.data.integer = cnt;
8402 test->flags |= ACL_TEST_F_VOL_HDR;
8403 return 1;
8404}
8405
8406static int
8407acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8408 struct acl_expr *expr, struct acl_test *test)
8409{
8410 struct http_txn *txn = l7;
8411
8412 if (!txn)
8413 return 0;
8414
8415 if (txn->req.msg_state < HTTP_MSG_BODY)
8416 return 0;
8417
8418 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8419 /* ensure the indexes are not affected */
8420 return 0;
8421
8422 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008423 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008424}
8425
8426static int
8427acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8428 struct acl_expr *expr, struct acl_test *test)
8429{
8430 struct http_txn *txn = l7;
8431
8432 if (!txn)
8433 return 0;
8434
8435 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8436 return 0;
8437
8438 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008439 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008440}
8441
Willy Tarreau8797c062007-05-07 00:55:35 +02008442/************************************************************************/
8443/* All supported keywords must be declared here. */
8444/************************************************************************/
8445
8446/* Note: must not be declared <const> as its list will be overwritten */
8447static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008448 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8449
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008450 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008451 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8452 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008453 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008454
Willy Tarreauc4262962010-05-10 23:42:40 +02008455 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008456 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8457 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8458 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8459 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8460 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8461 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008462 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008463 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008464 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008465
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008466 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008467 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008468 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8469 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8470 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8471 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8472 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8473 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8474 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008475 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008476 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008477 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008478
Willy Tarreauc4262962010-05-10 23:42:40 +02008479 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008480 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8481 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8482 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8483 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8484 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8485 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8486 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008487 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008488 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008489 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008490
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008491 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8492 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8493 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8494 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8495 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8496 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8497 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8498 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8499 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8500
8501 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8502 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8503 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8504 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8505 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8506 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8507 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8508 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8509 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8510
Willy Tarreauc4262962010-05-10 23:42:40 +02008511 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008512 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8513 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8514 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8515 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8516 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8517 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008518 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008519
Willy Tarreau7f18e522010-10-22 20:04:13 +02008520 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8521 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8522 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008523 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008524}};
8525
Willy Tarreau4a568972010-05-12 08:08:50 +02008526/************************************************************************/
8527/* The code below is dedicated to pattern fetching and matching */
8528/************************************************************************/
8529
Willy Tarreaue428fb72011-12-16 21:50:30 +01008530/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008531static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008532pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8533 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008534{
8535 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008536
Willy Tarreaue428fb72011-12-16 21:50:30 +01008537 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8538 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008539}
8540
David Cournapeau16023ee2010-12-23 20:55:41 +09008541/*
8542 * Given a path string and its length, find the position of beginning of the
8543 * query string. Returns NULL if no query string is found in the path.
8544 *
8545 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8546 *
8547 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8548 */
8549static inline char *find_query_string(char *path, size_t path_l)
8550{
8551 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008552
David Cournapeau16023ee2010-12-23 20:55:41 +09008553 p = memchr(path, '?', path_l);
8554 return p ? p + 1 : NULL;
8555}
8556
8557static inline int is_param_delimiter(char c)
8558{
8559 return c == '&' || c == ';';
8560}
8561
8562/*
8563 * Given a url parameter, find the starting position of the first occurence,
8564 * or NULL if the parameter is not found.
8565 *
8566 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8567 * the function will return query_string+8.
8568 */
8569static char*
8570find_url_param_pos(char* query_string, size_t query_string_l,
8571 char* url_param_name, size_t url_param_name_l)
8572{
8573 char *pos, *last;
8574
8575 pos = query_string;
8576 last = query_string + query_string_l - url_param_name_l - 1;
8577
8578 while (pos <= last) {
8579 if (pos[url_param_name_l] == '=') {
8580 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8581 return pos;
8582 pos += url_param_name_l + 1;
8583 }
8584 while (pos <= last && !is_param_delimiter(*pos))
8585 pos++;
8586 pos++;
8587 }
8588 return NULL;
8589}
8590
8591/*
8592 * Given a url parameter name, returns its value and size into *value and
8593 * *value_l respectively, and returns non-zero. If the parameter is not found,
8594 * zero is returned and value/value_l are not touched.
8595 */
8596static int
8597find_url_param_value(char* path, size_t path_l,
8598 char* url_param_name, size_t url_param_name_l,
8599 char** value, size_t* value_l)
8600{
8601 char *query_string, *qs_end;
8602 char *arg_start;
8603 char *value_start, *value_end;
8604
8605 query_string = find_query_string(path, path_l);
8606 if (!query_string)
8607 return 0;
8608
8609 qs_end = path + path_l;
8610 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8611 url_param_name, url_param_name_l);
8612 if (!arg_start)
8613 return 0;
8614
8615 value_start = arg_start + url_param_name_l + 1;
8616 value_end = value_start;
8617
8618 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8619 value_end++;
8620
8621 *value = value_start;
8622 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008623 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008624}
8625
8626static int
8627pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8628 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8629{
8630 struct http_txn *txn = l7;
8631 struct http_msg *msg = &txn->req;
8632 char *url_param_value;
8633 size_t url_param_value_l;
8634
Willy Tarreau3a215be2012-03-09 21:39:51 +01008635 if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
David Cournapeau16023ee2010-12-23 20:55:41 +09008636 arg_p->data.str.str, arg_p->data.str.len,
8637 &url_param_value, &url_param_value_l))
8638 return 0;
8639
8640 data->str.str = url_param_value;
8641 data->str.len = url_param_value_l;
8642 return 1;
8643}
8644
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008645/* Try to find in request or response message is in <msg> and whose transaction
8646 * is in <txn> the last occurrence of a cookie name in all cookie header values
8647 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8648 * pointer and size of the last occurrence of the cookie value is returned into
8649 * <value> and <value_l>, and the function returns non-zero if the value was
8650 * found. Otherwise if the cookie was not found, zero is returned and neither
8651 * value nor value_l are touched. The input hdr string should begin at the
8652 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8653 * value may represent a list of values (cookie headers).
8654 */
8655
8656static int
8657find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8658 const char *hdr_name, int hdr_name_len,
8659 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008660 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008661{
8662 struct hdr_ctx ctx;
8663 int found = 0;
8664
8665 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008666 while (http_find_header2(hdr_name, hdr_name_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008667 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008668 if (ctx.vlen < cookie_name_l + 1)
8669 continue;
8670
Willy Tarreau4573af92012-04-06 18:20:06 +02008671 hdr = ctx.line + ctx.val;
8672 end = hdr + ctx.vlen;
8673 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8674 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008675 }
8676 return found;
8677}
8678
8679static int
8680pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8681 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8682{
8683 struct http_txn *txn = l7;
8684 struct http_msg *msg = &txn->req;
8685 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008686 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008687 int found = 0;
8688
8689 found = find_cookie_value(msg, txn, "Cookie", 6,
8690 arg_p->data.str.str, arg_p->data.str.len, 1,
8691 &cookie_value, &cookie_value_l);
8692 if (found) {
8693 data->str.str = cookie_value;
8694 data->str.len = cookie_value_l;
8695 }
8696
8697 return found;
8698}
8699
8700
8701static int
8702pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8703 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8704{
8705 struct http_txn *txn = l7;
8706 struct http_msg *msg = &txn->rsp;
8707 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008708 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008709 int found = 0;
8710
8711 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8712 arg_p->data.str.str, arg_p->data.str.len, 1,
8713 &cookie_value, &cookie_value_l);
8714 if (found) {
8715 data->str.str = cookie_value;
8716 data->str.len = cookie_value_l;
8717 }
8718
8719 return found;
8720}
8721
Emeric Brun485479d2010-09-23 18:02:19 +02008722
Willy Tarreau4a568972010-05-12 08:08:50 +02008723/************************************************************************/
8724/* All supported keywords must be declared here. */
8725/************************************************************************/
8726/* Note: must not be declared <const> as its list will be overwritten */
8727static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008728 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008729 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008730 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8731 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008732 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008733}};
8734
Willy Tarreau8797c062007-05-07 00:55:35 +02008735
8736__attribute__((constructor))
8737static void __http_protocol_init(void)
8738{
8739 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008740 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008741}
8742
8743
Willy Tarreau58f10d72006-12-04 02:26:12 +01008744/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008745 * Local variables:
8746 * c-indent-level: 8
8747 * c-basic-offset: 8
8748 * End:
8749 */