blob: dd2f0d07d8b7f08007b2c3f747e3e1781df7afb4 [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,
393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - size,
400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
404
405 write(-1, trash, size);
406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100412 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
413 * CRLF. Text length is measured first, so it cannot be NULL.
414 * 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 */
418int http_header_add_tail(struct buffer *b, struct http_msg *msg,
419 struct hdr_idx *hdr_idx, const char *text)
420{
421 int bytes, len;
422
423 len = strlen(text);
424 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
425 if (!bytes)
426 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100427 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100428 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
429}
430
431/*
432 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
433 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
434 * the buffer is only opened and the space reserved, but nothing is copied.
435 * The header is also automatically added to the index <hdr_idx>, and the end
436 * of headers is automatically adjusted. The number of bytes added is returned
437 * on success, otherwise <0 is returned indicating an error.
438 */
439int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
440 struct hdr_idx *hdr_idx, const char *text, int len)
441{
442 int bytes;
443
444 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
445 if (!bytes)
446 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100447 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
449}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
451/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100452 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
453 * If so, returns the position of the first non-space character relative to
454 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
455 * to return a pointer to the place after the first space. Returns 0 if the
456 * header name does not match. Checks are case-insensitive.
457 */
458int http_header_match2(const char *hdr, const char *end,
459 const char *name, int len)
460{
461 const char *val;
462
463 if (hdr + len >= end)
464 return 0;
465 if (hdr[len] != ':')
466 return 0;
467 if (strncasecmp(hdr, name, len) != 0)
468 return 0;
469 val = hdr + len + 1;
470 while (val < end && HTTP_IS_SPHT(*val))
471 val++;
472 if ((val >= end) && (len + 2 <= end - hdr))
473 return len + 2; /* we may replace starting from second space */
474 return val - hdr;
475}
476
Willy Tarreau68085d82010-01-18 14:54:04 +0100477/* Find the end of the header value contained between <s> and <e>. See RFC2616,
478 * par 2.2 for more information. Note that it requires a valid header to return
479 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100481char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482{
483 int quoted, qdpair;
484
485 quoted = qdpair = 0;
486 for (; s < e; s++) {
487 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200488 else if (quoted) {
489 if (*s == '\\') qdpair = 1;
490 else if (*s == '"') quoted = 0;
491 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 else if (*s == '"') quoted = 1;
493 else if (*s == ',') return s;
494 }
495 return s;
496}
497
498/* Find the first or next occurrence of header <name> in message buffer <sol>
499 * using headers index <idx>, and return it in the <ctx> structure. This
500 * structure holds everything necessary to use the header and find next
501 * occurrence. If its <idx> member is 0, the header is searched from the
502 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 * 1 when it finds a value, and 0 when there is no more. It is designed to work
504 * with headers defined as comma-separated lists. As a special case, if ctx->val
505 * is NULL when searching for a new values of a header, the current header is
506 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 */
508int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
Willy Tarreau68085d82010-01-18 14:54:04 +0100512 char *eol, *sov;
513 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515 cur_idx = ctx->idx;
516 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 /* We have previously returned a value, let's search
518 * another one on the same line.
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200521 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100522 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200523 eol = sol + idx->v[cur_idx].len;
524
525 if (sov >= eol)
526 /* no more values in this header */
527 goto next_hdr;
528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 /* values remaining for this header, skip the comma but save it
530 * for later use (eg: for header deletion).
531 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 sov++;
533 while (sov < eol && http_is_lws[(unsigned char)*sov])
534 sov++;
535
536 goto return_hdr;
537 }
538
539 /* first request for this header */
540 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100541 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 while (cur_idx) {
544 eol = sol + idx->v[cur_idx].len;
545
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200546 if (len == 0) {
547 /* No argument was passed, we want any header.
548 * To achieve this, we simply build a fake request. */
549 while (sol + len < eol && sol[len] != ':')
550 len++;
551 name = sol;
552 }
553
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 if ((len < eol - sol) &&
555 (sol[len] == ':') &&
556 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 sov = sol + len + 1;
559 while (sov < eol && http_is_lws[(unsigned char)*sov])
560 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 ctx->prev = old_idx;
564 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 ctx->idx = cur_idx;
566 ctx->val = sov - sol;
567
568 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200570 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200571 eol--;
572 ctx->tws++;
573 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 ctx->vlen = eol - sov;
575 return 1;
576 }
577 next_hdr:
578 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100579 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 cur_idx = idx->v[cur_idx].next;
581 }
582 return 0;
583}
584
585int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200587 struct hdr_ctx *ctx)
588{
589 return http_find_header2(name, strlen(name), sol, idx, ctx);
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Remove one value of a header. This only works on a <ctx> returned by one of
593 * the http_find_header functions. The value is removed, as well as surrounding
594 * commas if any. If the removed value was alone, the whole header is removed.
595 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
596 * message <msg>. The new index is returned. If it is zero, it means there is
597 * no more header, so any processing may stop. The ctx is always left in a form
598 * that can be handled by http_find_header2() to find next occurrence.
599 */
600int http_remove_header2(struct http_msg *msg, struct buffer *buf,
601 struct hdr_idx *idx, struct hdr_ctx *ctx)
602{
603 int cur_idx = ctx->idx;
604 char *sol = ctx->line;
605 struct hdr_idx_elem *hdr;
606 int delta, skip_comma;
607
608 if (!cur_idx)
609 return 0;
610
611 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200612 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* This was the only value of the header, we must now remove it entirely. */
614 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
615 http_msg_move_end(msg, delta);
616 idx->used--;
617 hdr->len = 0; /* unused entry */
618 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100619 if (idx->tail == ctx->idx)
620 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 ctx->idx = ctx->prev; /* walk back to the end of previous header */
622 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
623 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200624 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 return ctx->idx;
626 }
627
628 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200629 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
630 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100631 */
632
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200635 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 NULL, 0);
637 hdr->len += delta;
638 http_msg_move_end(msg, delta);
639 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200640 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 return ctx->idx;
642}
643
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644/* This function handles a server error at the stream interface level. The
645 * stream interface is assumed to be already in a closed state. An optional
646 * message is copied into the input buffer, and an HTTP status code stored.
647 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100648 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100650static void http_server_error(struct session *t, struct stream_interface *si,
651 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100653 buffer_auto_read(si->ob);
654 buffer_abort(si->ob);
655 buffer_auto_close(si->ob);
656 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200657 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100658 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100659 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100660 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100661 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 }
663 if (!(t->flags & SN_ERR_MASK))
664 t->flags |= err;
665 if (!(t->flags & SN_FINST_MASK))
666 t->flags |= finst;
667}
668
Willy Tarreau80587432006-12-24 17:47:20 +0100669/* This function returns the appropriate error location for the given session
670 * and message.
671 */
672
673struct chunk *error_message(struct session *s, int msgnum)
674{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200675 if (s->be->errmsg[msgnum].str)
676 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100677 else if (s->fe->errmsg[msgnum].str)
678 return &s->fe->errmsg[msgnum];
679 else
680 return &http_err_chunks[msgnum];
681}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683/*
684 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
685 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
686 */
687static http_meth_t find_http_meth(const char *str, const int len)
688{
689 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100690 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100691
692 m = ((unsigned)*str - 'A');
693
694 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 for (h = http_methods[m]; h->len > 0; h++) {
696 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100698 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100700 };
701 return HTTP_METH_OTHER;
702 }
703 return HTTP_METH_NONE;
704
705}
706
Willy Tarreau21d2af32008-02-14 20:25:24 +0100707/* Parse the URI from the given transaction (which is assumed to be in request
708 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
709 * It is returned otherwise.
710 */
711static char *
712http_get_path(struct http_txn *txn)
713{
714 char *ptr, *end;
715
Willy Tarreau962c3f42010-01-10 00:15:35 +0100716 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100717 end = ptr + txn->req.sl.rq.u_l;
718
719 if (ptr >= end)
720 return NULL;
721
722 /* RFC2616, par. 5.1.2 :
723 * Request-URI = "*" | absuri | abspath | authority
724 */
725
726 if (*ptr == '*')
727 return NULL;
728
729 if (isalpha((unsigned char)*ptr)) {
730 /* this is a scheme as described by RFC3986, par. 3.1 */
731 ptr++;
732 while (ptr < end &&
733 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
734 ptr++;
735 /* skip '://' */
736 if (ptr == end || *ptr++ != ':')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 if (ptr == end || *ptr++ != '/')
741 return NULL;
742 }
743 /* skip [user[:passwd]@]host[:[port]] */
744
745 while (ptr < end && *ptr != '/')
746 ptr++;
747
748 if (ptr == end)
749 return NULL;
750
751 /* OK, we got the '/' ! */
752 return ptr;
753}
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755/* Returns a 302 for a redirectable request. This may only be called just after
756 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
757 * left unchanged and will follow normal proxy processing.
758 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760{
761 struct http_txn *txn;
762 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764 char *path;
765 int len;
766
767 /* 1: create the response header */
768 rdr.len = strlen(HTTP_302);
769 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100770 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 memcpy(rdr.str, HTTP_302, rdr.len);
772
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 srv = target_srv(&s->target);
774
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100777 return;
778
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
781 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
782 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100783 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
785 /* 3: add the request URI */
786 txn = &s->txn;
787 path = http_get_path(txn);
788 if (!path)
789 return;
790
Willy Tarreau962c3f42010-01-10 00:15:35 +0100791 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200792 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793 return;
794
795 memcpy(rdr.str + rdr.len, path, len);
796 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100797
798 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
799 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
800 rdr.len += 29;
801 } else {
802 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
803 rdr.len += 23;
804 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100805
806 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100807 si->shutr(si);
808 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809 si->err_type = SI_ET_NONE;
810 si->err_loc = NULL;
811 si->state = SI_ST_CLO;
812
813 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100815
816 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (srv)
818 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819}
820
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100821/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 * that the server side is closed. Note that err_type is actually a
823 * bitmask, where almost only aborts may be cumulated with other
824 * values. We consider that aborted operations are more important
825 * than timeouts or errors due to the fact that nobody else in the
826 * logs might explain incomplete retries. All others should avoid
827 * being cumulated. It should normally not be possible to have multiple
828 * aborts at once, but just in case, the first one in sequence is reported.
829 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100832 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833
834 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
836 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
839 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100841 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
842 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
845 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
848 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
851 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
854 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855}
856
Willy Tarreau42250582007-04-01 01:30:43 +0200857extern const char sess_term_cond[8];
858extern const char sess_fin_state[8];
859extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200860struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200861struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100862struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100863
Willy Tarreau117f59e2007-03-04 18:17:17 +0100864/*
865 * Capture headers from message starting at <som> according to header list
866 * <cap_hdr>, and fill the <idx> structure appropriately.
867 */
868void capture_headers(char *som, struct hdr_idx *idx,
869 char **cap, struct cap_hdr *cap_hdr)
870{
871 char *eol, *sol, *col, *sov;
872 int cur_idx;
873 struct cap_hdr *h;
874 int len;
875
876 sol = som + hdr_idx_first_pos(idx);
877 cur_idx = hdr_idx_first_idx(idx);
878
879 while (cur_idx) {
880 eol = sol + idx->v[cur_idx].len;
881
882 col = sol;
883 while (col < eol && *col != ':')
884 col++;
885
886 sov = col + 1;
887 while (sov < eol && http_is_lws[(unsigned char)*sov])
888 sov++;
889
890 for (h = cap_hdr; h; h = h->next) {
891 if ((h->namelen == col - sol) &&
892 (strncasecmp(sol, h->name, h->namelen) == 0)) {
893 if (cap[h->index] == NULL)
894 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200895 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100896
897 if (cap[h->index] == NULL) {
898 Alert("HTTP capture : out of memory.\n");
899 continue;
900 }
901
902 len = eol - sov;
903 if (len > h->len)
904 len = h->len;
905
906 memcpy(cap[h->index], sov, len);
907 cap[h->index][len]=0;
908 }
909 }
910 sol = eol + idx->v[cur_idx].cr + 1;
911 cur_idx = idx->v[cur_idx].next;
912 }
913}
914
915
Willy Tarreau42250582007-04-01 01:30:43 +0200916/* either we find an LF at <ptr> or we jump to <bad>.
917 */
918#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
919
920/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
921 * otherwise to <http_msg_ood> with <state> set to <st>.
922 */
923#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
924 ptr++; \
925 if (likely(ptr < end)) \
926 goto good; \
927 else { \
928 state = (st); \
929 goto http_msg_ood; \
930 } \
931 } while (0)
932
933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100935 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100936 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
937 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
938 * will give undefined results.
939 * Note that it is upon the caller's responsibility to ensure that ptr < end,
940 * and that msg->sol points to the beginning of the response.
941 * If a complete line is found (which implies that at least one CR or LF is
942 * found before <end>, the updated <ptr> is returned, otherwise NULL is
943 * returned indicating an incomplete line (which does not mean that parts have
944 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
945 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
946 * upon next call.
947 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200948 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100949 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
950 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100953const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
954 unsigned int state, const char *ptr, const char *end,
955 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100956{
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 Tarreaub326fcc2007-03-03 13:54:32 +0100964 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
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 Tarreau962c3f42010-01-10 00:15:35 +0100973 msg->sl.st.c = (ptr - msg_buf) - msg->som;
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 Tarreau962c3f42010-01-10 00:15:35 +0100988 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - 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 Tarreau962c3f42010-01-10 00:15:35 +0100993 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - 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 Tarreau962c3f42010-01-10 00:15:35 +0100996 msg->sl.st.r = (ptr - msg_buf) - msg->som;
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 Tarreau962c3f42010-01-10 00:15:35 +01001003 msg->sl.st.r = (ptr - msg_buf) - msg->som;
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 Tarreau962c3f42010-01-10 00:15:35 +01001015 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - 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 */
1023 msg->sl.st.l = ptr - msg->sol;
1024 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)
1038 *ret_ptr = (char *)ptr;
1039 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,
1063 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001067 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 if (likely(HTTP_IS_TOKEN(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001072 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_CRLF(*ptr))) {
1077 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001078 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001080 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001082 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001084 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 msg->sl.rq.v_l = 0;
1086 goto http_msg_rqline_eol;
1087 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001088 state = HTTP_MSG_ERROR;
1089 break;
1090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001092 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001094 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 goto http_msg_rquri;
1096 }
1097 if (likely(HTTP_IS_SPHT(*ptr)))
1098 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1099 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1100 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001103 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001104 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001108 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001112 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001113 /* non-ASCII chars are forbidden unless option
1114 * accept-invalid-http-request is enabled in the frontend.
1115 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001116 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001117 if (msg->err_pos < -1)
1118 goto invalid_char;
1119 if (msg->err_pos == -1)
1120 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001121 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1122 }
1123
1124 if (likely(HTTP_IS_CRLF(*ptr))) {
1125 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1126 goto http_msg_req09_uri_e;
1127 }
1128
1129 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001130 invalid_char:
1131 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001132 state = HTTP_MSG_ERROR;
1133 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001135 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001136 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001138 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 goto http_msg_rqver;
1140 }
1141 if (likely(HTTP_IS_SPHT(*ptr)))
1142 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1143 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1144 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001147 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001148 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150
1151 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001152 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001153 http_msg_rqline_eol:
1154 /* We have seen the end of line. Note that we do not
1155 * necessarily have the \n yet, but at least we know that we
1156 * have EITHER \r OR \n, otherwise the request would not be
1157 * complete. We can then record the request length and return
1158 * to the caller which will be able to register it.
1159 */
1160 msg->sl.rq.l = ptr - msg->sol;
1161 return ptr;
1162 }
1163
1164 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001165 state = HTTP_MSG_ERROR;
1166 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168#ifdef DEBUG_FULL
1169 default:
1170 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1171 exit(1);
1172#endif
1173 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001176 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 if (ret_state)
1178 *ret_state = state;
1179 if (ret_ptr)
1180 *ret_ptr = (char *)ptr;
1181 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001183
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001184/*
1185 * Returns the data from Authorization header. Function may be called more
1186 * than once so data is stored in txn->auth_data. When no header is found
1187 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1188 * searching again for something we are unable to find anyway.
1189 */
1190
1191char get_http_auth_buff[BUFSIZE];
1192
1193int
1194get_http_auth(struct session *s)
1195{
1196
1197 struct http_txn *txn = &s->txn;
1198 struct chunk auth_method;
1199 struct hdr_ctx ctx;
1200 char *h, *p;
1201 int len;
1202
1203#ifdef DEBUG_AUTH
1204 printf("Auth for session %p: %d\n", s, txn->auth.method);
1205#endif
1206
1207 if (txn->auth.method == HTTP_AUTH_WRONG)
1208 return 0;
1209
1210 if (txn->auth.method)
1211 return 1;
1212
1213 txn->auth.method = HTTP_AUTH_WRONG;
1214
1215 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001216
1217 if (txn->flags & TX_USE_PX_CONN) {
1218 h = "Proxy-Authorization";
1219 len = strlen(h);
1220 } else {
1221 h = "Authorization";
1222 len = strlen(h);
1223 }
1224
1225 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001226 return 0;
1227
1228 h = ctx.line + ctx.val;
1229
1230 p = memchr(h, ' ', ctx.vlen);
1231 if (!p || p == h)
1232 return 0;
1233
1234 chunk_initlen(&auth_method, h, 0, p-h);
1235 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1236
1237 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1238
1239 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1240 get_http_auth_buff, BUFSIZE - 1);
1241
1242 if (len < 0)
1243 return 0;
1244
1245
1246 get_http_auth_buff[len] = '\0';
1247
1248 p = strchr(get_http_auth_buff, ':');
1249
1250 if (!p)
1251 return 0;
1252
1253 txn->auth.user = get_http_auth_buff;
1254 *p = '\0';
1255 txn->auth.pass = p+1;
1256
1257 txn->auth.method = HTTP_AUTH_BASIC;
1258 return 1;
1259 }
1260
1261 return 0;
1262}
1263
Willy Tarreau58f10d72006-12-04 02:26:12 +01001264
Willy Tarreau8973c702007-01-21 23:58:29 +01001265/*
1266 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001267 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * when data are missing and recalled at the exact same location with no
1269 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001270 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001271 * fields. Note that msg->som and msg->sol will be initialized after completing
1272 * the first state, so that none of the msg pointers has to be initialized
1273 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1276{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001277 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001280 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 ptr = buf->lr;
1282 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 if (unlikely(ptr >= end))
1285 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 /*
1289 * First, states that are specific to the response only.
1290 * We check them first so that request and headers are
1291 * closer to each other (accessed more often).
1292 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001296 /* we have a start of message, but we have to check
1297 * first if we need to remove some CRLF. We can only
1298 * do this when send_max=0.
1299 */
1300 char *beg = buf->w + buf->send_max;
1301 if (beg >= buf->data + buf->size)
1302 beg -= buf->size;
1303 if (unlikely(ptr != beg)) {
1304 if (buf->send_max)
1305 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001306 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001310 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 hdr_idx_init(idx);
1312 state = HTTP_MSG_RPVER;
1313 goto http_msg_rpver;
1314 }
1315
1316 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1317 goto http_msg_invalid;
1318
1319 if (unlikely(*ptr == '\n'))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1322 /* stop here */
1323
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001325 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 EXPECT_LF_HERE(ptr, http_msg_invalid);
1327 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1328 /* stop here */
1329
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001331 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPVER_SP:
1333 case HTTP_MSG_RPCODE:
1334 case HTTP_MSG_RPCODE_SP:
1335 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001336 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 if (unlikely(!ptr))
1339 return;
1340
1341 /* we have a full response and we know that we have either a CR
1342 * or an LF at <ptr>.
1343 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001344 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1346
1347 msg->sol = ptr;
1348 if (likely(*ptr == '\r'))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1350 goto http_msg_rpline_end;
1351
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001353 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 /* msg->sol must point to the first of CR or LF. */
1355 EXPECT_LF_HERE(ptr, http_msg_invalid);
1356 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1357 /* stop here */
1358
1359 /*
1360 * Second, states that are specific to the request only
1361 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001363 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001365 /* we have a start of message, but we have to check
1366 * first if we need to remove some CRLF. We can only
1367 * do this when send_max=0.
1368 */
1369 char *beg = buf->w + buf->send_max;
1370 if (beg >= buf->data + buf->size)
1371 beg -= buf->size;
1372 if (likely(ptr != beg)) {
1373 if (buf->send_max)
1374 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 Tarreau15de77e2010-01-02 21:59:16 +01001378 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001379 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001380 /* we will need this when keep-alive will be supported
1381 hdr_idx_init(idx);
1382 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 state = HTTP_MSG_RQMETH;
1384 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1388 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(*ptr == '\n'))
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001396 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 EXPECT_LF_HERE(ptr, http_msg_invalid);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH_SP:
1404 case HTTP_MSG_RQURI:
1405 case HTTP_MSG_RQURI_SP:
1406 case HTTP_MSG_RQVER:
1407 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001408 &buf->lr, &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 Tarreaub326fcc2007-03-03 13:54:32 +01001415 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 msg->sol = ptr;
1419 if (likely(*ptr == '\r'))
1420 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001424 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 /* check for HTTP/0.9 request : no version information available.
1426 * msg->sol must point to the first of CR or LF.
1427 */
1428 if (unlikely(msg->sl.rq.v_l == 0))
1429 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 EXPECT_LF_HERE(ptr, http_msg_invalid);
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /*
1436 * Common states below
1437 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001439 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 msg->sol = ptr;
1441 if (likely(!HTTP_IS_CRLF(*ptr))) {
1442 goto http_msg_hdr_name;
1443 }
1444
1445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1447 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001450 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 /* assumes msg->sol points to the first char */
1452 if (likely(HTTP_IS_TOKEN(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 if (likely(*ptr == ':')) {
1456 msg->col = ptr - buf->data;
1457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
1464 msg->err_pos = ptr - buf->data; /* >= 0 now */
1465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* assumes msg->sol points to the first char and msg->col to the colon */
1472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
1476 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
1495 for (; buf->data+msg->sov < ptr; msg->sov++)
1496 buf->data[msg->sov] = ' ';
1497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
1500 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 /* assumes msg->sol points to the first char, msg->col to the
1506 * colon, and msg->sov points to the first character of the
1507 * value.
1508 */
1509 if (likely(!HTTP_IS_CRLF(*ptr)))
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 msg->eol = ptr;
1513 /* Note: we could also copy eol into ->eoh so that we have the
1514 * real header end in case it ends with lots of LWS, but is this
1515 * really needed ?
1516 */
1517 if (likely(*ptr == '\r'))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1519 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 EXPECT_LF_HERE(ptr, http_msg_invalid);
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1529 /* LWS: replace HT,CR,LF with spaces */
1530 for (; msg->eol < ptr; msg->eol++)
1531 *msg->eol = ' ';
1532 goto http_msg_hdr_val;
1533 }
1534 http_msg_complete_header:
1535 /*
1536 * It was a new header, so the last one is finished.
1537 * Assumes msg->sol points to the first char, msg->col to the
1538 * colon, msg->sov points to the first character of the value
1539 * and msg->eol to the first CR or LF so we know how the line
1540 * ends. We insert last header into the index.
1541 */
1542 /*
1543 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1544 write(2, msg->sol, msg->eol-msg->sol);
1545 fprintf(stderr,"\n");
1546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1549 idx, idx->tail) < 0))
1550 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 msg->sol = ptr;
1553 if (likely(!HTTP_IS_CRLF(*ptr))) {
1554 goto http_msg_hdr_name;
1555 }
1556
1557 if (likely(*ptr == '\r'))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1559 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001562 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 /* Assumes msg->sol points to the first of either CR or LF */
1564 EXPECT_LF_HERE(ptr, http_msg_invalid);
1565 ptr++;
1566 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001567 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001569 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001570 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 return;
1572#ifdef DEBUG_FULL
1573 default:
1574 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1575 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001576#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 }
1578 http_msg_ood:
1579 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 buf->lr = ptr;
1582 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 http_msg_invalid:
1585 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001586 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001587 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 return;
1589}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001590
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001591/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1592 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1593 * nothing is done and 1 is returned.
1594 */
1595static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1596{
1597 int delta;
1598 char *cur_end;
1599
1600 if (msg->sl.rq.v_l != 0)
1601 return 1;
1602
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603 cur_end = msg->sol + msg->sl.rq.l;
1604 delta = 0;
1605
1606 if (msg->sl.rq.u_l == 0) {
1607 /* if no URI was set, add "/" */
1608 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1609 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001610 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001611 }
1612 /* add HTTP version */
1613 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001614 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 cur_end += delta;
1616 cur_end = (char *)http_parse_reqline(msg, req->data,
1617 HTTP_MSG_RQMETH,
1618 msg->sol, cur_end + 1,
1619 NULL, NULL);
1620 if (unlikely(!cur_end))
1621 return 0;
1622
1623 /* we have a full HTTP/1.0 request now and we know that
1624 * we have either a CR or an LF at <ptr>.
1625 */
1626 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1627 return 1;
1628}
1629
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001630/* Parse the Connection: header of an HTTP request, looking for both "close"
1631 * and "keep-alive" values. If a buffer is provided and we already know that
1632 * some headers may safely be removed, we remove them now. The <to_del> flags
1633 * are used for that :
1634 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1635 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1636 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1637 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1638 * harmless combinations may be removed. Do not call that after changes have
1639 * been processed. If unused, the buffer can be NULL, and no data will be
1640 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001642void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001643{
Willy Tarreau5b154472009-12-21 20:11:07 +01001644 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001645 const char *hdr_val = "Connection";
1646 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001647
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001648 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 return;
1650
Willy Tarreau88d349d2010-01-25 12:15:43 +01001651 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1652 hdr_val = "Proxy-Connection";
1653 hdr_len = 16;
1654 }
1655
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001658 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1660 txn->flags |= TX_HDR_CONN_KAL;
1661 if ((to_del & 2) && buf)
1662 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1663 else
1664 txn->flags |= TX_CON_KAL_SET;
1665 }
1666 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1667 txn->flags |= TX_HDR_CONN_CLO;
1668 if ((to_del & 1) && buf)
1669 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1670 else
1671 txn->flags |= TX_CON_CLO_SET;
1672 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001673 }
1674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 txn->flags |= TX_HDR_CONN_PRS;
1676 return;
1677}
Willy Tarreau5b154472009-12-21 20:11:07 +01001678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679/* Apply desired changes on the Connection: header. Values may be removed and/or
1680 * added depending on the <wanted> flags, which are exclusively composed of
1681 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1682 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1683 */
1684void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1685{
1686 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687 const char *hdr_val = "Connection";
1688 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689
1690 ctx.idx = 0;
1691
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692
1693 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1694 hdr_val = "Proxy-Connection";
1695 hdr_len = 16;
1696 }
1697
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001699 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1701 if (wanted & TX_CON_KAL_SET)
1702 txn->flags |= TX_CON_KAL_SET;
1703 else
1704 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1707 if (wanted & TX_CON_CLO_SET)
1708 txn->flags |= TX_CON_CLO_SET;
1709 else
1710 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001711 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001712 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713
1714 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1715 return;
1716
1717 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1718 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001719 hdr_val = "Connection: close";
1720 hdr_len = 17;
1721 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1722 hdr_val = "Proxy-Connection: close";
1723 hdr_len = 23;
1724 }
1725 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 }
1727
1728 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1729 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001730 hdr_val = "Connection: keep-alive";
1731 hdr_len = 22;
1732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection: keep-alive";
1734 hdr_len = 28;
1735 }
1736 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001737 }
1738 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739}
1740
Willy Tarreaud98cf932009-12-27 22:54:55 +01001741/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1742 * first byte of body, and increments msg->sov by the number of bytes parsed,
1743 * so that we know we can forward between ->som and ->sov. Note that due to
1744 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1745 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001750{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001751 char *ptr = buf->lr;
1752 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001753 unsigned int chunk = 0;
1754
1755 /* The chunk size is in the following form, though we are only
1756 * interested in the size and CRLF :
1757 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1758 */
1759 while (1) {
1760 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001762 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001763 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 if (c < 0) /* not a hex digit anymore */
1765 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 if (++ptr >= end)
1767 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001768 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001769 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001770 chunk = (chunk << 4) + c;
1771 }
1772
Willy Tarreaud98cf932009-12-27 22:54:55 +01001773 /* empty size not allowed */
1774 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001775 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001776
1777 while (http_is_spht[(unsigned char)*ptr]) {
1778 if (++ptr >= end)
1779 ptr = buf->data;
1780 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001781 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 }
1783
Willy Tarreaud98cf932009-12-27 22:54:55 +01001784 /* Up to there, we know that at least one byte is present at *ptr. Check
1785 * for the end of chunk size.
1786 */
1787 while (1) {
1788 if (likely(HTTP_IS_CRLF(*ptr))) {
1789 /* we now have a CR or an LF at ptr */
1790 if (likely(*ptr == '\r')) {
1791 if (++ptr >= end)
1792 ptr = buf->data;
1793 if (ptr == buf->r)
1794 return 0;
1795 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001796
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001798 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (++ptr >= end)
1800 ptr = buf->data;
1801 /* done */
1802 break;
1803 }
1804 else if (*ptr == ';') {
1805 /* chunk extension, ends at next CRLF */
1806 if (++ptr >= end)
1807 ptr = buf->data;
1808 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001809 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810
1811 while (!HTTP_IS_CRLF(*ptr)) {
1812 if (++ptr >= end)
1813 ptr = buf->data;
1814 if (ptr == buf->r)
1815 return 0;
1816 }
1817 /* we have a CRLF now, loop above */
1818 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001819 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001820 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001821 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 }
1823
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 /* OK we found our CRLF and now <ptr> points to the next byte,
1825 * which may or may not be present. We save that into ->lr and
1826 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001827 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 msg->sov += ptr - buf->lr;
1829 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01001830 msg->chunk_len = chunk;
1831 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001833 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001834 error:
1835 msg->err_pos = ptr - buf->data;
1836 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001837}
1838
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839/* This function skips trailers in the buffer <buf> associated with HTTP
1840 * message <msg>. The first visited position is buf->lr. If the end of
1841 * the trailers is found, it is automatically scheduled to be forwarded,
1842 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1843 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001844 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1845 * zero. If a parse error is encountered, the function returns < 0 and does not
1846 * change anything except maybe buf->lr and msg->sov. Note that the message
1847 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1848 * which implies that all non-trailers data have already been scheduled for
1849 * forwarding, and that the difference between msg->som and msg->sov exactly
1850 * matches the length of trailers already parsed and not forwarded. It is also
1851 * important to note that this function is designed to be able to parse wrapped
1852 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001853 */
1854int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1855{
1856 /* we have buf->lr which points to next line. Look for CRLF. */
1857 while (1) {
1858 char *p1 = NULL, *p2 = NULL;
1859 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001860 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861
1862 /* scan current line and stop at LF or CRLF */
1863 while (1) {
1864 if (ptr == buf->r)
1865 return 0;
1866
1867 if (*ptr == '\n') {
1868 if (!p1)
1869 p1 = ptr;
1870 p2 = ptr;
1871 break;
1872 }
1873
1874 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001875 if (p1) {
1876 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001878 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 p1 = ptr;
1880 }
1881
1882 ptr++;
1883 if (ptr >= buf->data + buf->size)
1884 ptr = buf->data;
1885 }
1886
1887 /* after LF; point to beginning of next line */
1888 p2++;
1889 if (p2 >= buf->data + buf->size)
1890 p2 = buf->data;
1891
Willy Tarreau638cd022010-01-03 07:42:04 +01001892 bytes = p2 - buf->lr;
1893 if (bytes < 0)
1894 bytes += buf->size;
1895
1896 /* schedule this line for forwarding */
1897 msg->sov += bytes;
1898 if (msg->sov >= buf->size)
1899 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900
Willy Tarreau638cd022010-01-03 07:42:04 +01001901 if (p1 == buf->lr) {
1902 /* LF/CRLF at beginning of line => end of trailers at p2.
1903 * Everything was scheduled for forwarding, there's nothing
1904 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001905 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001906 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907 msg->msg_state = HTTP_MSG_DONE;
1908 return 1;
1909 }
1910 /* OK, next line then */
1911 buf->lr = p2;
1912 }
1913}
1914
1915/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1916 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1917 * ->som, buf->lr in order to include this part into the next forwarding phase.
1918 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1919 * not enough data are available, the function does not change anything and
1920 * returns zero. If a parse error is encountered, the function returns < 0 and
1921 * does not change anything. Note: this function is designed to parse wrapped
1922 * CRLF at the end of the buffer.
1923 */
1924int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1925{
1926 char *ptr;
1927 int bytes;
1928
1929 /* NB: we'll check data availabilty at the end. It's not a
1930 * problem because whatever we match first will be checked
1931 * against the correct length.
1932 */
1933 bytes = 1;
1934 ptr = buf->lr;
1935 if (*ptr == '\r') {
1936 bytes++;
1937 ptr++;
1938 if (ptr >= buf->data + buf->size)
1939 ptr = buf->data;
1940 }
1941
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01001942 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 return 0;
1944
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001945 if (*ptr != '\n') {
1946 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001948 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949
1950 ptr++;
1951 if (ptr >= buf->data + buf->size)
1952 ptr = buf->data;
1953 buf->lr = ptr;
1954 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1955 msg->sov = ptr - buf->data;
1956 msg->som = msg->sov - bytes;
1957 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1958 return 1;
1959}
Willy Tarreau5b154472009-12-21 20:11:07 +01001960
Willy Tarreau83e3af02009-12-28 17:39:57 +01001961void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1962{
1963 char *end = buf->data + buf->size;
1964 int off = buf->data + buf->size - buf->w;
1965
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 */
1970 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01001971 int block1 = buf->l;
1972 int block2 = 0;
1973 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001974 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01001975 block1 = buf->data + buf->size - buf->w;
1976 block2 = buf->r - buf->data;
1977 }
1978 if (block2)
1979 memcpy(swap_buffer, buf->data, block2);
1980 memmove(buf->data, buf->w, block1);
1981 if (block2)
1982 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001983 }
1984
1985 /* adjust all known pointers */
1986 buf->w = buf->data;
1987 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
1988 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
1989 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1990 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1991
1992 /* adjust relative pointers */
1993 msg->som = 0;
1994 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
1995 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
1996 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
1997
Willy Tarreau83e3af02009-12-28 17:39:57 +01001998 if (msg->err_pos >= 0) {
1999 msg->err_pos += off;
2000 if (msg->err_pos >= buf->size)
2001 msg->err_pos -= buf->size;
2002 }
2003
2004 buf->flags &= ~BF_FULL;
2005 if (buf->l >= buffer_max_len(buf))
2006 buf->flags |= BF_FULL;
2007}
2008
Willy Tarreaud787e662009-07-07 10:14:51 +02002009/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2010 * processing can continue on next analysers, or zero if it either needs more
2011 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2012 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2013 * when it has nothing left to do, and may remove any analyser when it wants to
2014 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002015 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002016int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002017{
Willy Tarreau59234e92008-11-30 23:51:27 +01002018 /*
2019 * We will parse the partial (or complete) lines.
2020 * We will check the request syntax, and also join multi-line
2021 * headers. An index of all the lines will be elaborated while
2022 * parsing.
2023 *
2024 * For the parsing, we use a 28 states FSM.
2025 *
2026 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002027 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002028 * req->data + msg->eoh = end of processed headers / start of current one
2029 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002030 * req->lr = first non-visited byte
2031 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002032 *
2033 * At end of parsing, we may perform a capture of the error (if any), and
2034 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2035 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2036 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002037 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002038
Willy Tarreau59234e92008-11-30 23:51:27 +01002039 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002040 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002041 struct http_txn *txn = &s->txn;
2042 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002043 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002044
Willy Tarreau6bf17362009-02-24 10:48:35 +01002045 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2046 now_ms, __FUNCTION__,
2047 s,
2048 req,
2049 req->rex, req->wex,
2050 req->flags,
2051 req->l,
2052 req->analysers);
2053
Willy Tarreau52a0c602009-08-16 22:45:38 +02002054 /* we're speaking HTTP here, so let's speak HTTP to the client */
2055 s->srv_error = http_return_srv_error;
2056
Willy Tarreau83e3af02009-12-28 17:39:57 +01002057 /* There's a protected area at the end of the buffer for rewriting
2058 * purposes. We don't want to start to parse the request if the
2059 * protected area is affected, because we may have to move processed
2060 * data later, which is much more complicated.
2061 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002062 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002063 if ((txn->flags & TX_NOT_FIRST) &&
2064 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002065 req->r < req->lr ||
2066 req->r > req->data + req->size - global.tune.maxrewrite)) {
2067 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002068 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2069 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002070 /* some data has still not left the buffer, wake us once that's done */
2071 buffer_dont_connect(req);
2072 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2073 return 0;
2074 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002075 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002076 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002077 }
2078
Willy Tarreau065e8332010-01-08 00:30:20 +01002079 /* Note that we have the same problem with the response ; we
2080 * may want to send a redirect, error or anything which requires
2081 * some spare space. So we'll ensure that we have at least
2082 * maxrewrite bytes available in the response buffer before
2083 * processing that one. This will only affect pipelined
2084 * keep-alive requests.
2085 */
2086 if ((txn->flags & TX_NOT_FIRST) &&
2087 unlikely((s->rep->flags & BF_FULL) ||
2088 s->rep->r < s->rep->lr ||
2089 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2090 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002091 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2092 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002093 /* don't let a connection request be initiated */
2094 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002095 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002096 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002097 return 0;
2098 }
2099 }
2100
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002101 if (likely(req->lr < req->r))
2102 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002103 }
2104
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 /* 1: we might have to print this header in debug mode */
2106 if (unlikely((global.mode & MODE_DEBUG) &&
2107 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002108 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002109 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau663308b2010-06-07 14:06:08 +02002112 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 eol = sol + msg->sl.rq.l;
2114 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002115
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 sol += hdr_idx_first_pos(&txn->hdr_idx);
2117 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 while (cur_idx) {
2120 eol = sol + txn->hdr_idx.v[cur_idx].len;
2121 debug_hdr("clihdr", s, sol, eol);
2122 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2123 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002124 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 }
2126
Willy Tarreau58f10d72006-12-04 02:26:12 +01002127
Willy Tarreau59234e92008-11-30 23:51:27 +01002128 /*
2129 * Now we quickly check if we have found a full valid request.
2130 * If not so, we check the FD and buffer states before leaving.
2131 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002132 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002133 * requests are checked first. When waiting for a second request
2134 * on a keep-alive session, if we encounter and error, close, t/o,
2135 * we note the error in the session flags but don't set any state.
2136 * Since the error will be noted there, it will not be counted by
2137 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002138 * Last, we may increase some tracked counters' http request errors on
2139 * the cases that are deliberately the client's fault. For instance,
2140 * a timeout or connection reset is not counted as an error. However
2141 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002143
Willy Tarreau655dce92009-11-08 13:10:58 +01002144 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002145 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002147 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002148 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002149 session_inc_http_req_ctr(s);
2150 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002151 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002152 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002154
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 /* 1: Since we are in header mode, if there's no space
2156 * left for headers, we won't be able to free more
2157 * later, so the session will never terminate. We
2158 * must terminate it now.
2159 */
2160 if (unlikely(req->flags & BF_FULL)) {
2161 /* FIXME: check if URI is set and return Status
2162 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002163 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002164 session_inc_http_req_ctr(s);
2165 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002166 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002167 if (msg->err_pos < 0)
2168 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 goto return_bad_req;
2170 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 /* 2: have we encountered a read error ? */
2173 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002174 if (!(s->flags & SN_ERR_MASK))
2175 s->flags |= SN_ERR_CLICL;
2176
Willy Tarreaufcffa692010-01-10 14:21:19 +01002177 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002178 goto failed_keep_alive;
2179
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002181 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002182 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002183 session_inc_http_err_ctr(s);
2184 }
2185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 msg->msg_state = HTTP_MSG_ERROR;
2187 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002188
Willy Tarreauda7ff642010-06-23 11:44:09 +02002189 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002190 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002191 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002192 if (s->listener->counters)
2193 s->listener->counters->failed_req++;
2194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 if (!(s->flags & SN_FINST_MASK))
2196 s->flags |= SN_FINST_R;
2197 return 0;
2198 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002199
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 /* 3: has the read timeout expired ? */
2201 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002202 if (!(s->flags & SN_ERR_MASK))
2203 s->flags |= SN_ERR_CLITO;
2204
Willy Tarreaufcffa692010-01-10 14:21:19 +01002205 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002206 goto failed_keep_alive;
2207
Willy Tarreau59234e92008-11-30 23:51:27 +01002208 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002209 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002210 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002211 session_inc_http_err_ctr(s);
2212 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 txn->status = 408;
2214 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2215 msg->msg_state = HTTP_MSG_ERROR;
2216 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002217
Willy Tarreauda7ff642010-06-23 11:44:09 +02002218 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002219 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002220 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002221 if (s->listener->counters)
2222 s->listener->counters->failed_req++;
2223
Willy Tarreau59234e92008-11-30 23:51:27 +01002224 if (!(s->flags & SN_FINST_MASK))
2225 s->flags |= SN_FINST_R;
2226 return 0;
2227 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002228
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 /* 4: have we encountered a close ? */
2230 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002231 if (!(s->flags & SN_ERR_MASK))
2232 s->flags |= SN_ERR_CLICL;
2233
Willy Tarreaufcffa692010-01-10 14:21:19 +01002234 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002235 goto failed_keep_alive;
2236
Willy Tarreau4076a152009-04-02 15:18:36 +02002237 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002238 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002239 txn->status = 400;
2240 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2241 msg->msg_state = HTTP_MSG_ERROR;
2242 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002243
Willy Tarreauda7ff642010-06-23 11:44:09 +02002244 session_inc_http_err_ctr(s);
2245 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002246 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002247 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002248 if (s->listener->counters)
2249 s->listener->counters->failed_req++;
2250
Willy Tarreau59234e92008-11-30 23:51:27 +01002251 if (!(s->flags & SN_FINST_MASK))
2252 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002253 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002254 }
2255
Willy Tarreau520d95e2009-09-19 21:04:57 +02002256 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002257 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002258 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002259#ifdef TCP_QUICKACK
2260 if (s->listener->options & LI_O_NOQUICKACK) {
2261 /* We need more data, we have to re-enable quick-ack in case we
2262 * previously disabled it, otherwise we might cause the client
2263 * to delay next data.
2264 */
2265 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2266 }
2267#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002268
Willy Tarreaufcffa692010-01-10 14:21:19 +01002269 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2270 /* If the client starts to talk, let's fall back to
2271 * request timeout processing.
2272 */
2273 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002274 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002275 }
2276
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002278 if (!tick_isset(req->analyse_exp)) {
2279 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2280 (txn->flags & TX_WAIT_NEXT_RQ) &&
2281 tick_isset(s->be->timeout.httpka))
2282 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2283 else
2284 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /* we're not ready yet */
2288 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002289
2290 failed_keep_alive:
2291 /* Here we process low-level errors for keep-alive requests. In
2292 * short, if the request is not the first one and it experiences
2293 * a timeout, read error or shutdown, we just silently close so
2294 * that the client can try again.
2295 */
2296 txn->status = 0;
2297 msg->msg_state = HTTP_MSG_RQBEFORE;
2298 req->analysers = 0;
2299 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002300 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002301 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002302 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002303 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002304
Willy Tarreaud787e662009-07-07 10:14:51 +02002305 /* OK now we have a complete HTTP request with indexed headers. Let's
2306 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002307 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2308 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2309 * points to the CRLF of the request line. req->lr points to the first
2310 * byte after the last LF. msg->col and msg->sov point to the first
2311 * byte of data. msg->eol cannot be trusted because it may have been
2312 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002313 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002314
Willy Tarreauda7ff642010-06-23 11:44:09 +02002315 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002316 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2317
Willy Tarreaub16a5742010-01-10 14:46:16 +01002318 if (txn->flags & TX_WAIT_NEXT_RQ) {
2319 /* kill the pending keep-alive timeout */
2320 txn->flags &= ~TX_WAIT_NEXT_RQ;
2321 req->analyse_exp = TICK_ETERNITY;
2322 }
2323
2324
Willy Tarreaud787e662009-07-07 10:14:51 +02002325 /* Maybe we found in invalid header name while we were configured not
2326 * to block on that, so we have to capture it now.
2327 */
2328 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002329 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /*
2332 * 1: identify the method
2333 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002334 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002335
2336 /* we can make use of server redirect on GET and HEAD */
2337 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2338 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002339
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 /*
2341 * 2: check if the URI matches the monitor_uri.
2342 * We have to do this for every request which gets in, because
2343 * the monitor-uri is defined by the frontend.
2344 */
2345 if (unlikely((s->fe->monitor_uri_len != 0) &&
2346 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002347 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 s->fe->monitor_uri,
2349 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002350 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002352 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002356 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002359 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2360 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002361
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 ret = acl_pass(ret);
2363 if (cond->pol == ACL_COND_UNLESS)
2364 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 if (ret) {
2367 /* we fail this request, let's return 503 service unavail */
2368 txn->status = 503;
2369 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2370 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002371 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002372 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 /* nothing to fail, let's reply normaly */
2375 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002376 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 goto return_prx_cond;
2378 }
2379
2380 /*
2381 * 3: Maybe we have to copy the original REQURI for the logs ?
2382 * Note: we cannot log anymore if the request has been
2383 * classified as invalid.
2384 */
2385 if (unlikely(s->logs.logwait & LW_REQ)) {
2386 /* we have a complete HTTP request that we must log */
2387 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2388 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (urilen >= REQURI_LEN)
2391 urilen = REQURI_LEN - 1;
2392 memcpy(txn->uri, &req->data[msg->som], urilen);
2393 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002394
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 if (!(s->logs.logwait &= ~LW_REQ))
2396 s->do_log(s);
2397 } else {
2398 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002399 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 }
Willy Tarreau06619262006-12-17 08:37:22 +01002401
William Lallemanda73203e2012-03-12 12:48:57 +01002402 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2403 s->unique_id = pool_alloc2(pool2_uniqueid);
2404 }
2405
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002407 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2408 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002409
Willy Tarreau5b154472009-12-21 20:11:07 +01002410 /* ... and check if the request is HTTP/1.1 or above */
2411 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002412 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2413 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2414 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002415 txn->flags |= TX_REQ_VER_11;
2416
2417 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002418 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002419
Willy Tarreau88d349d2010-01-25 12:15:43 +01002420 /* if the frontend has "option http-use-proxy-header", we'll check if
2421 * we have what looks like a proxied connection instead of a connection,
2422 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2423 * Note that this is *not* RFC-compliant, however browsers and proxies
2424 * happen to do that despite being non-standard :-(
2425 * We consider that a request not beginning with either '/' or '*' is
2426 * a proxied connection, which covers both "scheme://location" and
2427 * CONNECT ip:port.
2428 */
2429 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2430 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2431 txn->flags |= TX_USE_PX_CONN;
2432
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002433 /* transfer length unknown*/
2434 txn->flags &= ~TX_REQ_XFER_LEN;
2435
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002437 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002438 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002440
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002441 /* 6: determine the transfer-length.
2442 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2443 * the presence of a message-body in a REQUEST and its transfer length
2444 * must be determined that way (in order of precedence) :
2445 * 1. The presence of a message-body in a request is signaled by the
2446 * inclusion of a Content-Length or Transfer-Encoding header field
2447 * in the request's header fields. When a request message contains
2448 * both a message-body of non-zero length and a method that does
2449 * not define any semantics for that request message-body, then an
2450 * origin server SHOULD either ignore the message-body or respond
2451 * with an appropriate error message (e.g., 413). A proxy or
2452 * gateway, when presented the same request, SHOULD either forward
2453 * the request inbound with the message- body or ignore the
2454 * message-body when determining a response.
2455 *
2456 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2457 * and the "chunked" transfer-coding (Section 6.2) is used, the
2458 * transfer-length is defined by the use of this transfer-coding.
2459 * If a Transfer-Encoding header field is present and the "chunked"
2460 * transfer-coding is not present, the transfer-length is defined
2461 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 * 3. If a Content-Length header field is present, its decimal value in
2464 * OCTETs represents both the entity-length and the transfer-length.
2465 * If a message is received with both a Transfer-Encoding header
2466 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002468 * 4. By the server closing the connection. (Closing the connection
2469 * cannot be used to indicate the end of a request body, since that
2470 * would leave no possibility for the server to send back a response.)
2471 *
2472 * Whenever a transfer-coding is applied to a message-body, the set of
2473 * transfer-codings MUST include "chunked", unless the message indicates
2474 * it is terminated by closing the connection. When the "chunked"
2475 * transfer-coding is used, it MUST be the last transfer-coding applied
2476 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002477 */
2478
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002479 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002480 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002482 while ((txn->flags & TX_REQ_VER_11) &&
2483 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2485 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2486 else if (txn->flags & TX_REQ_TE_CHNK) {
2487 /* bad transfer-encoding (chunked followed by something else) */
2488 use_close_only = 1;
2489 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2490 break;
2491 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002492 }
2493
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002495 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002496 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2497 signed long long cl;
2498
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 if (!ctx.vlen) {
2500 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002502 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503
Willy Tarreauad14f752011-09-02 20:33:27 +02002504 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2505 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002506 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002507 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508
Willy Tarreauad14f752011-09-02 20:33:27 +02002509 if (cl < 0) {
2510 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002512 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002513
Willy Tarreauad14f752011-09-02 20:33:27 +02002514 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) {
2515 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002516 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002517 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002518
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002519 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002520 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002521 }
2522
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002523 /* bodyless requests have a known length */
2524 if (!use_close_only)
2525 txn->flags |= TX_REQ_XFER_LEN;
2526
Willy Tarreaud787e662009-07-07 10:14:51 +02002527 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002528 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002529 req->analyse_exp = TICK_ETERNITY;
2530 return 1;
2531
2532 return_bad_req:
2533 /* We centralize bad requests processing here */
2534 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2535 /* we detected a parsing error. We want to archive this request
2536 * in the dedicated proxy area for later troubleshooting.
2537 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002538 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002539 }
2540
2541 txn->req.msg_state = HTTP_MSG_ERROR;
2542 txn->status = 400;
2543 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002544
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002545 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002546 if (s->listener->counters)
2547 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002548
2549 return_prx_cond:
2550 if (!(s->flags & SN_ERR_MASK))
2551 s->flags |= SN_ERR_PRXCOND;
2552 if (!(s->flags & SN_FINST_MASK))
2553 s->flags |= SN_FINST_R;
2554
2555 req->analysers = 0;
2556 req->analyse_exp = TICK_ETERNITY;
2557 return 0;
2558}
2559
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560/* We reached the stats page through a POST request.
2561 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002562 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002563 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002564int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002566 struct proxy *px = NULL;
2567 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002569 char key[LINESIZE];
2570 int action = ST_ADM_ACTION_NONE;
2571 int reprocess = 0;
2572
2573 int total_servers = 0;
2574 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575
2576 char *first_param, *cur_param, *next_param, *end_params;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002577 char *st_cur_param, *st_next_param;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002578
2579 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002580 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581
2582 cur_param = next_param = end_params;
2583
Cyril Bonté23b39d92011-02-10 22:54:44 +01002584 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002585 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002586 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587 return 1;
2588 }
2589 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002590 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002591 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002592 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593 }
2594
2595 *end_params = '\0';
2596
Willy Tarreau295a8372011-03-10 11:25:07 +01002597 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002598
2599 /*
2600 * Parse the parameters in reverse order to only store the last value.
2601 * From the html form, the backend and the action are at the end.
2602 */
2603 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002604 char *value;
2605 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002606
2607 cur_param--;
2608 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002609 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002610 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002611 poffset = (cur_param != first_param ? 1 : 0);
2612 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2613 if ((plen > 0) && (plen <= sizeof(key))) {
2614 strncpy(key, cur_param + poffset, plen);
2615 key[plen - 1] = '\0';
2616 } else {
2617 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2618 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002619 }
2620
2621 /* Parse the value */
2622 value = key;
2623 while (*value != '\0' && *value != '=') {
2624 value++;
2625 }
2626 if (*value == '=') {
2627 /* Ok, a value is found, we can mark the end of the key */
2628 *value++ = '\0';
2629 }
2630
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002631 if (!url_decode(key) || !url_decode(value))
2632 break;
2633
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002635 if (!px && (strcmp(key, "b") == 0)) {
2636 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2637 /* the backend name is unknown or ambiguous (duplicate names) */
2638 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2639 goto out;
2640 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002642 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002644 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002645 }
2646 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002647 action = ST_ADM_ACTION_ENABLE;
2648 }
2649 else {
2650 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2651 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002652 }
2653 }
2654 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002655 if (!(px && action)) {
2656 /*
2657 * Indicates that we'll need to reprocess the parameters
2658 * as soon as backend and action are known
2659 */
2660 if (!reprocess) {
2661 st_cur_param = cur_param;
2662 st_next_param = next_param;
2663 }
2664 reprocess = 1;
2665 }
2666 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002667 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002668 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002669 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002670 /* Not already in maintenance, we can change the server state */
2671 sv->state |= SRV_MAINTAIN;
2672 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002673 altered_servers++;
2674 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002675 }
2676 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002677 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002678 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002679 /* Already in maintenance, we can change the server state */
2680 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002681 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002682 altered_servers++;
2683 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002684 }
2685 break;
2686 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002687 } else {
2688 /* the server name is unknown or ambiguous (duplicate names) */
2689 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002690 }
2691 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002692 if (reprocess && px && action) {
2693 /* Now, we know the backend and the action chosen by the user.
2694 * We can safely restart from the first server parameter
2695 * to reprocess them
2696 */
2697 cur_param = st_cur_param;
2698 next_param = st_next_param;
2699 reprocess = 0;
2700 goto reprocess_servers;
2701 }
2702
Cyril Bonté70be45d2010-10-12 00:14:35 +02002703 next_param = cur_param;
2704 }
2705 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002706
2707 if (total_servers == 0) {
2708 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2709 }
2710 else if (altered_servers == 0) {
2711 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2712 }
2713 else if (altered_servers == total_servers) {
2714 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2715 }
2716 else {
2717 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2718 }
2719 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002720 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721}
2722
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002723/* returns a pointer to the first rule which forbids access (deny or http_auth),
2724 * or NULL if everything's OK.
2725 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002726static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002727http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2728{
Willy Tarreauff011f22011-01-06 17:51:27 +01002729 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002730
Willy Tarreauff011f22011-01-06 17:51:27 +01002731 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002732 int ret = 1;
2733
Willy Tarreauff011f22011-01-06 17:51:27 +01002734 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002735 continue;
2736
2737 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002738 if (rule->cond) {
2739 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002740 ret = acl_pass(ret);
2741
Willy Tarreauff011f22011-01-06 17:51:27 +01002742 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002743 ret = !ret;
2744 }
2745
2746 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002747 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002748 return NULL; /* no problem */
2749 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002750 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002751 }
2752 }
2753 return NULL;
2754}
2755
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756/* This stream analyser runs all HTTP request processing which is common to
2757 * frontends and backends, which means blocking ACLs, filters, connection-close,
2758 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002759 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 * either needs more data or wants to immediately abort the request (eg: deny,
2761 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002762 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002764{
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 struct http_txn *txn = &s->txn;
2766 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002768 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002770 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002771 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002772
Willy Tarreau655dce92009-11-08 13:10:58 +01002773 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002774 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002775 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002776 return 0;
2777 }
2778
Willy Tarreau3a816292009-07-07 10:55:49 +02002779 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 req->analyse_exp = TICK_ETERNITY;
2781
2782 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2783 now_ms, __FUNCTION__,
2784 s,
2785 req,
2786 req->rex, req->wex,
2787 req->flags,
2788 req->l,
2789 req->analysers);
2790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 /* first check whether we have some ACLs set to block this request */
2792 list_for_each_entry(cond, &px->block_cond, list) {
2793 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002794
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002795 ret = acl_pass(ret);
2796 if (cond->pol == ACL_COND_UNLESS)
2797 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002798
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002799 if (ret) {
2800 txn->status = 403;
2801 /* let's log the request time */
2802 s->logs.tv_request = now;
2803 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002804 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002808
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002809 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002810 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002811
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002812 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002813 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002814 do_stats = stats_check_uri(s->rep->prod, txn, px);
2815 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002816 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 +01002817 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002818 else
2819 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002820
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002821 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002822 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002823 txn->status = 403;
2824 s->logs.tv_request = now;
2825 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002826 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002827 s->fe->fe_counters.denied_req++;
2828 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2829 s->be->be_counters.denied_req++;
2830 if (s->listener->counters)
2831 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002832 goto return_prx_cond;
2833 }
2834
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002835 /* try headers filters */
2836 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002837 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002838 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002839
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 /* has the request been denied ? */
2841 if (txn->flags & TX_CLDENY) {
2842 /* no need to go further */
2843 txn->status = 403;
2844 /* let's log the request time */
2845 s->logs.tv_request = now;
2846 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002847 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 goto return_prx_cond;
2849 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002850
2851 /* When a connection is tarpitted, we use the tarpit timeout,
2852 * which may be the same as the connect timeout if unspecified.
2853 * If unset, then set it to zero because we really want it to
2854 * eventually expire. We build the tarpit as an analyser.
2855 */
2856 if (txn->flags & TX_CLTARPIT) {
2857 buffer_erase(s->req);
2858 /* wipe the request out so that we can drop the connection early
2859 * if the client closes first.
2860 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002861 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002862 req->analysers = 0; /* remove switching rules etc... */
2863 req->analysers |= AN_REQ_HTTP_TARPIT;
2864 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2865 if (!req->analyse_exp)
2866 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002867 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002868 return 1;
2869 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002870 }
Willy Tarreau06619262006-12-17 08:37:22 +01002871
Willy Tarreau5b154472009-12-21 20:11:07 +01002872 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2873 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002874 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2875 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002876 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2877 * avoid to redo the same work if FE and BE have the same settings (common).
2878 * The method consists in checking if options changed between the two calls
2879 * (implying that either one is non-null, or one of them is non-null and we
2880 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002881 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002882
Willy Tarreaudc008c52010-02-01 16:20:08 +01002883 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2884 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2885 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2886 (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 +01002887 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002888
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002889 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2890 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002891 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002892 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2893 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002894 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002895 tmp = TX_CON_WANT_CLO;
2896
Willy Tarreau5b154472009-12-21 20:11:07 +01002897 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2898 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002899
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002900 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2901 /* parse the Connection header and possibly clean it */
2902 int to_del = 0;
2903 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002904 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2905 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906 to_del |= 2; /* remove "keep-alive" */
2907 if (!(txn->flags & TX_REQ_VER_11))
2908 to_del |= 1; /* remove "close" */
2909 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002911
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002912 /* check if client or config asks for explicit close in KAL/SCL */
2913 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2914 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2915 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2916 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002917 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002918 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2919 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002920 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2921 }
Willy Tarreau78599912009-10-17 20:12:21 +02002922
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002923 /* we can be blocked here because the request needs to be authenticated,
2924 * either to pass or to access stats.
2925 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002926 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002927 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002928 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002929
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002930 if (!realm)
2931 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2932
Willy Tarreau844a7e72010-01-31 21:46:18 +01002933 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002934 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2935 txn->status = 401;
2936 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937 /* on 401 we still count one error, because normal browsing
2938 * won't significantly increase the counter but brute force
2939 * attempts will.
2940 */
2941 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002942 goto return_prx_cond;
2943 }
2944
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002945 /* add request headers from the rule sets in the same order */
2946 list_for_each_entry(wl, &px->req_add, list) {
2947 if (wl->cond) {
2948 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2949 ret = acl_pass(ret);
2950 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2951 ret = !ret;
2952 if (!ret)
2953 continue;
2954 }
2955
2956 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2957 goto return_bad_req;
2958 }
2959
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002960 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002961 struct stats_admin_rule *stats_admin_rule;
2962
2963 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002964 * FIXME!!! that one is rather dangerous, we want to
2965 * make it follow standard rules (eg: clear req->analysers).
2966 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002967
Cyril Bonté474be412010-10-12 00:14:36 +02002968 /* now check whether we have some admin rules for this request */
2969 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2970 int ret = 1;
2971
2972 if (stats_admin_rule->cond) {
2973 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2974 ret = acl_pass(ret);
2975 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2976 ret = !ret;
2977 }
2978
2979 if (ret) {
2980 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002981 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002982 break;
2983 }
2984 }
2985
Cyril Bonté70be45d2010-10-12 00:14:35 +02002986 /* Was the status page requested with a POST ? */
2987 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002988 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002989 if (msg->msg_state < HTTP_MSG_100_SENT) {
2990 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2991 * send an HTTP/1.1 100 Continue intermediate response.
2992 */
2993 if (txn->flags & TX_REQ_VER_11) {
2994 struct hdr_ctx ctx;
2995 ctx.idx = 0;
2996 /* Expect is allowed in 1.1, look for it */
2997 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
2998 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2999 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3000 }
3001 }
3002 msg->msg_state = HTTP_MSG_100_SENT;
3003 s->logs.tv_request = now; /* update the request timer to reflect full request */
3004 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003005 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003006 /* we need more data */
3007 req->analysers |= an_bit;
3008 buffer_dont_connect(req);
3009 return 0;
3010 }
Cyril Bonté474be412010-10-12 00:14:36 +02003011 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003012 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003013 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003014 }
3015
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003016 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003017 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003018 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003019 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003020 s->rep->prod->applet.private = s;
3021 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003022 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003023 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3024 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003025
3026 return 0;
3027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 /* check whether we have some ACLs set to redirect this request */
3031 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003032 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003033
Willy Tarreauf285f542010-01-03 20:03:03 +01003034 if (rule->cond) {
3035 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3036 ret = acl_pass(ret);
3037 if (rule->cond->pol == ACL_COND_UNLESS)
3038 ret = !ret;
3039 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003040
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003042 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 /* build redirect message */
3046 switch(rule->code) {
3047 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 msg_fmt = HTTP_303;
3049 break;
3050 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 msg_fmt = HTTP_301;
3052 break;
3053 case 302:
3054 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 msg_fmt = HTTP_302;
3056 break;
3057 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003058
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003059 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 switch(rule->type) {
3063 case REDIRECT_TYPE_PREFIX: {
3064 const char *path;
3065 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 path = http_get_path(txn);
3068 /* build message using path */
3069 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003070 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003071 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3072 int qs = 0;
3073 while (qs < pathlen) {
3074 if (path[qs] == '?') {
3075 pathlen = qs;
3076 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003077 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003079 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003080 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003081 } else {
3082 path = "/";
3083 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003084 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003085
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003086 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003087 goto return_bad_req;
3088
3089 /* add prefix. Note that if prefix == "/", we don't want to
3090 * add anything, otherwise it makes it hard for the user to
3091 * configure a self-redirection.
3092 */
3093 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003094 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3095 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003096 }
3097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 /* add path */
3099 memcpy(rdr.str + rdr.len, path, pathlen);
3100 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003101
3102 /* append a slash at the end of the location is needed and missing */
3103 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3104 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3105 if (rdr.len > rdr.size - 5)
3106 goto return_bad_req;
3107 rdr.str[rdr.len] = '/';
3108 rdr.len++;
3109 }
3110
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003111 break;
3112 }
3113 case REDIRECT_TYPE_LOCATION:
3114 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003115 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003117
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118 /* add location */
3119 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3120 rdr.len += rule->rdr_len;
3121 break;
3122 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 if (rule->cookie_len) {
3125 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3126 rdr.len += 14;
3127 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3128 rdr.len += rule->cookie_len;
3129 memcpy(rdr.str + rdr.len, "\r\n", 2);
3130 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003131 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003132
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003133 /* add end of headers and the keep-alive/close status.
3134 * We may choose to set keep-alive if the Location begins
3135 * with a slash, because the client will come back to the
3136 * same server.
3137 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003138 txn->status = rule->code;
3139 /* let's log the request time */
3140 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003141
3142 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3143 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003144 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003145 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3146 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3147 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003148 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003149 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3150 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3151 rdr.len += 30;
3152 } else {
3153 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3154 rdr.len += 24;
3155 }
Willy Tarreau75661452010-01-10 10:35:01 +01003156 }
3157 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3158 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003159 buffer_write(req->prod->ob, rdr.str, rdr.len);
3160 /* "eat" the request */
3161 buffer_ignore(req, msg->sov - msg->som);
3162 msg->som = msg->sov;
3163 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003164 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3165 txn->req.msg_state = HTTP_MSG_CLOSED;
3166 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003167 break;
3168 } else {
3169 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003170 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3171 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3172 rdr.len += 29;
3173 } else {
3174 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3175 rdr.len += 23;
3176 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003177 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003178 goto return_prx_cond;
3179 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003180 }
3181 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003182
Willy Tarreau2be39392010-01-03 17:24:51 +01003183 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3184 * If this happens, then the data will not come immediately, so we must
3185 * send all what we have without waiting. Note that due to the small gain
3186 * in waiting for the body of the request, it's easier to simply put the
3187 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3188 * itself once used.
3189 */
3190 req->flags |= BF_SEND_DONTWAIT;
3191
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003192 /* that's OK for us now, let's move on to next analysers */
3193 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003194
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003195 return_bad_req:
3196 /* We centralize bad requests processing here */
3197 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3198 /* we detected a parsing error. We want to archive this request
3199 * in the dedicated proxy area for later troubleshooting.
3200 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003201 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003202 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 txn->req.msg_state = HTTP_MSG_ERROR;
3205 txn->status = 400;
3206 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003207
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003208 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003209 if (s->listener->counters)
3210 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 return_prx_cond:
3213 if (!(s->flags & SN_ERR_MASK))
3214 s->flags |= SN_ERR_PRXCOND;
3215 if (!(s->flags & SN_FINST_MASK))
3216 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003217
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 req->analysers = 0;
3219 req->analyse_exp = TICK_ETERNITY;
3220 return 0;
3221}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003222
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003223/* This function performs all the processing enabled for the current request.
3224 * It returns 1 if the processing can continue on next analysers, or zero if it
3225 * needs more data, encounters an error, or wants to immediately abort the
3226 * request. It relies on buffers flags, and updates s->req->analysers.
3227 */
3228int http_process_request(struct session *s, struct buffer *req, int an_bit)
3229{
3230 struct http_txn *txn = &s->txn;
3231 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003232
Willy Tarreau655dce92009-11-08 13:10:58 +01003233 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003234 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003235 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003236 return 0;
3237 }
3238
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003239 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3240 now_ms, __FUNCTION__,
3241 s,
3242 req,
3243 req->rex, req->wex,
3244 req->flags,
3245 req->l,
3246 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003247
Willy Tarreau59234e92008-11-30 23:51:27 +01003248 /*
3249 * Right now, we know that we have processed the entire headers
3250 * and that unwanted requests have been filtered out. We can do
3251 * whatever we want with the remaining request. Also, now we
3252 * may have separate values for ->fe, ->be.
3253 */
Willy Tarreau06619262006-12-17 08:37:22 +01003254
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 /*
3256 * If HTTP PROXY is set we simply get remote server address
3257 * parsing incoming request.
3258 */
3259 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003260 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003262
Willy Tarreau59234e92008-11-30 23:51:27 +01003263 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003264 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 * Note that doing so might move headers in the request, but
3266 * the fields will stay coherent and the URI will not move.
3267 * This should only be performed in the backend.
3268 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003269 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3271 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003272
Willy Tarreau59234e92008-11-30 23:51:27 +01003273 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003274 * 8: the appsession cookie was looked up very early in 1.2,
3275 * so let's do the same now.
3276 */
3277
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003278 /* It needs to look into the URI unless persistence must be ignored */
3279 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003280 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003281 }
3282
William Lallemanda73203e2012-03-12 12:48:57 +01003283 /* add unique-id if "header-unique-id" is specified */
3284
3285 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3286 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3287
3288 if (s->fe->header_unique_id && s->unique_id) {
3289 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3290 if (ret < 0 || ret > global.tune.bufsize)
3291 goto return_bad_req;
3292 if(unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, trash) < 0))
3293 goto return_bad_req;
3294 }
3295
Cyril Bontéb21570a2009-11-29 20:04:48 +01003296 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 * 9: add X-Forwarded-For if either the frontend or the backend
3298 * asks for it.
3299 */
3300 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003301 struct hdr_ctx ctx = { .idx = 0 };
3302
3303 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003304 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003305 /* The header is set to be added only if none is present
3306 * and we found it, so don't do anything.
3307 */
3308 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003309 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003310 /* Add an X-Forwarded-For header unless the source IP is
3311 * in the 'except' network range.
3312 */
3313 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003314 (((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 +01003315 != s->fe->except_net.s_addr) &&
3316 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003317 (((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 +01003318 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003319 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003321 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003322
3323 /* Note: we rely on the backend to get the header name to be used for
3324 * x-forwarded-for, because the header is really meant for the backends.
3325 * However, if the backend did not specify any option, we have to rely
3326 * on the frontend's header name.
3327 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 if (s->be->fwdfor_hdr_len) {
3329 len = s->be->fwdfor_hdr_len;
3330 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003331 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003332 len = s->fe->fwdfor_hdr_len;
3333 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003334 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003335 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003336
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003337 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003338 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003339 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003340 }
3341 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003342 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 /* FIXME: for the sake of completeness, we should also support
3344 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003345 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003346 int len;
3347 char pn[INET6_ADDRSTRLEN];
3348 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003349 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003350 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003351
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 /* Note: we rely on the backend to get the header name to be used for
3353 * x-forwarded-for, because the header is really meant for the backends.
3354 * However, if the backend did not specify any option, we have to rely
3355 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003356 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003357 if (s->be->fwdfor_hdr_len) {
3358 len = s->be->fwdfor_hdr_len;
3359 memcpy(trash, s->be->fwdfor_hdr_name, len);
3360 } else {
3361 len = s->fe->fwdfor_hdr_len;
3362 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003363 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003364 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003365
Willy Tarreau59234e92008-11-30 23:51:27 +01003366 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003367 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003368 goto return_bad_req;
3369 }
3370 }
3371
3372 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003373 * 10: add X-Original-To if either the frontend or the backend
3374 * asks for it.
3375 */
3376 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3377
3378 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003379 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003380 /* Add an X-Original-To header unless the destination IP is
3381 * in the 'except' network range.
3382 */
3383 if (!(s->flags & SN_FRT_ADDR_SET))
3384 get_frt_addr(s);
3385
Willy Tarreau6471afb2011-09-23 10:54:59 +02003386 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003387 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003388 (((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 +02003389 != s->fe->except_to.s_addr) &&
3390 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003391 (((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 +02003392 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003393 int len;
3394 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003395 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003396
3397 /* Note: we rely on the backend to get the header name to be used for
3398 * x-original-to, because the header is really meant for the backends.
3399 * However, if the backend did not specify any option, we have to rely
3400 * on the frontend's header name.
3401 */
3402 if (s->be->orgto_hdr_len) {
3403 len = s->be->orgto_hdr_len;
3404 memcpy(trash, s->be->orgto_hdr_name, len);
3405 } else {
3406 len = s->fe->orgto_hdr_len;
3407 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003408 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003409 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3410
3411 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003412 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003413 goto return_bad_req;
3414 }
3415 }
3416 }
3417
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003418 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3419 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003420 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003421 unsigned int want_flags = 0;
3422
3423 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003424 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3425 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3426 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003427 want_flags |= TX_CON_CLO_SET;
3428 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003429 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3430 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3431 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003432 want_flags |= TX_CON_KAL_SET;
3433 }
3434
3435 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3436 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003438
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003439
Willy Tarreau522d6c02009-12-06 18:49:18 +01003440 /* If we have no server assigned yet and we're balancing on url_param
3441 * with a POST request, we may be interested in checking the body for
3442 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003443 */
3444 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3445 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003446 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003447 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003448 buffer_dont_connect(req);
3449 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003451
Willy Tarreau5e205522011-12-17 16:34:27 +01003452 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003453 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003454#ifdef TCP_QUICKACK
3455 /* We expect some data from the client. Unless we know for sure
3456 * we already have a full request, we have to re-enable quick-ack
3457 * in case we previously disabled it, otherwise we might cause
3458 * the client to delay further data.
3459 */
3460 if ((s->listener->options & LI_O_NOQUICKACK) &&
3461 ((txn->flags & TX_REQ_TE_CHNK) ||
3462 (msg->body_len > req->l - txn->req.eoh - 2)))
3463 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3464#endif
3465 }
Willy Tarreau03945942009-12-22 16:50:27 +01003466
Willy Tarreau59234e92008-11-30 23:51:27 +01003467 /*************************************************************
3468 * OK, that's finished for the headers. We have done what we *
3469 * could. Let's switch to the DATA state. *
3470 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003471 req->analyse_exp = TICK_ETERNITY;
3472 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003473
Willy Tarreau59234e92008-11-30 23:51:27 +01003474 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003475 /* OK let's go on with the BODY now */
3476 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003477
Willy Tarreau59234e92008-11-30 23:51:27 +01003478 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003479 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003480 /* we detected a parsing error. We want to archive this request
3481 * in the dedicated proxy area for later troubleshooting.
3482 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003483 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003484 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003485
Willy Tarreau59234e92008-11-30 23:51:27 +01003486 txn->req.msg_state = HTTP_MSG_ERROR;
3487 txn->status = 400;
3488 req->analysers = 0;
3489 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003490
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003491 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003492 if (s->listener->counters)
3493 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003494
Willy Tarreau59234e92008-11-30 23:51:27 +01003495 if (!(s->flags & SN_ERR_MASK))
3496 s->flags |= SN_ERR_PRXCOND;
3497 if (!(s->flags & SN_FINST_MASK))
3498 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003499 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003500}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003501
Willy Tarreau60b85b02008-11-30 23:28:40 +01003502/* This function is an analyser which processes the HTTP tarpit. It always
3503 * returns zero, at the beginning because it prevents any other processing
3504 * from occurring, and at the end because it terminates the request.
3505 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003506int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003507{
3508 struct http_txn *txn = &s->txn;
3509
3510 /* This connection is being tarpitted. The CLIENT side has
3511 * already set the connect expiration date to the right
3512 * timeout. We just have to check that the client is still
3513 * there and that the timeout has not expired.
3514 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003515 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003516 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3517 !tick_is_expired(req->analyse_exp, now_ms))
3518 return 0;
3519
3520 /* We will set the queue timer to the time spent, just for
3521 * logging purposes. We fake a 500 server error, so that the
3522 * attacker will not suspect his connection has been tarpitted.
3523 * It will not cause trouble to the logs because we can exclude
3524 * the tarpitted connections by filtering on the 'PT' status flags.
3525 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003526 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3527
3528 txn->status = 500;
3529 if (req->flags != BF_READ_ERROR)
3530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3531
3532 req->analysers = 0;
3533 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003534
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003535 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003536 if (s->listener->counters)
3537 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003538
Willy Tarreau60b85b02008-11-30 23:28:40 +01003539 if (!(s->flags & SN_ERR_MASK))
3540 s->flags |= SN_ERR_PRXCOND;
3541 if (!(s->flags & SN_FINST_MASK))
3542 s->flags |= SN_FINST_T;
3543 return 0;
3544}
3545
Willy Tarreaud34af782008-11-30 23:36:37 +01003546/* This function is an analyser which processes the HTTP request body. It looks
3547 * for parameters to be used for the load balancing algorithm (url_param). It
3548 * must only be called after the standard HTTP request processing has occurred,
3549 * because it expects the request to be parsed. It returns zero if it needs to
3550 * read more data, or 1 once it has completed its analysis.
3551 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003552int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003553{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003554 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003555 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003556 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003557
3558 /* We have to parse the HTTP request body to find any required data.
3559 * "balance url_param check_post" should have been the only way to get
3560 * into this. We were brought here after HTTP header analysis, so all
3561 * related structures are ready.
3562 */
3563
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3565 goto missing_data;
3566
3567 if (msg->msg_state < HTTP_MSG_100_SENT) {
3568 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3569 * send an HTTP/1.1 100 Continue intermediate response.
3570 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003571 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003572 struct hdr_ctx ctx;
3573 ctx.idx = 0;
3574 /* Expect is allowed in 1.1, look for it */
3575 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3576 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3577 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3578 }
3579 }
3580 msg->msg_state = HTTP_MSG_100_SENT;
3581 }
3582
3583 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003584 /* we have msg->col and msg->sov which both point to the first
3585 * byte of message body. msg->som still points to the beginning
3586 * of the message. We must save the body in req->lr because it
3587 * survives buffer re-alignments.
3588 */
3589 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 if (txn->flags & TX_REQ_TE_CHNK)
3591 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3592 else
3593 msg->msg_state = HTTP_MSG_DATA;
3594 }
3595
3596 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003597 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003598 * set ->sov and ->lr to point to the body and switch to DATA or
3599 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003600 */
3601 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003602
Willy Tarreau115acb92009-12-26 13:56:06 +01003603 if (!ret)
3604 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003605 else if (ret < 0) {
3606 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003607 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003608 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003609 }
3610
Willy Tarreaud98cf932009-12-27 22:54:55 +01003611 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 * We have the first non-header byte in msg->col, which is either the
3613 * beginning of the chunk size or of the data. The first data byte is in
3614 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3615 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003616 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617
Willy Tarreau124d9912011-03-01 20:30:48 +01003618 if (msg->body_len < limit)
3619 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620
Willy Tarreau7c96f672009-12-27 22:47:25 +01003621 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622 goto http_end;
3623
3624 missing_data:
3625 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003626 if (req->flags & BF_FULL) {
3627 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003628 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003629 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003630
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3632 txn->status = 408;
3633 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003634
3635 if (!(s->flags & SN_ERR_MASK))
3636 s->flags |= SN_ERR_CLITO;
3637 if (!(s->flags & SN_FINST_MASK))
3638 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003639 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003640 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641
3642 /* we get here if we need to wait for more data */
3643 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003644 /* Not enough data. We'll re-use the http-request
3645 * timeout here. Ideally, we should set the timeout
3646 * relative to the accept() date. We just set the
3647 * request timeout once at the beginning of the
3648 * request.
3649 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003650 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003651 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003652 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003653 return 0;
3654 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003655
3656 http_end:
3657 /* The situation will not evolve, so let's give up on the analysis. */
3658 s->logs.tv_request = now; /* update the request timer to reflect full request */
3659 req->analysers &= ~an_bit;
3660 req->analyse_exp = TICK_ETERNITY;
3661 return 1;
3662
3663 return_bad_req: /* let's centralize all bad requests */
3664 txn->req.msg_state = HTTP_MSG_ERROR;
3665 txn->status = 400;
3666 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3667
Willy Tarreau79ebac62010-06-07 13:47:49 +02003668 if (!(s->flags & SN_ERR_MASK))
3669 s->flags |= SN_ERR_PRXCOND;
3670 if (!(s->flags & SN_FINST_MASK))
3671 s->flags |= SN_FINST_R;
3672
Willy Tarreau522d6c02009-12-06 18:49:18 +01003673 return_err_msg:
3674 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003675 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003676 if (s->listener->counters)
3677 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003679}
3680
Mark Lamourinec2247f02012-01-04 13:02:01 -05003681int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3682
3683 struct hdr_ctx ctx;
3684
Mark Lamourinec2247f02012-01-04 13:02:01 -05003685 char *hdr_name = be->server_id_hdr_name;
3686 int hdr_name_len = be->server_id_hdr_len;
3687
3688 char *hdr_val;
3689
William Lallemandd9e90662012-01-30 17:27:17 +01003690 ctx.idx = 0;
3691
Mark Lamourinec2247f02012-01-04 13:02:01 -05003692 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3693 /* remove any existing values from the header */
3694 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3695 }
3696
3697 /* Add the new header requested with the server value */
3698 hdr_val = trash;
3699 memcpy(hdr_val, hdr_name, hdr_name_len);
3700 hdr_val += hdr_name_len;
3701 *hdr_val++ = ':';
3702 *hdr_val++ = ' ';
3703 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3704 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3705
3706 return 0;
3707}
3708
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709/* Terminate current transaction and prepare a new one. This is very tricky
3710 * right now but it works.
3711 */
3712void http_end_txn_clean_session(struct session *s)
3713{
3714 /* FIXME: We need a more portable way of releasing a backend's and a
3715 * server's connections. We need a safer way to reinitialize buffer
3716 * flags. We also need a more accurate method for computing per-request
3717 * data.
3718 */
3719 http_silent_debug(__LINE__, s);
3720
3721 s->req->cons->flags |= SI_FL_NOLINGER;
3722 s->req->cons->shutr(s->req->cons);
3723 s->req->cons->shutw(s->req->cons);
3724
3725 http_silent_debug(__LINE__, s);
3726
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003727 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003729 if (unlikely(s->srv_conn))
3730 sess_change_server(s, NULL);
3731 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732
3733 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3734 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003735 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
3737 if (s->txn.status) {
3738 int n;
3739
3740 n = s->txn.status / 100;
3741 if (n < 1 || n > 5)
3742 n = 0;
3743
3744 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003745 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746
Willy Tarreau24657792010-02-26 10:30:28 +01003747 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003749 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003750 }
3751
3752 /* don't count other requests' data */
3753 s->logs.bytes_in -= s->req->l - s->req->send_max;
3754 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3755
3756 /* let's do a final log if we need it */
3757 if (s->logs.logwait &&
3758 !(s->flags & SN_MONITOR) &&
3759 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3760 s->do_log(s);
3761 }
3762
3763 s->logs.accept_date = date; /* user-visible date for logging */
3764 s->logs.tv_accept = now; /* corrected date for internal use */
3765 tv_zero(&s->logs.tv_request);
3766 s->logs.t_queue = -1;
3767 s->logs.t_connect = -1;
3768 s->logs.t_data = -1;
3769 s->logs.t_close = 0;
3770 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3771 s->logs.srv_queue_size = 0; /* we will get this number soon */
3772
3773 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3774 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3775
3776 if (s->pend_pos)
3777 pendconn_free(s->pend_pos);
3778
Willy Tarreau827aee92011-03-10 16:55:02 +01003779 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 if (s->flags & SN_CURR_SESS) {
3781 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003782 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003784 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3785 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 }
3787
Willy Tarreau9e000c62011-03-10 14:03:36 +01003788 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789
3790 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3791 s->req->cons->fd = -1; /* just to help with debugging */
3792 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003793 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 s->req->cons->err_loc = NULL;
3795 s->req->cons->exp = TICK_ETERNITY;
3796 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003797 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3798 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 +02003799 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 +01003800 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3801 s->txn.meth = 0;
3802 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003803 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003804 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 s->req->cons->flags |= SI_FL_INDEP_STR;
3806
Willy Tarreau96e31212011-05-30 18:10:30 +02003807 if (s->fe->options2 & PR_O2_NODELAY) {
3808 s->req->flags |= BF_NEVER_WAIT;
3809 s->rep->flags |= BF_NEVER_WAIT;
3810 }
3811
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812 /* if the request buffer is not empty, it means we're
3813 * about to process another request, so send pending
3814 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003815 * Just don't do this if the buffer is close to be full,
3816 * because the request will wait for it to flush a little
3817 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003819 if (s->req->l > s->req->send_max) {
3820 if (s->rep->send_max &&
3821 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003822 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3823 s->rep->flags |= BF_EXPECT_MORE;
3824 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003825
3826 /* we're removing the analysers, we MUST re-enable events detection */
3827 buffer_auto_read(s->req);
3828 buffer_auto_close(s->req);
3829 buffer_auto_read(s->rep);
3830 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831
3832 /* make ->lr point to the first non-forwarded byte */
3833 s->req->lr = s->req->w + s->req->send_max;
3834 if (s->req->lr >= s->req->data + s->req->size)
3835 s->req->lr -= s->req->size;
3836 s->rep->lr = s->rep->w + s->rep->send_max;
3837 if (s->rep->lr >= s->rep->data + s->rep->size)
3838 s->rep->lr -= s->req->size;
3839
Willy Tarreau342b11c2010-11-24 16:22:09 +01003840 s->req->analysers = s->listener->analysers;
3841 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003842 s->rep->analysers = 0;
3843
3844 http_silent_debug(__LINE__, s);
3845}
3846
3847
3848/* This function updates the request state machine according to the response
3849 * state machine and buffer flags. It returns 1 if it changes anything (flag
3850 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3851 * it is only used to find when a request/response couple is complete. Both
3852 * this function and its equivalent should loop until both return zero. It
3853 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3854 */
3855int http_sync_req_state(struct session *s)
3856{
3857 struct buffer *buf = s->req;
3858 struct http_txn *txn = &s->txn;
3859 unsigned int old_flags = buf->flags;
3860 unsigned int old_state = txn->req.msg_state;
3861
3862 http_silent_debug(__LINE__, s);
3863 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3864 return 0;
3865
3866 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003867 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003868 * We can shut the read side unless we want to abort_on_close,
3869 * or we have a POST request. The issue with POST requests is
3870 * that some browsers still send a CRLF after the request, and
3871 * this CRLF must be read so that it does not remain in the kernel
3872 * buffers, otherwise a close could cause an RST on some systems
3873 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003874 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003875 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003876 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877
3878 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3879 goto wait_other_side;
3880
3881 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3882 /* The server has not finished to respond, so we
3883 * don't want to move in order not to upset it.
3884 */
3885 goto wait_other_side;
3886 }
3887
3888 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3889 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003890 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 txn->req.msg_state = HTTP_MSG_TUNNEL;
3892 goto wait_other_side;
3893 }
3894
3895 /* When we get here, it means that both the request and the
3896 * response have finished receiving. Depending on the connection
3897 * mode, we'll have to wait for the last bytes to leave in either
3898 * direction, and sometimes for a close to be effective.
3899 */
3900
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003901 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3902 /* Server-close mode : queue a connection close to the server */
3903 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003905 }
3906 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3907 /* Option forceclose is set, or either side wants to close,
3908 * let's enforce it now that we're not expecting any new
3909 * data to come. The caller knows the session is complete
3910 * once both states are CLOSED.
3911 */
3912 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913 buffer_shutr_now(buf);
3914 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003916 }
3917 else {
3918 /* The last possible modes are keep-alive and tunnel. Since tunnel
3919 * mode does not set the body analyser, we can't reach this place
3920 * in tunnel mode, so we're left with keep-alive only.
3921 * This mode is currently not implemented, we switch to tunnel mode.
3922 */
3923 buffer_auto_read(buf);
3924 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003925 }
3926
3927 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3928 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003929 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3930
Willy Tarreau610ecce2010-01-04 21:15:02 +01003931 if (!(buf->flags & BF_OUT_EMPTY)) {
3932 txn->req.msg_state = HTTP_MSG_CLOSING;
3933 goto http_msg_closing;
3934 }
3935 else {
3936 txn->req.msg_state = HTTP_MSG_CLOSED;
3937 goto http_msg_closed;
3938 }
3939 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003940 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003941 }
3942
3943 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3944 http_msg_closing:
3945 /* nothing else to forward, just waiting for the output buffer
3946 * to be empty and for the shutw_now to take effect.
3947 */
3948 if (buf->flags & BF_OUT_EMPTY) {
3949 txn->req.msg_state = HTTP_MSG_CLOSED;
3950 goto http_msg_closed;
3951 }
3952 else if (buf->flags & BF_SHUTW) {
3953 txn->req.msg_state = HTTP_MSG_ERROR;
3954 goto wait_other_side;
3955 }
3956 }
3957
3958 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3959 http_msg_closed:
3960 goto wait_other_side;
3961 }
3962
3963 wait_other_side:
3964 http_silent_debug(__LINE__, s);
3965 return txn->req.msg_state != old_state || buf->flags != old_flags;
3966}
3967
3968
3969/* This function updates the response state machine according to the request
3970 * state machine and buffer flags. It returns 1 if it changes anything (flag
3971 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3972 * it is only used to find when a request/response couple is complete. Both
3973 * this function and its equivalent should loop until both return zero. It
3974 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3975 */
3976int http_sync_res_state(struct session *s)
3977{
3978 struct buffer *buf = s->rep;
3979 struct http_txn *txn = &s->txn;
3980 unsigned int old_flags = buf->flags;
3981 unsigned int old_state = txn->rsp.msg_state;
3982
3983 http_silent_debug(__LINE__, s);
3984 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3985 return 0;
3986
3987 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3988 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003989 * still monitor the server connection for a possible close
3990 * while the request is being uploaded, so we don't disable
3991 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003993 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003994
3995 if (txn->req.msg_state == HTTP_MSG_ERROR)
3996 goto wait_other_side;
3997
3998 if (txn->req.msg_state < HTTP_MSG_DONE) {
3999 /* The client seems to still be sending data, probably
4000 * because we got an error response during an upload.
4001 * We have the choice of either breaking the connection
4002 * or letting it pass through. Let's do the later.
4003 */
4004 goto wait_other_side;
4005 }
4006
4007 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4008 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004009 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4011 goto wait_other_side;
4012 }
4013
4014 /* When we get here, it means that both the request and the
4015 * response have finished receiving. Depending on the connection
4016 * mode, we'll have to wait for the last bytes to leave in either
4017 * direction, and sometimes for a close to be effective.
4018 */
4019
4020 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4021 /* Server-close mode : shut read and wait for the request
4022 * side to close its output buffer. The caller will detect
4023 * when we're in DONE and the other is in CLOSED and will
4024 * catch that for the final cleanup.
4025 */
4026 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4027 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004029 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4030 /* Option forceclose is set, or either side wants to close,
4031 * let's enforce it now that we're not expecting any new
4032 * data to come. The caller knows the session is complete
4033 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004035 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4036 buffer_shutr_now(buf);
4037 buffer_shutw_now(buf);
4038 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 }
4040 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004041 /* The last possible modes are keep-alive and tunnel. Since tunnel
4042 * mode does not set the body analyser, we can't reach this place
4043 * in tunnel mode, so we're left with keep-alive only.
4044 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004045 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004046 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 }
4049
4050 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4051 /* if we've just closed an output, let's switch */
4052 if (!(buf->flags & BF_OUT_EMPTY)) {
4053 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4054 goto http_msg_closing;
4055 }
4056 else {
4057 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4058 goto http_msg_closed;
4059 }
4060 }
4061 goto wait_other_side;
4062 }
4063
4064 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4065 http_msg_closing:
4066 /* nothing else to forward, just waiting for the output buffer
4067 * to be empty and for the shutw_now to take effect.
4068 */
4069 if (buf->flags & BF_OUT_EMPTY) {
4070 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4071 goto http_msg_closed;
4072 }
4073 else if (buf->flags & BF_SHUTW) {
4074 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004075 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004076 if (target_srv(&s->target))
4077 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 goto wait_other_side;
4079 }
4080 }
4081
4082 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4083 http_msg_closed:
4084 /* drop any pending data */
4085 buffer_ignore(buf, buf->l - buf->send_max);
4086 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004087 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 goto wait_other_side;
4089 }
4090
4091 wait_other_side:
4092 http_silent_debug(__LINE__, s);
4093 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4094}
4095
4096
4097/* Resync the request and response state machines. Return 1 if either state
4098 * changes.
4099 */
4100int http_resync_states(struct session *s)
4101{
4102 struct http_txn *txn = &s->txn;
4103 int old_req_state = txn->req.msg_state;
4104 int old_res_state = txn->rsp.msg_state;
4105
4106 http_silent_debug(__LINE__, s);
4107 http_sync_req_state(s);
4108 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004109 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 if (!http_sync_res_state(s))
4111 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 if (!http_sync_req_state(s))
4114 break;
4115 }
4116 http_silent_debug(__LINE__, s);
4117 /* OK, both state machines agree on a compatible state.
4118 * There are a few cases we're interested in :
4119 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4120 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4121 * directions, so let's simply disable both analysers.
4122 * - HTTP_MSG_CLOSED on the response only means we must abort the
4123 * request.
4124 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4125 * with server-close mode means we've completed one request and we
4126 * must re-initialize the server connection.
4127 */
4128
4129 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4130 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4131 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4132 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4133 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004134 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004135 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004136 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004137 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004138 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004140 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4141 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004142 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004143 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004144 s->rep->analysers = 0;
4145 buffer_auto_close(s->rep);
4146 buffer_auto_read(s->rep);
4147 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 buffer_abort(s->req);
4149 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004150 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004151 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 }
4153 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4154 txn->rsp.msg_state == HTTP_MSG_DONE &&
4155 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4156 /* server-close: terminate this server connection and
4157 * reinitialize a fresh-new transaction.
4158 */
4159 http_end_txn_clean_session(s);
4160 }
4161
4162 http_silent_debug(__LINE__, s);
4163 return txn->req.msg_state != old_req_state ||
4164 txn->rsp.msg_state != old_res_state;
4165}
4166
Willy Tarreaud98cf932009-12-27 22:54:55 +01004167/* This function is an analyser which forwards request body (including chunk
4168 * sizes if any). It is called as soon as we must forward, even if we forward
4169 * zero byte. The only situation where it must not be called is when we're in
4170 * tunnel mode and we want to forward till the close. It's used both to forward
4171 * remaining data and to resync after end of body. It expects the msg_state to
4172 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4173 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004174 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004175 * bytes of pending data + the headers if not already done (between som and sov).
4176 * It eventually adjusts som to match sov after the data in between have been sent.
4177 */
4178int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4179{
4180 struct http_txn *txn = &s->txn;
4181 struct http_msg *msg = &s->txn.req;
4182
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004183 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4184 return 0;
4185
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004186 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4187 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004188 /* Output closed while we were sending data. We must abort and
4189 * wake the other side up.
4190 */
4191 msg->msg_state = HTTP_MSG_ERROR;
4192 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004193 return 1;
4194 }
4195
Willy Tarreau4fe41902010-06-07 22:27:41 +02004196 /* in most states, we should abort in case of early close */
4197 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004198
4199 /* Note that we don't have to send 100-continue back because we don't
4200 * need the data to complete our job, and it's up to the server to
4201 * decide whether to return 100, 417 or anything else in return of
4202 * an "Expect: 100-continue" header.
4203 */
4204
4205 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4206 /* we have msg->col and msg->sov which both point to the first
4207 * byte of message body. msg->som still points to the beginning
4208 * of the message. We must save the body in req->lr because it
4209 * survives buffer re-alignments.
4210 */
4211 req->lr = req->data + msg->sov;
4212 if (txn->flags & TX_REQ_TE_CHNK)
4213 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4214 else {
4215 msg->msg_state = HTTP_MSG_DATA;
4216 }
4217 }
4218
Willy Tarreaud98cf932009-12-27 22:54:55 +01004219 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004220 int bytes;
4221
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004223 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004224 bytes = msg->sov - msg->som;
4225 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004226 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004227 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4228 bytes += req->size;
4229 msg->chunk_len += (unsigned int)bytes;
4230 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004231 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004232
Willy Tarreaucaabe412010-01-03 23:08:28 +01004233 if (msg->msg_state == HTTP_MSG_DATA) {
4234 /* must still forward */
4235 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004236 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004237
4238 /* nothing left to forward */
4239 if (txn->flags & TX_REQ_TE_CHNK)
4240 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004242 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004243 }
4244 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004245 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 * set ->sov and ->lr to point to the body and switch to DATA or
4247 * TRAILERS state.
4248 */
4249 int ret = http_parse_chunk_size(req, msg);
4250
4251 if (!ret)
4252 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004253 else if (ret < 0) {
4254 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004255 if (msg->err_pos >= 0)
4256 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004257 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004258 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004260 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4262 /* we want the CRLF after the data */
4263 int ret;
4264
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004265 req->lr = req->w + req->send_max;
4266 if (req->lr >= req->data + req->size)
4267 req->lr -= req->size;
4268
Willy Tarreaud98cf932009-12-27 22:54:55 +01004269 ret = http_skip_chunk_crlf(req, msg);
4270
4271 if (ret == 0)
4272 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004273 else if (ret < 0) {
4274 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004275 if (msg->err_pos >= 0)
4276 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004277 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004278 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004279 /* we're in MSG_CHUNK_SIZE now */
4280 }
4281 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4282 int ret = http_forward_trailers(req, msg);
4283
4284 if (ret == 0)
4285 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004286 else if (ret < 0) {
4287 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004288 if (msg->err_pos >= 0)
4289 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004290 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004291 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004292 /* we're in HTTP_MSG_DONE now */
4293 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004295 int old_state = msg->msg_state;
4296
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004298 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004299 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4300 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4301 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 if (http_resync_states(s)) {
4303 /* some state changes occurred, maybe the analyser
4304 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004305 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004306 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4307 if (req->flags & BF_SHUTW) {
4308 /* request errors are most likely due to
4309 * the server aborting the transfer.
4310 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004311 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004312 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004313 if (msg->err_pos >= 0)
4314 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004316 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004318 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004319
4320 /* If "option abortonclose" is set on the backend, we
4321 * want to monitor the client's connection and forward
4322 * any shutdown notification to the server, which will
4323 * decide whether to close or to go on processing the
4324 * request.
4325 */
4326 if (s->be->options & PR_O_ABRT_CLOSE) {
4327 buffer_auto_read(req);
4328 buffer_auto_close(req);
4329 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004330 else if (s->txn.meth == HTTP_METH_POST) {
4331 /* POST requests may require to read extra CRLF
4332 * sent by broken browsers and which could cause
4333 * an RST to be sent upon close on some systems
4334 * (eg: Linux).
4335 */
4336 buffer_auto_read(req);
4337 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004338
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004340 }
4341 }
4342
Willy Tarreaud98cf932009-12-27 22:54:55 +01004343 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004344 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004345 if (req->flags & BF_SHUTR) {
4346 if (!(s->flags & SN_ERR_MASK))
4347 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004348 if (!(s->flags & SN_FINST_MASK)) {
4349 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4350 s->flags |= SN_FINST_H;
4351 else
4352 s->flags |= SN_FINST_D;
4353 }
4354
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004355 s->fe->fe_counters.cli_aborts++;
4356 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004357 if (target_srv(&s->target))
4358 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004359
4360 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004361 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004362
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004363 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364 if (req->flags & BF_SHUTW)
4365 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004367 /* When TE: chunked is used, we need to get there again to parse remaining
4368 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4369 */
4370 if (txn->flags & TX_REQ_TE_CHNK)
4371 buffer_dont_close(req);
4372
Willy Tarreau5c620922011-05-11 19:56:11 +02004373 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004374 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4375 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004376 * modes are already handled by the stream sock layer. We must not do
4377 * this in content-length mode because it could present the MSG_MORE
4378 * flag with the last block of forwarded data, which would cause an
4379 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004380 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004381 if (txn->flags & TX_REQ_TE_CHNK)
4382 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004383
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004385 return 0;
4386
Willy Tarreaud98cf932009-12-27 22:54:55 +01004387 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004388 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004389 if (s->listener->counters)
4390 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004391 return_bad_req_stats_ok:
4392 txn->req.msg_state = HTTP_MSG_ERROR;
4393 if (txn->status) {
4394 /* Note: we don't send any error if some data were already sent */
4395 stream_int_retnclose(req->prod, NULL);
4396 } else {
4397 txn->status = 400;
4398 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4399 }
4400 req->analysers = 0;
4401 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004402
4403 if (!(s->flags & SN_ERR_MASK))
4404 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004405 if (!(s->flags & SN_FINST_MASK)) {
4406 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4407 s->flags |= SN_FINST_H;
4408 else
4409 s->flags |= SN_FINST_D;
4410 }
4411 return 0;
4412
4413 aborted_xfer:
4414 txn->req.msg_state = HTTP_MSG_ERROR;
4415 if (txn->status) {
4416 /* Note: we don't send any error if some data were already sent */
4417 stream_int_retnclose(req->prod, NULL);
4418 } else {
4419 txn->status = 502;
4420 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4421 }
4422 req->analysers = 0;
4423 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4424
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004425 s->fe->fe_counters.srv_aborts++;
4426 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004427 if (target_srv(&s->target))
4428 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004429
4430 if (!(s->flags & SN_ERR_MASK))
4431 s->flags |= SN_ERR_SRVCL;
4432 if (!(s->flags & SN_FINST_MASK)) {
4433 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4434 s->flags |= SN_FINST_H;
4435 else
4436 s->flags |= SN_FINST_D;
4437 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004438 return 0;
4439}
4440
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4442 * processing can continue on next analysers, or zero if it either needs more
4443 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4444 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4445 * when it has nothing left to do, and may remove any analyser when it wants to
4446 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004447 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004448int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004449{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 struct http_txn *txn = &s->txn;
4451 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004452 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004453 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004455 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004456
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004457 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004458 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004460 rep,
4461 rep->rex, rep->wex,
4462 rep->flags,
4463 rep->l,
4464 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004465
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004466 /*
4467 * Now parse the partial (or complete) lines.
4468 * We will check the response syntax, and also join multi-line
4469 * headers. An index of all the lines will be elaborated while
4470 * parsing.
4471 *
4472 * For the parsing, we use a 28 states FSM.
4473 *
4474 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004475 * rep->data + msg->som = beginning of response
4476 * rep->data + msg->eoh = end of processed headers / start of current one
4477 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 * rep->lr = first non-visited byte
4479 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004480 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004481 */
4482
Willy Tarreau83e3af02009-12-28 17:39:57 +01004483 /* There's a protected area at the end of the buffer for rewriting
4484 * purposes. We don't want to start to parse the request if the
4485 * protected area is affected, because we may have to move processed
4486 * data later, which is much more complicated.
4487 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004488 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4489 if (unlikely((rep->flags & BF_FULL) ||
4490 rep->r < rep->lr ||
4491 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4492 if (rep->send_max) {
4493 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004494 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4495 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004496 buffer_dont_close(rep);
4497 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4498 return 0;
4499 }
4500 if (rep->l <= rep->size - global.tune.maxrewrite)
4501 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004502 }
4503
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004504 if (likely(rep->lr < rep->r))
4505 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004506 }
4507
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004508 /* 1: we might have to print this header in debug mode */
4509 if (unlikely((global.mode & MODE_DEBUG) &&
4510 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004511 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004512 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004514
Willy Tarreau663308b2010-06-07 14:06:08 +02004515 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004516 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004517 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004518
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 sol += hdr_idx_first_pos(&txn->hdr_idx);
4520 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004521
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522 while (cur_idx) {
4523 eol = sol + txn->hdr_idx.v[cur_idx].len;
4524 debug_hdr("srvhdr", s, sol, eol);
4525 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4526 cur_idx = txn->hdr_idx.v[cur_idx].next;
4527 }
4528 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 /*
4531 * Now we quickly check if we have found a full valid response.
4532 * If not so, we check the FD and buffer states before leaving.
4533 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004534 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 * responses are checked first.
4536 *
4537 * Depending on whether the client is still there or not, we
4538 * may send an error response back or not. Note that normally
4539 * we should only check for HTTP status there, and check I/O
4540 * errors somewhere else.
4541 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004542
Willy Tarreau655dce92009-11-08 13:10:58 +01004543 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 /* Invalid response */
4545 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4546 /* we detected a parsing error. We want to archive this response
4547 * in the dedicated proxy area for later troubleshooting.
4548 */
4549 hdr_response_bad:
4550 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004551 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004552
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004553 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004554 if (target_srv(&s->target)) {
4555 target_srv(&s->target)->counters.failed_resp++;
4556 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004557 }
Willy Tarreau64648412010-03-05 10:41:54 +01004558 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004559 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 rep->analysers = 0;
4561 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004562 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004563 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4565
4566 if (!(s->flags & SN_ERR_MASK))
4567 s->flags |= SN_ERR_PRXCOND;
4568 if (!(s->flags & SN_FINST_MASK))
4569 s->flags |= SN_FINST_H;
4570
4571 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004572 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004573
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 /* too large response does not fit in buffer. */
4575 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004576 if (msg->err_pos < 0)
4577 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004578 goto hdr_response_bad;
4579 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 /* read error */
4582 else if (rep->flags & BF_READ_ERROR) {
4583 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004584 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004585
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004586 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004587 if (target_srv(&s->target)) {
4588 target_srv(&s->target)->counters.failed_resp++;
4589 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004590 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004591
Willy Tarreau90deb182010-01-07 00:20:41 +01004592 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 rep->analysers = 0;
4594 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004595 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004596 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004598
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 if (!(s->flags & SN_ERR_MASK))
4600 s->flags |= SN_ERR_SRVCL;
4601 if (!(s->flags & SN_FINST_MASK))
4602 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004603 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004604 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004605
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 /* read timeout : return a 504 to the client. */
4607 else if (rep->flags & BF_READ_TIMEOUT) {
4608 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004609 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004610
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004611 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004612 if (target_srv(&s->target)) {
4613 target_srv(&s->target)->counters.failed_resp++;
4614 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004615 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004616
Willy Tarreau90deb182010-01-07 00:20:41 +01004617 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 rep->analysers = 0;
4619 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004620 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004621 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004622 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 if (!(s->flags & SN_ERR_MASK))
4625 s->flags |= SN_ERR_SRVTO;
4626 if (!(s->flags & SN_FINST_MASK))
4627 s->flags |= SN_FINST_H;
4628 return 0;
4629 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004630
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004631 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004633 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004634 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004635
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004636 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004637 if (target_srv(&s->target)) {
4638 target_srv(&s->target)->counters.failed_resp++;
4639 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004640 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004641
Willy Tarreau90deb182010-01-07 00:20:41 +01004642 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 rep->analysers = 0;
4644 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004645 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004646 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 if (!(s->flags & SN_ERR_MASK))
4650 s->flags |= SN_ERR_SRVCL;
4651 if (!(s->flags & SN_FINST_MASK))
4652 s->flags |= SN_FINST_H;
4653 return 0;
4654 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004655
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 /* write error to client (we don't send any message then) */
4657 else if (rep->flags & BF_WRITE_ERROR) {
4658 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004659 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004660
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004661 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004663 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664
4665 if (!(s->flags & SN_ERR_MASK))
4666 s->flags |= SN_ERR_CLICL;
4667 if (!(s->flags & SN_FINST_MASK))
4668 s->flags |= SN_FINST_H;
4669
4670 /* process_session() will take care of the error */
4671 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004672 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004673
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004674 buffer_dont_close(rep);
4675 return 0;
4676 }
4677
4678 /* More interesting part now : we know that we have a complete
4679 * response which at least looks like HTTP. We have an indicator
4680 * of each header's length, so we can parse them quickly.
4681 */
4682
4683 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004684 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004685
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004686 /*
4687 * 1: get the status code
4688 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004689 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690 if (n < 1 || n > 5)
4691 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004692 /* when the client triggers a 4xx from the server, it's most often due
4693 * to a missing object or permission. These events should be tracked
4694 * because if they happen often, it may indicate a brute force or a
4695 * vulnerability scan.
4696 */
4697 if (n == 4)
4698 session_inc_http_err_ctr(s);
4699
Willy Tarreau827aee92011-03-10 16:55:02 +01004700 if (target_srv(&s->target))
4701 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702
Willy Tarreau5b154472009-12-21 20:11:07 +01004703 /* check if the response is HTTP/1.1 or above */
4704 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004705 ((msg->sol[5] > '1') ||
4706 ((msg->sol[5] == '1') &&
4707 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004708 txn->flags |= TX_RES_VER_11;
4709
4710 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004711 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 +01004712
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004713 /* transfer length unknown*/
4714 txn->flags &= ~TX_RES_XFER_LEN;
4715
Willy Tarreau962c3f42010-01-10 00:15:35 +01004716 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717
Willy Tarreau39650402010-03-15 19:44:39 +01004718 /* Adjust server's health based on status code. Note: status codes 501
4719 * and 505 are triggered on demand by client request, so we must not
4720 * count them as server failures.
4721 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004722 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004723 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004724 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004725 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004726 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004727 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004728
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 /*
4730 * 2: check for cacheability.
4731 */
4732
4733 switch (txn->status) {
4734 case 200:
4735 case 203:
4736 case 206:
4737 case 300:
4738 case 301:
4739 case 410:
4740 /* RFC2616 @13.4:
4741 * "A response received with a status code of
4742 * 200, 203, 206, 300, 301 or 410 MAY be stored
4743 * by a cache (...) unless a cache-control
4744 * directive prohibits caching."
4745 *
4746 * RFC2616 @9.5: POST method :
4747 * "Responses to this method are not cacheable,
4748 * unless the response includes appropriate
4749 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004750 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 if (likely(txn->meth != HTTP_METH_POST) &&
4752 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4753 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4754 break;
4755 default:
4756 break;
4757 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004758
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759 /*
4760 * 3: we may need to capture headers
4761 */
4762 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004763 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004764 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 txn->rsp.cap, s->fe->rsp_cap);
4766
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004767 /* 4: determine the transfer-length.
4768 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4769 * the presence of a message-body in a RESPONSE and its transfer length
4770 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004771 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004772 * All responses to the HEAD request method MUST NOT include a
4773 * message-body, even though the presence of entity-header fields
4774 * might lead one to believe they do. All 1xx (informational), 204
4775 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4776 * message-body. All other responses do include a message-body,
4777 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004778 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004779 * 1. Any response which "MUST NOT" include a message-body (such as the
4780 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4781 * always terminated by the first empty line after the header fields,
4782 * regardless of the entity-header fields present in the message.
4783 *
4784 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4785 * the "chunked" transfer-coding (Section 6.2) is used, the
4786 * transfer-length is defined by the use of this transfer-coding.
4787 * If a Transfer-Encoding header field is present and the "chunked"
4788 * transfer-coding is not present, the transfer-length is defined by
4789 * the sender closing the connection.
4790 *
4791 * 3. If a Content-Length header field is present, its decimal value in
4792 * OCTETs represents both the entity-length and the transfer-length.
4793 * If a message is received with both a Transfer-Encoding header
4794 * field and a Content-Length header field, the latter MUST be ignored.
4795 *
4796 * 4. If the message uses the media type "multipart/byteranges", and
4797 * the transfer-length is not otherwise specified, then this self-
4798 * delimiting media type defines the transfer-length. This media
4799 * type MUST NOT be used unless the sender knows that the recipient
4800 * can parse it; the presence in a request of a Range header with
4801 * multiple byte-range specifiers from a 1.1 client implies that the
4802 * client can parse multipart/byteranges responses.
4803 *
4804 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004805 */
4806
4807 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004808 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004809 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004810 * FIXME: should we parse anyway and return an error on chunked encoding ?
4811 */
4812 if (txn->meth == HTTP_METH_HEAD ||
4813 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004814 txn->status == 204 || txn->status == 304) {
4815 txn->flags |= TX_RES_XFER_LEN;
4816 goto skip_content_length;
4817 }
4818
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004819 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004820 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004821 while ((txn->flags & TX_RES_VER_11) &&
4822 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004823 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4824 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4825 else if (txn->flags & TX_RES_TE_CHNK) {
4826 /* bad transfer-encoding (chunked followed by something else) */
4827 use_close_only = 1;
4828 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4829 break;
4830 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004831 }
4832
4833 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4834 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004835 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4837 signed long long cl;
4838
Willy Tarreauad14f752011-09-02 20:33:27 +02004839 if (!ctx.vlen) {
4840 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004841 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004842 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004843
Willy Tarreauad14f752011-09-02 20:33:27 +02004844 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4845 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004846 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004847 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004848
Willy Tarreauad14f752011-09-02 20:33:27 +02004849 if (cl < 0) {
4850 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004851 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004852 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004853
Willy Tarreauad14f752011-09-02 20:33:27 +02004854 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
4855 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004856 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004857 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004858
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004859 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004860 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004861 }
4862
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004863 /* FIXME: we should also implement the multipart/byterange method.
4864 * For now on, we resort to close mode in this case (unknown length).
4865 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004866skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004867
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868 /* end of job, return OK */
4869 rep->analysers &= ~an_bit;
4870 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004871 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004872 return 1;
4873}
4874
4875/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004876 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4877 * and updates t->rep->analysers. It might make sense to explode it into several
4878 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004879 */
4880int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4881{
4882 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883 struct http_msg *msg = &txn->rsp;
4884 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004885 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004886
4887 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4888 now_ms, __FUNCTION__,
4889 t,
4890 rep,
4891 rep->rex, rep->wex,
4892 rep->flags,
4893 rep->l,
4894 rep->analysers);
4895
Willy Tarreau655dce92009-11-08 13:10:58 +01004896 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004897 return 0;
4898
4899 rep->analysers &= ~an_bit;
4900 rep->analyse_exp = TICK_ETERNITY;
4901
Willy Tarreau5b154472009-12-21 20:11:07 +01004902 /* Now we have to check if we need to modify the Connection header.
4903 * This is more difficult on the response than it is on the request,
4904 * because we can have two different HTTP versions and we don't know
4905 * how the client will interprete a response. For instance, let's say
4906 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4907 * HTTP/1.1 response without any header. Maybe it will bound itself to
4908 * HTTP/1.0 because it only knows about it, and will consider the lack
4909 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4910 * the lack of header as a keep-alive. Thus we will use two flags
4911 * indicating how a request MAY be understood by the client. In case
4912 * of multiple possibilities, we'll fix the header to be explicit. If
4913 * ambiguous cases such as both close and keepalive are seen, then we
4914 * will fall back to explicit close. Note that we won't take risks with
4915 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004916 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004917 */
4918
Willy Tarreaudc008c52010-02-01 16:20:08 +01004919 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4920 txn->status == 101)) {
4921 /* Either we've established an explicit tunnel, or we're
4922 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004923 * to understand the next protocols. We have to switch to tunnel
4924 * mode, so that we transfer the request and responses then let
4925 * this protocol pass unmodified. When we later implement specific
4926 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004927 * header which contains information about that protocol for
4928 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004929 */
4930 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4931 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004932 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4933 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4934 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004935 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004936
Willy Tarreau60466522010-01-18 19:08:45 +01004937 /* on unknown transfer length, we must close */
4938 if (!(txn->flags & TX_RES_XFER_LEN) &&
4939 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4940 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004941
Willy Tarreau60466522010-01-18 19:08:45 +01004942 /* now adjust header transformations depending on current state */
4943 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4944 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4945 to_del |= 2; /* remove "keep-alive" on any response */
4946 if (!(txn->flags & TX_RES_VER_11))
4947 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004948 }
Willy Tarreau60466522010-01-18 19:08:45 +01004949 else { /* SCL / KAL */
4950 to_del |= 1; /* remove "close" on any response */
4951 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4952 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004953 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004954
Willy Tarreau60466522010-01-18 19:08:45 +01004955 /* Parse and remove some headers from the connection header */
4956 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004957
Willy Tarreau60466522010-01-18 19:08:45 +01004958 /* Some keep-alive responses are converted to Server-close if
4959 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004960 */
Willy Tarreau60466522010-01-18 19:08:45 +01004961 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4962 if ((txn->flags & TX_HDR_CONN_CLO) ||
4963 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4964 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004965 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004966 }
4967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004968 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004969 /*
4970 * 3: we will have to evaluate the filters.
4971 * As opposed to version 1.2, now they will be evaluated in the
4972 * filters order and not in the header order. This means that
4973 * each filter has to be validated among all headers.
4974 *
4975 * Filters are tried with ->be first, then with ->fe if it is
4976 * different from ->be.
4977 */
4978
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979 cur_proxy = t->be;
4980 while (1) {
4981 struct proxy *rule_set = cur_proxy;
4982
4983 /* try headers filters */
4984 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004985 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004987 if (target_srv(&t->target)) {
4988 target_srv(&t->target)->counters.failed_resp++;
4989 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004990 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004991 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004992 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004993 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004994 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004995 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004996 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004997 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 if (!(t->flags & SN_ERR_MASK))
4999 t->flags |= SN_ERR_PRXCOND;
5000 if (!(t->flags & SN_FINST_MASK))
5001 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005002 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005003 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005004 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005005
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005006 /* has the response been denied ? */
5007 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005008 if (target_srv(&t->target))
5009 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005010
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005011 t->be->be_counters.denied_resp++;
5012 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005013 if (t->listener->counters)
5014 t->listener->counters->denied_resp++;
5015
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005016 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005017 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005018
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005019 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005020 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005021 if (txn->status < 200)
5022 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005023 if (wl->cond) {
5024 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5025 ret = acl_pass(ret);
5026 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5027 ret = !ret;
5028 if (!ret)
5029 continue;
5030 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005031 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005032 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005033 }
5034
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035 /* check whether we're already working on the frontend */
5036 if (cur_proxy == t->fe)
5037 break;
5038 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005039 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005042 * We may be facing a 100-continue response, in which case this
5043 * is not the right response, and we're waiting for the next one.
5044 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005045 * next one.
5046 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005047 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005048 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005049 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005050 msg->msg_state = HTTP_MSG_RPBEFORE;
5051 txn->status = 0;
5052 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5053 return 1;
5054 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005055 else if (unlikely(txn->status < 200))
5056 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005057
5058 /* we don't have any 1xx status code now */
5059
5060 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 * 4: check for server cookie.
5062 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005063 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5064 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005065 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005066
Willy Tarreaubaaee002006-06-26 02:48:02 +02005067
Willy Tarreaua15645d2007-03-18 16:22:39 +01005068 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005069 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005070 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005071 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005072 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005074 /*
5075 * 6: add server cookie in the response if needed
5076 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005077 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005078 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005079 (!(t->flags & SN_DIRECT) ||
5080 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5081 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5082 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5083 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005084 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5085 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005086 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005087 /* the server is known, it's not the one the client requested, or the
5088 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089 * insert a set-cookie here, except if we want to insert only on POST
5090 * requests and this one isn't. Note that servers which don't have cookies
5091 * (eg: some backup servers) will return a full cookie removal request.
5092 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005093 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005094 len = sprintf(trash,
5095 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5096 t->be->cookie_name);
5097 }
5098 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005099 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005100
5101 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5102 /* emit last_date, which is mandatory */
5103 trash[len++] = COOKIE_DELIM_DATE;
5104 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5105 if (t->be->cookie_maxlife) {
5106 /* emit first_date, which is either the original one or
5107 * the current date.
5108 */
5109 trash[len++] = COOKIE_DELIM_DATE;
5110 s30tob64(txn->cookie_first_date ?
5111 txn->cookie_first_date >> 2 :
5112 (date.tv_sec+3) >> 2, trash + len);
5113 len += 5;
5114 }
5115 }
5116 len += sprintf(trash + len, "; path=/");
5117 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005118
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 if (t->be->cookie_domain)
5120 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005123 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005124 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005125
Willy Tarreauf1348312010-10-07 15:54:11 +02005126 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005127 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005128 /* the server did not change, only the date was updated */
5129 txn->flags |= TX_SCK_UPDATED;
5130 else
5131 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005132
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005133 /* Here, we will tell an eventual cache on the client side that we don't
5134 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5135 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5136 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5137 */
5138 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5141
5142 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005143 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005145 }
5146 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005147
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 /*
5149 * 7: check if result will be cacheable with a cookie.
5150 * We'll block the response if security checks have caught
5151 * nasty things such as a cacheable cookie.
5152 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005153 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5154 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005155 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005156
5157 /* we're in presence of a cacheable response containing
5158 * a set-cookie header. We'll block it as requested by
5159 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005161 if (target_srv(&t->target))
5162 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005163
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005164 t->be->be_counters.denied_resp++;
5165 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005166 if (t->listener->counters)
5167 t->listener->counters->denied_resp++;
5168
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005170 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 send_log(t->be, LOG_ALERT,
5172 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005173 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 goto return_srv_prx_502;
5175 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005176
5177 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005178 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005179 */
Willy Tarreau60466522010-01-18 19:08:45 +01005180 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5181 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5182 unsigned int want_flags = 0;
5183
5184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5185 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5186 /* we want a keep-alive response here. Keep-alive header
5187 * required if either side is not 1.1.
5188 */
5189 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5190 want_flags |= TX_CON_KAL_SET;
5191 }
5192 else {
5193 /* we want a close response here. Close header required if
5194 * the server is 1.1, regardless of the client.
5195 */
5196 if (txn->flags & TX_RES_VER_11)
5197 want_flags |= TX_CON_CLO_SET;
5198 }
5199
5200 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5201 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005202 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005203
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005204 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005205 if ((txn->flags & TX_RES_XFER_LEN) ||
5206 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005208
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005209 /*************************************************************
5210 * OK, that's finished for the headers. We have done what we *
5211 * could. Let's switch to the DATA state. *
5212 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005215
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 /* if the user wants to log as soon as possible, without counting
5217 * bytes from the server, then this is the right moment. We have
5218 * to temporarily assign bytes_out to log what we currently have.
5219 */
5220 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5221 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5222 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005223 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005224 t->logs.bytes_out = 0;
5225 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005226
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005227 /* Note: we must not try to cheat by jumping directly to DATA,
5228 * otherwise we would not let the client side wake up.
5229 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005230
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005231 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005233 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005234}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005235
Willy Tarreaud98cf932009-12-27 22:54:55 +01005236/* This function is an analyser which forwards response body (including chunk
5237 * sizes if any). It is called as soon as we must forward, even if we forward
5238 * zero byte. The only situation where it must not be called is when we're in
5239 * tunnel mode and we want to forward till the close. It's used both to forward
5240 * remaining data and to resync after end of body. It expects the msg_state to
5241 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5242 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005243 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005244 * bytes of pending data + the headers if not already done (between som and sov).
5245 * It eventually adjusts som to match sov after the data in between have been sent.
5246 */
5247int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5248{
5249 struct http_txn *txn = &s->txn;
5250 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005251 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005253 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5254 return 0;
5255
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005256 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005257 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005258 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005259 /* Output closed while we were sending data. We must abort and
5260 * wake the other side up.
5261 */
5262 msg->msg_state = HTTP_MSG_ERROR;
5263 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005264 return 1;
5265 }
5266
Willy Tarreau4fe41902010-06-07 22:27:41 +02005267 /* in most states, we should abort in case of early close */
5268 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005269
Willy Tarreaud98cf932009-12-27 22:54:55 +01005270 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5271 /* we have msg->col and msg->sov which both point to the first
5272 * byte of message body. msg->som still points to the beginning
5273 * of the message. We must save the body in req->lr because it
5274 * survives buffer re-alignments.
5275 */
5276 res->lr = res->data + msg->sov;
5277 if (txn->flags & TX_RES_TE_CHNK)
5278 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5279 else {
5280 msg->msg_state = HTTP_MSG_DATA;
5281 }
5282 }
5283
Willy Tarreaud98cf932009-12-27 22:54:55 +01005284 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005285 int bytes;
5286
Willy Tarreau610ecce2010-01-04 21:15:02 +01005287 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005288 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005289 bytes = msg->sov - msg->som;
5290 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005291 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005292 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5293 bytes += res->size;
5294 msg->chunk_len += (unsigned int)bytes;
5295 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005296 }
5297
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005298
Willy Tarreaucaabe412010-01-03 23:08:28 +01005299 if (msg->msg_state == HTTP_MSG_DATA) {
5300 /* must still forward */
5301 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005302 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005303
5304 /* nothing left to forward */
5305 if (txn->flags & TX_RES_TE_CHNK)
5306 msg->msg_state = HTTP_MSG_DATA_CRLF;
5307 else
5308 msg->msg_state = HTTP_MSG_DONE;
5309 }
5310 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005311 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005312 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5313 */
5314 int ret = http_parse_chunk_size(res, msg);
5315
5316 if (!ret)
5317 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005318 else if (ret < 0) {
5319 if (msg->err_pos >= 0)
5320 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005322 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005324 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005325 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5326 /* we want the CRLF after the data */
5327 int ret;
5328
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005329 res->lr = res->w + res->send_max;
5330 if (res->lr >= res->data + res->size)
5331 res->lr -= res->size;
5332
Willy Tarreaud98cf932009-12-27 22:54:55 +01005333 ret = http_skip_chunk_crlf(res, msg);
5334
5335 if (!ret)
5336 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005337 else if (ret < 0) {
5338 if (msg->err_pos >= 0)
5339 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005341 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 /* we're in MSG_CHUNK_SIZE now */
5343 }
5344 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5345 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005346
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 if (ret == 0)
5348 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005349 else if (ret < 0) {
5350 if (msg->err_pos >= 0)
5351 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005353 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005354 /* we're in HTTP_MSG_DONE now */
5355 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005356 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005357 int old_state = msg->msg_state;
5358
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005360 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005361 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5362 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5363 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364 if (http_resync_states(s)) {
5365 http_silent_debug(__LINE__, s);
5366 /* some state changes occurred, maybe the analyser
5367 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005368 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005369 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5370 if (res->flags & BF_SHUTW) {
5371 /* response errors are most likely due to
5372 * the client aborting the transfer.
5373 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005374 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005375 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005376 if (msg->err_pos >= 0)
5377 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005379 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005381 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 }
5384 }
5385
Willy Tarreaud98cf932009-12-27 22:54:55 +01005386 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005387 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005388 if (res->flags & BF_SHUTR) {
5389 if (!(s->flags & SN_ERR_MASK))
5390 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005391 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005392 if (target_srv(&s->target))
5393 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005394 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005395 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005396
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005397 if (res->flags & BF_SHUTW)
5398 goto aborted_xfer;
5399
Willy Tarreau40dba092010-03-04 18:14:51 +01005400 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 if (!s->req->analysers)
5402 goto return_bad_res;
5403
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005404 /* forward any pending data */
5405 bytes = msg->sov - msg->som;
5406 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005408 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5409 bytes += res->size;
5410 msg->chunk_len += (unsigned int)bytes;
5411 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 }
5413
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005414 /* When TE: chunked is used, we need to get there again to parse remaining
5415 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5416 * Similarly, with keep-alive on the client side, we don't want to forward a
5417 * close.
5418 */
5419 if ((txn->flags & TX_RES_TE_CHNK) ||
5420 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5421 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5422 buffer_dont_close(res);
5423
Willy Tarreau5c620922011-05-11 19:56:11 +02005424 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005425 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5426 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005427 * modes are already handled by the stream sock layer. We must not do
5428 * this in content-length mode because it could present the MSG_MORE
5429 * flag with the last block of forwarded data, which would cause an
5430 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005431 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005432 if (txn->flags & TX_RES_TE_CHNK)
5433 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005434
Willy Tarreaud98cf932009-12-27 22:54:55 +01005435 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005437 return 0;
5438
Willy Tarreau40dba092010-03-04 18:14:51 +01005439 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005440 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005441 if (target_srv(&s->target))
5442 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005443
5444 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005446 /* don't send any error message as we're in the body */
5447 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005449 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005450 if (target_srv(&s->target))
5451 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452
5453 if (!(s->flags & SN_ERR_MASK))
5454 s->flags |= SN_ERR_PRXCOND;
5455 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005456 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005457 return 0;
5458
5459 aborted_xfer:
5460 txn->rsp.msg_state = HTTP_MSG_ERROR;
5461 /* don't send any error message as we're in the body */
5462 stream_int_retnclose(res->cons, NULL);
5463 res->analysers = 0;
5464 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5465
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005466 s->fe->fe_counters.cli_aborts++;
5467 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005468 if (target_srv(&s->target))
5469 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005470
5471 if (!(s->flags & SN_ERR_MASK))
5472 s->flags |= SN_ERR_CLICL;
5473 if (!(s->flags & SN_FINST_MASK))
5474 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005475 return 0;
5476}
5477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005478/* Iterate the same filter through all request headers.
5479 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005480 * Since it can manage the switch to another backend, it updates the per-proxy
5481 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005482 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005483int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005484{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005485 char term;
5486 char *cur_ptr, *cur_end, *cur_next;
5487 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005488 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005489 struct hdr_idx_elem *cur_hdr;
5490 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 last_hdr = 0;
5493
Willy Tarreau962c3f42010-01-10 00:15:35 +01005494 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005495 old_idx = 0;
5496
5497 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005498 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005499 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005500 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005501 (exp->action == ACT_ALLOW ||
5502 exp->action == ACT_DENY ||
5503 exp->action == ACT_TARPIT))
5504 return 0;
5505
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005506 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 if (!cur_idx)
5508 break;
5509
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005510 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005511 cur_ptr = cur_next;
5512 cur_end = cur_ptr + cur_hdr->len;
5513 cur_next = cur_end + cur_hdr->cr + 1;
5514
5515 /* Now we have one header between cur_ptr and cur_end,
5516 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005517 */
5518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005519 /* The annoying part is that pattern matching needs
5520 * that we modify the contents to null-terminate all
5521 * strings before testing them.
5522 */
5523
5524 term = *cur_end;
5525 *cur_end = '\0';
5526
5527 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5528 switch (exp->action) {
5529 case ACT_SETBE:
5530 /* It is not possible to jump a second time.
5531 * FIXME: should we return an HTTP/500 here so that
5532 * the admin knows there's a problem ?
5533 */
5534 if (t->be != t->fe)
5535 break;
5536
5537 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005538 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005539 last_hdr = 1;
5540 break;
5541
5542 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005543 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005544 last_hdr = 1;
5545 break;
5546
5547 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005548 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005549 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005550
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005551 t->fe->fe_counters.denied_req++;
5552 if (t->fe != t->be)
5553 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005554 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005555 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005557 break;
5558
5559 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005560 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005562
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005563 t->fe->fe_counters.denied_req++;
5564 if (t->fe != t->be)
5565 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005566 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005567 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005569 break;
5570
5571 case ACT_REPLACE:
5572 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5573 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5574 /* FIXME: if the user adds a newline in the replacement, the
5575 * index will not be recalculated for now, and the new line
5576 * will not be counted as a new header.
5577 */
5578
5579 cur_end += delta;
5580 cur_next += delta;
5581 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005582 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 break;
5584
5585 case ACT_REMOVE:
5586 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5587 cur_next += delta;
5588
Willy Tarreaufa355d42009-11-29 18:12:29 +01005589 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005590 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5591 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 cur_hdr->len = 0;
5593 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005594 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 break;
5596
5597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005598 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005599 if (cur_end)
5600 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005602 /* keep the link from this header to next one in case of later
5603 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005604 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005605 old_idx = cur_idx;
5606 }
5607 return 0;
5608}
5609
5610
5611/* Apply the filter to the request line.
5612 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5613 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005614 * Since it can manage the switch to another backend, it updates the per-proxy
5615 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 */
5617int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5618{
5619 char term;
5620 char *cur_ptr, *cur_end;
5621 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005622 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624
Willy Tarreau58f10d72006-12-04 02:26:12 +01005625
Willy Tarreau3d300592007-03-18 18:34:41 +01005626 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005628 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005629 (exp->action == ACT_ALLOW ||
5630 exp->action == ACT_DENY ||
5631 exp->action == ACT_TARPIT))
5632 return 0;
5633 else if (exp->action == ACT_REMOVE)
5634 return 0;
5635
5636 done = 0;
5637
Willy Tarreau962c3f42010-01-10 00:15:35 +01005638 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005639 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640
5641 /* Now we have the request line between cur_ptr and cur_end */
5642
5643 /* The annoying part is that pattern matching needs
5644 * that we modify the contents to null-terminate all
5645 * strings before testing them.
5646 */
5647
5648 term = *cur_end;
5649 *cur_end = '\0';
5650
5651 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5652 switch (exp->action) {
5653 case ACT_SETBE:
5654 /* It is not possible to jump a second time.
5655 * FIXME: should we return an HTTP/500 here so that
5656 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005657 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 if (t->be != t->fe)
5659 break;
5660
5661 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005662 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 done = 1;
5664 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005667 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 done = 1;
5669 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005672 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005673
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005674 t->fe->fe_counters.denied_req++;
5675 if (t->fe != t->be)
5676 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005677 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005678 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 done = 1;
5681 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005684 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005685
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005686 t->fe->fe_counters.denied_req++;
5687 if (t->fe != t->be)
5688 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005689 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005690 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005692 done = 1;
5693 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005694
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695 case ACT_REPLACE:
5696 *cur_end = term; /* restore the string terminator */
5697 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5698 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5699 /* FIXME: if the user adds a newline in the replacement, the
5700 * index will not be recalculated for now, and the new line
5701 * will not be counted as a new header.
5702 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005703
Willy Tarreaufa355d42009-11-29 18:12:29 +01005704 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005706 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 HTTP_MSG_RQMETH,
5708 cur_ptr, cur_end + 1,
5709 NULL, NULL);
5710 if (unlikely(!cur_end))
5711 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 /* we have a full request and we know that we have either a CR
5714 * or an LF at <ptr>.
5715 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005716 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5717 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005718 /* there is no point trying this regex on headers */
5719 return 1;
5720 }
5721 }
5722 *cur_end = term; /* restore the string terminator */
5723 return done;
5724}
Willy Tarreau97de6242006-12-27 17:18:38 +01005725
Willy Tarreau58f10d72006-12-04 02:26:12 +01005726
Willy Tarreau58f10d72006-12-04 02:26:12 +01005727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005729 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731 * unparsable request. Since it can manage the switch to another backend, it
5732 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005734int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005736 struct http_txn *txn = &s->txn;
5737 struct hdr_exp *exp;
5738
5739 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005742 /*
5743 * The interleaving of transformations and verdicts
5744 * makes it difficult to decide to continue or stop
5745 * the evaluation.
5746 */
5747
Willy Tarreau6c123b12010-01-28 20:22:06 +01005748 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5749 break;
5750
Willy Tarreau3d300592007-03-18 18:34:41 +01005751 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005753 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005754 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005755
5756 /* if this filter had a condition, evaluate it now and skip to
5757 * next filter if the condition does not match.
5758 */
5759 if (exp->cond) {
5760 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5761 ret = acl_pass(ret);
5762 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5763 ret = !ret;
5764
5765 if (!ret)
5766 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005767 }
5768
5769 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005770 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 if (unlikely(ret < 0))
5772 return -1;
5773
5774 if (likely(ret == 0)) {
5775 /* The filter did not match the request, it can be
5776 * iterated through all headers.
5777 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005778 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005779 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005780 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005781 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005782}
5783
5784
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785
Willy Tarreau58f10d72006-12-04 02:26:12 +01005786/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005787 * Try to retrieve the server associated to the appsession.
5788 * If the server is found, it's assigned to the session.
5789 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005790void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005791 struct http_txn *txn = &t->txn;
5792 appsess *asession = NULL;
5793 char *sessid_temp = NULL;
5794
Cyril Bontéb21570a2009-11-29 20:04:48 +01005795 if (len > t->be->appsession_len) {
5796 len = t->be->appsession_len;
5797 }
5798
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005799 if (t->be->options2 & PR_O2_AS_REQL) {
5800 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005801 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005802 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005803 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005804 }
5805
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005806 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005807 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5808 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5809 return;
5810 }
5811
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005812 memcpy(txn->sessid, buf, len);
5813 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005814 }
5815
5816 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5817 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5818 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5819 return;
5820 }
5821
Cyril Bontéb21570a2009-11-29 20:04:48 +01005822 memcpy(sessid_temp, buf, len);
5823 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005824
5825 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5826 /* free previously allocated memory */
5827 pool_free2(apools.sessid, sessid_temp);
5828
5829 if (asession != NULL) {
5830 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5831 if (!(t->be->options2 & PR_O2_AS_REQL))
5832 asession->request_count++;
5833
5834 if (asession->serverid != NULL) {
5835 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005836
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005837 while (srv) {
5838 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005839 if ((srv->state & SRV_RUNNING) ||
5840 (t->be->options & PR_O_PERSIST) ||
5841 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005842 /* we found the server and it's usable */
5843 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005844 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005845 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005846 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005847
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005848 break;
5849 } else {
5850 txn->flags &= ~TX_CK_MASK;
5851 txn->flags |= TX_CK_DOWN;
5852 }
5853 }
5854 srv = srv->next;
5855 }
5856 }
5857 }
5858}
5859
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005860/* Find the end of a cookie value contained between <s> and <e>. It works the
5861 * same way as with headers above except that the semi-colon also ends a token.
5862 * See RFC2965 for more information. Note that it requires a valid header to
5863 * return a valid result.
5864 */
5865char *find_cookie_value_end(char *s, const char *e)
5866{
5867 int quoted, qdpair;
5868
5869 quoted = qdpair = 0;
5870 for (; s < e; s++) {
5871 if (qdpair) qdpair = 0;
5872 else if (quoted) {
5873 if (*s == '\\') qdpair = 1;
5874 else if (*s == '"') quoted = 0;
5875 }
5876 else if (*s == '"') quoted = 1;
5877 else if (*s == ',' || *s == ';') return s;
5878 }
5879 return s;
5880}
5881
5882/* Delete a value in a header between delimiters <from> and <next> in buffer
5883 * <buf>. The number of characters displaced is returned, and the pointer to
5884 * the first delimiter is updated if required. The function tries as much as
5885 * possible to respect the following principles :
5886 * - replace <from> delimiter by the <next> one unless <from> points to a
5887 * colon, in which case <next> is simply removed
5888 * - set exactly one space character after the new first delimiter, unless
5889 * there are not enough characters in the block being moved to do so.
5890 * - remove unneeded spaces before the previous delimiter and after the new
5891 * one.
5892 *
5893 * It is the caller's responsibility to ensure that :
5894 * - <from> points to a valid delimiter or the colon ;
5895 * - <next> points to a valid delimiter or the final CR/LF ;
5896 * - there are non-space chars before <from> ;
5897 * - there is a CR/LF at or after <next>.
5898 */
5899int del_hdr_value(struct buffer *buf, char **from, char *next)
5900{
5901 char *prev = *from;
5902
5903 if (*prev == ':') {
5904 /* We're removing the first value, preserve the colon and add a
5905 * space if possible.
5906 */
5907 if (!http_is_crlf[(unsigned char)*next])
5908 next++;
5909 prev++;
5910 if (prev < next)
5911 *prev++ = ' ';
5912
5913 while (http_is_spht[(unsigned char)*next])
5914 next++;
5915 } else {
5916 /* Remove useless spaces before the old delimiter. */
5917 while (http_is_spht[(unsigned char)*(prev-1)])
5918 prev--;
5919 *from = prev;
5920
5921 /* copy the delimiter and if possible a space if we're
5922 * not at the end of the line.
5923 */
5924 if (!http_is_crlf[(unsigned char)*next]) {
5925 *prev++ = *next++;
5926 if (prev + 1 < next)
5927 *prev++ = ' ';
5928 while (http_is_spht[(unsigned char)*next])
5929 next++;
5930 }
5931 }
5932 return buffer_replace2(buf, prev, next, NULL, 0);
5933}
5934
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005935/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005936 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005937 * desirable to call it only when needed. This code is quite complex because
5938 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5939 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005940 */
5941void manage_client_side_cookies(struct session *t, struct buffer *req)
5942{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005943 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005944 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005946 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5947 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005949 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005950 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005951 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005952
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005953 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005954 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005955 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005956
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005957 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005958 hdr_beg = hdr_next;
5959 hdr_end = hdr_beg + cur_hdr->len;
5960 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005962 /* We have one full header between hdr_beg and hdr_end, and the
5963 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005964 * "Cookie:" headers.
5965 */
5966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005967 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005968 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969 old_idx = cur_idx;
5970 continue;
5971 }
5972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005973 del_from = NULL; /* nothing to be deleted */
5974 preserve_hdr = 0; /* assume we may kill the whole header */
5975
Willy Tarreau58f10d72006-12-04 02:26:12 +01005976 /* Now look for cookies. Conforming to RFC2109, we have to support
5977 * attributes whose name begin with a '$', and associate them with
5978 * the right cookie, if we want to delete this cookie.
5979 * So there are 3 cases for each cookie read :
5980 * 1) it's a special attribute, beginning with a '$' : ignore it.
5981 * 2) it's a server id cookie that we *MAY* want to delete : save
5982 * some pointers on it (last semi-colon, beginning of cookie...)
5983 * 3) it's an application cookie : we *MAY* have to delete a previous
5984 * "special" cookie.
5985 * At the end of loop, if a "special" cookie remains, we may have to
5986 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005987 * *MUST* delete it.
5988 *
5989 * Note: RFC2965 is unclear about the processing of spaces around
5990 * the equal sign in the ATTR=VALUE form. A careful inspection of
5991 * the RFC explicitly allows spaces before it, and not within the
5992 * tokens (attrs or values). An inspection of RFC2109 allows that
5993 * too but section 10.1.3 lets one think that spaces may be allowed
5994 * after the equal sign too, resulting in some (rare) buggy
5995 * implementations trying to do that. So let's do what servers do.
5996 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5997 * allowed quoted strings in values, with any possible character
5998 * after a backslash, including control chars and delimitors, which
5999 * causes parsing to become ambiguous. Browsers also allow spaces
6000 * within values even without quotes.
6001 *
6002 * We have to keep multiple pointers in order to support cookie
6003 * removal at the beginning, middle or end of header without
6004 * corrupting the header. All of these headers are valid :
6005 *
6006 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6007 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6008 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6009 * | | | | | | | | |
6010 * | | | | | | | | hdr_end <--+
6011 * | | | | | | | +--> next
6012 * | | | | | | +----> val_end
6013 * | | | | | +-----------> val_beg
6014 * | | | | +--------------> equal
6015 * | | | +----------------> att_end
6016 * | | +---------------------> att_beg
6017 * | +--------------------------> prev
6018 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006019 */
6020
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6022 /* Iterate through all cookies on this line */
6023
6024 /* find att_beg */
6025 att_beg = prev + 1;
6026 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6027 att_beg++;
6028
6029 /* find att_end : this is the first character after the last non
6030 * space before the equal. It may be equal to hdr_end.
6031 */
6032 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 while (equal < hdr_end) {
6035 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006037 if (http_is_spht[(unsigned char)*equal++])
6038 continue;
6039 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040 }
6041
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006042 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6043 * is between <att_beg> and <equal>, both may be identical.
6044 */
6045
6046 /* look for end of cookie if there is an equal sign */
6047 if (equal < hdr_end && *equal == '=') {
6048 /* look for the beginning of the value */
6049 val_beg = equal + 1;
6050 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6051 val_beg++;
6052
6053 /* find the end of the value, respecting quotes */
6054 next = find_cookie_value_end(val_beg, hdr_end);
6055
6056 /* make val_end point to the first white space or delimitor after the value */
6057 val_end = next;
6058 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6059 val_end--;
6060 } else {
6061 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006062 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 /* We have nothing to do with attributes beginning with '$'. However,
6065 * they will automatically be removed if a header before them is removed,
6066 * since they're supposed to be linked together.
6067 */
6068 if (*att_beg == '$')
6069 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006071 /* Ignore cookies with no equal sign */
6072 if (equal == next) {
6073 /* This is not our cookie, so we must preserve it. But if we already
6074 * scheduled another cookie for removal, we cannot remove the
6075 * complete header, but we can remove the previous block itself.
6076 */
6077 preserve_hdr = 1;
6078 if (del_from != NULL) {
6079 int delta = del_hdr_value(req, &del_from, prev);
6080 val_end += delta;
6081 next += delta;
6082 hdr_end += delta;
6083 hdr_next += delta;
6084 cur_hdr->len += delta;
6085 http_msg_move_end(&txn->req, delta);
6086 prev = del_from;
6087 del_from = NULL;
6088 }
6089 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006090 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 /* if there are spaces around the equal sign, we need to
6093 * strip them otherwise we'll get trouble for cookie captures,
6094 * or even for rewrites. Since this happens extremely rarely,
6095 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006097 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6098 int stripped_before = 0;
6099 int stripped_after = 0;
6100
6101 if (att_end != equal) {
6102 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6103 equal += stripped_before;
6104 val_beg += stripped_before;
6105 }
6106
6107 if (val_beg > equal + 1) {
6108 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6109 val_beg += stripped_after;
6110 stripped_before += stripped_after;
6111 }
6112
6113 val_end += stripped_before;
6114 next += stripped_before;
6115 hdr_end += stripped_before;
6116 hdr_next += stripped_before;
6117 cur_hdr->len += stripped_before;
6118 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006120 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006122 /* First, let's see if we want to capture this cookie. We check
6123 * that we don't already have a client side cookie, because we
6124 * can only capture one. Also as an optimisation, we ignore
6125 * cookies shorter than the declared name.
6126 */
6127 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6128 (val_end - att_beg >= t->fe->capture_namelen) &&
6129 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6130 int log_len = val_end - att_beg;
6131
6132 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6133 Alert("HTTP logging : out of memory.\n");
6134 } else {
6135 if (log_len > t->fe->capture_len)
6136 log_len = t->fe->capture_len;
6137 memcpy(txn->cli_cookie, att_beg, log_len);
6138 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006140 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006141
Willy Tarreaubca99692010-10-06 19:25:55 +02006142 /* Persistence cookies in passive, rewrite or insert mode have the
6143 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006144 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006145 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006146 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006147 * For cookies in prefix mode, the form is :
6148 *
6149 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006150 */
6151 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6152 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6153 struct server *srv = t->be->srv;
6154 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006155
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006156 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6157 * have the server ID between val_beg and delim, and the original cookie between
6158 * delim+1 and val_end. Otherwise, delim==val_end :
6159 *
6160 * Cookie: NAME=SRV; # in all but prefix modes
6161 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6162 * | || || | |+-> next
6163 * | || || | +--> val_end
6164 * | || || +---------> delim
6165 * | || |+------------> val_beg
6166 * | || +-------------> att_end = equal
6167 * | |+-----------------> att_beg
6168 * | +------------------> prev
6169 * +-------------------------> hdr_beg
6170 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006172 if (t->be->options & PR_O_COOK_PFX) {
6173 for (delim = val_beg; delim < val_end; delim++)
6174 if (*delim == COOKIE_DELIM)
6175 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006176 } else {
6177 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006178 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006179 /* Now check if the cookie contains a date field, which would
6180 * appear after a vertical bar ('|') just after the server name
6181 * and before the delimiter.
6182 */
6183 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6184 if (vbar1) {
6185 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006186 * right is the last seen date. It is a base64 encoded
6187 * 30-bit value representing the UNIX date since the
6188 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006189 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006190 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006191 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006192 if (val_end - vbar1 >= 5) {
6193 val = b64tos30(vbar1);
6194 if (val > 0)
6195 txn->cookie_last_date = val << 2;
6196 }
6197 /* look for a second vertical bar */
6198 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6199 if (vbar1 && (val_end - vbar1 > 5)) {
6200 val = b64tos30(vbar1 + 1);
6201 if (val > 0)
6202 txn->cookie_first_date = val << 2;
6203 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006204 }
6205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206
Willy Tarreauf64d1412010-10-07 20:06:11 +02006207 /* if the cookie has an expiration date and the proxy wants to check
6208 * it, then we do that now. We first check if the cookie is too old,
6209 * then only if it has expired. We detect strict overflow because the
6210 * time resolution here is not great (4 seconds). Cookies with dates
6211 * in the future are ignored if their offset is beyond one day. This
6212 * allows an admin to fix timezone issues without expiring everyone
6213 * and at the same time avoids keeping unwanted side effects for too
6214 * long.
6215 */
6216 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006217 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6218 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006219 txn->flags &= ~TX_CK_MASK;
6220 txn->flags |= TX_CK_OLD;
6221 delim = val_beg; // let's pretend we have not found the cookie
6222 txn->cookie_first_date = 0;
6223 txn->cookie_last_date = 0;
6224 }
6225 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006226 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6227 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006228 txn->flags &= ~TX_CK_MASK;
6229 txn->flags |= TX_CK_EXPIRED;
6230 delim = val_beg; // let's pretend we have not found the cookie
6231 txn->cookie_first_date = 0;
6232 txn->cookie_last_date = 0;
6233 }
6234
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 /* Here, we'll look for the first running server which supports the cookie.
6236 * This allows to share a same cookie between several servers, for example
6237 * to dedicate backup servers to specific servers only.
6238 * However, to prevent clients from sticking to cookie-less backup server
6239 * when they have incidentely learned an empty cookie, we simply ignore
6240 * empty cookies and mark them as invalid.
6241 * The same behaviour is applied when persistence must be ignored.
6242 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006243 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 while (srv) {
6247 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6248 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6249 if ((srv->state & SRV_RUNNING) ||
6250 (t->be->options & PR_O_PERSIST) ||
6251 (t->flags & SN_FORCE_PRST)) {
6252 /* we found the server and we can use it */
6253 txn->flags &= ~TX_CK_MASK;
6254 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6255 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006256 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 break;
6258 } else {
6259 /* we found a server, but it's down,
6260 * mark it as such and go on in case
6261 * another one is available.
6262 */
6263 txn->flags &= ~TX_CK_MASK;
6264 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006267 srv = srv->next;
6268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreauf64d1412010-10-07 20:06:11 +02006270 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006271 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006273 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6274 txn->flags |= TX_CK_UNUSED;
6275 else
6276 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 /* depending on the cookie mode, we may have to either :
6280 * - delete the complete cookie if we're in insert+indirect mode, so that
6281 * the server never sees it ;
6282 * - remove the server id from the cookie value, and tag the cookie as an
6283 * application cookie so that it does not get accidentely removed later,
6284 * if we're in cookie prefix mode
6285 */
6286 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6287 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006288
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6290 val_end += delta;
6291 next += delta;
6292 hdr_end += delta;
6293 hdr_next += delta;
6294 cur_hdr->len += delta;
6295 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 del_from = NULL;
6298 preserve_hdr = 1; /* we want to keep this cookie */
6299 }
6300 else if (del_from == NULL &&
6301 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6302 del_from = prev;
6303 }
6304 } else {
6305 /* This is not our cookie, so we must preserve it. But if we already
6306 * scheduled another cookie for removal, we cannot remove the
6307 * complete header, but we can remove the previous block itself.
6308 */
6309 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006310
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 if (del_from != NULL) {
6312 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006313 if (att_beg >= del_from)
6314 att_beg += delta;
6315 if (att_end >= del_from)
6316 att_end += delta;
6317 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006318 val_end += delta;
6319 next += delta;
6320 hdr_end += delta;
6321 hdr_next += delta;
6322 cur_hdr->len += delta;
6323 http_msg_move_end(&txn->req, delta);
6324 prev = del_from;
6325 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 /* Look for the appsession cookie unless persistence must be ignored */
6330 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6331 int cmp_len, value_len;
6332 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 if (t->be->options2 & PR_O2_AS_PFX) {
6335 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6336 value_begin = att_beg + t->be->appsession_name_len;
6337 value_len = val_end - att_beg - t->be->appsession_name_len;
6338 } else {
6339 cmp_len = att_end - att_beg;
6340 value_begin = val_beg;
6341 value_len = val_end - val_beg;
6342 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 /* let's see if the cookie is our appcookie */
6345 if (cmp_len == t->be->appsession_name_len &&
6346 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6347 manage_client_side_appsession(t, value_begin, value_len);
6348 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349 }
6350
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 /* continue with next cookie on this header line */
6352 att_beg = next;
6353 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006355 /* There are no more cookies on this line.
6356 * We may still have one (or several) marked for deletion at the
6357 * end of the line. We must do this now in two ways :
6358 * - if some cookies must be preserved, we only delete from the
6359 * mark to the end of line ;
6360 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006362 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 if (preserve_hdr) {
6365 delta = del_hdr_value(req, &del_from, hdr_end);
6366 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006367 cur_hdr->len += delta;
6368 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006370
6371 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006372 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6373 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006374 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006375 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006377 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006378 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 }
6380
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384}
6385
6386
Willy Tarreaua15645d2007-03-18 16:22:39 +01006387/* Iterate the same filter through all response headers contained in <rtr>.
6388 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6389 */
6390int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6391{
6392 char term;
6393 char *cur_ptr, *cur_end, *cur_next;
6394 int cur_idx, old_idx, last_hdr;
6395 struct http_txn *txn = &t->txn;
6396 struct hdr_idx_elem *cur_hdr;
6397 int len, delta;
6398
6399 last_hdr = 0;
6400
Willy Tarreau962c3f42010-01-10 00:15:35 +01006401 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006402 old_idx = 0;
6403
6404 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006405 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006406 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006407 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006408 (exp->action == ACT_ALLOW ||
6409 exp->action == ACT_DENY))
6410 return 0;
6411
6412 cur_idx = txn->hdr_idx.v[old_idx].next;
6413 if (!cur_idx)
6414 break;
6415
6416 cur_hdr = &txn->hdr_idx.v[cur_idx];
6417 cur_ptr = cur_next;
6418 cur_end = cur_ptr + cur_hdr->len;
6419 cur_next = cur_end + cur_hdr->cr + 1;
6420
6421 /* Now we have one header between cur_ptr and cur_end,
6422 * and the next header starts at cur_next.
6423 */
6424
6425 /* The annoying part is that pattern matching needs
6426 * that we modify the contents to null-terminate all
6427 * strings before testing them.
6428 */
6429
6430 term = *cur_end;
6431 *cur_end = '\0';
6432
6433 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6434 switch (exp->action) {
6435 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006436 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437 last_hdr = 1;
6438 break;
6439
6440 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006441 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442 last_hdr = 1;
6443 break;
6444
6445 case ACT_REPLACE:
6446 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6447 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6448 /* FIXME: if the user adds a newline in the replacement, the
6449 * index will not be recalculated for now, and the new line
6450 * will not be counted as a new header.
6451 */
6452
6453 cur_end += delta;
6454 cur_next += delta;
6455 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006456 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457 break;
6458
6459 case ACT_REMOVE:
6460 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6461 cur_next += delta;
6462
Willy Tarreaufa355d42009-11-29 18:12:29 +01006463 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006464 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6465 txn->hdr_idx.used--;
6466 cur_hdr->len = 0;
6467 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006468 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469 break;
6470
6471 }
6472 }
6473 if (cur_end)
6474 *cur_end = term; /* restore the string terminator */
6475
6476 /* keep the link from this header to next one in case of later
6477 * removal of next header.
6478 */
6479 old_idx = cur_idx;
6480 }
6481 return 0;
6482}
6483
6484
6485/* Apply the filter to the status line in the response buffer <rtr>.
6486 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6487 * or -1 if a replacement resulted in an invalid status line.
6488 */
6489int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6490{
6491 char term;
6492 char *cur_ptr, *cur_end;
6493 int done;
6494 struct http_txn *txn = &t->txn;
6495 int len, delta;
6496
6497
Willy Tarreau3d300592007-03-18 18:34:41 +01006498 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006500 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006501 (exp->action == ACT_ALLOW ||
6502 exp->action == ACT_DENY))
6503 return 0;
6504 else if (exp->action == ACT_REMOVE)
6505 return 0;
6506
6507 done = 0;
6508
Willy Tarreau962c3f42010-01-10 00:15:35 +01006509 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006510 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006511
6512 /* Now we have the status line between cur_ptr and cur_end */
6513
6514 /* The annoying part is that pattern matching needs
6515 * that we modify the contents to null-terminate all
6516 * strings before testing them.
6517 */
6518
6519 term = *cur_end;
6520 *cur_end = '\0';
6521
6522 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6523 switch (exp->action) {
6524 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006525 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 done = 1;
6527 break;
6528
6529 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006530 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531 done = 1;
6532 break;
6533
6534 case ACT_REPLACE:
6535 *cur_end = term; /* restore the string terminator */
6536 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6537 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6538 /* FIXME: if the user adds a newline in the replacement, the
6539 * index will not be recalculated for now, and the new line
6540 * will not be counted as a new header.
6541 */
6542
Willy Tarreaufa355d42009-11-29 18:12:29 +01006543 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006546 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547 cur_ptr, cur_end + 1,
6548 NULL, NULL);
6549 if (unlikely(!cur_end))
6550 return -1;
6551
6552 /* we have a full respnse and we know that we have either a CR
6553 * or an LF at <ptr>.
6554 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006555 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006556 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 /* there is no point trying this regex on headers */
6558 return 1;
6559 }
6560 }
6561 *cur_end = term; /* restore the string terminator */
6562 return done;
6563}
6564
6565
6566
6567/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006568 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6570 * unparsable response.
6571 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006572int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006574 struct http_txn *txn = &s->txn;
6575 struct hdr_exp *exp;
6576
6577 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 int ret;
6579
6580 /*
6581 * The interleaving of transformations and verdicts
6582 * makes it difficult to decide to continue or stop
6583 * the evaluation.
6584 */
6585
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006586 if (txn->flags & TX_SVDENY)
6587 break;
6588
Willy Tarreau3d300592007-03-18 18:34:41 +01006589 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6591 exp->action == ACT_PASS)) {
6592 exp = exp->next;
6593 continue;
6594 }
6595
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006596 /* if this filter had a condition, evaluate it now and skip to
6597 * next filter if the condition does not match.
6598 */
6599 if (exp->cond) {
6600 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6601 ret = acl_pass(ret);
6602 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6603 ret = !ret;
6604 if (!ret)
6605 continue;
6606 }
6607
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006609 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 if (unlikely(ret < 0))
6611 return -1;
6612
6613 if (likely(ret == 0)) {
6614 /* The filter did not match the response, it can be
6615 * iterated through all headers.
6616 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006617 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 }
6620 return 0;
6621}
6622
6623
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006625 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006626 * desirable to call it only when needed. This function is also used when we
6627 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006629void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006630{
6631 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006632 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006633 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006635 char *hdr_beg, *hdr_end, *hdr_next;
6636 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637
Willy Tarreaua15645d2007-03-18 16:22:39 +01006638 /* Iterate through the headers.
6639 * we start with the start line.
6640 */
6641 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006642 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643
6644 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6645 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006646 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647
6648 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006649 hdr_beg = hdr_next;
6650 hdr_end = hdr_beg + cur_hdr->len;
6651 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 /* We have one full header between hdr_beg and hdr_end, and the
6654 * next header starts at hdr_next. We're only interested in
6655 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 */
6657
Willy Tarreau24581ba2010-08-31 22:39:35 +02006658 is_cookie2 = 0;
6659 prev = hdr_beg + 10;
6660 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006661 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006662 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6663 if (!val) {
6664 old_idx = cur_idx;
6665 continue;
6666 }
6667 is_cookie2 = 1;
6668 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669 }
6670
Willy Tarreau24581ba2010-08-31 22:39:35 +02006671 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6672 * <prev> points to the colon.
6673 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006674 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6677 * check-cache is enabled) and we are not interested in checking
6678 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006679 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006680 if (t->be->cookie_name == NULL &&
6681 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006682 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683 return;
6684
Willy Tarreau24581ba2010-08-31 22:39:35 +02006685 /* OK so now we know we have to process this response cookie.
6686 * The format of the Set-Cookie header is slightly different
6687 * from the format of the Cookie header in that it does not
6688 * support the comma as a cookie delimiter (thus the header
6689 * cannot be folded) because the Expires attribute described in
6690 * the original Netscape's spec may contain an unquoted date
6691 * with a comma inside. We have to live with this because
6692 * many browsers don't support Max-Age and some browsers don't
6693 * support quoted strings. However the Set-Cookie2 header is
6694 * clean.
6695 *
6696 * We have to keep multiple pointers in order to support cookie
6697 * removal at the beginning, middle or end of header without
6698 * corrupting the header (in case of set-cookie2). A special
6699 * pointer, <scav> points to the beginning of the set-cookie-av
6700 * fields after the first semi-colon. The <next> pointer points
6701 * either to the end of line (set-cookie) or next unquoted comma
6702 * (set-cookie2). All of these headers are valid :
6703 *
6704 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6705 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6706 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6707 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6708 * | | | | | | | | | |
6709 * | | | | | | | | +-> next hdr_end <--+
6710 * | | | | | | | +------------> scav
6711 * | | | | | | +--------------> val_end
6712 * | | | | | +--------------------> val_beg
6713 * | | | | +----------------------> equal
6714 * | | | +------------------------> att_end
6715 * | | +----------------------------> att_beg
6716 * | +------------------------------> prev
6717 * +-----------------------------------------> hdr_beg
6718 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719
Willy Tarreau24581ba2010-08-31 22:39:35 +02006720 for (; prev < hdr_end; prev = next) {
6721 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722
Willy Tarreau24581ba2010-08-31 22:39:35 +02006723 /* find att_beg */
6724 att_beg = prev + 1;
6725 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6726 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727
Willy Tarreau24581ba2010-08-31 22:39:35 +02006728 /* find att_end : this is the first character after the last non
6729 * space before the equal. It may be equal to hdr_end.
6730 */
6731 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 while (equal < hdr_end) {
6734 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6735 break;
6736 if (http_is_spht[(unsigned char)*equal++])
6737 continue;
6738 att_end = equal;
6739 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
Willy Tarreau24581ba2010-08-31 22:39:35 +02006741 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6742 * is between <att_beg> and <equal>, both may be identical.
6743 */
6744
6745 /* look for end of cookie if there is an equal sign */
6746 if (equal < hdr_end && *equal == '=') {
6747 /* look for the beginning of the value */
6748 val_beg = equal + 1;
6749 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6750 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751
Willy Tarreau24581ba2010-08-31 22:39:35 +02006752 /* find the end of the value, respecting quotes */
6753 next = find_cookie_value_end(val_beg, hdr_end);
6754
6755 /* make val_end point to the first white space or delimitor after the value */
6756 val_end = next;
6757 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6758 val_end--;
6759 } else {
6760 /* <equal> points to next comma, semi-colon or EOL */
6761 val_beg = val_end = next = equal;
6762 }
6763
6764 if (next < hdr_end) {
6765 /* Set-Cookie2 supports multiple cookies, and <next> points to
6766 * a colon or semi-colon before the end. So skip all attr-value
6767 * pairs and look for the next comma. For Set-Cookie, since
6768 * commas are permitted in values, skip to the end.
6769 */
6770 if (is_cookie2)
6771 next = find_hdr_value_end(next, hdr_end);
6772 else
6773 next = hdr_end;
6774 }
6775
6776 /* Now everything is as on the diagram above */
6777
6778 /* Ignore cookies with no equal sign */
6779 if (equal == val_end)
6780 continue;
6781
6782 /* If there are spaces around the equal sign, we need to
6783 * strip them otherwise we'll get trouble for cookie captures,
6784 * or even for rewrites. Since this happens extremely rarely,
6785 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006787 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6788 int stripped_before = 0;
6789 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 if (att_end != equal) {
6792 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6793 equal += stripped_before;
6794 val_beg += stripped_before;
6795 }
6796
6797 if (val_beg > equal + 1) {
6798 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6799 val_beg += stripped_after;
6800 stripped_before += stripped_after;
6801 }
6802
6803 val_end += stripped_before;
6804 next += stripped_before;
6805 hdr_end += stripped_before;
6806 hdr_next += stripped_before;
6807 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006808 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006809 }
6810
6811 /* First, let's see if we want to capture this cookie. We check
6812 * that we don't already have a server side cookie, because we
6813 * can only capture one. Also as an optimisation, we ignore
6814 * cookies shorter than the declared name.
6815 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006816 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006817 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006818 (val_end - att_beg >= t->fe->capture_namelen) &&
6819 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6820 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006821 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822 Alert("HTTP logging : out of memory.\n");
6823 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006824 else {
6825 if (log_len > t->fe->capture_len)
6826 log_len = t->fe->capture_len;
6827 memcpy(txn->srv_cookie, att_beg, log_len);
6828 txn->srv_cookie[log_len] = 0;
6829 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 }
6831
Willy Tarreau827aee92011-03-10 16:55:02 +01006832 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 if (!(t->flags & SN_IGNORE_PRST) &&
6835 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6836 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006837 /* assume passive cookie by default */
6838 txn->flags &= ~TX_SCK_MASK;
6839 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840
6841 /* If the cookie is in insert mode on a known server, we'll delete
6842 * this occurrence because we'll insert another one later.
6843 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006844 * a direct access.
6845 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006846 if (t->be->options2 & PR_O2_COOK_PSV) {
6847 /* The "preserve" flag was set, we don't want to touch the
6848 * server's cookie.
6849 */
6850 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006851 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006852 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 /* this cookie must be deleted */
6854 if (*prev == ':' && next == hdr_end) {
6855 /* whole header */
6856 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6857 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6858 txn->hdr_idx.used--;
6859 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006860 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 hdr_next += delta;
6862 http_msg_move_end(&txn->rsp, delta);
6863 /* note: while both invalid now, <next> and <hdr_end>
6864 * are still equal, so the for() will stop as expected.
6865 */
6866 } else {
6867 /* just remove the value */
6868 int delta = del_hdr_value(res, &prev, next);
6869 next = prev;
6870 hdr_end += delta;
6871 hdr_next += delta;
6872 cur_hdr->len += delta;
6873 http_msg_move_end(&txn->rsp, delta);
6874 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006875 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006876 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006879 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006880 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881 * with this server since we know it.
6882 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006883 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 next += delta;
6885 hdr_end += delta;
6886 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006888 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreauf1348312010-10-07 15:54:11 +02006890 txn->flags &= ~TX_SCK_MASK;
6891 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006893 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006897 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 next += delta;
6899 hdr_end += delta;
6900 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006902 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903
Willy Tarreau827aee92011-03-10 16:55:02 +01006904 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006905 txn->flags &= ~TX_SCK_MASK;
6906 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907 }
6908 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006909 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6910 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006911 int cmp_len, value_len;
6912 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913
Cyril Bontéb21570a2009-11-29 20:04:48 +01006914 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006915 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6916 value_begin = att_beg + t->be->appsession_name_len;
6917 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006918 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006919 cmp_len = att_end - att_beg;
6920 value_begin = val_beg;
6921 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006922 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006923
Cyril Bonté17530c32010-04-06 21:11:10 +02006924 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006925 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6926 /* free a possibly previously allocated memory */
6927 pool_free2(apools.sessid, txn->sessid);
6928
Cyril Bontéb21570a2009-11-29 20:04:48 +01006929 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006930 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006931 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6932 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6933 return;
6934 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006935 memcpy(txn->sessid, value_begin, value_len);
6936 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006937 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006938 }
6939 /* that's done for this cookie, check the next one on the same
6940 * line when next != hdr_end (only if is_cookie2).
6941 */
6942 }
6943 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006946
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006947 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006948 appsess *asession = NULL;
6949 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006950 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006952 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006953 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6954 Alert("Not enough Memory process_srv():asession:calloc().\n");
6955 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6956 return;
6957 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006958 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6959
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006960 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6961 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6962 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006963 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006964 return;
6965 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006966 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006967 asession->sessid[t->be->appsession_len] = 0;
6968
Willy Tarreau827aee92011-03-10 16:55:02 +01006969 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006970 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006971 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006972 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006973 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006974 return;
6975 }
6976 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006977 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006978
6979 asession->request_count = 0;
6980 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6981 }
6982
6983 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6984 asession->request_count++;
6985 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986}
6987
6988
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989/*
6990 * Check if response is cacheable or not. Updates t->flags.
6991 */
6992void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6993{
6994 struct http_txn *txn = &t->txn;
6995 char *p1, *p2;
6996
6997 char *cur_ptr, *cur_end, *cur_next;
6998 int cur_idx;
6999
Willy Tarreau5df51872007-11-25 16:20:08 +01007000 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 return;
7002
7003 /* Iterate through the headers.
7004 * we start with the start line.
7005 */
7006 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007007 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008
7009 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7010 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007011 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012
7013 cur_hdr = &txn->hdr_idx.v[cur_idx];
7014 cur_ptr = cur_next;
7015 cur_end = cur_ptr + cur_hdr->len;
7016 cur_next = cur_end + cur_hdr->cr + 1;
7017
7018 /* We have one full header between cur_ptr and cur_end, and the
7019 * next header starts at cur_next. We're only interested in
7020 * "Cookie:" headers.
7021 */
7022
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007023 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7024 if (val) {
7025 if ((cur_end - (cur_ptr + val) >= 8) &&
7026 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7027 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7028 return;
7029 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 }
7031
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007032 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7033 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 continue;
7035
7036 /* OK, right now we know we have a cache-control header at cur_ptr */
7037
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007038 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007039
7040 if (p1 >= cur_end) /* no more info */
7041 continue;
7042
7043 /* p1 is at the beginning of the value */
7044 p2 = p1;
7045
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007046 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 p2++;
7048
7049 /* we have a complete value between p1 and p2 */
7050 if (p2 < cur_end && *p2 == '=') {
7051 /* we have something of the form no-cache="set-cookie" */
7052 if ((cur_end - p1 >= 21) &&
7053 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7054 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007055 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 continue;
7057 }
7058
7059 /* OK, so we know that either p2 points to the end of string or to a comma */
7060 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7061 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7062 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7063 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007064 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 return;
7066 }
7067
7068 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007069 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007070 continue;
7071 }
7072 }
7073}
7074
7075
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076/*
7077 * Try to retrieve a known appsession in the URI, then the associated server.
7078 * If the server is found, it's assigned to the session.
7079 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007080void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 char *end_params, *first_param, *cur_param, *next_param;
7083 char separator;
7084 int value_len;
7085
7086 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007088 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007089 (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 +01007090 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007091 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092
Cyril Bontéb21570a2009-11-29 20:04:48 +01007093 first_param = NULL;
7094 switch (mode) {
7095 case PR_O2_AS_M_PP:
7096 first_param = memchr(begin, ';', len);
7097 break;
7098 case PR_O2_AS_M_QS:
7099 first_param = memchr(begin, '?', len);
7100 break;
7101 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Cyril Bontéb21570a2009-11-29 20:04:48 +01007103 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106
Cyril Bontéb21570a2009-11-29 20:04:48 +01007107 switch (mode) {
7108 case PR_O2_AS_M_PP:
7109 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7110 end_params = (char *) begin + len;
7111 }
7112 separator = ';';
7113 break;
7114 case PR_O2_AS_M_QS:
7115 end_params = (char *) begin + len;
7116 separator = '&';
7117 break;
7118 default:
7119 /* unknown mode, shouldn't happen */
7120 return;
7121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122
Cyril Bontéb21570a2009-11-29 20:04:48 +01007123 cur_param = next_param = end_params;
7124 while (cur_param > first_param) {
7125 cur_param--;
7126 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7127 /* let's see if this is the appsession parameter */
7128 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7129 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7130 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7131 /* Cool... it's the right one */
7132 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7133 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7134 if (value_len > 0) {
7135 manage_client_side_appsession(t, cur_param, value_len);
7136 }
7137 break;
7138 }
7139 next_param = cur_param;
7140 }
7141 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007142#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007143 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007144 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007146}
7147
Willy Tarreaub2513902006-12-17 14:52:38 +01007148/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007149 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007150 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007151 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007152 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007153 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007154 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007155 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007156 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007157int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007158{
7159 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007161
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007162 if (!uri_auth)
7163 return 0;
7164
Cyril Bonté70be45d2010-10-12 00:14:35 +02007165 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007166 return 0;
7167
Willy Tarreau295a8372011-03-10 11:25:07 +01007168 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007169 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007172 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007173 return 0;
7174
Willy Tarreau962c3f42010-01-10 00:15:35 +01007175 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007177 /* the URI is in h */
7178 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007179 return 0;
7180
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007181 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007182 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007183 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007184 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007185 break;
7186 }
7187 h++;
7188 }
7189
7190 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007191 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7192 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007193 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007194 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007195 break;
7196 }
7197 h++;
7198 }
7199 }
7200
Willy Tarreau962c3f42010-01-10 00:15:35 +01007201 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7202 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007203 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007204 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007205 break;
7206 }
7207 h++;
7208 }
7209
Cyril Bonté70be45d2010-10-12 00:14:35 +02007210 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7211 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7212 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007213 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007214 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007215 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7216 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7217 si->applet.ctx.stats.st_code = i;
7218 break;
7219 }
7220 }
7221 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007222 break;
7223 }
7224 h++;
7225 }
7226
Willy Tarreau295a8372011-03-10 11:25:07 +01007227 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007228
Willy Tarreaub2513902006-12-17 14:52:38 +01007229 return 1;
7230}
7231
Willy Tarreau4076a152009-04-02 15:18:36 +02007232/*
7233 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007234 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7235 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007236 */
7237void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7238 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007239 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007240{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007241 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7242 int len1 = buf->size - msg->som;
7243 es->len = buf->r - (buf->data + msg->som) + buf->size;
7244 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7245 if (es->len > len1 && len1 < sizeof(es->buf))
7246 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7247 }
7248 else {
7249 es->len = buf->r - (buf->data + msg->som);
7250 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7251 }
7252
Willy Tarreau4076a152009-04-02 15:18:36 +02007253 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007254 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007255 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007256 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007257 else
7258 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7259
Willy Tarreau4076a152009-04-02 15:18:36 +02007260 es->when = date; // user-visible date
7261 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007262 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007263 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007264 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007265 es->state = state;
7266 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007267 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007268}
Willy Tarreaub2513902006-12-17 14:52:38 +01007269
Willy Tarreau294c4732011-12-16 21:35:50 +01007270/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7271 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7272 * performed over the whole headers. Otherwise it must contain a valid header
7273 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7274 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7275 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7276 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7277 * -1.
7278 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007279 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007280unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7281 struct hdr_idx *idx, int occ,
7282 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007283{
Willy Tarreau294c4732011-12-16 21:35:50 +01007284 struct hdr_ctx local_ctx;
7285 char *ptr_hist[MAX_HDR_HISTORY];
7286 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007287 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007288 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007289
Willy Tarreau294c4732011-12-16 21:35:50 +01007290 if (!ctx) {
7291 local_ctx.idx = 0;
7292 ctx = &local_ctx;
7293 }
7294
Willy Tarreaubce70882009-09-07 11:51:47 +02007295 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007296 /* search from the beginning */
7297 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007298 occ--;
7299 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007300 *vptr = ctx->line + ctx->val;
7301 *vlen = ctx->vlen;
7302 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007303 }
7304 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007305 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007306 }
7307
7308 /* negative occurrence, we scan all the list then walk back */
7309 if (-occ > MAX_HDR_HISTORY)
7310 return 0;
7311
Willy Tarreau294c4732011-12-16 21:35:50 +01007312 found = hist_ptr = 0;
7313 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7314 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7315 len_hist[hist_ptr] = ctx->vlen;
7316 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007317 hist_ptr = 0;
7318 found++;
7319 }
7320 if (-occ > found)
7321 return 0;
7322 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7323 * find occurrence -occ, so we have to check [hist_ptr+occ].
7324 */
7325 hist_ptr += occ;
7326 if (hist_ptr >= MAX_HDR_HISTORY)
7327 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007328 *vptr = ptr_hist[hist_ptr];
7329 *vlen = len_hist[hist_ptr];
7330 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007331}
7332
Willy Tarreaubaaee002006-06-26 02:48:02 +02007333/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 * Print a debug line with a header
7335 */
7336void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7337{
7338 int len, max;
7339 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007340 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007341 max = end - start;
7342 UBOUND(max, sizeof(trash) - len - 1);
7343 len += strlcpy2(trash + len, start, max + 1);
7344 trash[len++] = '\n';
7345 write(1, trash, len);
7346}
7347
Willy Tarreau0937bc42009-12-22 15:03:09 +01007348/*
7349 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7350 * the required fields are properly allocated and that we only need to (re)init
7351 * them. This should be used before processing any new request.
7352 */
7353void http_init_txn(struct session *s)
7354{
7355 struct http_txn *txn = &s->txn;
7356 struct proxy *fe = s->fe;
7357
7358 txn->flags = 0;
7359 txn->status = -1;
7360
William Lallemand5f232402012-04-05 18:02:55 +02007361 global.req_count++;
7362
Willy Tarreauf64d1412010-10-07 20:06:11 +02007363 txn->cookie_first_date = 0;
7364 txn->cookie_last_date = 0;
7365
Willy Tarreau0937bc42009-12-22 15:03:09 +01007366 txn->req.sol = txn->req.eol = NULL;
7367 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7368 txn->rsp.sol = txn->rsp.eol = NULL;
7369 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007370 txn->req.chunk_len = 0LL;
7371 txn->req.body_len = 0LL;
7372 txn->rsp.chunk_len = 0LL;
7373 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007374 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7375 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007376
7377 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007378
7379 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7380 if (fe->options2 & PR_O2_REQBUG_OK)
7381 txn->req.err_pos = -1; /* let buggy requests pass */
7382
Willy Tarreau46023632010-01-07 22:51:47 +01007383 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007384 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7385
Willy Tarreau46023632010-01-07 22:51:47 +01007386 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007387 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7388
7389 if (txn->hdr_idx.v)
7390 hdr_idx_init(&txn->hdr_idx);
7391}
7392
7393/* to be used at the end of a transaction */
7394void http_end_txn(struct session *s)
7395{
7396 struct http_txn *txn = &s->txn;
7397
7398 /* these ones will have been dynamically allocated */
7399 pool_free2(pool2_requri, txn->uri);
7400 pool_free2(pool2_capture, txn->cli_cookie);
7401 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007402 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007403 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007404
William Lallemanda73203e2012-03-12 12:48:57 +01007405 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007406 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007407 txn->uri = NULL;
7408 txn->srv_cookie = NULL;
7409 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007410
7411 if (txn->req.cap) {
7412 struct cap_hdr *h;
7413 for (h = s->fe->req_cap; h; h = h->next)
7414 pool_free2(h->pool, txn->req.cap[h->index]);
7415 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7416 }
7417
7418 if (txn->rsp.cap) {
7419 struct cap_hdr *h;
7420 for (h = s->fe->rsp_cap; h; h = h->next)
7421 pool_free2(h->pool, txn->rsp.cap[h->index]);
7422 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7423 }
7424
Willy Tarreau0937bc42009-12-22 15:03:09 +01007425}
7426
7427/* to be used at the end of a transaction to prepare a new one */
7428void http_reset_txn(struct session *s)
7429{
7430 http_end_txn(s);
7431 http_init_txn(s);
7432
7433 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007434 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007435 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007436 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007437 /* re-init store persistence */
7438 s->store_count = 0;
7439
Willy Tarreau0937bc42009-12-22 15:03:09 +01007440 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007441
7442 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7443
Willy Tarreau739cfba2010-01-25 23:11:14 +01007444 /* We must trim any excess data from the response buffer, because we
7445 * may have blocked an invalid response from a server that we don't
7446 * want to accidentely forward once we disable the analysers, nor do
7447 * we want those data to come along with next response. A typical
7448 * example of such data would be from a buggy server responding to
7449 * a HEAD with some data, or sending more than the advertised
7450 * content-length.
7451 */
7452 if (unlikely(s->rep->l > s->rep->send_max)) {
7453 s->rep->l = s->rep->send_max;
7454 s->rep->r = s->rep->w + s->rep->l;
7455 if (s->rep->r >= s->rep->data + s->rep->size)
7456 s->rep->r -= s->rep->size;
7457 }
7458
Willy Tarreau0937bc42009-12-22 15:03:09 +01007459 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007460 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007461
Willy Tarreaud04e8582010-05-31 12:31:35 +02007462 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007463 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007464
7465 s->req->rex = TICK_ETERNITY;
7466 s->req->wex = TICK_ETERNITY;
7467 s->req->analyse_exp = TICK_ETERNITY;
7468 s->rep->rex = TICK_ETERNITY;
7469 s->rep->wex = TICK_ETERNITY;
7470 s->rep->analyse_exp = TICK_ETERNITY;
7471}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472
Willy Tarreauff011f22011-01-06 17:51:27 +01007473void free_http_req_rules(struct list *r) {
7474 struct http_req_rule *tr, *pr;
7475
7476 list_for_each_entry_safe(pr, tr, r, list) {
7477 LIST_DEL(&pr->list);
7478 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7479 free(pr->http_auth.realm);
7480
7481 free(pr);
7482 }
7483}
7484
7485struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7486{
7487 struct http_req_rule *rule;
7488 int cur_arg;
7489
7490 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7491 if (!rule) {
7492 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7493 return NULL;
7494 }
7495
7496 if (!*args[0]) {
7497 goto req_error_parsing;
7498 } else if (!strcmp(args[0], "allow")) {
7499 rule->action = HTTP_REQ_ACT_ALLOW;
7500 cur_arg = 1;
7501 } else if (!strcmp(args[0], "deny")) {
7502 rule->action = HTTP_REQ_ACT_DENY;
7503 cur_arg = 1;
7504 } else if (!strcmp(args[0], "auth")) {
7505 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7506 cur_arg = 1;
7507
7508 while(*args[cur_arg]) {
7509 if (!strcmp(args[cur_arg], "realm")) {
7510 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7511 cur_arg+=2;
7512 continue;
7513 } else
7514 break;
7515 }
7516 } else {
7517req_error_parsing:
7518 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7519 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7520 return NULL;
7521 }
7522
7523 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7524 struct acl_cond *cond;
7525
7526 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7527 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7528 file, linenum, args[0]);
7529 return NULL;
7530 }
7531 rule->cond = cond;
7532 }
7533 else if (*args[cur_arg]) {
7534 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7535 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7536 file, linenum, args[0], args[cur_arg]);
7537 return NULL;
7538 }
7539
7540 return rule;
7541}
7542
Willy Tarreau8797c062007-05-07 00:55:35 +02007543/************************************************************************/
7544/* The code below is dedicated to ACL parsing and matching */
7545/************************************************************************/
7546
7547
7548
7549
7550/* 1. Check on METHOD
7551 * We use the pre-parsed method if it is known, and store its number as an
7552 * integer. If it is unknown, we use the pointer and the length.
7553 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007554static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007555{
7556 int len, meth;
7557
Willy Tarreauae8b7962007-06-09 23:10:04 +02007558 len = strlen(*text);
7559 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007560
7561 pattern->val.i = meth;
7562 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007563 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007564 if (!pattern->ptr.str)
7565 return 0;
7566 pattern->len = len;
7567 }
7568 return 1;
7569}
7570
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007571/* This function fetches the method of current HTTP request and stores
7572 * it in the global pattern struct as a chunk. There are two possibilities :
7573 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7574 * in <len> and <ptr> is NULL ;
7575 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7576 * <len> to its length.
7577 * This is intended to be used with acl_match_meth() only.
7578 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007579static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007580acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7581 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007582{
7583 int meth;
7584 struct http_txn *txn = l7;
7585
Willy Tarreaub6866442008-07-14 23:54:42 +02007586 if (!txn)
7587 return 0;
7588
Willy Tarreau655dce92009-11-08 13:10:58 +01007589 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007590 return 0;
7591
Willy Tarreau8797c062007-05-07 00:55:35 +02007592 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007593 temp_pattern.data.str.len = meth;
7594 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007595 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007596 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7597 /* ensure the indexes are not affected */
7598 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007599 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7600 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007601 }
7602 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7603 return 1;
7604}
7605
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007606/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007607static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7608{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007609 int icase;
7610
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007611
7612 if (temp_pattern.data.str.str == NULL) {
7613 /* well-known method */
7614 if (temp_pattern.data.str.len == pattern->val.i)
7615 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007616 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007617 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007618
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007619 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7620 if (pattern->val.i != HTTP_METH_OTHER)
7621 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007622
7623 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007624 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007625 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007626
7627 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007628 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7629 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007630 return ACL_PAT_FAIL;
7631 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007632}
7633
7634/* 2. Check on Request/Status Version
7635 * We simply compare strings here.
7636 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007637static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007638{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007639 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007640 if (!pattern->ptr.str)
7641 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007642 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007643 return 1;
7644}
7645
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007646static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007647acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7648 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007649{
7650 struct http_txn *txn = l7;
7651 char *ptr;
7652 int len;
7653
Willy Tarreaub6866442008-07-14 23:54:42 +02007654 if (!txn)
7655 return 0;
7656
Willy Tarreau655dce92009-11-08 13:10:58 +01007657 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007658 return 0;
7659
Willy Tarreau8797c062007-05-07 00:55:35 +02007660 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007661 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007662
7663 while ((len-- > 0) && (*ptr++ != '/'));
7664 if (len <= 0)
7665 return 0;
7666
Willy Tarreau664092c2011-12-16 19:11:42 +01007667 temp_pattern.data.str.str = ptr;
7668 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007669
7670 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7671 return 1;
7672}
7673
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007674static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007675acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7676 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007677{
7678 struct http_txn *txn = l7;
7679 char *ptr;
7680 int len;
7681
Willy Tarreaub6866442008-07-14 23:54:42 +02007682 if (!txn)
7683 return 0;
7684
Willy Tarreau655dce92009-11-08 13:10:58 +01007685 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007686 return 0;
7687
Willy Tarreau8797c062007-05-07 00:55:35 +02007688 len = txn->rsp.sl.st.v_l;
7689 ptr = txn->rsp.sol;
7690
7691 while ((len-- > 0) && (*ptr++ != '/'));
7692 if (len <= 0)
7693 return 0;
7694
Willy Tarreau664092c2011-12-16 19:11:42 +01007695 temp_pattern.data.str.str = ptr;
7696 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007697
7698 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7699 return 1;
7700}
7701
7702/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007703static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007704acl_fetch_stcode(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 char *ptr;
7709 int len;
7710
Willy Tarreaub6866442008-07-14 23:54:42 +02007711 if (!txn)
7712 return 0;
7713
Willy Tarreau655dce92009-11-08 13:10:58 +01007714 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007715 return 0;
7716
Willy Tarreau8797c062007-05-07 00:55:35 +02007717 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007718 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007719
Willy Tarreaua5e37562011-12-16 17:06:15 +01007720 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007721 test->flags = ACL_TEST_F_VOL_1ST;
7722 return 1;
7723}
7724
7725/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007726static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007727acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7728 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007729{
7730 struct http_txn *txn = l7;
7731
Willy Tarreaub6866442008-07-14 23:54:42 +02007732 if (!txn)
7733 return 0;
7734
Willy Tarreau655dce92009-11-08 13:10:58 +01007735 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007736 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007737
Willy Tarreauc11416f2007-06-17 16:58:38 +02007738 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7739 /* ensure the indexes are not affected */
7740 return 0;
7741
Willy Tarreau664092c2011-12-16 19:11:42 +01007742 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7743 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007744
Willy Tarreauf3d25982007-05-08 22:45:09 +02007745 /* we do not need to set READ_ONLY because the data is in a buffer */
7746 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007747 return 1;
7748}
7749
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007750static int
7751acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7752 struct acl_expr *expr, struct acl_test *test)
7753{
7754 struct http_txn *txn = l7;
7755
Willy Tarreaub6866442008-07-14 23:54:42 +02007756 if (!txn)
7757 return 0;
7758
Willy Tarreau655dce92009-11-08 13:10:58 +01007759 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007760 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007761
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007762 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7763 /* ensure the indexes are not affected */
7764 return 0;
7765
7766 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007767 url2sa(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 +01007768 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7769 return 0;
7770 temp_pattern.type = PATTERN_TYPE_IP;
7771 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007772
7773 /*
7774 * If we are parsing url in frontend space, we prepare backend stage
7775 * to not parse again the same url ! optimization lazyness...
7776 */
7777 if (px->options & PR_O_HTTP_PROXY)
7778 l4->flags |= SN_ADDR_SET;
7779
Willy Tarreauf4362b32011-12-16 17:49:52 +01007780 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007781 return 1;
7782}
7783
7784static int
7785acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7786 struct acl_expr *expr, struct acl_test *test)
7787{
7788 struct http_txn *txn = l7;
7789
Willy Tarreaub6866442008-07-14 23:54:42 +02007790 if (!txn)
7791 return 0;
7792
Willy Tarreau655dce92009-11-08 13:10:58 +01007793 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007794 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007795
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007796 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7797 /* ensure the indexes are not affected */
7798 return 0;
7799
7800 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007801 url2sa(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 +01007802 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007803
7804 if (px->options & PR_O_HTTP_PROXY)
7805 l4->flags |= SN_ADDR_SET;
7806
7807 test->flags = ACL_TEST_F_READ_ONLY;
7808 return 1;
7809}
7810
Willy Tarreauc11416f2007-06-17 16:58:38 +02007811/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7812 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7813 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007814static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007815acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007816 struct acl_expr *expr, struct acl_test *test)
7817{
7818 struct http_txn *txn = l7;
7819 struct hdr_idx *idx = &txn->hdr_idx;
7820 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007821
Willy Tarreaub6866442008-07-14 23:54:42 +02007822 if (!txn)
7823 return 0;
7824
Willy Tarreau33a7e692007-06-10 19:45:56 +02007825 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7826 /* search for header from the beginning */
7827 ctx->idx = 0;
7828
Willy Tarreau33a7e692007-06-10 19:45:56 +02007829 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7830 test->flags |= ACL_TEST_F_FETCH_MORE;
7831 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007832 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7833 temp_pattern.data.str.len = ctx->vlen;
7834
Willy Tarreau33a7e692007-06-10 19:45:56 +02007835 return 1;
7836 }
7837
7838 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7839 test->flags |= ACL_TEST_F_VOL_HDR;
7840 return 0;
7841}
7842
Willy Tarreau33a7e692007-06-10 19:45:56 +02007843static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007844acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7845 struct acl_expr *expr, struct acl_test *test)
7846{
7847 struct http_txn *txn = l7;
7848
Willy Tarreaub6866442008-07-14 23:54:42 +02007849 if (!txn)
7850 return 0;
7851
Willy Tarreau655dce92009-11-08 13:10:58 +01007852 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007853 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007854
Willy Tarreauc11416f2007-06-17 16:58:38 +02007855 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7856 /* ensure the indexes are not affected */
7857 return 0;
7858
7859 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7860}
7861
7862static int
7863acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7864 struct acl_expr *expr, struct acl_test *test)
7865{
7866 struct http_txn *txn = l7;
7867
Willy Tarreaub6866442008-07-14 23:54:42 +02007868 if (!txn)
7869 return 0;
7870
Willy Tarreau655dce92009-11-08 13:10:58 +01007871 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007872 return 0;
7873
7874 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7875}
7876
7877/* 6. Check on HTTP header count. The number of occurrences is returned.
7878 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7879 */
7880static int
7881acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007882 struct acl_expr *expr, struct acl_test *test)
7883{
7884 struct http_txn *txn = l7;
7885 struct hdr_idx *idx = &txn->hdr_idx;
7886 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007887 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007888
Willy Tarreaub6866442008-07-14 23:54:42 +02007889 if (!txn)
7890 return 0;
7891
Willy Tarreau33a7e692007-06-10 19:45:56 +02007892 ctx.idx = 0;
7893 cnt = 0;
7894 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7895 cnt++;
7896
Willy Tarreaua5e37562011-12-16 17:06:15 +01007897 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007898 test->flags = ACL_TEST_F_VOL_HDR;
7899 return 1;
7900}
7901
Willy Tarreauc11416f2007-06-17 16:58:38 +02007902static int
7903acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7904 struct acl_expr *expr, struct acl_test *test)
7905{
7906 struct http_txn *txn = l7;
7907
Willy Tarreaub6866442008-07-14 23:54:42 +02007908 if (!txn)
7909 return 0;
7910
Willy Tarreau655dce92009-11-08 13:10:58 +01007911 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007912 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007913
Willy Tarreauc11416f2007-06-17 16:58:38 +02007914 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7915 /* ensure the indexes are not affected */
7916 return 0;
7917
7918 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7919}
7920
7921static int
7922acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7923 struct acl_expr *expr, struct acl_test *test)
7924{
7925 struct http_txn *txn = l7;
7926
Willy Tarreaub6866442008-07-14 23:54:42 +02007927 if (!txn)
7928 return 0;
7929
Willy Tarreau655dce92009-11-08 13:10:58 +01007930 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007931 return 0;
7932
7933 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7934}
7935
Willy Tarreau33a7e692007-06-10 19:45:56 +02007936/* 7. Check on HTTP header's integer value. The integer value is returned.
7937 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007938 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007939 */
7940static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007941acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007942 struct acl_expr *expr, struct acl_test *test)
7943{
7944 struct http_txn *txn = l7;
7945 struct hdr_idx *idx = &txn->hdr_idx;
7946 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007947
Willy Tarreaub6866442008-07-14 23:54:42 +02007948 if (!txn)
7949 return 0;
7950
Willy Tarreau33a7e692007-06-10 19:45:56 +02007951 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7952 /* search for header from the beginning */
7953 ctx->idx = 0;
7954
Willy Tarreau33a7e692007-06-10 19:45:56 +02007955 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7956 test->flags |= ACL_TEST_F_FETCH_MORE;
7957 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007958 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007959 return 1;
7960 }
7961
7962 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7963 test->flags |= ACL_TEST_F_VOL_HDR;
7964 return 0;
7965}
7966
Willy Tarreauc11416f2007-06-17 16:58:38 +02007967static int
7968acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7969 struct acl_expr *expr, struct acl_test *test)
7970{
7971 struct http_txn *txn = l7;
7972
Willy Tarreaub6866442008-07-14 23:54:42 +02007973 if (!txn)
7974 return 0;
7975
Willy Tarreau655dce92009-11-08 13:10:58 +01007976 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007977 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007978
Willy Tarreauc11416f2007-06-17 16:58:38 +02007979 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7980 /* ensure the indexes are not affected */
7981 return 0;
7982
7983 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7984}
7985
7986static int
7987acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7988 struct acl_expr *expr, struct acl_test *test)
7989{
7990 struct http_txn *txn = l7;
7991
Willy Tarreaub6866442008-07-14 23:54:42 +02007992 if (!txn)
7993 return 0;
7994
Willy Tarreau655dce92009-11-08 13:10:58 +01007995 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007996 return 0;
7997
7998 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7999}
8000
Willy Tarreau106f9792009-09-19 07:54:16 +02008001/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8002 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8003 */
8004static int
8005acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8006 struct acl_expr *expr, struct acl_test *test)
8007{
8008 struct http_txn *txn = l7;
8009 struct hdr_idx *idx = &txn->hdr_idx;
8010 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8011
8012 if (!txn)
8013 return 0;
8014
8015 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8016 /* search for header from the beginning */
8017 ctx->idx = 0;
8018
Willy Tarreauf4362b32011-12-16 17:49:52 +01008019 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008020 test->flags |= ACL_TEST_F_FETCH_MORE;
8021 test->flags |= ACL_TEST_F_VOL_HDR;
8022 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008023 temp_pattern.type = PATTERN_TYPE_IP;
8024 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8025 return 1;
8026 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008027 }
8028
8029 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8030 test->flags |= ACL_TEST_F_VOL_HDR;
8031 return 0;
8032}
8033
8034static int
8035acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8036 struct acl_expr *expr, struct acl_test *test)
8037{
8038 struct http_txn *txn = l7;
8039
8040 if (!txn)
8041 return 0;
8042
Willy Tarreau655dce92009-11-08 13:10:58 +01008043 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008044 return 0;
8045
8046 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8047 /* ensure the indexes are not affected */
8048 return 0;
8049
8050 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8051}
8052
8053static int
8054acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8055 struct acl_expr *expr, struct acl_test *test)
8056{
8057 struct http_txn *txn = l7;
8058
8059 if (!txn)
8060 return 0;
8061
Willy Tarreau655dce92009-11-08 13:10:58 +01008062 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008063 return 0;
8064
8065 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8066}
8067
Willy Tarreau737b0c12007-06-10 21:28:46 +02008068/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8069 * the first '/' after the possible hostname, and ends before the possible '?'.
8070 */
8071static int
8072acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8073 struct acl_expr *expr, struct acl_test *test)
8074{
8075 struct http_txn *txn = l7;
8076 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008077
Willy Tarreaub6866442008-07-14 23:54:42 +02008078 if (!txn)
8079 return 0;
8080
Willy Tarreau655dce92009-11-08 13:10:58 +01008081 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008082 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008083
Willy Tarreauc11416f2007-06-17 16:58:38 +02008084 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8085 /* ensure the indexes are not affected */
8086 return 0;
8087
Willy Tarreau962c3f42010-01-10 00:15:35 +01008088 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008089 ptr = http_get_path(txn);
8090 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008091 return 0;
8092
8093 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008094 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008095
8096 while (ptr < end && *ptr != '?')
8097 ptr++;
8098
Willy Tarreau664092c2011-12-16 19:11:42 +01008099 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008100
8101 /* we do not need to set READ_ONLY because the data is in a buffer */
8102 test->flags = ACL_TEST_F_VOL_1ST;
8103 return 1;
8104}
8105
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008106static int
8107acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8108 struct acl_expr *expr, struct acl_test *test)
8109{
8110 struct buffer *req = s->req;
8111 struct http_txn *txn = &s->txn;
8112 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008113
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008114 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8115 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8116 */
8117
8118 if (!s || !req)
8119 return 0;
8120
Willy Tarreau655dce92009-11-08 13:10:58 +01008121 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008122 /* Already decoded as OK */
8123 test->flags |= ACL_TEST_F_SET_RES_PASS;
8124 return 1;
8125 }
8126
8127 /* Try to decode HTTP request */
8128 if (likely(req->lr < req->r))
8129 http_msg_analyzer(req, msg, &txn->hdr_idx);
8130
Willy Tarreau655dce92009-11-08 13:10:58 +01008131 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008132 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8133 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8134 return 1;
8135 }
8136 /* wait for final state */
8137 test->flags |= ACL_TEST_F_MAY_CHANGE;
8138 return 0;
8139 }
8140
8141 /* OK we got a valid HTTP request. We have some minor preparation to
8142 * perform so that further checks can rely on HTTP tests.
8143 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008144 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008145 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8146 s->flags |= SN_REDIRECTABLE;
8147
8148 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8149 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8150 return 1;
8151 }
8152
8153 test->flags |= ACL_TEST_F_SET_RES_PASS;
8154 return 1;
8155}
8156
Willy Tarreau7f18e522010-10-22 20:04:13 +02008157/* return a valid test if the current request is the first one on the connection */
8158static int
8159acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8160 struct acl_expr *expr, struct acl_test *test)
8161{
8162 if (!s)
8163 return 0;
8164
8165 if (s->txn.flags & TX_NOT_FIRST)
8166 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8167 else
8168 test->flags |= ACL_TEST_F_SET_RES_PASS;
8169
8170 return 1;
8171}
8172
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008173static int
8174acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8175 struct acl_expr *expr, struct acl_test *test)
8176{
8177
8178 if (!s)
8179 return 0;
8180
8181 if (!get_http_auth(s))
8182 return 0;
8183
8184 test->ctx.a[0] = expr->arg.ul;
8185 test->ctx.a[1] = s->txn.auth.user;
8186 test->ctx.a[2] = s->txn.auth.pass;
8187
8188 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8189
8190 return 1;
8191}
Willy Tarreau8797c062007-05-07 00:55:35 +02008192
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008193/* Try to find the next occurrence of a cookie name in a cookie header value.
8194 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8195 * the cookie value is returned into *value and *value_l, and the function
8196 * returns a pointer to the next pointer to search from if the value was found.
8197 * Otherwise if the cookie was not found, NULL is returned and neither value
8198 * nor value_l are touched. The input <hdr> string should first point to the
8199 * header's value, and the <hdr_end> pointer must point to the first character
8200 * not part of the value. <list> must be non-zero if value may represent a list
8201 * of values (cookie headers). This makes it faster to abort parsing when no
8202 * list is expected.
8203 */
8204static char *
8205extract_cookie_value(char *hdr, const char *hdr_end,
8206 char *cookie_name, size_t cookie_name_l, int list,
8207 char **value, size_t *value_l)
8208{
8209 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8210 char *next;
8211
8212 /* we search at least a cookie name followed by an equal, and more
8213 * generally something like this :
8214 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8215 */
8216 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8217 /* Iterate through all cookies on this line */
8218
8219 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8220 att_beg++;
8221
8222 /* find att_end : this is the first character after the last non
8223 * space before the equal. It may be equal to hdr_end.
8224 */
8225 equal = att_end = att_beg;
8226
8227 while (equal < hdr_end) {
8228 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8229 break;
8230 if (http_is_spht[(unsigned char)*equal++])
8231 continue;
8232 att_end = equal;
8233 }
8234
8235 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8236 * is between <att_beg> and <equal>, both may be identical.
8237 */
8238
8239 /* look for end of cookie if there is an equal sign */
8240 if (equal < hdr_end && *equal == '=') {
8241 /* look for the beginning of the value */
8242 val_beg = equal + 1;
8243 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8244 val_beg++;
8245
8246 /* find the end of the value, respecting quotes */
8247 next = find_cookie_value_end(val_beg, hdr_end);
8248
8249 /* make val_end point to the first white space or delimitor after the value */
8250 val_end = next;
8251 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8252 val_end--;
8253 } else {
8254 val_beg = val_end = next = equal;
8255 }
8256
8257 /* We have nothing to do with attributes beginning with '$'. However,
8258 * they will automatically be removed if a header before them is removed,
8259 * since they're supposed to be linked together.
8260 */
8261 if (*att_beg == '$')
8262 continue;
8263
8264 /* Ignore cookies with no equal sign */
8265 if (equal == next)
8266 continue;
8267
8268 /* Now we have the cookie name between att_beg and att_end, and
8269 * its value between val_beg and val_end.
8270 */
8271
8272 if (att_end - att_beg == cookie_name_l &&
8273 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8274 /* let's return this value and indicate where to go on from */
8275 *value = val_beg;
8276 *value_l = val_end - val_beg;
8277 return next + 1;
8278 }
8279
8280 /* Set-Cookie headers only have the name in the first attr=value part */
8281 if (!list)
8282 break;
8283 }
8284
8285 return NULL;
8286}
8287
8288/* Iterate over all cookies present in a request. The context is stored in
8289 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8290 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8291 * non-null, then multiple cookies may be parsed on the same line.
8292 * The cookie name is in expr->arg and the name length in expr->arg_len.
8293 */
8294static int
8295acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8296 const char *hdr_name, int hdr_name_len, int multi,
8297 struct acl_expr *expr, struct acl_test *test)
8298{
8299 struct http_txn *txn = l7;
8300 struct hdr_idx *idx = &txn->hdr_idx;
8301 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8302
8303 if (!txn)
8304 return 0;
8305
8306 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8307 /* search for the header from the beginning, we must first initialize
8308 * the search parameters.
8309 */
8310 test->ctx.a[0] = NULL;
8311 ctx->idx = 0;
8312 }
8313
8314 while (1) {
8315 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8316 if (!test->ctx.a[0]) {
8317 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8318 goto out;
8319
8320 if (ctx->vlen < expr->arg_len + 1)
8321 continue;
8322
8323 test->ctx.a[0] = ctx->line + ctx->val;
8324 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8325 }
8326
8327 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8328 expr->arg.str, expr->arg_len, multi,
8329 &temp_pattern.data.str.str,
8330 &temp_pattern.data.str.len);
8331 if (test->ctx.a[0]) {
8332 /* one value was returned into temp_pattern.data.str.{str,len} */
8333 test->flags |= ACL_TEST_F_FETCH_MORE;
8334 test->flags |= ACL_TEST_F_VOL_HDR;
8335 return 1;
8336 }
8337 }
8338
8339 out:
8340 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8341 test->flags |= ACL_TEST_F_VOL_HDR;
8342 return 0;
8343}
8344
8345static int
8346acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8347 struct acl_expr *expr, struct acl_test *test)
8348{
8349 struct http_txn *txn = l7;
8350
8351 if (!txn)
8352 return 0;
8353
8354 if (txn->req.msg_state < HTTP_MSG_BODY)
8355 return 0;
8356
8357 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8358 /* ensure the indexes are not affected */
8359 return 0;
8360
8361 /* The Cookie header allows multiple cookies on the same line */
8362 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8363}
8364
8365static int
8366acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8367 struct acl_expr *expr, struct acl_test *test)
8368{
8369 struct http_txn *txn = l7;
8370
8371 if (!txn)
8372 return 0;
8373
8374 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8375 return 0;
8376
8377 /* The Set-Cookie header allows only one cookie on the same line */
8378 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8379}
8380
8381/* Iterate over all cookies present in a request to count how many occurrences
8382 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8383 * multiple cookies may be parsed on the same line.
8384 */
8385static int
8386acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8387 const char *hdr_name, int hdr_name_len, int multi,
8388 struct acl_expr *expr, struct acl_test *test)
8389{
8390 struct http_txn *txn = l7;
8391 struct hdr_idx *idx = &txn->hdr_idx;
8392 struct hdr_ctx ctx;
8393 int cnt;
8394 char *val_beg, *val_end;
8395
8396 if (!txn)
8397 return 0;
8398
8399 val_beg = NULL;
8400 ctx.idx = 0;
8401 cnt = 0;
8402
8403 while (1) {
8404 /* Note: val_beg == NULL every time we need to fetch a new header */
8405 if (!val_beg) {
8406 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8407 break;
8408
8409 if (ctx.vlen < expr->arg_len + 1)
8410 continue;
8411
8412 val_beg = ctx.line + ctx.val;
8413 val_end = val_beg + ctx.vlen;
8414 }
8415
8416 while ((val_beg = extract_cookie_value(val_beg, val_end,
8417 expr->arg.str, expr->arg_len, multi,
8418 &temp_pattern.data.str.str,
8419 &temp_pattern.data.str.len))) {
8420 cnt++;
8421 }
8422 }
8423
8424 temp_pattern.data.integer = cnt;
8425 test->flags |= ACL_TEST_F_VOL_HDR;
8426 return 1;
8427}
8428
8429static int
8430acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8431 struct acl_expr *expr, struct acl_test *test)
8432{
8433 struct http_txn *txn = l7;
8434
8435 if (!txn)
8436 return 0;
8437
8438 if (txn->req.msg_state < HTTP_MSG_BODY)
8439 return 0;
8440
8441 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8442 /* ensure the indexes are not affected */
8443 return 0;
8444
8445 /* The Cookie header allows multiple cookies on the same line */
8446 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8447}
8448
8449static int
8450acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8451 struct acl_expr *expr, struct acl_test *test)
8452{
8453 struct http_txn *txn = l7;
8454
8455 if (!txn)
8456 return 0;
8457
8458 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8459 return 0;
8460
8461 /* The Set-Cookie header allows only one cookie on the same line */
8462 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8463}
8464
Willy Tarreau8797c062007-05-07 00:55:35 +02008465/************************************************************************/
8466/* All supported keywords must be declared here. */
8467/************************************************************************/
8468
8469/* Note: must not be declared <const> as its list will be overwritten */
8470static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008471 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8472
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008473 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008474 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8475 { "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 +02008476 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008477
Willy Tarreauc4262962010-05-10 23:42:40 +02008478 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008479 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8480 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8481 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8482 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8483 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8484 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008485 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008486 { "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 +02008487 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008488
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008489 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008490 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008491 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8492 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8493 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8494 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8495 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8496 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8497 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008498 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008499 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008500 { "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 +02008501
Willy Tarreauc4262962010-05-10 23:42:40 +02008502 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008503 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8504 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8505 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8506 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8507 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8508 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8509 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008510 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008511 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008512 { "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 +02008513
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008514 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8515 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8516 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8517 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8518 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8519 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8520 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8521 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8522 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8523
8524 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8525 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8526 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8527 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8528 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8529 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8530 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8531 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8532 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8533
Willy Tarreauc4262962010-05-10 23:42:40 +02008534 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008535 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8536 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8537 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8538 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8539 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8540 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008541 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008542
Willy Tarreau7f18e522010-10-22 20:04:13 +02008543 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8544 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8545 { "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 +02008546 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008547}};
8548
Willy Tarreau4a568972010-05-12 08:08:50 +02008549/************************************************************************/
8550/* The code below is dedicated to pattern fetching and matching */
8551/************************************************************************/
8552
Willy Tarreaue428fb72011-12-16 21:50:30 +01008553/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008554static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008555pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8556 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008557{
8558 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008559
Willy Tarreaue428fb72011-12-16 21:50:30 +01008560 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8561 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008562}
8563
David Cournapeau16023ee2010-12-23 20:55:41 +09008564/*
8565 * Given a path string and its length, find the position of beginning of the
8566 * query string. Returns NULL if no query string is found in the path.
8567 *
8568 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8569 *
8570 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8571 */
8572static inline char *find_query_string(char *path, size_t path_l)
8573{
8574 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008575
David Cournapeau16023ee2010-12-23 20:55:41 +09008576 p = memchr(path, '?', path_l);
8577 return p ? p + 1 : NULL;
8578}
8579
8580static inline int is_param_delimiter(char c)
8581{
8582 return c == '&' || c == ';';
8583}
8584
8585/*
8586 * Given a url parameter, find the starting position of the first occurence,
8587 * or NULL if the parameter is not found.
8588 *
8589 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8590 * the function will return query_string+8.
8591 */
8592static char*
8593find_url_param_pos(char* query_string, size_t query_string_l,
8594 char* url_param_name, size_t url_param_name_l)
8595{
8596 char *pos, *last;
8597
8598 pos = query_string;
8599 last = query_string + query_string_l - url_param_name_l - 1;
8600
8601 while (pos <= last) {
8602 if (pos[url_param_name_l] == '=') {
8603 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8604 return pos;
8605 pos += url_param_name_l + 1;
8606 }
8607 while (pos <= last && !is_param_delimiter(*pos))
8608 pos++;
8609 pos++;
8610 }
8611 return NULL;
8612}
8613
8614/*
8615 * Given a url parameter name, returns its value and size into *value and
8616 * *value_l respectively, and returns non-zero. If the parameter is not found,
8617 * zero is returned and value/value_l are not touched.
8618 */
8619static int
8620find_url_param_value(char* path, size_t path_l,
8621 char* url_param_name, size_t url_param_name_l,
8622 char** value, size_t* value_l)
8623{
8624 char *query_string, *qs_end;
8625 char *arg_start;
8626 char *value_start, *value_end;
8627
8628 query_string = find_query_string(path, path_l);
8629 if (!query_string)
8630 return 0;
8631
8632 qs_end = path + path_l;
8633 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8634 url_param_name, url_param_name_l);
8635 if (!arg_start)
8636 return 0;
8637
8638 value_start = arg_start + url_param_name_l + 1;
8639 value_end = value_start;
8640
8641 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8642 value_end++;
8643
8644 *value = value_start;
8645 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008646 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008647}
8648
8649static int
8650pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8651 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8652{
8653 struct http_txn *txn = l7;
8654 struct http_msg *msg = &txn->req;
8655 char *url_param_value;
8656 size_t url_param_value_l;
8657
8658 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8659 arg_p->data.str.str, arg_p->data.str.len,
8660 &url_param_value, &url_param_value_l))
8661 return 0;
8662
8663 data->str.str = url_param_value;
8664 data->str.len = url_param_value_l;
8665 return 1;
8666}
8667
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008668/* Try to find in request or response message is in <msg> and whose transaction
8669 * is in <txn> the last occurrence of a cookie name in all cookie header values
8670 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8671 * pointer and size of the last occurrence of the cookie value is returned into
8672 * <value> and <value_l>, and the function returns non-zero if the value was
8673 * found. Otherwise if the cookie was not found, zero is returned and neither
8674 * value nor value_l are touched. The input hdr string should begin at the
8675 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8676 * value may represent a list of values (cookie headers).
8677 */
8678
8679static int
8680find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8681 const char *hdr_name, int hdr_name_len,
8682 char *cookie_name, size_t cookie_name_l, int list,
8683 char **value, size_t *value_l)
8684{
8685 struct hdr_ctx ctx;
8686 int found = 0;
8687
8688 ctx.idx = 0;
8689 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008690 char *hdr, *end;
8691
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008692 if (ctx.vlen < cookie_name_l + 1)
8693 continue;
8694
Willy Tarreau4573af92012-04-06 18:20:06 +02008695 hdr = ctx.line + ctx.val;
8696 end = hdr + ctx.vlen;
8697 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8698 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008699 }
8700 return found;
8701}
8702
8703static int
8704pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8705 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8706{
8707 struct http_txn *txn = l7;
8708 struct http_msg *msg = &txn->req;
8709 char *cookie_value;
8710 size_t cookie_value_l;
8711 int found = 0;
8712
8713 found = find_cookie_value(msg, txn, "Cookie", 6,
8714 arg_p->data.str.str, arg_p->data.str.len, 1,
8715 &cookie_value, &cookie_value_l);
8716 if (found) {
8717 data->str.str = cookie_value;
8718 data->str.len = cookie_value_l;
8719 }
8720
8721 return found;
8722}
8723
8724
8725static int
8726pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8727 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8728{
8729 struct http_txn *txn = l7;
8730 struct http_msg *msg = &txn->rsp;
8731 char *cookie_value;
8732 size_t cookie_value_l;
8733 int found = 0;
8734
8735 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8736 arg_p->data.str.str, arg_p->data.str.len, 1,
8737 &cookie_value, &cookie_value_l);
8738 if (found) {
8739 data->str.str = cookie_value;
8740 data->str.len = cookie_value_l;
8741 }
8742
8743 return found;
8744}
8745
Emeric Brun485479d2010-09-23 18:02:19 +02008746
Willy Tarreau4a568972010-05-12 08:08:50 +02008747/************************************************************************/
8748/* All supported keywords must be declared here. */
8749/************************************************************************/
8750/* Note: must not be declared <const> as its list will be overwritten */
8751static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008752 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008753 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008754 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8755 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008756 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008757}};
8758
Willy Tarreau8797c062007-05-07 00:55:35 +02008759
8760__attribute__((constructor))
8761static void __http_protocol_init(void)
8762{
8763 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008764 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008765}
8766
8767
Willy Tarreau58f10d72006-12-04 02:26:12 +01008768/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008769 * Local variables:
8770 * c-indent-level: 8
8771 * c-basic-offset: 8
8772 * End:
8773 */