blob: 98386aaa68f9a4ac58e13686ce33582424ec3c36 [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 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003383 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003384
Willy Tarreau6471afb2011-09-23 10:54:59 +02003385 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003386 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003387 (((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 +02003388 != s->fe->except_to.s_addr) &&
3389 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003390 (((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 +02003391 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003392 int len;
3393 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003394 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003395
3396 /* Note: we rely on the backend to get the header name to be used for
3397 * x-original-to, because the header is really meant for the backends.
3398 * However, if the backend did not specify any option, we have to rely
3399 * on the frontend's header name.
3400 */
3401 if (s->be->orgto_hdr_len) {
3402 len = s->be->orgto_hdr_len;
3403 memcpy(trash, s->be->orgto_hdr_name, len);
3404 } else {
3405 len = s->fe->orgto_hdr_len;
3406 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003407 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003408 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3409
3410 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003411 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003412 goto return_bad_req;
3413 }
3414 }
3415 }
3416
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003417 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3418 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003419 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003420 unsigned int want_flags = 0;
3421
3422 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003423 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3424 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3425 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003426 want_flags |= TX_CON_CLO_SET;
3427 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003428 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3429 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3430 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003431 want_flags |= TX_CON_KAL_SET;
3432 }
3433
3434 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3435 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003437
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003438
Willy Tarreau522d6c02009-12-06 18:49:18 +01003439 /* If we have no server assigned yet and we're balancing on url_param
3440 * with a POST request, we may be interested in checking the body for
3441 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 */
3443 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3444 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003445 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003446 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003447 buffer_dont_connect(req);
3448 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003449 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003450
Willy Tarreau5e205522011-12-17 16:34:27 +01003451 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003452 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003453#ifdef TCP_QUICKACK
3454 /* We expect some data from the client. Unless we know for sure
3455 * we already have a full request, we have to re-enable quick-ack
3456 * in case we previously disabled it, otherwise we might cause
3457 * the client to delay further data.
3458 */
3459 if ((s->listener->options & LI_O_NOQUICKACK) &&
3460 ((txn->flags & TX_REQ_TE_CHNK) ||
3461 (msg->body_len > req->l - txn->req.eoh - 2)))
3462 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3463#endif
3464 }
Willy Tarreau03945942009-12-22 16:50:27 +01003465
Willy Tarreau59234e92008-11-30 23:51:27 +01003466 /*************************************************************
3467 * OK, that's finished for the headers. We have done what we *
3468 * could. Let's switch to the DATA state. *
3469 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003470 req->analyse_exp = TICK_ETERNITY;
3471 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003472
Willy Tarreau59234e92008-11-30 23:51:27 +01003473 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003474 /* OK let's go on with the BODY now */
3475 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003476
Willy Tarreau59234e92008-11-30 23:51:27 +01003477 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003478 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003479 /* we detected a parsing error. We want to archive this request
3480 * in the dedicated proxy area for later troubleshooting.
3481 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003482 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003483 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003484
Willy Tarreau59234e92008-11-30 23:51:27 +01003485 txn->req.msg_state = HTTP_MSG_ERROR;
3486 txn->status = 400;
3487 req->analysers = 0;
3488 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003489
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003490 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003491 if (s->listener->counters)
3492 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003493
Willy Tarreau59234e92008-11-30 23:51:27 +01003494 if (!(s->flags & SN_ERR_MASK))
3495 s->flags |= SN_ERR_PRXCOND;
3496 if (!(s->flags & SN_FINST_MASK))
3497 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003498 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003499}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003500
Willy Tarreau60b85b02008-11-30 23:28:40 +01003501/* This function is an analyser which processes the HTTP tarpit. It always
3502 * returns zero, at the beginning because it prevents any other processing
3503 * from occurring, and at the end because it terminates the request.
3504 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003505int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003506{
3507 struct http_txn *txn = &s->txn;
3508
3509 /* This connection is being tarpitted. The CLIENT side has
3510 * already set the connect expiration date to the right
3511 * timeout. We just have to check that the client is still
3512 * there and that the timeout has not expired.
3513 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003514 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003515 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3516 !tick_is_expired(req->analyse_exp, now_ms))
3517 return 0;
3518
3519 /* We will set the queue timer to the time spent, just for
3520 * logging purposes. We fake a 500 server error, so that the
3521 * attacker will not suspect his connection has been tarpitted.
3522 * It will not cause trouble to the logs because we can exclude
3523 * the tarpitted connections by filtering on the 'PT' status flags.
3524 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003525 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3526
3527 txn->status = 500;
3528 if (req->flags != BF_READ_ERROR)
3529 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3530
3531 req->analysers = 0;
3532 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003533
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003534 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003535 if (s->listener->counters)
3536 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003537
Willy Tarreau60b85b02008-11-30 23:28:40 +01003538 if (!(s->flags & SN_ERR_MASK))
3539 s->flags |= SN_ERR_PRXCOND;
3540 if (!(s->flags & SN_FINST_MASK))
3541 s->flags |= SN_FINST_T;
3542 return 0;
3543}
3544
Willy Tarreaud34af782008-11-30 23:36:37 +01003545/* This function is an analyser which processes the HTTP request body. It looks
3546 * for parameters to be used for the load balancing algorithm (url_param). It
3547 * must only be called after the standard HTTP request processing has occurred,
3548 * because it expects the request to be parsed. It returns zero if it needs to
3549 * read more data, or 1 once it has completed its analysis.
3550 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003551int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003552{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003554 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003555 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003556
3557 /* We have to parse the HTTP request body to find any required data.
3558 * "balance url_param check_post" should have been the only way to get
3559 * into this. We were brought here after HTTP header analysis, so all
3560 * related structures are ready.
3561 */
3562
Willy Tarreau522d6c02009-12-06 18:49:18 +01003563 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3564 goto missing_data;
3565
3566 if (msg->msg_state < HTTP_MSG_100_SENT) {
3567 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3568 * send an HTTP/1.1 100 Continue intermediate response.
3569 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003570 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003571 struct hdr_ctx ctx;
3572 ctx.idx = 0;
3573 /* Expect is allowed in 1.1, look for it */
3574 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3575 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3576 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3577 }
3578 }
3579 msg->msg_state = HTTP_MSG_100_SENT;
3580 }
3581
3582 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003583 /* we have msg->col and msg->sov which both point to the first
3584 * byte of message body. msg->som still points to the beginning
3585 * of the message. We must save the body in req->lr because it
3586 * survives buffer re-alignments.
3587 */
3588 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589 if (txn->flags & TX_REQ_TE_CHNK)
3590 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3591 else
3592 msg->msg_state = HTTP_MSG_DATA;
3593 }
3594
3595 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003596 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003597 * set ->sov and ->lr to point to the body and switch to DATA or
3598 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003599 */
3600 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003601
Willy Tarreau115acb92009-12-26 13:56:06 +01003602 if (!ret)
3603 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003604 else if (ret < 0) {
3605 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003606 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003607 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003608 }
3609
Willy Tarreaud98cf932009-12-27 22:54:55 +01003610 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003611 * We have the first non-header byte in msg->col, which is either the
3612 * beginning of the chunk size or of the data. The first data byte is in
3613 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3614 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003615 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003616
Willy Tarreau124d9912011-03-01 20:30:48 +01003617 if (msg->body_len < limit)
3618 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003619
Willy Tarreau7c96f672009-12-27 22:47:25 +01003620 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 goto http_end;
3622
3623 missing_data:
3624 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003625 if (req->flags & BF_FULL) {
3626 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003627 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003628 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003629
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3631 txn->status = 408;
3632 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003633
3634 if (!(s->flags & SN_ERR_MASK))
3635 s->flags |= SN_ERR_CLITO;
3636 if (!(s->flags & SN_FINST_MASK))
3637 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003638 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003639 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003640
3641 /* we get here if we need to wait for more data */
3642 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003643 /* Not enough data. We'll re-use the http-request
3644 * timeout here. Ideally, we should set the timeout
3645 * relative to the accept() date. We just set the
3646 * request timeout once at the beginning of the
3647 * request.
3648 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003649 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003650 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003651 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003652 return 0;
3653 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654
3655 http_end:
3656 /* The situation will not evolve, so let's give up on the analysis. */
3657 s->logs.tv_request = now; /* update the request timer to reflect full request */
3658 req->analysers &= ~an_bit;
3659 req->analyse_exp = TICK_ETERNITY;
3660 return 1;
3661
3662 return_bad_req: /* let's centralize all bad requests */
3663 txn->req.msg_state = HTTP_MSG_ERROR;
3664 txn->status = 400;
3665 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3666
Willy Tarreau79ebac62010-06-07 13:47:49 +02003667 if (!(s->flags & SN_ERR_MASK))
3668 s->flags |= SN_ERR_PRXCOND;
3669 if (!(s->flags & SN_FINST_MASK))
3670 s->flags |= SN_FINST_R;
3671
Willy Tarreau522d6c02009-12-06 18:49:18 +01003672 return_err_msg:
3673 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003674 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003675 if (s->listener->counters)
3676 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003677 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003678}
3679
Mark Lamourinec2247f02012-01-04 13:02:01 -05003680int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3681
3682 struct hdr_ctx ctx;
3683
Mark Lamourinec2247f02012-01-04 13:02:01 -05003684 char *hdr_name = be->server_id_hdr_name;
3685 int hdr_name_len = be->server_id_hdr_len;
3686
3687 char *hdr_val;
3688
William Lallemandd9e90662012-01-30 17:27:17 +01003689 ctx.idx = 0;
3690
Mark Lamourinec2247f02012-01-04 13:02:01 -05003691 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3692 /* remove any existing values from the header */
3693 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3694 }
3695
3696 /* Add the new header requested with the server value */
3697 hdr_val = trash;
3698 memcpy(hdr_val, hdr_name, hdr_name_len);
3699 hdr_val += hdr_name_len;
3700 *hdr_val++ = ':';
3701 *hdr_val++ = ' ';
3702 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3703 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3704
3705 return 0;
3706}
3707
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708/* Terminate current transaction and prepare a new one. This is very tricky
3709 * right now but it works.
3710 */
3711void http_end_txn_clean_session(struct session *s)
3712{
3713 /* FIXME: We need a more portable way of releasing a backend's and a
3714 * server's connections. We need a safer way to reinitialize buffer
3715 * flags. We also need a more accurate method for computing per-request
3716 * data.
3717 */
3718 http_silent_debug(__LINE__, s);
3719
3720 s->req->cons->flags |= SI_FL_NOLINGER;
3721 s->req->cons->shutr(s->req->cons);
3722 s->req->cons->shutw(s->req->cons);
3723
3724 http_silent_debug(__LINE__, s);
3725
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003726 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003728 if (unlikely(s->srv_conn))
3729 sess_change_server(s, NULL);
3730 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731
3732 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3733 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003734 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735
3736 if (s->txn.status) {
3737 int n;
3738
3739 n = s->txn.status / 100;
3740 if (n < 1 || n > 5)
3741 n = 0;
3742
3743 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003744 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745
Willy Tarreau24657792010-02-26 10:30:28 +01003746 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003748 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 }
3750
3751 /* don't count other requests' data */
3752 s->logs.bytes_in -= s->req->l - s->req->send_max;
3753 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3754
3755 /* let's do a final log if we need it */
3756 if (s->logs.logwait &&
3757 !(s->flags & SN_MONITOR) &&
3758 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3759 s->do_log(s);
3760 }
3761
3762 s->logs.accept_date = date; /* user-visible date for logging */
3763 s->logs.tv_accept = now; /* corrected date for internal use */
3764 tv_zero(&s->logs.tv_request);
3765 s->logs.t_queue = -1;
3766 s->logs.t_connect = -1;
3767 s->logs.t_data = -1;
3768 s->logs.t_close = 0;
3769 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3770 s->logs.srv_queue_size = 0; /* we will get this number soon */
3771
3772 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3773 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3774
3775 if (s->pend_pos)
3776 pendconn_free(s->pend_pos);
3777
Willy Tarreau827aee92011-03-10 16:55:02 +01003778 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003779 if (s->flags & SN_CURR_SESS) {
3780 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003781 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003783 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3784 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 }
3786
Willy Tarreau9e000c62011-03-10 14:03:36 +01003787 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788
3789 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3790 s->req->cons->fd = -1; /* just to help with debugging */
3791 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003792 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 s->req->cons->err_loc = NULL;
3794 s->req->cons->exp = TICK_ETERNITY;
3795 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003796 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3797 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 +02003798 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 +01003799 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3800 s->txn.meth = 0;
3801 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003802 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003803 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 s->req->cons->flags |= SI_FL_INDEP_STR;
3805
Willy Tarreau96e31212011-05-30 18:10:30 +02003806 if (s->fe->options2 & PR_O2_NODELAY) {
3807 s->req->flags |= BF_NEVER_WAIT;
3808 s->rep->flags |= BF_NEVER_WAIT;
3809 }
3810
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 /* if the request buffer is not empty, it means we're
3812 * about to process another request, so send pending
3813 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003814 * Just don't do this if the buffer is close to be full,
3815 * because the request will wait for it to flush a little
3816 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003818 if (s->req->l > s->req->send_max) {
3819 if (s->rep->send_max &&
3820 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003821 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3822 s->rep->flags |= BF_EXPECT_MORE;
3823 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003824
3825 /* we're removing the analysers, we MUST re-enable events detection */
3826 buffer_auto_read(s->req);
3827 buffer_auto_close(s->req);
3828 buffer_auto_read(s->rep);
3829 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003830
3831 /* make ->lr point to the first non-forwarded byte */
3832 s->req->lr = s->req->w + s->req->send_max;
3833 if (s->req->lr >= s->req->data + s->req->size)
3834 s->req->lr -= s->req->size;
3835 s->rep->lr = s->rep->w + s->rep->send_max;
3836 if (s->rep->lr >= s->rep->data + s->rep->size)
3837 s->rep->lr -= s->req->size;
3838
Willy Tarreau342b11c2010-11-24 16:22:09 +01003839 s->req->analysers = s->listener->analysers;
3840 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003841 s->rep->analysers = 0;
3842
3843 http_silent_debug(__LINE__, s);
3844}
3845
3846
3847/* This function updates the request state machine according to the response
3848 * state machine and buffer flags. It returns 1 if it changes anything (flag
3849 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3850 * it is only used to find when a request/response couple is complete. Both
3851 * this function and its equivalent should loop until both return zero. It
3852 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3853 */
3854int http_sync_req_state(struct session *s)
3855{
3856 struct buffer *buf = s->req;
3857 struct http_txn *txn = &s->txn;
3858 unsigned int old_flags = buf->flags;
3859 unsigned int old_state = txn->req.msg_state;
3860
3861 http_silent_debug(__LINE__, s);
3862 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3863 return 0;
3864
3865 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003866 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003867 * We can shut the read side unless we want to abort_on_close,
3868 * or we have a POST request. The issue with POST requests is
3869 * that some browsers still send a CRLF after the request, and
3870 * this CRLF must be read so that it does not remain in the kernel
3871 * buffers, otherwise a close could cause an RST on some systems
3872 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003873 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003874 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003875 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876
3877 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3878 goto wait_other_side;
3879
3880 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3881 /* The server has not finished to respond, so we
3882 * don't want to move in order not to upset it.
3883 */
3884 goto wait_other_side;
3885 }
3886
3887 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3888 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003889 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890 txn->req.msg_state = HTTP_MSG_TUNNEL;
3891 goto wait_other_side;
3892 }
3893
3894 /* When we get here, it means that both the request and the
3895 * response have finished receiving. Depending on the connection
3896 * mode, we'll have to wait for the last bytes to leave in either
3897 * direction, and sometimes for a close to be effective.
3898 */
3899
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003900 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3901 /* Server-close mode : queue a connection close to the server */
3902 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003904 }
3905 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3906 /* Option forceclose is set, or either side wants to close,
3907 * let's enforce it now that we're not expecting any new
3908 * data to come. The caller knows the session is complete
3909 * once both states are CLOSED.
3910 */
3911 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003912 buffer_shutr_now(buf);
3913 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003914 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003915 }
3916 else {
3917 /* The last possible modes are keep-alive and tunnel. Since tunnel
3918 * mode does not set the body analyser, we can't reach this place
3919 * in tunnel mode, so we're left with keep-alive only.
3920 * This mode is currently not implemented, we switch to tunnel mode.
3921 */
3922 buffer_auto_read(buf);
3923 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924 }
3925
3926 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3927 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003928 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3929
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 if (!(buf->flags & BF_OUT_EMPTY)) {
3931 txn->req.msg_state = HTTP_MSG_CLOSING;
3932 goto http_msg_closing;
3933 }
3934 else {
3935 txn->req.msg_state = HTTP_MSG_CLOSED;
3936 goto http_msg_closed;
3937 }
3938 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003939 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 }
3941
3942 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3943 http_msg_closing:
3944 /* nothing else to forward, just waiting for the output buffer
3945 * to be empty and for the shutw_now to take effect.
3946 */
3947 if (buf->flags & BF_OUT_EMPTY) {
3948 txn->req.msg_state = HTTP_MSG_CLOSED;
3949 goto http_msg_closed;
3950 }
3951 else if (buf->flags & BF_SHUTW) {
3952 txn->req.msg_state = HTTP_MSG_ERROR;
3953 goto wait_other_side;
3954 }
3955 }
3956
3957 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3958 http_msg_closed:
3959 goto wait_other_side;
3960 }
3961
3962 wait_other_side:
3963 http_silent_debug(__LINE__, s);
3964 return txn->req.msg_state != old_state || buf->flags != old_flags;
3965}
3966
3967
3968/* This function updates the response state machine according to the request
3969 * state machine and buffer flags. It returns 1 if it changes anything (flag
3970 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3971 * it is only used to find when a request/response couple is complete. Both
3972 * this function and its equivalent should loop until both return zero. It
3973 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3974 */
3975int http_sync_res_state(struct session *s)
3976{
3977 struct buffer *buf = s->rep;
3978 struct http_txn *txn = &s->txn;
3979 unsigned int old_flags = buf->flags;
3980 unsigned int old_state = txn->rsp.msg_state;
3981
3982 http_silent_debug(__LINE__, s);
3983 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3984 return 0;
3985
3986 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3987 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003988 * still monitor the server connection for a possible close
3989 * while the request is being uploaded, so we don't disable
3990 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003992 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003993
3994 if (txn->req.msg_state == HTTP_MSG_ERROR)
3995 goto wait_other_side;
3996
3997 if (txn->req.msg_state < HTTP_MSG_DONE) {
3998 /* The client seems to still be sending data, probably
3999 * because we got an error response during an upload.
4000 * We have the choice of either breaking the connection
4001 * or letting it pass through. Let's do the later.
4002 */
4003 goto wait_other_side;
4004 }
4005
4006 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4007 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004008 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4010 goto wait_other_side;
4011 }
4012
4013 /* When we get here, it means that both the request and the
4014 * response have finished receiving. Depending on the connection
4015 * mode, we'll have to wait for the last bytes to leave in either
4016 * direction, and sometimes for a close to be effective.
4017 */
4018
4019 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4020 /* Server-close mode : shut read and wait for the request
4021 * side to close its output buffer. The caller will detect
4022 * when we're in DONE and the other is in CLOSED and will
4023 * catch that for the final cleanup.
4024 */
4025 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4026 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004028 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4029 /* Option forceclose is set, or either side wants to close,
4030 * let's enforce it now that we're not expecting any new
4031 * data to come. The caller knows the session is complete
4032 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004034 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4035 buffer_shutr_now(buf);
4036 buffer_shutw_now(buf);
4037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 }
4039 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004040 /* The last possible modes are keep-alive and tunnel. Since tunnel
4041 * mode does not set the body analyser, we can't reach this place
4042 * in tunnel mode, so we're left with keep-alive only.
4043 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004045 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 }
4048
4049 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4050 /* if we've just closed an output, let's switch */
4051 if (!(buf->flags & BF_OUT_EMPTY)) {
4052 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4053 goto http_msg_closing;
4054 }
4055 else {
4056 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4057 goto http_msg_closed;
4058 }
4059 }
4060 goto wait_other_side;
4061 }
4062
4063 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4064 http_msg_closing:
4065 /* nothing else to forward, just waiting for the output buffer
4066 * to be empty and for the shutw_now to take effect.
4067 */
4068 if (buf->flags & BF_OUT_EMPTY) {
4069 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4070 goto http_msg_closed;
4071 }
4072 else if (buf->flags & BF_SHUTW) {
4073 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004074 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004075 if (target_srv(&s->target))
4076 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077 goto wait_other_side;
4078 }
4079 }
4080
4081 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4082 http_msg_closed:
4083 /* drop any pending data */
4084 buffer_ignore(buf, buf->l - buf->send_max);
4085 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004086 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 goto wait_other_side;
4088 }
4089
4090 wait_other_side:
4091 http_silent_debug(__LINE__, s);
4092 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4093}
4094
4095
4096/* Resync the request and response state machines. Return 1 if either state
4097 * changes.
4098 */
4099int http_resync_states(struct session *s)
4100{
4101 struct http_txn *txn = &s->txn;
4102 int old_req_state = txn->req.msg_state;
4103 int old_res_state = txn->rsp.msg_state;
4104
4105 http_silent_debug(__LINE__, s);
4106 http_sync_req_state(s);
4107 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 if (!http_sync_res_state(s))
4110 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004111 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 if (!http_sync_req_state(s))
4113 break;
4114 }
4115 http_silent_debug(__LINE__, s);
4116 /* OK, both state machines agree on a compatible state.
4117 * There are a few cases we're interested in :
4118 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4119 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4120 * directions, so let's simply disable both analysers.
4121 * - HTTP_MSG_CLOSED on the response only means we must abort the
4122 * request.
4123 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4124 * with server-close mode means we've completed one request and we
4125 * must re-initialize the server connection.
4126 */
4127
4128 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4129 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4130 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4131 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4132 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004133 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004134 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004136 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004137 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004139 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4140 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004141 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004142 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004143 s->rep->analysers = 0;
4144 buffer_auto_close(s->rep);
4145 buffer_auto_read(s->rep);
4146 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 buffer_abort(s->req);
4148 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004149 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004151 }
4152 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4153 txn->rsp.msg_state == HTTP_MSG_DONE &&
4154 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4155 /* server-close: terminate this server connection and
4156 * reinitialize a fresh-new transaction.
4157 */
4158 http_end_txn_clean_session(s);
4159 }
4160
4161 http_silent_debug(__LINE__, s);
4162 return txn->req.msg_state != old_req_state ||
4163 txn->rsp.msg_state != old_res_state;
4164}
4165
Willy Tarreaud98cf932009-12-27 22:54:55 +01004166/* This function is an analyser which forwards request body (including chunk
4167 * sizes if any). It is called as soon as we must forward, even if we forward
4168 * zero byte. The only situation where it must not be called is when we're in
4169 * tunnel mode and we want to forward till the close. It's used both to forward
4170 * remaining data and to resync after end of body. It expects the msg_state to
4171 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4172 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004173 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004174 * bytes of pending data + the headers if not already done (between som and sov).
4175 * It eventually adjusts som to match sov after the data in between have been sent.
4176 */
4177int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4178{
4179 struct http_txn *txn = &s->txn;
4180 struct http_msg *msg = &s->txn.req;
4181
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004182 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4183 return 0;
4184
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004185 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4186 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004187 /* Output closed while we were sending data. We must abort and
4188 * wake the other side up.
4189 */
4190 msg->msg_state = HTTP_MSG_ERROR;
4191 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004192 return 1;
4193 }
4194
Willy Tarreau4fe41902010-06-07 22:27:41 +02004195 /* in most states, we should abort in case of early close */
4196 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004197
4198 /* Note that we don't have to send 100-continue back because we don't
4199 * need the data to complete our job, and it's up to the server to
4200 * decide whether to return 100, 417 or anything else in return of
4201 * an "Expect: 100-continue" header.
4202 */
4203
4204 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4205 /* we have msg->col and msg->sov which both point to the first
4206 * byte of message body. msg->som still points to the beginning
4207 * of the message. We must save the body in req->lr because it
4208 * survives buffer re-alignments.
4209 */
4210 req->lr = req->data + msg->sov;
4211 if (txn->flags & TX_REQ_TE_CHNK)
4212 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4213 else {
4214 msg->msg_state = HTTP_MSG_DATA;
4215 }
4216 }
4217
Willy Tarreaud98cf932009-12-27 22:54:55 +01004218 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004219 int bytes;
4220
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004222 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004223 bytes = msg->sov - msg->som;
4224 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004225 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004226 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4227 bytes += req->size;
4228 msg->chunk_len += (unsigned int)bytes;
4229 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004230 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004231
Willy Tarreaucaabe412010-01-03 23:08:28 +01004232 if (msg->msg_state == HTTP_MSG_DATA) {
4233 /* must still forward */
4234 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004235 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004236
4237 /* nothing left to forward */
4238 if (txn->flags & TX_REQ_TE_CHNK)
4239 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004241 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004242 }
4243 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004244 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245 * set ->sov and ->lr to point to the body and switch to DATA or
4246 * TRAILERS state.
4247 */
4248 int ret = http_parse_chunk_size(req, msg);
4249
4250 if (!ret)
4251 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004252 else if (ret < 0) {
4253 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004254 if (msg->err_pos >= 0)
4255 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004256 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004257 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004260 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4261 /* we want the CRLF after the data */
4262 int ret;
4263
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004264 req->lr = req->w + req->send_max;
4265 if (req->lr >= req->data + req->size)
4266 req->lr -= req->size;
4267
Willy Tarreaud98cf932009-12-27 22:54:55 +01004268 ret = http_skip_chunk_crlf(req, msg);
4269
4270 if (ret == 0)
4271 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004272 else if (ret < 0) {
4273 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004274 if (msg->err_pos >= 0)
4275 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004276 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004277 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004278 /* we're in MSG_CHUNK_SIZE now */
4279 }
4280 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4281 int ret = http_forward_trailers(req, msg);
4282
4283 if (ret == 0)
4284 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004285 else if (ret < 0) {
4286 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004287 if (msg->err_pos >= 0)
4288 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004289 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004290 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004291 /* we're in HTTP_MSG_DONE now */
4292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004294 int old_state = msg->msg_state;
4295
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004297 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004298 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4299 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4300 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 if (http_resync_states(s)) {
4302 /* some state changes occurred, maybe the analyser
4303 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004304 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004305 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4306 if (req->flags & BF_SHUTW) {
4307 /* request errors are most likely due to
4308 * the server aborting the transfer.
4309 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004310 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004311 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004312 if (msg->err_pos >= 0)
4313 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004314 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004315 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004317 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004318
4319 /* If "option abortonclose" is set on the backend, we
4320 * want to monitor the client's connection and forward
4321 * any shutdown notification to the server, which will
4322 * decide whether to close or to go on processing the
4323 * request.
4324 */
4325 if (s->be->options & PR_O_ABRT_CLOSE) {
4326 buffer_auto_read(req);
4327 buffer_auto_close(req);
4328 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004329 else if (s->txn.meth == HTTP_METH_POST) {
4330 /* POST requests may require to read extra CRLF
4331 * sent by broken browsers and which could cause
4332 * an RST to be sent upon close on some systems
4333 * (eg: Linux).
4334 */
4335 buffer_auto_read(req);
4336 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004337
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004339 }
4340 }
4341
Willy Tarreaud98cf932009-12-27 22:54:55 +01004342 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004343 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004344 if (req->flags & BF_SHUTR) {
4345 if (!(s->flags & SN_ERR_MASK))
4346 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004347 if (!(s->flags & SN_FINST_MASK)) {
4348 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4349 s->flags |= SN_FINST_H;
4350 else
4351 s->flags |= SN_FINST_D;
4352 }
4353
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004354 s->fe->fe_counters.cli_aborts++;
4355 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004356 if (target_srv(&s->target))
4357 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004358
4359 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004360 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004361
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004362 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004363 if (req->flags & BF_SHUTW)
4364 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004366 /* When TE: chunked is used, we need to get there again to parse remaining
4367 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4368 */
4369 if (txn->flags & TX_REQ_TE_CHNK)
4370 buffer_dont_close(req);
4371
Willy Tarreau5c620922011-05-11 19:56:11 +02004372 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004373 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4374 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004375 * modes are already handled by the stream sock layer. We must not do
4376 * this in content-length mode because it could present the MSG_MORE
4377 * flag with the last block of forwarded data, which would cause an
4378 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004379 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004380 if (txn->flags & TX_REQ_TE_CHNK)
4381 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004382
Willy Tarreau610ecce2010-01-04 21:15:02 +01004383 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004384 return 0;
4385
Willy Tarreaud98cf932009-12-27 22:54:55 +01004386 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004387 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004388 if (s->listener->counters)
4389 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004390 return_bad_req_stats_ok:
4391 txn->req.msg_state = HTTP_MSG_ERROR;
4392 if (txn->status) {
4393 /* Note: we don't send any error if some data were already sent */
4394 stream_int_retnclose(req->prod, NULL);
4395 } else {
4396 txn->status = 400;
4397 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4398 }
4399 req->analysers = 0;
4400 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004401
4402 if (!(s->flags & SN_ERR_MASK))
4403 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004404 if (!(s->flags & SN_FINST_MASK)) {
4405 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4406 s->flags |= SN_FINST_H;
4407 else
4408 s->flags |= SN_FINST_D;
4409 }
4410 return 0;
4411
4412 aborted_xfer:
4413 txn->req.msg_state = HTTP_MSG_ERROR;
4414 if (txn->status) {
4415 /* Note: we don't send any error if some data were already sent */
4416 stream_int_retnclose(req->prod, NULL);
4417 } else {
4418 txn->status = 502;
4419 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4420 }
4421 req->analysers = 0;
4422 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4423
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004424 s->fe->fe_counters.srv_aborts++;
4425 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004426 if (target_srv(&s->target))
4427 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004428
4429 if (!(s->flags & SN_ERR_MASK))
4430 s->flags |= SN_ERR_SRVCL;
4431 if (!(s->flags & SN_FINST_MASK)) {
4432 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4433 s->flags |= SN_FINST_H;
4434 else
4435 s->flags |= SN_FINST_D;
4436 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437 return 0;
4438}
4439
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4441 * processing can continue on next analysers, or zero if it either needs more
4442 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4443 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4444 * when it has nothing left to do, and may remove any analyser when it wants to
4445 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004446 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004448{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 struct http_txn *txn = &s->txn;
4450 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004451 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004452 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004454 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004455
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004456 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 +02004457 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004459 rep,
4460 rep->rex, rep->wex,
4461 rep->flags,
4462 rep->l,
4463 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004464
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465 /*
4466 * Now parse the partial (or complete) lines.
4467 * We will check the response syntax, and also join multi-line
4468 * headers. An index of all the lines will be elaborated while
4469 * parsing.
4470 *
4471 * For the parsing, we use a 28 states FSM.
4472 *
4473 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004474 * rep->data + msg->som = beginning of response
4475 * rep->data + msg->eoh = end of processed headers / start of current one
4476 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 * rep->lr = first non-visited byte
4478 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004479 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 */
4481
Willy Tarreau83e3af02009-12-28 17:39:57 +01004482 /* There's a protected area at the end of the buffer for rewriting
4483 * purposes. We don't want to start to parse the request if the
4484 * protected area is affected, because we may have to move processed
4485 * data later, which is much more complicated.
4486 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004487 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4488 if (unlikely((rep->flags & BF_FULL) ||
4489 rep->r < rep->lr ||
4490 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4491 if (rep->send_max) {
4492 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004493 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4494 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004495 buffer_dont_close(rep);
4496 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4497 return 0;
4498 }
4499 if (rep->l <= rep->size - global.tune.maxrewrite)
4500 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004501 }
4502
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004503 if (likely(rep->lr < rep->r))
4504 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004505 }
4506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 /* 1: we might have to print this header in debug mode */
4508 if (unlikely((global.mode & MODE_DEBUG) &&
4509 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004510 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004511 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004512 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004513
Willy Tarreau663308b2010-06-07 14:06:08 +02004514 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004515 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 sol += hdr_idx_first_pos(&txn->hdr_idx);
4519 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004520
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 while (cur_idx) {
4522 eol = sol + txn->hdr_idx.v[cur_idx].len;
4523 debug_hdr("srvhdr", s, sol, eol);
4524 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4525 cur_idx = txn->hdr_idx.v[cur_idx].next;
4526 }
4527 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 /*
4530 * Now we quickly check if we have found a full valid response.
4531 * If not so, we check the FD and buffer states before leaving.
4532 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004533 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534 * responses are checked first.
4535 *
4536 * Depending on whether the client is still there or not, we
4537 * may send an error response back or not. Note that normally
4538 * we should only check for HTTP status there, and check I/O
4539 * errors somewhere else.
4540 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004541
Willy Tarreau655dce92009-11-08 13:10:58 +01004542 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543 /* Invalid response */
4544 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4545 /* we detected a parsing error. We want to archive this response
4546 * in the dedicated proxy area for later troubleshooting.
4547 */
4548 hdr_response_bad:
4549 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004550 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004552 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004553 if (target_srv(&s->target)) {
4554 target_srv(&s->target)->counters.failed_resp++;
4555 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004556 }
Willy Tarreau64648412010-03-05 10:41:54 +01004557 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004558 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004559 rep->analysers = 0;
4560 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004561 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004562 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004563 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4564
4565 if (!(s->flags & SN_ERR_MASK))
4566 s->flags |= SN_ERR_PRXCOND;
4567 if (!(s->flags & SN_FINST_MASK))
4568 s->flags |= SN_FINST_H;
4569
4570 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 /* too large response does not fit in buffer. */
4574 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004575 if (msg->err_pos < 0)
4576 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 goto hdr_response_bad;
4578 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004579
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580 /* read error */
4581 else if (rep->flags & BF_READ_ERROR) {
4582 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004583 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004584
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004585 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004586 if (target_srv(&s->target)) {
4587 target_srv(&s->target)->counters.failed_resp++;
4588 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004589 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004590
Willy Tarreau90deb182010-01-07 00:20:41 +01004591 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004592 rep->analysers = 0;
4593 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004594 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004595 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004596 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 if (!(s->flags & SN_ERR_MASK))
4599 s->flags |= SN_ERR_SRVCL;
4600 if (!(s->flags & SN_FINST_MASK))
4601 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004602 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004604
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004605 /* read timeout : return a 504 to the client. */
4606 else if (rep->flags & BF_READ_TIMEOUT) {
4607 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004608 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004609
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004610 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004611 if (target_srv(&s->target)) {
4612 target_srv(&s->target)->counters.failed_resp++;
4613 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004614 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004615
Willy Tarreau90deb182010-01-07 00:20:41 +01004616 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 rep->analysers = 0;
4618 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004619 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004620 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004622
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 if (!(s->flags & SN_ERR_MASK))
4624 s->flags |= SN_ERR_SRVTO;
4625 if (!(s->flags & SN_FINST_MASK))
4626 s->flags |= SN_FINST_H;
4627 return 0;
4628 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004629
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004630 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004632 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004633 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004634
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004635 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004636 if (target_srv(&s->target)) {
4637 target_srv(&s->target)->counters.failed_resp++;
4638 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004639 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004640
Willy Tarreau90deb182010-01-07 00:20:41 +01004641 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 rep->analysers = 0;
4643 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004644 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004645 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004647
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 if (!(s->flags & SN_ERR_MASK))
4649 s->flags |= SN_ERR_SRVCL;
4650 if (!(s->flags & SN_FINST_MASK))
4651 s->flags |= SN_FINST_H;
4652 return 0;
4653 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655 /* write error to client (we don't send any message then) */
4656 else if (rep->flags & BF_WRITE_ERROR) {
4657 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004658 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004659
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004660 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004662 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663
4664 if (!(s->flags & SN_ERR_MASK))
4665 s->flags |= SN_ERR_CLICL;
4666 if (!(s->flags & SN_FINST_MASK))
4667 s->flags |= SN_FINST_H;
4668
4669 /* process_session() will take care of the error */
4670 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004671 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004672
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673 buffer_dont_close(rep);
4674 return 0;
4675 }
4676
4677 /* More interesting part now : we know that we have a complete
4678 * response which at least looks like HTTP. We have an indicator
4679 * of each header's length, so we can parse them quickly.
4680 */
4681
4682 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004683 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004685 /*
4686 * 1: get the status code
4687 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004688 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004689 if (n < 1 || n > 5)
4690 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004691 /* when the client triggers a 4xx from the server, it's most often due
4692 * to a missing object or permission. These events should be tracked
4693 * because if they happen often, it may indicate a brute force or a
4694 * vulnerability scan.
4695 */
4696 if (n == 4)
4697 session_inc_http_err_ctr(s);
4698
Willy Tarreau827aee92011-03-10 16:55:02 +01004699 if (target_srv(&s->target))
4700 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004701
Willy Tarreau5b154472009-12-21 20:11:07 +01004702 /* check if the response is HTTP/1.1 or above */
4703 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004704 ((msg->sol[5] > '1') ||
4705 ((msg->sol[5] == '1') &&
4706 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004707 txn->flags |= TX_RES_VER_11;
4708
4709 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004710 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 +01004711
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004712 /* transfer length unknown*/
4713 txn->flags &= ~TX_RES_XFER_LEN;
4714
Willy Tarreau962c3f42010-01-10 00:15:35 +01004715 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716
Willy Tarreau39650402010-03-15 19:44:39 +01004717 /* Adjust server's health based on status code. Note: status codes 501
4718 * and 505 are triggered on demand by client request, so we must not
4719 * count them as server failures.
4720 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004721 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004722 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004723 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004724 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004725 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004726 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004728 /*
4729 * 2: check for cacheability.
4730 */
4731
4732 switch (txn->status) {
4733 case 200:
4734 case 203:
4735 case 206:
4736 case 300:
4737 case 301:
4738 case 410:
4739 /* RFC2616 @13.4:
4740 * "A response received with a status code of
4741 * 200, 203, 206, 300, 301 or 410 MAY be stored
4742 * by a cache (...) unless a cache-control
4743 * directive prohibits caching."
4744 *
4745 * RFC2616 @9.5: POST method :
4746 * "Responses to this method are not cacheable,
4747 * unless the response includes appropriate
4748 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004749 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004750 if (likely(txn->meth != HTTP_METH_POST) &&
4751 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4752 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4753 break;
4754 default:
4755 break;
4756 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004757
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 /*
4759 * 3: we may need to capture headers
4760 */
4761 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004762 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004763 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 txn->rsp.cap, s->fe->rsp_cap);
4765
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004766 /* 4: determine the transfer-length.
4767 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4768 * the presence of a message-body in a RESPONSE and its transfer length
4769 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004770 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004771 * All responses to the HEAD request method MUST NOT include a
4772 * message-body, even though the presence of entity-header fields
4773 * might lead one to believe they do. All 1xx (informational), 204
4774 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4775 * message-body. All other responses do include a message-body,
4776 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004777 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004778 * 1. Any response which "MUST NOT" include a message-body (such as the
4779 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4780 * always terminated by the first empty line after the header fields,
4781 * regardless of the entity-header fields present in the message.
4782 *
4783 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4784 * the "chunked" transfer-coding (Section 6.2) is used, the
4785 * transfer-length is defined by the use of this transfer-coding.
4786 * If a Transfer-Encoding header field is present and the "chunked"
4787 * transfer-coding is not present, the transfer-length is defined by
4788 * the sender closing the connection.
4789 *
4790 * 3. If a Content-Length header field is present, its decimal value in
4791 * OCTETs represents both the entity-length and the transfer-length.
4792 * If a message is received with both a Transfer-Encoding header
4793 * field and a Content-Length header field, the latter MUST be ignored.
4794 *
4795 * 4. If the message uses the media type "multipart/byteranges", and
4796 * the transfer-length is not otherwise specified, then this self-
4797 * delimiting media type defines the transfer-length. This media
4798 * type MUST NOT be used unless the sender knows that the recipient
4799 * can parse it; the presence in a request of a Range header with
4800 * multiple byte-range specifiers from a 1.1 client implies that the
4801 * client can parse multipart/byteranges responses.
4802 *
4803 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004804 */
4805
4806 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004807 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004809 * FIXME: should we parse anyway and return an error on chunked encoding ?
4810 */
4811 if (txn->meth == HTTP_METH_HEAD ||
4812 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004813 txn->status == 204 || txn->status == 304) {
4814 txn->flags |= TX_RES_XFER_LEN;
4815 goto skip_content_length;
4816 }
4817
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004818 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004820 while ((txn->flags & TX_RES_VER_11) &&
4821 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004822 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4823 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4824 else if (txn->flags & TX_RES_TE_CHNK) {
4825 /* bad transfer-encoding (chunked followed by something else) */
4826 use_close_only = 1;
4827 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4828 break;
4829 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004830 }
4831
4832 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4833 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004834 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004835 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4836 signed long long cl;
4837
Willy Tarreauad14f752011-09-02 20:33:27 +02004838 if (!ctx.vlen) {
4839 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004840 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004841 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004842
Willy Tarreauad14f752011-09-02 20:33:27 +02004843 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4844 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004845 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004846 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004847
Willy Tarreauad14f752011-09-02 20:33:27 +02004848 if (cl < 0) {
4849 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004850 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004851 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004852
Willy Tarreauad14f752011-09-02 20:33:27 +02004853 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
4854 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004855 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004856 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004857
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004858 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004859 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004860 }
4861
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004862 /* FIXME: we should also implement the multipart/byterange method.
4863 * For now on, we resort to close mode in this case (unknown length).
4864 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004865skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 /* end of job, return OK */
4868 rep->analysers &= ~an_bit;
4869 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004870 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 return 1;
4872}
4873
4874/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004875 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4876 * and updates t->rep->analysers. It might make sense to explode it into several
4877 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004878 */
4879int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4880{
4881 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 struct http_msg *msg = &txn->rsp;
4883 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004884 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004885
4886 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4887 now_ms, __FUNCTION__,
4888 t,
4889 rep,
4890 rep->rex, rep->wex,
4891 rep->flags,
4892 rep->l,
4893 rep->analysers);
4894
Willy Tarreau655dce92009-11-08 13:10:58 +01004895 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004896 return 0;
4897
4898 rep->analysers &= ~an_bit;
4899 rep->analyse_exp = TICK_ETERNITY;
4900
Willy Tarreau5b154472009-12-21 20:11:07 +01004901 /* Now we have to check if we need to modify the Connection header.
4902 * This is more difficult on the response than it is on the request,
4903 * because we can have two different HTTP versions and we don't know
4904 * how the client will interprete a response. For instance, let's say
4905 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4906 * HTTP/1.1 response without any header. Maybe it will bound itself to
4907 * HTTP/1.0 because it only knows about it, and will consider the lack
4908 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4909 * the lack of header as a keep-alive. Thus we will use two flags
4910 * indicating how a request MAY be understood by the client. In case
4911 * of multiple possibilities, we'll fix the header to be explicit. If
4912 * ambiguous cases such as both close and keepalive are seen, then we
4913 * will fall back to explicit close. Note that we won't take risks with
4914 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004915 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004916 */
4917
Willy Tarreaudc008c52010-02-01 16:20:08 +01004918 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4919 txn->status == 101)) {
4920 /* Either we've established an explicit tunnel, or we're
4921 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004922 * to understand the next protocols. We have to switch to tunnel
4923 * mode, so that we transfer the request and responses then let
4924 * this protocol pass unmodified. When we later implement specific
4925 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004926 * header which contains information about that protocol for
4927 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004928 */
4929 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4930 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004931 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4932 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4933 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004934 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004935
Willy Tarreau60466522010-01-18 19:08:45 +01004936 /* on unknown transfer length, we must close */
4937 if (!(txn->flags & TX_RES_XFER_LEN) &&
4938 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4939 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004940
Willy Tarreau60466522010-01-18 19:08:45 +01004941 /* now adjust header transformations depending on current state */
4942 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4943 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4944 to_del |= 2; /* remove "keep-alive" on any response */
4945 if (!(txn->flags & TX_RES_VER_11))
4946 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004947 }
Willy Tarreau60466522010-01-18 19:08:45 +01004948 else { /* SCL / KAL */
4949 to_del |= 1; /* remove "close" on any response */
4950 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4951 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004952 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004953
Willy Tarreau60466522010-01-18 19:08:45 +01004954 /* Parse and remove some headers from the connection header */
4955 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004956
Willy Tarreau60466522010-01-18 19:08:45 +01004957 /* Some keep-alive responses are converted to Server-close if
4958 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004959 */
Willy Tarreau60466522010-01-18 19:08:45 +01004960 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4961 if ((txn->flags & TX_HDR_CONN_CLO) ||
4962 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4963 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004964 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004965 }
4966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 /*
4969 * 3: we will have to evaluate the filters.
4970 * As opposed to version 1.2, now they will be evaluated in the
4971 * filters order and not in the header order. This means that
4972 * each filter has to be validated among all headers.
4973 *
4974 * Filters are tried with ->be first, then with ->fe if it is
4975 * different from ->be.
4976 */
4977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 cur_proxy = t->be;
4979 while (1) {
4980 struct proxy *rule_set = cur_proxy;
4981
4982 /* try headers filters */
4983 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004984 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004985 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004986 if (target_srv(&t->target)) {
4987 target_srv(&t->target)->counters.failed_resp++;
4988 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004989 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004990 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004992 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004993 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004994 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004995 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004996 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004997 if (!(t->flags & SN_ERR_MASK))
4998 t->flags |= SN_ERR_PRXCOND;
4999 if (!(t->flags & SN_FINST_MASK))
5000 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005001 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005002 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005003 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005004
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005005 /* has the response been denied ? */
5006 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005007 if (target_srv(&t->target))
5008 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005009
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005010 t->be->be_counters.denied_resp++;
5011 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005012 if (t->listener->counters)
5013 t->listener->counters->denied_resp++;
5014
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005015 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005016 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005017
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005018 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005019 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005020 if (txn->status < 200)
5021 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005022 if (wl->cond) {
5023 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5024 ret = acl_pass(ret);
5025 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5026 ret = !ret;
5027 if (!ret)
5028 continue;
5029 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005030 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005032 }
5033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005034 /* check whether we're already working on the frontend */
5035 if (cur_proxy == t->fe)
5036 break;
5037 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005038 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005041 * We may be facing a 100-continue response, in which case this
5042 * is not the right response, and we're waiting for the next one.
5043 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005044 * next one.
5045 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005046 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005047 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005048 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005049 msg->msg_state = HTTP_MSG_RPBEFORE;
5050 txn->status = 0;
5051 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5052 return 1;
5053 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005054 else if (unlikely(txn->status < 200))
5055 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005056
5057 /* we don't have any 1xx status code now */
5058
5059 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005060 * 4: check for server cookie.
5061 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005062 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5063 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005065
Willy Tarreaubaaee002006-06-26 02:48:02 +02005066
Willy Tarreaua15645d2007-03-18 16:22:39 +01005067 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005068 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005070 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005071 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005072
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005073 /*
5074 * 6: add server cookie in the response if needed
5075 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005076 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005077 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005078 (!(t->flags & SN_DIRECT) ||
5079 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5080 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5081 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5082 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005083 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5084 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005086 /* the server is known, it's not the one the client requested, or the
5087 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088 * insert a set-cookie here, except if we want to insert only on POST
5089 * requests and this one isn't. Note that servers which don't have cookies
5090 * (eg: some backup servers) will return a full cookie removal request.
5091 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005092 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005093 len = sprintf(trash,
5094 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5095 t->be->cookie_name);
5096 }
5097 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005098 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005099
5100 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5101 /* emit last_date, which is mandatory */
5102 trash[len++] = COOKIE_DELIM_DATE;
5103 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5104 if (t->be->cookie_maxlife) {
5105 /* emit first_date, which is either the original one or
5106 * the current date.
5107 */
5108 trash[len++] = COOKIE_DELIM_DATE;
5109 s30tob64(txn->cookie_first_date ?
5110 txn->cookie_first_date >> 2 :
5111 (date.tv_sec+3) >> 2, trash + len);
5112 len += 5;
5113 }
5114 }
5115 len += sprintf(trash + len, "; path=/");
5116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005117
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 if (t->be->cookie_domain)
5119 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005122 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005123 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005124
Willy Tarreauf1348312010-10-07 15:54:11 +02005125 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005126 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005127 /* the server did not change, only the date was updated */
5128 txn->flags |= TX_SCK_UPDATED;
5129 else
5130 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005131
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005132 /* Here, we will tell an eventual cache on the client side that we don't
5133 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5134 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5135 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5136 */
5137 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5140
5141 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005142 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005144 }
5145 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005146
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005147 /*
5148 * 7: check if result will be cacheable with a cookie.
5149 * We'll block the response if security checks have caught
5150 * nasty things such as a cacheable cookie.
5151 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005152 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5153 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005154 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005155
5156 /* we're in presence of a cacheable response containing
5157 * a set-cookie header. We'll block it as requested by
5158 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005159 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005160 if (target_srv(&t->target))
5161 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005162
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005163 t->be->be_counters.denied_resp++;
5164 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005165 if (t->listener->counters)
5166 t->listener->counters->denied_resp++;
5167
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005169 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170 send_log(t->be, LOG_ALERT,
5171 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005172 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 goto return_srv_prx_502;
5174 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005175
5176 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005177 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005178 */
Willy Tarreau60466522010-01-18 19:08:45 +01005179 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5180 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5181 unsigned int want_flags = 0;
5182
5183 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5184 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5185 /* we want a keep-alive response here. Keep-alive header
5186 * required if either side is not 1.1.
5187 */
5188 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5189 want_flags |= TX_CON_KAL_SET;
5190 }
5191 else {
5192 /* we want a close response here. Close header required if
5193 * the server is 1.1, regardless of the client.
5194 */
5195 if (txn->flags & TX_RES_VER_11)
5196 want_flags |= TX_CON_CLO_SET;
5197 }
5198
5199 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5200 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005201 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005202
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005203 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005204 if ((txn->flags & TX_RES_XFER_LEN) ||
5205 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005206 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /*************************************************************
5209 * OK, that's finished for the headers. We have done what we *
5210 * could. Let's switch to the DATA state. *
5211 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005212
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005213 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005214
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215 /* if the user wants to log as soon as possible, without counting
5216 * bytes from the server, then this is the right moment. We have
5217 * to temporarily assign bytes_out to log what we currently have.
5218 */
5219 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5220 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5221 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005222 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005223 t->logs.bytes_out = 0;
5224 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005225
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005226 /* Note: we must not try to cheat by jumping directly to DATA,
5227 * otherwise we would not let the client side wake up.
5228 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005229
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005230 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005232 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005233}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005234
Willy Tarreaud98cf932009-12-27 22:54:55 +01005235/* This function is an analyser which forwards response body (including chunk
5236 * sizes if any). It is called as soon as we must forward, even if we forward
5237 * zero byte. The only situation where it must not be called is when we're in
5238 * tunnel mode and we want to forward till the close. It's used both to forward
5239 * remaining data and to resync after end of body. It expects the msg_state to
5240 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5241 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005242 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005243 * bytes of pending data + the headers if not already done (between som and sov).
5244 * It eventually adjusts som to match sov after the data in between have been sent.
5245 */
5246int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5247{
5248 struct http_txn *txn = &s->txn;
5249 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005250 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005252 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5253 return 0;
5254
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005255 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005256 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005257 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005258 /* Output closed while we were sending data. We must abort and
5259 * wake the other side up.
5260 */
5261 msg->msg_state = HTTP_MSG_ERROR;
5262 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005263 return 1;
5264 }
5265
Willy Tarreau4fe41902010-06-07 22:27:41 +02005266 /* in most states, we should abort in case of early close */
5267 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005268
Willy Tarreaud98cf932009-12-27 22:54:55 +01005269 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5270 /* we have msg->col and msg->sov which both point to the first
5271 * byte of message body. msg->som still points to the beginning
5272 * of the message. We must save the body in req->lr because it
5273 * survives buffer re-alignments.
5274 */
5275 res->lr = res->data + msg->sov;
5276 if (txn->flags & TX_RES_TE_CHNK)
5277 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5278 else {
5279 msg->msg_state = HTTP_MSG_DATA;
5280 }
5281 }
5282
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005284 int bytes;
5285
Willy Tarreau610ecce2010-01-04 21:15:02 +01005286 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005287 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005288 bytes = msg->sov - msg->som;
5289 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005290 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005291 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5292 bytes += res->size;
5293 msg->chunk_len += (unsigned int)bytes;
5294 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005295 }
5296
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005297
Willy Tarreaucaabe412010-01-03 23:08:28 +01005298 if (msg->msg_state == HTTP_MSG_DATA) {
5299 /* must still forward */
5300 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005301 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005302
5303 /* nothing left to forward */
5304 if (txn->flags & TX_RES_TE_CHNK)
5305 msg->msg_state = HTTP_MSG_DATA_CRLF;
5306 else
5307 msg->msg_state = HTTP_MSG_DONE;
5308 }
5309 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005310 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5312 */
5313 int ret = http_parse_chunk_size(res, msg);
5314
5315 if (!ret)
5316 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005317 else if (ret < 0) {
5318 if (msg->err_pos >= 0)
5319 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005320 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005321 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005322 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005324 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5325 /* we want the CRLF after the data */
5326 int ret;
5327
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005328 res->lr = res->w + res->send_max;
5329 if (res->lr >= res->data + res->size)
5330 res->lr -= res->size;
5331
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332 ret = http_skip_chunk_crlf(res, msg);
5333
5334 if (!ret)
5335 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005336 else if (ret < 0) {
5337 if (msg->err_pos >= 0)
5338 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005340 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005341 /* we're in MSG_CHUNK_SIZE now */
5342 }
5343 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5344 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005345
Willy Tarreaud98cf932009-12-27 22:54:55 +01005346 if (ret == 0)
5347 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005348 else if (ret < 0) {
5349 if (msg->err_pos >= 0)
5350 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005351 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005352 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005353 /* we're in HTTP_MSG_DONE now */
5354 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005356 int old_state = msg->msg_state;
5357
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005359 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005360 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5361 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5362 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363 if (http_resync_states(s)) {
5364 http_silent_debug(__LINE__, s);
5365 /* some state changes occurred, maybe the analyser
5366 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005367 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005368 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5369 if (res->flags & BF_SHUTW) {
5370 /* response errors are most likely due to
5371 * the client aborting the transfer.
5372 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005373 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005374 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005375 if (msg->err_pos >= 0)
5376 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005378 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005380 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005382 }
5383 }
5384
Willy Tarreaud98cf932009-12-27 22:54:55 +01005385 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005386 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005387 if (res->flags & BF_SHUTR) {
5388 if (!(s->flags & SN_ERR_MASK))
5389 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005390 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005391 if (target_srv(&s->target))
5392 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005393 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005394 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005395
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005396 if (res->flags & BF_SHUTW)
5397 goto aborted_xfer;
5398
Willy Tarreau40dba092010-03-04 18:14:51 +01005399 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 if (!s->req->analysers)
5401 goto return_bad_res;
5402
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005403 /* forward any pending data */
5404 bytes = msg->sov - msg->som;
5405 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005407 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5408 bytes += res->size;
5409 msg->chunk_len += (unsigned int)bytes;
5410 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 }
5412
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005413 /* When TE: chunked is used, we need to get there again to parse remaining
5414 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5415 * Similarly, with keep-alive on the client side, we don't want to forward a
5416 * close.
5417 */
5418 if ((txn->flags & TX_RES_TE_CHNK) ||
5419 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5420 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5421 buffer_dont_close(res);
5422
Willy Tarreau5c620922011-05-11 19:56:11 +02005423 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005424 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5425 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005426 * modes are already handled by the stream sock layer. We must not do
5427 * this in content-length mode because it could present the MSG_MORE
5428 * flag with the last block of forwarded data, which would cause an
5429 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005430 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005431 if (txn->flags & TX_RES_TE_CHNK)
5432 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005433
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436 return 0;
5437
Willy Tarreau40dba092010-03-04 18:14:51 +01005438 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005439 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005440 if (target_srv(&s->target))
5441 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005442
5443 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005445 /* don't send any error message as we're in the body */
5446 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005448 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005449 if (target_srv(&s->target))
5450 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005451
5452 if (!(s->flags & SN_ERR_MASK))
5453 s->flags |= SN_ERR_PRXCOND;
5454 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005455 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005456 return 0;
5457
5458 aborted_xfer:
5459 txn->rsp.msg_state = HTTP_MSG_ERROR;
5460 /* don't send any error message as we're in the body */
5461 stream_int_retnclose(res->cons, NULL);
5462 res->analysers = 0;
5463 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5464
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005465 s->fe->fe_counters.cli_aborts++;
5466 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005467 if (target_srv(&s->target))
5468 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005469
5470 if (!(s->flags & SN_ERR_MASK))
5471 s->flags |= SN_ERR_CLICL;
5472 if (!(s->flags & SN_FINST_MASK))
5473 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005474 return 0;
5475}
5476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005477/* Iterate the same filter through all request headers.
5478 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005479 * Since it can manage the switch to another backend, it updates the per-proxy
5480 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005481 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005482int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005483{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005484 char term;
5485 char *cur_ptr, *cur_end, *cur_next;
5486 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005487 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488 struct hdr_idx_elem *cur_hdr;
5489 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005491 last_hdr = 0;
5492
Willy Tarreau962c3f42010-01-10 00:15:35 +01005493 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005494 old_idx = 0;
5495
5496 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005497 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005498 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005499 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005500 (exp->action == ACT_ALLOW ||
5501 exp->action == ACT_DENY ||
5502 exp->action == ACT_TARPIT))
5503 return 0;
5504
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005505 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005506 if (!cur_idx)
5507 break;
5508
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005509 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005510 cur_ptr = cur_next;
5511 cur_end = cur_ptr + cur_hdr->len;
5512 cur_next = cur_end + cur_hdr->cr + 1;
5513
5514 /* Now we have one header between cur_ptr and cur_end,
5515 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005516 */
5517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005518 /* The annoying part is that pattern matching needs
5519 * that we modify the contents to null-terminate all
5520 * strings before testing them.
5521 */
5522
5523 term = *cur_end;
5524 *cur_end = '\0';
5525
5526 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5527 switch (exp->action) {
5528 case ACT_SETBE:
5529 /* It is not possible to jump a second time.
5530 * FIXME: should we return an HTTP/500 here so that
5531 * the admin knows there's a problem ?
5532 */
5533 if (t->be != t->fe)
5534 break;
5535
5536 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005537 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 last_hdr = 1;
5539 break;
5540
5541 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005542 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005543 last_hdr = 1;
5544 break;
5545
5546 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005547 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005548 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005549
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005550 t->fe->fe_counters.denied_req++;
5551 if (t->fe != t->be)
5552 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005553 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005554 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005556 break;
5557
5558 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005559 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005560 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005561
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005562 t->fe->fe_counters.denied_req++;
5563 if (t->fe != t->be)
5564 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005565 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005566 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 break;
5569
5570 case ACT_REPLACE:
5571 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5572 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5573 /* FIXME: if the user adds a newline in the replacement, the
5574 * index will not be recalculated for now, and the new line
5575 * will not be counted as a new header.
5576 */
5577
5578 cur_end += delta;
5579 cur_next += delta;
5580 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005581 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005582 break;
5583
5584 case ACT_REMOVE:
5585 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5586 cur_next += delta;
5587
Willy Tarreaufa355d42009-11-29 18:12:29 +01005588 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005589 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5590 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005591 cur_hdr->len = 0;
5592 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005593 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005594 break;
5595
5596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005597 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 if (cur_end)
5599 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005601 /* keep the link from this header to next one in case of later
5602 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005603 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 old_idx = cur_idx;
5605 }
5606 return 0;
5607}
5608
5609
5610/* Apply the filter to the request line.
5611 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5612 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005613 * Since it can manage the switch to another backend, it updates the per-proxy
5614 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005615 */
5616int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5617{
5618 char term;
5619 char *cur_ptr, *cur_end;
5620 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005621 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005623
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624
Willy Tarreau3d300592007-03-18 18:34:41 +01005625 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005626 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005627 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 (exp->action == ACT_ALLOW ||
5629 exp->action == ACT_DENY ||
5630 exp->action == ACT_TARPIT))
5631 return 0;
5632 else if (exp->action == ACT_REMOVE)
5633 return 0;
5634
5635 done = 0;
5636
Willy Tarreau962c3f42010-01-10 00:15:35 +01005637 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005638 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639
5640 /* Now we have the request line between cur_ptr and cur_end */
5641
5642 /* The annoying part is that pattern matching needs
5643 * that we modify the contents to null-terminate all
5644 * strings before testing them.
5645 */
5646
5647 term = *cur_end;
5648 *cur_end = '\0';
5649
5650 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5651 switch (exp->action) {
5652 case ACT_SETBE:
5653 /* It is not possible to jump a second time.
5654 * FIXME: should we return an HTTP/500 here so that
5655 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005656 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 if (t->be != t->fe)
5658 break;
5659
5660 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005661 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 done = 1;
5663 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005665 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005666 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 done = 1;
5668 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005670 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005671 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005672
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005673 t->fe->fe_counters.denied_req++;
5674 if (t->fe != t->be)
5675 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005676 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005677 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 done = 1;
5680 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005683 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005684
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005685 t->fe->fe_counters.denied_req++;
5686 if (t->fe != t->be)
5687 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005688 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005689 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 done = 1;
5692 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005694 case ACT_REPLACE:
5695 *cur_end = term; /* restore the string terminator */
5696 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5697 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5698 /* FIXME: if the user adds a newline in the replacement, the
5699 * index will not be recalculated for now, and the new line
5700 * will not be counted as a new header.
5701 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005702
Willy Tarreaufa355d42009-11-29 18:12:29 +01005703 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005704 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005705 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005706 HTTP_MSG_RQMETH,
5707 cur_ptr, cur_end + 1,
5708 NULL, NULL);
5709 if (unlikely(!cur_end))
5710 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005711
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 /* we have a full request and we know that we have either a CR
5713 * or an LF at <ptr>.
5714 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005715 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5716 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 /* there is no point trying this regex on headers */
5718 return 1;
5719 }
5720 }
5721 *cur_end = term; /* restore the string terminator */
5722 return done;
5723}
Willy Tarreau97de6242006-12-27 17:18:38 +01005724
Willy Tarreau58f10d72006-12-04 02:26:12 +01005725
Willy Tarreau58f10d72006-12-04 02:26:12 +01005726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005727/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005728 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 * unparsable request. Since it can manage the switch to another backend, it
5731 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005732 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005733int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005735 struct http_txn *txn = &s->txn;
5736 struct hdr_exp *exp;
5737
5738 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005741 /*
5742 * The interleaving of transformations and verdicts
5743 * makes it difficult to decide to continue or stop
5744 * the evaluation.
5745 */
5746
Willy Tarreau6c123b12010-01-28 20:22:06 +01005747 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5748 break;
5749
Willy Tarreau3d300592007-03-18 18:34:41 +01005750 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005752 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005754
5755 /* if this filter had a condition, evaluate it now and skip to
5756 * next filter if the condition does not match.
5757 */
5758 if (exp->cond) {
5759 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5760 ret = acl_pass(ret);
5761 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5762 ret = !ret;
5763
5764 if (!ret)
5765 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 }
5767
5768 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005769 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 if (unlikely(ret < 0))
5771 return -1;
5772
5773 if (likely(ret == 0)) {
5774 /* The filter did not match the request, it can be
5775 * iterated through all headers.
5776 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005777 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005778 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005779 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005780 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005781}
5782
5783
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784
Willy Tarreau58f10d72006-12-04 02:26:12 +01005785/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005786 * Try to retrieve the server associated to the appsession.
5787 * If the server is found, it's assigned to the session.
5788 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005789void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005790 struct http_txn *txn = &t->txn;
5791 appsess *asession = NULL;
5792 char *sessid_temp = NULL;
5793
Cyril Bontéb21570a2009-11-29 20:04:48 +01005794 if (len > t->be->appsession_len) {
5795 len = t->be->appsession_len;
5796 }
5797
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005798 if (t->be->options2 & PR_O2_AS_REQL) {
5799 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005800 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005801 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005802 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005803 }
5804
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005805 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005806 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5807 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5808 return;
5809 }
5810
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005811 memcpy(txn->sessid, buf, len);
5812 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005813 }
5814
5815 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5816 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5817 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5818 return;
5819 }
5820
Cyril Bontéb21570a2009-11-29 20:04:48 +01005821 memcpy(sessid_temp, buf, len);
5822 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005823
5824 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5825 /* free previously allocated memory */
5826 pool_free2(apools.sessid, sessid_temp);
5827
5828 if (asession != NULL) {
5829 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5830 if (!(t->be->options2 & PR_O2_AS_REQL))
5831 asession->request_count++;
5832
5833 if (asession->serverid != NULL) {
5834 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005835
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005836 while (srv) {
5837 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005838 if ((srv->state & SRV_RUNNING) ||
5839 (t->be->options & PR_O_PERSIST) ||
5840 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005841 /* we found the server and it's usable */
5842 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005843 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005844 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005845 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005846
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005847 break;
5848 } else {
5849 txn->flags &= ~TX_CK_MASK;
5850 txn->flags |= TX_CK_DOWN;
5851 }
5852 }
5853 srv = srv->next;
5854 }
5855 }
5856 }
5857}
5858
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005859/* Find the end of a cookie value contained between <s> and <e>. It works the
5860 * same way as with headers above except that the semi-colon also ends a token.
5861 * See RFC2965 for more information. Note that it requires a valid header to
5862 * return a valid result.
5863 */
5864char *find_cookie_value_end(char *s, const char *e)
5865{
5866 int quoted, qdpair;
5867
5868 quoted = qdpair = 0;
5869 for (; s < e; s++) {
5870 if (qdpair) qdpair = 0;
5871 else if (quoted) {
5872 if (*s == '\\') qdpair = 1;
5873 else if (*s == '"') quoted = 0;
5874 }
5875 else if (*s == '"') quoted = 1;
5876 else if (*s == ',' || *s == ';') return s;
5877 }
5878 return s;
5879}
5880
5881/* Delete a value in a header between delimiters <from> and <next> in buffer
5882 * <buf>. The number of characters displaced is returned, and the pointer to
5883 * the first delimiter is updated if required. The function tries as much as
5884 * possible to respect the following principles :
5885 * - replace <from> delimiter by the <next> one unless <from> points to a
5886 * colon, in which case <next> is simply removed
5887 * - set exactly one space character after the new first delimiter, unless
5888 * there are not enough characters in the block being moved to do so.
5889 * - remove unneeded spaces before the previous delimiter and after the new
5890 * one.
5891 *
5892 * It is the caller's responsibility to ensure that :
5893 * - <from> points to a valid delimiter or the colon ;
5894 * - <next> points to a valid delimiter or the final CR/LF ;
5895 * - there are non-space chars before <from> ;
5896 * - there is a CR/LF at or after <next>.
5897 */
5898int del_hdr_value(struct buffer *buf, char **from, char *next)
5899{
5900 char *prev = *from;
5901
5902 if (*prev == ':') {
5903 /* We're removing the first value, preserve the colon and add a
5904 * space if possible.
5905 */
5906 if (!http_is_crlf[(unsigned char)*next])
5907 next++;
5908 prev++;
5909 if (prev < next)
5910 *prev++ = ' ';
5911
5912 while (http_is_spht[(unsigned char)*next])
5913 next++;
5914 } else {
5915 /* Remove useless spaces before the old delimiter. */
5916 while (http_is_spht[(unsigned char)*(prev-1)])
5917 prev--;
5918 *from = prev;
5919
5920 /* copy the delimiter and if possible a space if we're
5921 * not at the end of the line.
5922 */
5923 if (!http_is_crlf[(unsigned char)*next]) {
5924 *prev++ = *next++;
5925 if (prev + 1 < next)
5926 *prev++ = ' ';
5927 while (http_is_spht[(unsigned char)*next])
5928 next++;
5929 }
5930 }
5931 return buffer_replace2(buf, prev, next, NULL, 0);
5932}
5933
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005934/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005935 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005936 * desirable to call it only when needed. This code is quite complex because
5937 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5938 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939 */
5940void manage_client_side_cookies(struct session *t, struct buffer *req)
5941{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005942 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005943 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005945 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5946 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005948 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005949 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005950 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005951
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005952 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005953 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005954 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005956 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005957 hdr_beg = hdr_next;
5958 hdr_end = hdr_beg + cur_hdr->len;
5959 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005960
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005961 /* We have one full header between hdr_beg and hdr_end, and the
5962 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005963 * "Cookie:" headers.
5964 */
5965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005966 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005967 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005968 old_idx = cur_idx;
5969 continue;
5970 }
5971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005972 del_from = NULL; /* nothing to be deleted */
5973 preserve_hdr = 0; /* assume we may kill the whole header */
5974
Willy Tarreau58f10d72006-12-04 02:26:12 +01005975 /* Now look for cookies. Conforming to RFC2109, we have to support
5976 * attributes whose name begin with a '$', and associate them with
5977 * the right cookie, if we want to delete this cookie.
5978 * So there are 3 cases for each cookie read :
5979 * 1) it's a special attribute, beginning with a '$' : ignore it.
5980 * 2) it's a server id cookie that we *MAY* want to delete : save
5981 * some pointers on it (last semi-colon, beginning of cookie...)
5982 * 3) it's an application cookie : we *MAY* have to delete a previous
5983 * "special" cookie.
5984 * At the end of loop, if a "special" cookie remains, we may have to
5985 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005986 * *MUST* delete it.
5987 *
5988 * Note: RFC2965 is unclear about the processing of spaces around
5989 * the equal sign in the ATTR=VALUE form. A careful inspection of
5990 * the RFC explicitly allows spaces before it, and not within the
5991 * tokens (attrs or values). An inspection of RFC2109 allows that
5992 * too but section 10.1.3 lets one think that spaces may be allowed
5993 * after the equal sign too, resulting in some (rare) buggy
5994 * implementations trying to do that. So let's do what servers do.
5995 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5996 * allowed quoted strings in values, with any possible character
5997 * after a backslash, including control chars and delimitors, which
5998 * causes parsing to become ambiguous. Browsers also allow spaces
5999 * within values even without quotes.
6000 *
6001 * We have to keep multiple pointers in order to support cookie
6002 * removal at the beginning, middle or end of header without
6003 * corrupting the header. All of these headers are valid :
6004 *
6005 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6006 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6007 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6008 * | | | | | | | | |
6009 * | | | | | | | | hdr_end <--+
6010 * | | | | | | | +--> next
6011 * | | | | | | +----> val_end
6012 * | | | | | +-----------> val_beg
6013 * | | | | +--------------> equal
6014 * | | | +----------------> att_end
6015 * | | +---------------------> att_beg
6016 * | +--------------------------> prev
6017 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006018 */
6019
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006020 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6021 /* Iterate through all cookies on this line */
6022
6023 /* find att_beg */
6024 att_beg = prev + 1;
6025 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6026 att_beg++;
6027
6028 /* find att_end : this is the first character after the last non
6029 * space before the equal. It may be equal to hdr_end.
6030 */
6031 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 while (equal < hdr_end) {
6034 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006035 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006036 if (http_is_spht[(unsigned char)*equal++])
6037 continue;
6038 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039 }
6040
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006041 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6042 * is between <att_beg> and <equal>, both may be identical.
6043 */
6044
6045 /* look for end of cookie if there is an equal sign */
6046 if (equal < hdr_end && *equal == '=') {
6047 /* look for the beginning of the value */
6048 val_beg = equal + 1;
6049 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6050 val_beg++;
6051
6052 /* find the end of the value, respecting quotes */
6053 next = find_cookie_value_end(val_beg, hdr_end);
6054
6055 /* make val_end point to the first white space or delimitor after the value */
6056 val_end = next;
6057 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6058 val_end--;
6059 } else {
6060 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006061 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006062
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006063 /* We have nothing to do with attributes beginning with '$'. However,
6064 * they will automatically be removed if a header before them is removed,
6065 * since they're supposed to be linked together.
6066 */
6067 if (*att_beg == '$')
6068 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 /* Ignore cookies with no equal sign */
6071 if (equal == next) {
6072 /* This is not our cookie, so we must preserve it. But if we already
6073 * scheduled another cookie for removal, we cannot remove the
6074 * complete header, but we can remove the previous block itself.
6075 */
6076 preserve_hdr = 1;
6077 if (del_from != NULL) {
6078 int delta = del_hdr_value(req, &del_from, prev);
6079 val_end += delta;
6080 next += delta;
6081 hdr_end += delta;
6082 hdr_next += delta;
6083 cur_hdr->len += delta;
6084 http_msg_move_end(&txn->req, delta);
6085 prev = del_from;
6086 del_from = NULL;
6087 }
6088 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006091 /* if there are spaces around the equal sign, we need to
6092 * strip them otherwise we'll get trouble for cookie captures,
6093 * or even for rewrites. Since this happens extremely rarely,
6094 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006095 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006096 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6097 int stripped_before = 0;
6098 int stripped_after = 0;
6099
6100 if (att_end != equal) {
6101 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6102 equal += stripped_before;
6103 val_beg += stripped_before;
6104 }
6105
6106 if (val_beg > equal + 1) {
6107 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6108 val_beg += stripped_after;
6109 stripped_before += stripped_after;
6110 }
6111
6112 val_end += stripped_before;
6113 next += stripped_before;
6114 hdr_end += stripped_before;
6115 hdr_next += stripped_before;
6116 cur_hdr->len += stripped_before;
6117 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 /* First, let's see if we want to capture this cookie. We check
6122 * that we don't already have a client side cookie, because we
6123 * can only capture one. Also as an optimisation, we ignore
6124 * cookies shorter than the declared name.
6125 */
6126 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6127 (val_end - att_beg >= t->fe->capture_namelen) &&
6128 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6129 int log_len = val_end - att_beg;
6130
6131 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6132 Alert("HTTP logging : out of memory.\n");
6133 } else {
6134 if (log_len > t->fe->capture_len)
6135 log_len = t->fe->capture_len;
6136 memcpy(txn->cli_cookie, att_beg, log_len);
6137 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140
Willy Tarreaubca99692010-10-06 19:25:55 +02006141 /* Persistence cookies in passive, rewrite or insert mode have the
6142 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006143 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006144 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006145 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006146 * For cookies in prefix mode, the form is :
6147 *
6148 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006149 */
6150 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6151 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6152 struct server *srv = t->be->srv;
6153 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6156 * have the server ID between val_beg and delim, and the original cookie between
6157 * delim+1 and val_end. Otherwise, delim==val_end :
6158 *
6159 * Cookie: NAME=SRV; # in all but prefix modes
6160 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6161 * | || || | |+-> next
6162 * | || || | +--> val_end
6163 * | || || +---------> delim
6164 * | || |+------------> val_beg
6165 * | || +-------------> att_end = equal
6166 * | |+-----------------> att_beg
6167 * | +------------------> prev
6168 * +-------------------------> hdr_beg
6169 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006171 if (t->be->options & PR_O_COOK_PFX) {
6172 for (delim = val_beg; delim < val_end; delim++)
6173 if (*delim == COOKIE_DELIM)
6174 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006175 } else {
6176 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006177 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006178 /* Now check if the cookie contains a date field, which would
6179 * appear after a vertical bar ('|') just after the server name
6180 * and before the delimiter.
6181 */
6182 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6183 if (vbar1) {
6184 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006185 * right is the last seen date. It is a base64 encoded
6186 * 30-bit value representing the UNIX date since the
6187 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006188 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006189 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006190 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006191 if (val_end - vbar1 >= 5) {
6192 val = b64tos30(vbar1);
6193 if (val > 0)
6194 txn->cookie_last_date = val << 2;
6195 }
6196 /* look for a second vertical bar */
6197 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6198 if (vbar1 && (val_end - vbar1 > 5)) {
6199 val = b64tos30(vbar1 + 1);
6200 if (val > 0)
6201 txn->cookie_first_date = val << 2;
6202 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006203 }
6204 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006205
Willy Tarreauf64d1412010-10-07 20:06:11 +02006206 /* if the cookie has an expiration date and the proxy wants to check
6207 * it, then we do that now. We first check if the cookie is too old,
6208 * then only if it has expired. We detect strict overflow because the
6209 * time resolution here is not great (4 seconds). Cookies with dates
6210 * in the future are ignored if their offset is beyond one day. This
6211 * allows an admin to fix timezone issues without expiring everyone
6212 * and at the same time avoids keeping unwanted side effects for too
6213 * long.
6214 */
6215 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006216 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6217 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006218 txn->flags &= ~TX_CK_MASK;
6219 txn->flags |= TX_CK_OLD;
6220 delim = val_beg; // let's pretend we have not found the cookie
6221 txn->cookie_first_date = 0;
6222 txn->cookie_last_date = 0;
6223 }
6224 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006225 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6226 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006227 txn->flags &= ~TX_CK_MASK;
6228 txn->flags |= TX_CK_EXPIRED;
6229 delim = val_beg; // let's pretend we have not found the cookie
6230 txn->cookie_first_date = 0;
6231 txn->cookie_last_date = 0;
6232 }
6233
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 /* Here, we'll look for the first running server which supports the cookie.
6235 * This allows to share a same cookie between several servers, for example
6236 * to dedicate backup servers to specific servers only.
6237 * However, to prevent clients from sticking to cookie-less backup server
6238 * when they have incidentely learned an empty cookie, we simply ignore
6239 * empty cookies and mark them as invalid.
6240 * The same behaviour is applied when persistence must be ignored.
6241 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006242 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 while (srv) {
6246 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6247 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6248 if ((srv->state & SRV_RUNNING) ||
6249 (t->be->options & PR_O_PERSIST) ||
6250 (t->flags & SN_FORCE_PRST)) {
6251 /* we found the server and we can use it */
6252 txn->flags &= ~TX_CK_MASK;
6253 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6254 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006255 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 break;
6257 } else {
6258 /* we found a server, but it's down,
6259 * mark it as such and go on in case
6260 * another one is available.
6261 */
6262 txn->flags &= ~TX_CK_MASK;
6263 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 srv = srv->next;
6267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268
Willy Tarreauf64d1412010-10-07 20:06:11 +02006269 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006270 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006272 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6273 txn->flags |= TX_CK_UNUSED;
6274 else
6275 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006276 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 /* depending on the cookie mode, we may have to either :
6279 * - delete the complete cookie if we're in insert+indirect mode, so that
6280 * the server never sees it ;
6281 * - remove the server id from the cookie value, and tag the cookie as an
6282 * application cookie so that it does not get accidentely removed later,
6283 * if we're in cookie prefix mode
6284 */
6285 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6286 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6289 val_end += delta;
6290 next += delta;
6291 hdr_end += delta;
6292 hdr_next += delta;
6293 cur_hdr->len += delta;
6294 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 del_from = NULL;
6297 preserve_hdr = 1; /* we want to keep this cookie */
6298 }
6299 else if (del_from == NULL &&
6300 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6301 del_from = prev;
6302 }
6303 } else {
6304 /* This is not our cookie, so we must preserve it. But if we already
6305 * scheduled another cookie for removal, we cannot remove the
6306 * complete header, but we can remove the previous block itself.
6307 */
6308 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 if (del_from != NULL) {
6311 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006312 if (att_beg >= del_from)
6313 att_beg += delta;
6314 if (att_end >= del_from)
6315 att_end += delta;
6316 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 val_end += delta;
6318 next += delta;
6319 hdr_end += delta;
6320 hdr_next += delta;
6321 cur_hdr->len += delta;
6322 http_msg_move_end(&txn->req, delta);
6323 prev = del_from;
6324 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 /* Look for the appsession cookie unless persistence must be ignored */
6329 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6330 int cmp_len, value_len;
6331 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 if (t->be->options2 & PR_O2_AS_PFX) {
6334 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6335 value_begin = att_beg + t->be->appsession_name_len;
6336 value_len = val_end - att_beg - t->be->appsession_name_len;
6337 } else {
6338 cmp_len = att_end - att_beg;
6339 value_begin = val_beg;
6340 value_len = val_end - val_beg;
6341 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006342
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 /* let's see if the cookie is our appcookie */
6344 if (cmp_len == t->be->appsession_name_len &&
6345 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6346 manage_client_side_appsession(t, value_begin, value_len);
6347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 }
6349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 /* continue with next cookie on this header line */
6351 att_beg = next;
6352 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 /* There are no more cookies on this line.
6355 * We may still have one (or several) marked for deletion at the
6356 * end of the line. We must do this now in two ways :
6357 * - if some cookies must be preserved, we only delete from the
6358 * mark to the end of line ;
6359 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006362 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 if (preserve_hdr) {
6364 delta = del_hdr_value(req, &del_from, hdr_end);
6365 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366 cur_hdr->len += delta;
6367 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006368 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006369
6370 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006371 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6372 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006374 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006377 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378 }
6379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006383}
6384
6385
Willy Tarreaua15645d2007-03-18 16:22:39 +01006386/* Iterate the same filter through all response headers contained in <rtr>.
6387 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6388 */
6389int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6390{
6391 char term;
6392 char *cur_ptr, *cur_end, *cur_next;
6393 int cur_idx, old_idx, last_hdr;
6394 struct http_txn *txn = &t->txn;
6395 struct hdr_idx_elem *cur_hdr;
6396 int len, delta;
6397
6398 last_hdr = 0;
6399
Willy Tarreau962c3f42010-01-10 00:15:35 +01006400 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006401 old_idx = 0;
6402
6403 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006404 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006406 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006407 (exp->action == ACT_ALLOW ||
6408 exp->action == ACT_DENY))
6409 return 0;
6410
6411 cur_idx = txn->hdr_idx.v[old_idx].next;
6412 if (!cur_idx)
6413 break;
6414
6415 cur_hdr = &txn->hdr_idx.v[cur_idx];
6416 cur_ptr = cur_next;
6417 cur_end = cur_ptr + cur_hdr->len;
6418 cur_next = cur_end + cur_hdr->cr + 1;
6419
6420 /* Now we have one header between cur_ptr and cur_end,
6421 * and the next header starts at cur_next.
6422 */
6423
6424 /* The annoying part is that pattern matching needs
6425 * that we modify the contents to null-terminate all
6426 * strings before testing them.
6427 */
6428
6429 term = *cur_end;
6430 *cur_end = '\0';
6431
6432 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6433 switch (exp->action) {
6434 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006435 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006436 last_hdr = 1;
6437 break;
6438
6439 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006440 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 last_hdr = 1;
6442 break;
6443
6444 case ACT_REPLACE:
6445 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6446 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6447 /* FIXME: if the user adds a newline in the replacement, the
6448 * index will not be recalculated for now, and the new line
6449 * will not be counted as a new header.
6450 */
6451
6452 cur_end += delta;
6453 cur_next += delta;
6454 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006455 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006456 break;
6457
6458 case ACT_REMOVE:
6459 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6460 cur_next += delta;
6461
Willy Tarreaufa355d42009-11-29 18:12:29 +01006462 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6464 txn->hdr_idx.used--;
6465 cur_hdr->len = 0;
6466 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006467 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468 break;
6469
6470 }
6471 }
6472 if (cur_end)
6473 *cur_end = term; /* restore the string terminator */
6474
6475 /* keep the link from this header to next one in case of later
6476 * removal of next header.
6477 */
6478 old_idx = cur_idx;
6479 }
6480 return 0;
6481}
6482
6483
6484/* Apply the filter to the status line in the response buffer <rtr>.
6485 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6486 * or -1 if a replacement resulted in an invalid status line.
6487 */
6488int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6489{
6490 char term;
6491 char *cur_ptr, *cur_end;
6492 int done;
6493 struct http_txn *txn = &t->txn;
6494 int len, delta;
6495
6496
Willy Tarreau3d300592007-03-18 18:34:41 +01006497 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006499 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500 (exp->action == ACT_ALLOW ||
6501 exp->action == ACT_DENY))
6502 return 0;
6503 else if (exp->action == ACT_REMOVE)
6504 return 0;
6505
6506 done = 0;
6507
Willy Tarreau962c3f42010-01-10 00:15:35 +01006508 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006509 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006510
6511 /* Now we have the status line between cur_ptr and cur_end */
6512
6513 /* The annoying part is that pattern matching needs
6514 * that we modify the contents to null-terminate all
6515 * strings before testing them.
6516 */
6517
6518 term = *cur_end;
6519 *cur_end = '\0';
6520
6521 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6522 switch (exp->action) {
6523 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006524 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525 done = 1;
6526 break;
6527
6528 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006529 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 done = 1;
6531 break;
6532
6533 case ACT_REPLACE:
6534 *cur_end = term; /* restore the string terminator */
6535 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6536 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6537 /* FIXME: if the user adds a newline in the replacement, the
6538 * index will not be recalculated for now, and the new line
6539 * will not be counted as a new header.
6540 */
6541
Willy Tarreaufa355d42009-11-29 18:12:29 +01006542 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006545 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546 cur_ptr, cur_end + 1,
6547 NULL, NULL);
6548 if (unlikely(!cur_end))
6549 return -1;
6550
6551 /* we have a full respnse and we know that we have either a CR
6552 * or an LF at <ptr>.
6553 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006554 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006555 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 /* there is no point trying this regex on headers */
6557 return 1;
6558 }
6559 }
6560 *cur_end = term; /* restore the string terminator */
6561 return done;
6562}
6563
6564
6565
6566/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006567 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6569 * unparsable response.
6570 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006571int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006573 struct http_txn *txn = &s->txn;
6574 struct hdr_exp *exp;
6575
6576 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 int ret;
6578
6579 /*
6580 * The interleaving of transformations and verdicts
6581 * makes it difficult to decide to continue or stop
6582 * the evaluation.
6583 */
6584
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006585 if (txn->flags & TX_SVDENY)
6586 break;
6587
Willy Tarreau3d300592007-03-18 18:34:41 +01006588 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6590 exp->action == ACT_PASS)) {
6591 exp = exp->next;
6592 continue;
6593 }
6594
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006595 /* if this filter had a condition, evaluate it now and skip to
6596 * next filter if the condition does not match.
6597 */
6598 if (exp->cond) {
6599 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6600 ret = acl_pass(ret);
6601 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6602 ret = !ret;
6603 if (!ret)
6604 continue;
6605 }
6606
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006608 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 if (unlikely(ret < 0))
6610 return -1;
6611
6612 if (likely(ret == 0)) {
6613 /* The filter did not match the response, it can be
6614 * iterated through all headers.
6615 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006616 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618 }
6619 return 0;
6620}
6621
6622
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006624 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006625 * desirable to call it only when needed. This function is also used when we
6626 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006628void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629{
6630 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006631 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006632 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634 char *hdr_beg, *hdr_end, *hdr_next;
6635 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637 /* Iterate through the headers.
6638 * we start with the start line.
6639 */
6640 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006641 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642
6643 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6644 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006645 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006646
6647 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006648 hdr_beg = hdr_next;
6649 hdr_end = hdr_beg + cur_hdr->len;
6650 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006651
Willy Tarreau24581ba2010-08-31 22:39:35 +02006652 /* We have one full header between hdr_beg and hdr_end, and the
6653 * next header starts at hdr_next. We're only interested in
6654 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 */
6656
Willy Tarreau24581ba2010-08-31 22:39:35 +02006657 is_cookie2 = 0;
6658 prev = hdr_beg + 10;
6659 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006660 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006661 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6662 if (!val) {
6663 old_idx = cur_idx;
6664 continue;
6665 }
6666 is_cookie2 = 1;
6667 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 }
6669
Willy Tarreau24581ba2010-08-31 22:39:35 +02006670 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6671 * <prev> points to the colon.
6672 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006673 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674
Willy Tarreau24581ba2010-08-31 22:39:35 +02006675 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6676 * check-cache is enabled) and we are not interested in checking
6677 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006678 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006679 if (t->be->cookie_name == NULL &&
6680 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006681 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006682 return;
6683
Willy Tarreau24581ba2010-08-31 22:39:35 +02006684 /* OK so now we know we have to process this response cookie.
6685 * The format of the Set-Cookie header is slightly different
6686 * from the format of the Cookie header in that it does not
6687 * support the comma as a cookie delimiter (thus the header
6688 * cannot be folded) because the Expires attribute described in
6689 * the original Netscape's spec may contain an unquoted date
6690 * with a comma inside. We have to live with this because
6691 * many browsers don't support Max-Age and some browsers don't
6692 * support quoted strings. However the Set-Cookie2 header is
6693 * clean.
6694 *
6695 * We have to keep multiple pointers in order to support cookie
6696 * removal at the beginning, middle or end of header without
6697 * corrupting the header (in case of set-cookie2). A special
6698 * pointer, <scav> points to the beginning of the set-cookie-av
6699 * fields after the first semi-colon. The <next> pointer points
6700 * either to the end of line (set-cookie) or next unquoted comma
6701 * (set-cookie2). All of these headers are valid :
6702 *
6703 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6704 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6705 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6706 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6707 * | | | | | | | | | |
6708 * | | | | | | | | +-> next hdr_end <--+
6709 * | | | | | | | +------------> scav
6710 * | | | | | | +--------------> val_end
6711 * | | | | | +--------------------> val_beg
6712 * | | | | +----------------------> equal
6713 * | | | +------------------------> att_end
6714 * | | +----------------------------> att_beg
6715 * | +------------------------------> prev
6716 * +-----------------------------------------> hdr_beg
6717 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718
Willy Tarreau24581ba2010-08-31 22:39:35 +02006719 for (; prev < hdr_end; prev = next) {
6720 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721
Willy Tarreau24581ba2010-08-31 22:39:35 +02006722 /* find att_beg */
6723 att_beg = prev + 1;
6724 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6725 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726
Willy Tarreau24581ba2010-08-31 22:39:35 +02006727 /* find att_end : this is the first character after the last non
6728 * space before the equal. It may be equal to hdr_end.
6729 */
6730 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731
Willy Tarreau24581ba2010-08-31 22:39:35 +02006732 while (equal < hdr_end) {
6733 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6734 break;
6735 if (http_is_spht[(unsigned char)*equal++])
6736 continue;
6737 att_end = equal;
6738 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
Willy Tarreau24581ba2010-08-31 22:39:35 +02006740 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6741 * is between <att_beg> and <equal>, both may be identical.
6742 */
6743
6744 /* look for end of cookie if there is an equal sign */
6745 if (equal < hdr_end && *equal == '=') {
6746 /* look for the beginning of the value */
6747 val_beg = equal + 1;
6748 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6749 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
Willy Tarreau24581ba2010-08-31 22:39:35 +02006751 /* find the end of the value, respecting quotes */
6752 next = find_cookie_value_end(val_beg, hdr_end);
6753
6754 /* make val_end point to the first white space or delimitor after the value */
6755 val_end = next;
6756 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6757 val_end--;
6758 } else {
6759 /* <equal> points to next comma, semi-colon or EOL */
6760 val_beg = val_end = next = equal;
6761 }
6762
6763 if (next < hdr_end) {
6764 /* Set-Cookie2 supports multiple cookies, and <next> points to
6765 * a colon or semi-colon before the end. So skip all attr-value
6766 * pairs and look for the next comma. For Set-Cookie, since
6767 * commas are permitted in values, skip to the end.
6768 */
6769 if (is_cookie2)
6770 next = find_hdr_value_end(next, hdr_end);
6771 else
6772 next = hdr_end;
6773 }
6774
6775 /* Now everything is as on the diagram above */
6776
6777 /* Ignore cookies with no equal sign */
6778 if (equal == val_end)
6779 continue;
6780
6781 /* If there are spaces around the equal sign, we need to
6782 * strip them otherwise we'll get trouble for cookie captures,
6783 * or even for rewrites. Since this happens extremely rarely,
6784 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006785 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6787 int stripped_before = 0;
6788 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789
Willy Tarreau24581ba2010-08-31 22:39:35 +02006790 if (att_end != equal) {
6791 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6792 equal += stripped_before;
6793 val_beg += stripped_before;
6794 }
6795
6796 if (val_beg > equal + 1) {
6797 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6798 val_beg += stripped_after;
6799 stripped_before += stripped_after;
6800 }
6801
6802 val_end += stripped_before;
6803 next += stripped_before;
6804 hdr_end += stripped_before;
6805 hdr_next += stripped_before;
6806 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006807 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006808 }
6809
6810 /* First, let's see if we want to capture this cookie. We check
6811 * that we don't already have a server side cookie, because we
6812 * can only capture one. Also as an optimisation, we ignore
6813 * cookies shorter than the declared name.
6814 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006815 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006816 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 (val_end - att_beg >= t->fe->capture_namelen) &&
6818 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6819 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006820 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821 Alert("HTTP logging : out of memory.\n");
6822 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006823 else {
6824 if (log_len > t->fe->capture_len)
6825 log_len = t->fe->capture_len;
6826 memcpy(txn->srv_cookie, att_beg, log_len);
6827 txn->srv_cookie[log_len] = 0;
6828 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 }
6830
Willy Tarreau827aee92011-03-10 16:55:02 +01006831 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006833 if (!(t->flags & SN_IGNORE_PRST) &&
6834 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6835 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006836 /* assume passive cookie by default */
6837 txn->flags &= ~TX_SCK_MASK;
6838 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839
6840 /* If the cookie is in insert mode on a known server, we'll delete
6841 * this occurrence because we'll insert another one later.
6842 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 * a direct access.
6844 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006845 if (t->be->options2 & PR_O2_COOK_PSV) {
6846 /* The "preserve" flag was set, we don't want to touch the
6847 * server's cookie.
6848 */
6849 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006850 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006851 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 /* this cookie must be deleted */
6853 if (*prev == ':' && next == hdr_end) {
6854 /* whole header */
6855 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6856 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6857 txn->hdr_idx.used--;
6858 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006859 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006860 hdr_next += delta;
6861 http_msg_move_end(&txn->rsp, delta);
6862 /* note: while both invalid now, <next> and <hdr_end>
6863 * are still equal, so the for() will stop as expected.
6864 */
6865 } else {
6866 /* just remove the value */
6867 int delta = del_hdr_value(res, &prev, next);
6868 next = prev;
6869 hdr_end += delta;
6870 hdr_next += delta;
6871 cur_hdr->len += delta;
6872 http_msg_move_end(&txn->rsp, delta);
6873 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006874 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006875 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006878 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006879 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 * with this server since we know it.
6881 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006882 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 next += delta;
6884 hdr_end += delta;
6885 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006887 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888
Willy Tarreauf1348312010-10-07 15:54:11 +02006889 txn->flags &= ~TX_SCK_MASK;
6890 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006892 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006896 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 next += delta;
6898 hdr_end += delta;
6899 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006901 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902
Willy Tarreau827aee92011-03-10 16:55:02 +01006903 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006904 txn->flags &= ~TX_SCK_MASK;
6905 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 }
6907 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006908 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6909 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006910 int cmp_len, value_len;
6911 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912
Cyril Bontéb21570a2009-11-29 20:04:48 +01006913 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6915 value_begin = att_beg + t->be->appsession_name_len;
6916 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006917 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918 cmp_len = att_end - att_beg;
6919 value_begin = val_beg;
6920 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006921 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006922
Cyril Bonté17530c32010-04-06 21:11:10 +02006923 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6925 /* free a possibly previously allocated memory */
6926 pool_free2(apools.sessid, txn->sessid);
6927
Cyril Bontéb21570a2009-11-29 20:04:48 +01006928 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006929 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006930 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6931 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6932 return;
6933 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006934 memcpy(txn->sessid, value_begin, value_len);
6935 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 }
6938 /* that's done for this cookie, check the next one on the same
6939 * line when next != hdr_end (only if is_cookie2).
6940 */
6941 }
6942 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006944 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006946 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947 appsess *asession = NULL;
6948 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006949 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006950 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006951 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006952 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6953 Alert("Not enough Memory process_srv():asession:calloc().\n");
6954 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6955 return;
6956 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006957 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6958
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006959 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6960 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6961 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006962 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006963 return;
6964 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006965 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006966 asession->sessid[t->be->appsession_len] = 0;
6967
Willy Tarreau827aee92011-03-10 16:55:02 +01006968 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006970 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006971 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006972 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006973 return;
6974 }
6975 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006976 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006977
6978 asession->request_count = 0;
6979 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6980 }
6981
6982 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6983 asession->request_count++;
6984 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985}
6986
6987
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988/*
6989 * Check if response is cacheable or not. Updates t->flags.
6990 */
6991void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6992{
6993 struct http_txn *txn = &t->txn;
6994 char *p1, *p2;
6995
6996 char *cur_ptr, *cur_end, *cur_next;
6997 int cur_idx;
6998
Willy Tarreau5df51872007-11-25 16:20:08 +01006999 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000 return;
7001
7002 /* Iterate through the headers.
7003 * we start with the start line.
7004 */
7005 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007006 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007
7008 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7009 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007010 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011
7012 cur_hdr = &txn->hdr_idx.v[cur_idx];
7013 cur_ptr = cur_next;
7014 cur_end = cur_ptr + cur_hdr->len;
7015 cur_next = cur_end + cur_hdr->cr + 1;
7016
7017 /* We have one full header between cur_ptr and cur_end, and the
7018 * next header starts at cur_next. We're only interested in
7019 * "Cookie:" headers.
7020 */
7021
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007022 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7023 if (val) {
7024 if ((cur_end - (cur_ptr + val) >= 8) &&
7025 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7026 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7027 return;
7028 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 }
7030
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007031 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7032 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033 continue;
7034
7035 /* OK, right now we know we have a cache-control header at cur_ptr */
7036
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007037 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007038
7039 if (p1 >= cur_end) /* no more info */
7040 continue;
7041
7042 /* p1 is at the beginning of the value */
7043 p2 = p1;
7044
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007045 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007046 p2++;
7047
7048 /* we have a complete value between p1 and p2 */
7049 if (p2 < cur_end && *p2 == '=') {
7050 /* we have something of the form no-cache="set-cookie" */
7051 if ((cur_end - p1 >= 21) &&
7052 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7053 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007054 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 continue;
7056 }
7057
7058 /* OK, so we know that either p2 points to the end of string or to a comma */
7059 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7060 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7061 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7062 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007063 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064 return;
7065 }
7066
7067 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007068 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 continue;
7070 }
7071 }
7072}
7073
7074
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075/*
7076 * Try to retrieve a known appsession in the URI, then the associated server.
7077 * If the server is found, it's assigned to the session.
7078 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007081 char *end_params, *first_param, *cur_param, *next_param;
7082 char separator;
7083 int value_len;
7084
7085 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007087 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007088 (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 +01007089 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007090 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Cyril Bontéb21570a2009-11-29 20:04:48 +01007092 first_param = NULL;
7093 switch (mode) {
7094 case PR_O2_AS_M_PP:
7095 first_param = memchr(begin, ';', len);
7096 break;
7097 case PR_O2_AS_M_QS:
7098 first_param = memchr(begin, '?', len);
7099 break;
7100 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
Cyril Bontéb21570a2009-11-29 20:04:48 +01007102 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105
Cyril Bontéb21570a2009-11-29 20:04:48 +01007106 switch (mode) {
7107 case PR_O2_AS_M_PP:
7108 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7109 end_params = (char *) begin + len;
7110 }
7111 separator = ';';
7112 break;
7113 case PR_O2_AS_M_QS:
7114 end_params = (char *) begin + len;
7115 separator = '&';
7116 break;
7117 default:
7118 /* unknown mode, shouldn't happen */
7119 return;
7120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121
Cyril Bontéb21570a2009-11-29 20:04:48 +01007122 cur_param = next_param = end_params;
7123 while (cur_param > first_param) {
7124 cur_param--;
7125 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7126 /* let's see if this is the appsession parameter */
7127 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7128 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7129 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7130 /* Cool... it's the right one */
7131 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7132 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7133 if (value_len > 0) {
7134 manage_client_side_appsession(t, cur_param, value_len);
7135 }
7136 break;
7137 }
7138 next_param = cur_param;
7139 }
7140 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007142 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007143 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145}
7146
Willy Tarreaub2513902006-12-17 14:52:38 +01007147/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007148 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007149 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007150 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007151 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007152 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007153 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007154 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007155 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007156int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007157{
7158 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007159 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007160
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007161 if (!uri_auth)
7162 return 0;
7163
Cyril Bonté70be45d2010-10-12 00:14:35 +02007164 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007165 return 0;
7166
Willy Tarreau295a8372011-03-10 11:25:07 +01007167 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007168 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007171 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007172 return 0;
7173
Willy Tarreau962c3f42010-01-10 00:15:35 +01007174 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007175
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007176 /* the URI is in h */
7177 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007178 return 0;
7179
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007180 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007181 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007182 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007183 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007184 break;
7185 }
7186 h++;
7187 }
7188
7189 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007190 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7191 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007192 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007193 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007194 break;
7195 }
7196 h++;
7197 }
7198 }
7199
Willy Tarreau962c3f42010-01-10 00:15:35 +01007200 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7201 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007202 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007203 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007204 break;
7205 }
7206 h++;
7207 }
7208
Cyril Bonté70be45d2010-10-12 00:14:35 +02007209 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7210 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7211 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007212 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007213 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007214 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7215 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7216 si->applet.ctx.stats.st_code = i;
7217 break;
7218 }
7219 }
7220 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007221 break;
7222 }
7223 h++;
7224 }
7225
Willy Tarreau295a8372011-03-10 11:25:07 +01007226 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007227
Willy Tarreaub2513902006-12-17 14:52:38 +01007228 return 1;
7229}
7230
Willy Tarreau4076a152009-04-02 15:18:36 +02007231/*
7232 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007233 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7234 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007235 */
7236void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7237 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007238 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007239{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007240 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7241 int len1 = buf->size - msg->som;
7242 es->len = buf->r - (buf->data + msg->som) + buf->size;
7243 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7244 if (es->len > len1 && len1 < sizeof(es->buf))
7245 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7246 }
7247 else {
7248 es->len = buf->r - (buf->data + msg->som);
7249 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7250 }
7251
Willy Tarreau4076a152009-04-02 15:18:36 +02007252 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007253 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007254 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007255 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007256 else
7257 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7258
Willy Tarreau4076a152009-04-02 15:18:36 +02007259 es->when = date; // user-visible date
7260 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007261 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007262 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007263 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007264 es->state = state;
7265 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007266 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007267}
Willy Tarreaub2513902006-12-17 14:52:38 +01007268
Willy Tarreau294c4732011-12-16 21:35:50 +01007269/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7270 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7271 * performed over the whole headers. Otherwise it must contain a valid header
7272 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7273 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7274 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7275 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7276 * -1.
7277 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007279unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7280 struct hdr_idx *idx, int occ,
7281 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007282{
Willy Tarreau294c4732011-12-16 21:35:50 +01007283 struct hdr_ctx local_ctx;
7284 char *ptr_hist[MAX_HDR_HISTORY];
7285 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007286 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007287 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007288
Willy Tarreau294c4732011-12-16 21:35:50 +01007289 if (!ctx) {
7290 local_ctx.idx = 0;
7291 ctx = &local_ctx;
7292 }
7293
Willy Tarreaubce70882009-09-07 11:51:47 +02007294 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007295 /* search from the beginning */
7296 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007297 occ--;
7298 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007299 *vptr = ctx->line + ctx->val;
7300 *vlen = ctx->vlen;
7301 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007302 }
7303 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007304 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007305 }
7306
7307 /* negative occurrence, we scan all the list then walk back */
7308 if (-occ > MAX_HDR_HISTORY)
7309 return 0;
7310
Willy Tarreau294c4732011-12-16 21:35:50 +01007311 found = hist_ptr = 0;
7312 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7313 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7314 len_hist[hist_ptr] = ctx->vlen;
7315 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007316 hist_ptr = 0;
7317 found++;
7318 }
7319 if (-occ > found)
7320 return 0;
7321 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7322 * find occurrence -occ, so we have to check [hist_ptr+occ].
7323 */
7324 hist_ptr += occ;
7325 if (hist_ptr >= MAX_HDR_HISTORY)
7326 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007327 *vptr = ptr_hist[hist_ptr];
7328 *vlen = len_hist[hist_ptr];
7329 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007330}
7331
Willy Tarreaubaaee002006-06-26 02:48:02 +02007332/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 * Print a debug line with a header
7334 */
7335void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7336{
7337 int len, max;
7338 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007339 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340 max = end - start;
7341 UBOUND(max, sizeof(trash) - len - 1);
7342 len += strlcpy2(trash + len, start, max + 1);
7343 trash[len++] = '\n';
7344 write(1, trash, len);
7345}
7346
Willy Tarreau0937bc42009-12-22 15:03:09 +01007347/*
7348 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7349 * the required fields are properly allocated and that we only need to (re)init
7350 * them. This should be used before processing any new request.
7351 */
7352void http_init_txn(struct session *s)
7353{
7354 struct http_txn *txn = &s->txn;
7355 struct proxy *fe = s->fe;
7356
7357 txn->flags = 0;
7358 txn->status = -1;
7359
William Lallemand5f232402012-04-05 18:02:55 +02007360 global.req_count++;
7361
Willy Tarreauf64d1412010-10-07 20:06:11 +02007362 txn->cookie_first_date = 0;
7363 txn->cookie_last_date = 0;
7364
Willy Tarreau0937bc42009-12-22 15:03:09 +01007365 txn->req.sol = txn->req.eol = NULL;
7366 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7367 txn->rsp.sol = txn->rsp.eol = NULL;
7368 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007369 txn->req.chunk_len = 0LL;
7370 txn->req.body_len = 0LL;
7371 txn->rsp.chunk_len = 0LL;
7372 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007373 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7374 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007375
7376 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007377
7378 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7379 if (fe->options2 & PR_O2_REQBUG_OK)
7380 txn->req.err_pos = -1; /* let buggy requests pass */
7381
Willy Tarreau46023632010-01-07 22:51:47 +01007382 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007383 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7384
Willy Tarreau46023632010-01-07 22:51:47 +01007385 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007386 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7387
7388 if (txn->hdr_idx.v)
7389 hdr_idx_init(&txn->hdr_idx);
7390}
7391
7392/* to be used at the end of a transaction */
7393void http_end_txn(struct session *s)
7394{
7395 struct http_txn *txn = &s->txn;
7396
7397 /* these ones will have been dynamically allocated */
7398 pool_free2(pool2_requri, txn->uri);
7399 pool_free2(pool2_capture, txn->cli_cookie);
7400 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007401 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007402 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007403
William Lallemanda73203e2012-03-12 12:48:57 +01007404 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007405 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007406 txn->uri = NULL;
7407 txn->srv_cookie = NULL;
7408 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007409
7410 if (txn->req.cap) {
7411 struct cap_hdr *h;
7412 for (h = s->fe->req_cap; h; h = h->next)
7413 pool_free2(h->pool, txn->req.cap[h->index]);
7414 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7415 }
7416
7417 if (txn->rsp.cap) {
7418 struct cap_hdr *h;
7419 for (h = s->fe->rsp_cap; h; h = h->next)
7420 pool_free2(h->pool, txn->rsp.cap[h->index]);
7421 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7422 }
7423
Willy Tarreau0937bc42009-12-22 15:03:09 +01007424}
7425
7426/* to be used at the end of a transaction to prepare a new one */
7427void http_reset_txn(struct session *s)
7428{
7429 http_end_txn(s);
7430 http_init_txn(s);
7431
7432 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007433 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007434 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007435 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007436 /* re-init store persistence */
7437 s->store_count = 0;
7438
Willy Tarreau0937bc42009-12-22 15:03:09 +01007439 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007440
7441 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7442
Willy Tarreau739cfba2010-01-25 23:11:14 +01007443 /* We must trim any excess data from the response buffer, because we
7444 * may have blocked an invalid response from a server that we don't
7445 * want to accidentely forward once we disable the analysers, nor do
7446 * we want those data to come along with next response. A typical
7447 * example of such data would be from a buggy server responding to
7448 * a HEAD with some data, or sending more than the advertised
7449 * content-length.
7450 */
7451 if (unlikely(s->rep->l > s->rep->send_max)) {
7452 s->rep->l = s->rep->send_max;
7453 s->rep->r = s->rep->w + s->rep->l;
7454 if (s->rep->r >= s->rep->data + s->rep->size)
7455 s->rep->r -= s->rep->size;
7456 }
7457
Willy Tarreau0937bc42009-12-22 15:03:09 +01007458 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007459 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007460
Willy Tarreaud04e8582010-05-31 12:31:35 +02007461 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007462 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007463
7464 s->req->rex = TICK_ETERNITY;
7465 s->req->wex = TICK_ETERNITY;
7466 s->req->analyse_exp = TICK_ETERNITY;
7467 s->rep->rex = TICK_ETERNITY;
7468 s->rep->wex = TICK_ETERNITY;
7469 s->rep->analyse_exp = TICK_ETERNITY;
7470}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007471
Willy Tarreauff011f22011-01-06 17:51:27 +01007472void free_http_req_rules(struct list *r) {
7473 struct http_req_rule *tr, *pr;
7474
7475 list_for_each_entry_safe(pr, tr, r, list) {
7476 LIST_DEL(&pr->list);
7477 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7478 free(pr->http_auth.realm);
7479
7480 free(pr);
7481 }
7482}
7483
7484struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7485{
7486 struct http_req_rule *rule;
7487 int cur_arg;
7488
7489 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7490 if (!rule) {
7491 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7492 return NULL;
7493 }
7494
7495 if (!*args[0]) {
7496 goto req_error_parsing;
7497 } else if (!strcmp(args[0], "allow")) {
7498 rule->action = HTTP_REQ_ACT_ALLOW;
7499 cur_arg = 1;
7500 } else if (!strcmp(args[0], "deny")) {
7501 rule->action = HTTP_REQ_ACT_DENY;
7502 cur_arg = 1;
7503 } else if (!strcmp(args[0], "auth")) {
7504 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7505 cur_arg = 1;
7506
7507 while(*args[cur_arg]) {
7508 if (!strcmp(args[cur_arg], "realm")) {
7509 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7510 cur_arg+=2;
7511 continue;
7512 } else
7513 break;
7514 }
7515 } else {
7516req_error_parsing:
7517 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7518 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7519 return NULL;
7520 }
7521
7522 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7523 struct acl_cond *cond;
7524
7525 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7526 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7527 file, linenum, args[0]);
7528 return NULL;
7529 }
7530 rule->cond = cond;
7531 }
7532 else if (*args[cur_arg]) {
7533 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7534 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7535 file, linenum, args[0], args[cur_arg]);
7536 return NULL;
7537 }
7538
7539 return rule;
7540}
7541
Willy Tarreau8797c062007-05-07 00:55:35 +02007542/************************************************************************/
7543/* The code below is dedicated to ACL parsing and matching */
7544/************************************************************************/
7545
7546
7547
7548
7549/* 1. Check on METHOD
7550 * We use the pre-parsed method if it is known, and store its number as an
7551 * integer. If it is unknown, we use the pointer and the length.
7552 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007553static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007554{
7555 int len, meth;
7556
Willy Tarreauae8b7962007-06-09 23:10:04 +02007557 len = strlen(*text);
7558 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007559
7560 pattern->val.i = meth;
7561 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007562 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007563 if (!pattern->ptr.str)
7564 return 0;
7565 pattern->len = len;
7566 }
7567 return 1;
7568}
7569
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007570/* This function fetches the method of current HTTP request and stores
7571 * it in the global pattern struct as a chunk. There are two possibilities :
7572 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7573 * in <len> and <ptr> is NULL ;
7574 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7575 * <len> to its length.
7576 * This is intended to be used with acl_match_meth() only.
7577 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007578static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007579acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7580 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007581{
7582 int meth;
7583 struct http_txn *txn = l7;
7584
Willy Tarreaub6866442008-07-14 23:54:42 +02007585 if (!txn)
7586 return 0;
7587
Willy Tarreau655dce92009-11-08 13:10:58 +01007588 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007589 return 0;
7590
Willy Tarreau8797c062007-05-07 00:55:35 +02007591 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007592 temp_pattern.data.str.len = meth;
7593 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007594 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007595 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7596 /* ensure the indexes are not affected */
7597 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007598 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7599 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007600 }
7601 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7602 return 1;
7603}
7604
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007605/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007606static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7607{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007608 int icase;
7609
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007610
7611 if (temp_pattern.data.str.str == NULL) {
7612 /* well-known method */
7613 if (temp_pattern.data.str.len == pattern->val.i)
7614 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007615 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007616 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007617
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007618 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7619 if (pattern->val.i != HTTP_METH_OTHER)
7620 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007621
7622 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007623 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007624 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007625
7626 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007627 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7628 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007629 return ACL_PAT_FAIL;
7630 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007631}
7632
7633/* 2. Check on Request/Status Version
7634 * We simply compare strings here.
7635 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007636static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007637{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007638 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007639 if (!pattern->ptr.str)
7640 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007641 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007642 return 1;
7643}
7644
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007645static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007646acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7647 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007648{
7649 struct http_txn *txn = l7;
7650 char *ptr;
7651 int len;
7652
Willy Tarreaub6866442008-07-14 23:54:42 +02007653 if (!txn)
7654 return 0;
7655
Willy Tarreau655dce92009-11-08 13:10:58 +01007656 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007657 return 0;
7658
Willy Tarreau8797c062007-05-07 00:55:35 +02007659 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007660 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007661
7662 while ((len-- > 0) && (*ptr++ != '/'));
7663 if (len <= 0)
7664 return 0;
7665
Willy Tarreau664092c2011-12-16 19:11:42 +01007666 temp_pattern.data.str.str = ptr;
7667 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007668
7669 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7670 return 1;
7671}
7672
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007673static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007674acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7675 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007676{
7677 struct http_txn *txn = l7;
7678 char *ptr;
7679 int len;
7680
Willy Tarreaub6866442008-07-14 23:54:42 +02007681 if (!txn)
7682 return 0;
7683
Willy Tarreau655dce92009-11-08 13:10:58 +01007684 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007685 return 0;
7686
Willy Tarreau8797c062007-05-07 00:55:35 +02007687 len = txn->rsp.sl.st.v_l;
7688 ptr = txn->rsp.sol;
7689
7690 while ((len-- > 0) && (*ptr++ != '/'));
7691 if (len <= 0)
7692 return 0;
7693
Willy Tarreau664092c2011-12-16 19:11:42 +01007694 temp_pattern.data.str.str = ptr;
7695 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007696
7697 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7698 return 1;
7699}
7700
7701/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007702static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007703acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7704 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007705{
7706 struct http_txn *txn = l7;
7707 char *ptr;
7708 int len;
7709
Willy Tarreaub6866442008-07-14 23:54:42 +02007710 if (!txn)
7711 return 0;
7712
Willy Tarreau655dce92009-11-08 13:10:58 +01007713 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007714 return 0;
7715
Willy Tarreau8797c062007-05-07 00:55:35 +02007716 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007717 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007718
Willy Tarreaua5e37562011-12-16 17:06:15 +01007719 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007720 test->flags = ACL_TEST_F_VOL_1ST;
7721 return 1;
7722}
7723
7724/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007725static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007726acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7727 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007728{
7729 struct http_txn *txn = l7;
7730
Willy Tarreaub6866442008-07-14 23:54:42 +02007731 if (!txn)
7732 return 0;
7733
Willy Tarreau655dce92009-11-08 13:10:58 +01007734 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007735 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007736
Willy Tarreauc11416f2007-06-17 16:58:38 +02007737 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7738 /* ensure the indexes are not affected */
7739 return 0;
7740
Willy Tarreau664092c2011-12-16 19:11:42 +01007741 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7742 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007743
Willy Tarreauf3d25982007-05-08 22:45:09 +02007744 /* we do not need to set READ_ONLY because the data is in a buffer */
7745 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007746 return 1;
7747}
7748
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007749static int
7750acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7751 struct acl_expr *expr, struct acl_test *test)
7752{
7753 struct http_txn *txn = l7;
7754
Willy Tarreaub6866442008-07-14 23:54:42 +02007755 if (!txn)
7756 return 0;
7757
Willy Tarreau655dce92009-11-08 13:10:58 +01007758 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007759 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007760
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007761 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7762 /* ensure the indexes are not affected */
7763 return 0;
7764
7765 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007766 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 +01007767 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7768 return 0;
7769 temp_pattern.type = PATTERN_TYPE_IP;
7770 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007771
7772 /*
7773 * If we are parsing url in frontend space, we prepare backend stage
7774 * to not parse again the same url ! optimization lazyness...
7775 */
7776 if (px->options & PR_O_HTTP_PROXY)
7777 l4->flags |= SN_ADDR_SET;
7778
Willy Tarreauf4362b32011-12-16 17:49:52 +01007779 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007780 return 1;
7781}
7782
7783static int
7784acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7785 struct acl_expr *expr, struct acl_test *test)
7786{
7787 struct http_txn *txn = l7;
7788
Willy Tarreaub6866442008-07-14 23:54:42 +02007789 if (!txn)
7790 return 0;
7791
Willy Tarreau655dce92009-11-08 13:10:58 +01007792 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007793 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007794
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007795 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7796 /* ensure the indexes are not affected */
7797 return 0;
7798
7799 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007800 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 +01007801 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007802
7803 if (px->options & PR_O_HTTP_PROXY)
7804 l4->flags |= SN_ADDR_SET;
7805
7806 test->flags = ACL_TEST_F_READ_ONLY;
7807 return 1;
7808}
7809
Willy Tarreauc11416f2007-06-17 16:58:38 +02007810/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7811 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7812 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007813static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007814acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007815 struct acl_expr *expr, struct acl_test *test)
7816{
7817 struct http_txn *txn = l7;
7818 struct hdr_idx *idx = &txn->hdr_idx;
7819 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007820
Willy Tarreaub6866442008-07-14 23:54:42 +02007821 if (!txn)
7822 return 0;
7823
Willy Tarreau33a7e692007-06-10 19:45:56 +02007824 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7825 /* search for header from the beginning */
7826 ctx->idx = 0;
7827
Willy Tarreau33a7e692007-06-10 19:45:56 +02007828 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7829 test->flags |= ACL_TEST_F_FETCH_MORE;
7830 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007831 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7832 temp_pattern.data.str.len = ctx->vlen;
7833
Willy Tarreau33a7e692007-06-10 19:45:56 +02007834 return 1;
7835 }
7836
7837 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7838 test->flags |= ACL_TEST_F_VOL_HDR;
7839 return 0;
7840}
7841
Willy Tarreau33a7e692007-06-10 19:45:56 +02007842static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007843acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7844 struct acl_expr *expr, struct acl_test *test)
7845{
7846 struct http_txn *txn = l7;
7847
Willy Tarreaub6866442008-07-14 23:54:42 +02007848 if (!txn)
7849 return 0;
7850
Willy Tarreau655dce92009-11-08 13:10:58 +01007851 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007852 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007853
Willy Tarreauc11416f2007-06-17 16:58:38 +02007854 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7855 /* ensure the indexes are not affected */
7856 return 0;
7857
7858 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7859}
7860
7861static int
7862acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7863 struct acl_expr *expr, struct acl_test *test)
7864{
7865 struct http_txn *txn = l7;
7866
Willy Tarreaub6866442008-07-14 23:54:42 +02007867 if (!txn)
7868 return 0;
7869
Willy Tarreau655dce92009-11-08 13:10:58 +01007870 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007871 return 0;
7872
7873 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7874}
7875
7876/* 6. Check on HTTP header count. The number of occurrences is returned.
7877 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7878 */
7879static int
7880acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007881 struct acl_expr *expr, struct acl_test *test)
7882{
7883 struct http_txn *txn = l7;
7884 struct hdr_idx *idx = &txn->hdr_idx;
7885 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007886 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007887
Willy Tarreaub6866442008-07-14 23:54:42 +02007888 if (!txn)
7889 return 0;
7890
Willy Tarreau33a7e692007-06-10 19:45:56 +02007891 ctx.idx = 0;
7892 cnt = 0;
7893 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7894 cnt++;
7895
Willy Tarreaua5e37562011-12-16 17:06:15 +01007896 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007897 test->flags = ACL_TEST_F_VOL_HDR;
7898 return 1;
7899}
7900
Willy Tarreauc11416f2007-06-17 16:58:38 +02007901static int
7902acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7903 struct acl_expr *expr, struct acl_test *test)
7904{
7905 struct http_txn *txn = l7;
7906
Willy Tarreaub6866442008-07-14 23:54:42 +02007907 if (!txn)
7908 return 0;
7909
Willy Tarreau655dce92009-11-08 13:10:58 +01007910 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007911 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007912
Willy Tarreauc11416f2007-06-17 16:58:38 +02007913 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7914 /* ensure the indexes are not affected */
7915 return 0;
7916
7917 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7918}
7919
7920static int
7921acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7922 struct acl_expr *expr, struct acl_test *test)
7923{
7924 struct http_txn *txn = l7;
7925
Willy Tarreaub6866442008-07-14 23:54:42 +02007926 if (!txn)
7927 return 0;
7928
Willy Tarreau655dce92009-11-08 13:10:58 +01007929 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007930 return 0;
7931
7932 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7933}
7934
Willy Tarreau33a7e692007-06-10 19:45:56 +02007935/* 7. Check on HTTP header's integer value. The integer value is returned.
7936 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007937 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007938 */
7939static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007940acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007941 struct acl_expr *expr, struct acl_test *test)
7942{
7943 struct http_txn *txn = l7;
7944 struct hdr_idx *idx = &txn->hdr_idx;
7945 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007946
Willy Tarreaub6866442008-07-14 23:54:42 +02007947 if (!txn)
7948 return 0;
7949
Willy Tarreau33a7e692007-06-10 19:45:56 +02007950 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7951 /* search for header from the beginning */
7952 ctx->idx = 0;
7953
Willy Tarreau33a7e692007-06-10 19:45:56 +02007954 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7955 test->flags |= ACL_TEST_F_FETCH_MORE;
7956 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007957 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007958 return 1;
7959 }
7960
7961 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7962 test->flags |= ACL_TEST_F_VOL_HDR;
7963 return 0;
7964}
7965
Willy Tarreauc11416f2007-06-17 16:58:38 +02007966static int
7967acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7968 struct acl_expr *expr, struct acl_test *test)
7969{
7970 struct http_txn *txn = l7;
7971
Willy Tarreaub6866442008-07-14 23:54:42 +02007972 if (!txn)
7973 return 0;
7974
Willy Tarreau655dce92009-11-08 13:10:58 +01007975 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007976 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007977
Willy Tarreauc11416f2007-06-17 16:58:38 +02007978 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7979 /* ensure the indexes are not affected */
7980 return 0;
7981
7982 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7983}
7984
7985static int
7986acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7987 struct acl_expr *expr, struct acl_test *test)
7988{
7989 struct http_txn *txn = l7;
7990
Willy Tarreaub6866442008-07-14 23:54:42 +02007991 if (!txn)
7992 return 0;
7993
Willy Tarreau655dce92009-11-08 13:10:58 +01007994 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007995 return 0;
7996
7997 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7998}
7999
Willy Tarreau106f9792009-09-19 07:54:16 +02008000/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8001 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8002 */
8003static int
8004acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8005 struct acl_expr *expr, struct acl_test *test)
8006{
8007 struct http_txn *txn = l7;
8008 struct hdr_idx *idx = &txn->hdr_idx;
8009 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8010
8011 if (!txn)
8012 return 0;
8013
8014 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8015 /* search for header from the beginning */
8016 ctx->idx = 0;
8017
Willy Tarreauf4362b32011-12-16 17:49:52 +01008018 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008019 test->flags |= ACL_TEST_F_FETCH_MORE;
8020 test->flags |= ACL_TEST_F_VOL_HDR;
8021 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008022 temp_pattern.type = PATTERN_TYPE_IP;
8023 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8024 return 1;
8025 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008026 }
8027
8028 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8029 test->flags |= ACL_TEST_F_VOL_HDR;
8030 return 0;
8031}
8032
8033static int
8034acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8035 struct acl_expr *expr, struct acl_test *test)
8036{
8037 struct http_txn *txn = l7;
8038
8039 if (!txn)
8040 return 0;
8041
Willy Tarreau655dce92009-11-08 13:10:58 +01008042 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008043 return 0;
8044
8045 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8046 /* ensure the indexes are not affected */
8047 return 0;
8048
8049 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8050}
8051
8052static int
8053acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8054 struct acl_expr *expr, struct acl_test *test)
8055{
8056 struct http_txn *txn = l7;
8057
8058 if (!txn)
8059 return 0;
8060
Willy Tarreau655dce92009-11-08 13:10:58 +01008061 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008062 return 0;
8063
8064 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8065}
8066
Willy Tarreau737b0c12007-06-10 21:28:46 +02008067/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8068 * the first '/' after the possible hostname, and ends before the possible '?'.
8069 */
8070static int
8071acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8072 struct acl_expr *expr, struct acl_test *test)
8073{
8074 struct http_txn *txn = l7;
8075 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008076
Willy Tarreaub6866442008-07-14 23:54:42 +02008077 if (!txn)
8078 return 0;
8079
Willy Tarreau655dce92009-11-08 13:10:58 +01008080 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008081 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008082
Willy Tarreauc11416f2007-06-17 16:58:38 +02008083 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8084 /* ensure the indexes are not affected */
8085 return 0;
8086
Willy Tarreau962c3f42010-01-10 00:15:35 +01008087 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008088 ptr = http_get_path(txn);
8089 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008090 return 0;
8091
8092 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008093 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008094
8095 while (ptr < end && *ptr != '?')
8096 ptr++;
8097
Willy Tarreau664092c2011-12-16 19:11:42 +01008098 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008099
8100 /* we do not need to set READ_ONLY because the data is in a buffer */
8101 test->flags = ACL_TEST_F_VOL_1ST;
8102 return 1;
8103}
8104
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008105static int
8106acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8107 struct acl_expr *expr, struct acl_test *test)
8108{
8109 struct buffer *req = s->req;
8110 struct http_txn *txn = &s->txn;
8111 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008112
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008113 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8114 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8115 */
8116
8117 if (!s || !req)
8118 return 0;
8119
Willy Tarreau655dce92009-11-08 13:10:58 +01008120 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008121 /* Already decoded as OK */
8122 test->flags |= ACL_TEST_F_SET_RES_PASS;
8123 return 1;
8124 }
8125
8126 /* Try to decode HTTP request */
8127 if (likely(req->lr < req->r))
8128 http_msg_analyzer(req, msg, &txn->hdr_idx);
8129
Willy Tarreau655dce92009-11-08 13:10:58 +01008130 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008131 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8132 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8133 return 1;
8134 }
8135 /* wait for final state */
8136 test->flags |= ACL_TEST_F_MAY_CHANGE;
8137 return 0;
8138 }
8139
8140 /* OK we got a valid HTTP request. We have some minor preparation to
8141 * perform so that further checks can rely on HTTP tests.
8142 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008143 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008144 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8145 s->flags |= SN_REDIRECTABLE;
8146
8147 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8148 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8149 return 1;
8150 }
8151
8152 test->flags |= ACL_TEST_F_SET_RES_PASS;
8153 return 1;
8154}
8155
Willy Tarreau7f18e522010-10-22 20:04:13 +02008156/* return a valid test if the current request is the first one on the connection */
8157static int
8158acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8159 struct acl_expr *expr, struct acl_test *test)
8160{
8161 if (!s)
8162 return 0;
8163
8164 if (s->txn.flags & TX_NOT_FIRST)
8165 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8166 else
8167 test->flags |= ACL_TEST_F_SET_RES_PASS;
8168
8169 return 1;
8170}
8171
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008172static int
8173acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8174 struct acl_expr *expr, struct acl_test *test)
8175{
8176
8177 if (!s)
8178 return 0;
8179
8180 if (!get_http_auth(s))
8181 return 0;
8182
8183 test->ctx.a[0] = expr->arg.ul;
8184 test->ctx.a[1] = s->txn.auth.user;
8185 test->ctx.a[2] = s->txn.auth.pass;
8186
8187 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8188
8189 return 1;
8190}
Willy Tarreau8797c062007-05-07 00:55:35 +02008191
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008192/* Try to find the next occurrence of a cookie name in a cookie header value.
8193 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8194 * the cookie value is returned into *value and *value_l, and the function
8195 * returns a pointer to the next pointer to search from if the value was found.
8196 * Otherwise if the cookie was not found, NULL is returned and neither value
8197 * nor value_l are touched. The input <hdr> string should first point to the
8198 * header's value, and the <hdr_end> pointer must point to the first character
8199 * not part of the value. <list> must be non-zero if value may represent a list
8200 * of values (cookie headers). This makes it faster to abort parsing when no
8201 * list is expected.
8202 */
8203static char *
8204extract_cookie_value(char *hdr, const char *hdr_end,
8205 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008206 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008207{
8208 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8209 char *next;
8210
8211 /* we search at least a cookie name followed by an equal, and more
8212 * generally something like this :
8213 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8214 */
8215 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8216 /* Iterate through all cookies on this line */
8217
8218 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8219 att_beg++;
8220
8221 /* find att_end : this is the first character after the last non
8222 * space before the equal. It may be equal to hdr_end.
8223 */
8224 equal = att_end = att_beg;
8225
8226 while (equal < hdr_end) {
8227 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8228 break;
8229 if (http_is_spht[(unsigned char)*equal++])
8230 continue;
8231 att_end = equal;
8232 }
8233
8234 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8235 * is between <att_beg> and <equal>, both may be identical.
8236 */
8237
8238 /* look for end of cookie if there is an equal sign */
8239 if (equal < hdr_end && *equal == '=') {
8240 /* look for the beginning of the value */
8241 val_beg = equal + 1;
8242 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8243 val_beg++;
8244
8245 /* find the end of the value, respecting quotes */
8246 next = find_cookie_value_end(val_beg, hdr_end);
8247
8248 /* make val_end point to the first white space or delimitor after the value */
8249 val_end = next;
8250 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8251 val_end--;
8252 } else {
8253 val_beg = val_end = next = equal;
8254 }
8255
8256 /* We have nothing to do with attributes beginning with '$'. However,
8257 * they will automatically be removed if a header before them is removed,
8258 * since they're supposed to be linked together.
8259 */
8260 if (*att_beg == '$')
8261 continue;
8262
8263 /* Ignore cookies with no equal sign */
8264 if (equal == next)
8265 continue;
8266
8267 /* Now we have the cookie name between att_beg and att_end, and
8268 * its value between val_beg and val_end.
8269 */
8270
8271 if (att_end - att_beg == cookie_name_l &&
8272 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8273 /* let's return this value and indicate where to go on from */
8274 *value = val_beg;
8275 *value_l = val_end - val_beg;
8276 return next + 1;
8277 }
8278
8279 /* Set-Cookie headers only have the name in the first attr=value part */
8280 if (!list)
8281 break;
8282 }
8283
8284 return NULL;
8285}
8286
8287/* Iterate over all cookies present in a request. The context is stored in
8288 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8289 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8290 * non-null, then multiple cookies may be parsed on the same line.
8291 * The cookie name is in expr->arg and the name length in expr->arg_len.
8292 */
8293static int
8294acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8295 const char *hdr_name, int hdr_name_len, int multi,
8296 struct acl_expr *expr, struct acl_test *test)
8297{
8298 struct http_txn *txn = l7;
8299 struct hdr_idx *idx = &txn->hdr_idx;
8300 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8301
8302 if (!txn)
8303 return 0;
8304
8305 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8306 /* search for the header from the beginning, we must first initialize
8307 * the search parameters.
8308 */
8309 test->ctx.a[0] = NULL;
8310 ctx->idx = 0;
8311 }
8312
8313 while (1) {
8314 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8315 if (!test->ctx.a[0]) {
8316 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8317 goto out;
8318
8319 if (ctx->vlen < expr->arg_len + 1)
8320 continue;
8321
8322 test->ctx.a[0] = ctx->line + ctx->val;
8323 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8324 }
8325
8326 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8327 expr->arg.str, expr->arg_len, multi,
8328 &temp_pattern.data.str.str,
8329 &temp_pattern.data.str.len);
8330 if (test->ctx.a[0]) {
8331 /* one value was returned into temp_pattern.data.str.{str,len} */
8332 test->flags |= ACL_TEST_F_FETCH_MORE;
8333 test->flags |= ACL_TEST_F_VOL_HDR;
8334 return 1;
8335 }
8336 }
8337
8338 out:
8339 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8340 test->flags |= ACL_TEST_F_VOL_HDR;
8341 return 0;
8342}
8343
8344static int
8345acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8346 struct acl_expr *expr, struct acl_test *test)
8347{
8348 struct http_txn *txn = l7;
8349
8350 if (!txn)
8351 return 0;
8352
8353 if (txn->req.msg_state < HTTP_MSG_BODY)
8354 return 0;
8355
8356 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8357 /* ensure the indexes are not affected */
8358 return 0;
8359
8360 /* The Cookie header allows multiple cookies on the same line */
8361 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8362}
8363
8364static int
8365acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8366 struct acl_expr *expr, struct acl_test *test)
8367{
8368 struct http_txn *txn = l7;
8369
8370 if (!txn)
8371 return 0;
8372
8373 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8374 return 0;
8375
8376 /* The Set-Cookie header allows only one cookie on the same line */
8377 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8378}
8379
8380/* Iterate over all cookies present in a request to count how many occurrences
8381 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8382 * multiple cookies may be parsed on the same line.
8383 */
8384static int
8385acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8386 const char *hdr_name, int hdr_name_len, int multi,
8387 struct acl_expr *expr, struct acl_test *test)
8388{
8389 struct http_txn *txn = l7;
8390 struct hdr_idx *idx = &txn->hdr_idx;
8391 struct hdr_ctx ctx;
8392 int cnt;
8393 char *val_beg, *val_end;
8394
8395 if (!txn)
8396 return 0;
8397
8398 val_beg = NULL;
8399 ctx.idx = 0;
8400 cnt = 0;
8401
8402 while (1) {
8403 /* Note: val_beg == NULL every time we need to fetch a new header */
8404 if (!val_beg) {
8405 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8406 break;
8407
8408 if (ctx.vlen < expr->arg_len + 1)
8409 continue;
8410
8411 val_beg = ctx.line + ctx.val;
8412 val_end = val_beg + ctx.vlen;
8413 }
8414
8415 while ((val_beg = extract_cookie_value(val_beg, val_end,
8416 expr->arg.str, expr->arg_len, multi,
8417 &temp_pattern.data.str.str,
8418 &temp_pattern.data.str.len))) {
8419 cnt++;
8420 }
8421 }
8422
8423 temp_pattern.data.integer = cnt;
8424 test->flags |= ACL_TEST_F_VOL_HDR;
8425 return 1;
8426}
8427
8428static int
8429acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8430 struct acl_expr *expr, struct acl_test *test)
8431{
8432 struct http_txn *txn = l7;
8433
8434 if (!txn)
8435 return 0;
8436
8437 if (txn->req.msg_state < HTTP_MSG_BODY)
8438 return 0;
8439
8440 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8441 /* ensure the indexes are not affected */
8442 return 0;
8443
8444 /* The Cookie header allows multiple cookies on the same line */
8445 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8446}
8447
8448static int
8449acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8450 struct acl_expr *expr, struct acl_test *test)
8451{
8452 struct http_txn *txn = l7;
8453
8454 if (!txn)
8455 return 0;
8456
8457 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8458 return 0;
8459
8460 /* The Set-Cookie header allows only one cookie on the same line */
8461 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8462}
8463
Willy Tarreau8797c062007-05-07 00:55:35 +02008464/************************************************************************/
8465/* All supported keywords must be declared here. */
8466/************************************************************************/
8467
8468/* Note: must not be declared <const> as its list will be overwritten */
8469static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008470 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8471
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008472 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008473 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8474 { "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 +02008475 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008476
Willy Tarreauc4262962010-05-10 23:42:40 +02008477 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008478 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8479 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8480 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8481 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8482 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8483 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008484 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008485 { "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 +02008486 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008487
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008488 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008489 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008490 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8491 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8492 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8493 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8494 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8495 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8496 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008497 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008498 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008499 { "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 +02008500
Willy Tarreauc4262962010-05-10 23:42:40 +02008501 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008502 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8503 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8504 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8505 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8506 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8507 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8508 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008509 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008510 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008511 { "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 +02008512
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008513 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8514 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8515 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8516 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8517 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8518 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8519 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8520 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8521 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8522
8523 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8524 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8525 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8526 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8527 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8528 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8529 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8530 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8531 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8532
Willy Tarreauc4262962010-05-10 23:42:40 +02008533 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008534 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8535 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8536 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8537 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8538 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8539 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008540 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008541
Willy Tarreau7f18e522010-10-22 20:04:13 +02008542 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8543 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8544 { "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 +02008545 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008546}};
8547
Willy Tarreau4a568972010-05-12 08:08:50 +02008548/************************************************************************/
8549/* The code below is dedicated to pattern fetching and matching */
8550/************************************************************************/
8551
Willy Tarreaue428fb72011-12-16 21:50:30 +01008552/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008553static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008554pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8555 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008556{
8557 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008558
Willy Tarreaue428fb72011-12-16 21:50:30 +01008559 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8560 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008561}
8562
David Cournapeau16023ee2010-12-23 20:55:41 +09008563/*
8564 * Given a path string and its length, find the position of beginning of the
8565 * query string. Returns NULL if no query string is found in the path.
8566 *
8567 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8568 *
8569 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8570 */
8571static inline char *find_query_string(char *path, size_t path_l)
8572{
8573 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008574
David Cournapeau16023ee2010-12-23 20:55:41 +09008575 p = memchr(path, '?', path_l);
8576 return p ? p + 1 : NULL;
8577}
8578
8579static inline int is_param_delimiter(char c)
8580{
8581 return c == '&' || c == ';';
8582}
8583
8584/*
8585 * Given a url parameter, find the starting position of the first occurence,
8586 * or NULL if the parameter is not found.
8587 *
8588 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8589 * the function will return query_string+8.
8590 */
8591static char*
8592find_url_param_pos(char* query_string, size_t query_string_l,
8593 char* url_param_name, size_t url_param_name_l)
8594{
8595 char *pos, *last;
8596
8597 pos = query_string;
8598 last = query_string + query_string_l - url_param_name_l - 1;
8599
8600 while (pos <= last) {
8601 if (pos[url_param_name_l] == '=') {
8602 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8603 return pos;
8604 pos += url_param_name_l + 1;
8605 }
8606 while (pos <= last && !is_param_delimiter(*pos))
8607 pos++;
8608 pos++;
8609 }
8610 return NULL;
8611}
8612
8613/*
8614 * Given a url parameter name, returns its value and size into *value and
8615 * *value_l respectively, and returns non-zero. If the parameter is not found,
8616 * zero is returned and value/value_l are not touched.
8617 */
8618static int
8619find_url_param_value(char* path, size_t path_l,
8620 char* url_param_name, size_t url_param_name_l,
8621 char** value, size_t* value_l)
8622{
8623 char *query_string, *qs_end;
8624 char *arg_start;
8625 char *value_start, *value_end;
8626
8627 query_string = find_query_string(path, path_l);
8628 if (!query_string)
8629 return 0;
8630
8631 qs_end = path + path_l;
8632 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8633 url_param_name, url_param_name_l);
8634 if (!arg_start)
8635 return 0;
8636
8637 value_start = arg_start + url_param_name_l + 1;
8638 value_end = value_start;
8639
8640 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8641 value_end++;
8642
8643 *value = value_start;
8644 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008645 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008646}
8647
8648static int
8649pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8650 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8651{
8652 struct http_txn *txn = l7;
8653 struct http_msg *msg = &txn->req;
8654 char *url_param_value;
8655 size_t url_param_value_l;
8656
8657 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8658 arg_p->data.str.str, arg_p->data.str.len,
8659 &url_param_value, &url_param_value_l))
8660 return 0;
8661
8662 data->str.str = url_param_value;
8663 data->str.len = url_param_value_l;
8664 return 1;
8665}
8666
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008667/* Try to find in request or response message is in <msg> and whose transaction
8668 * is in <txn> the last occurrence of a cookie name in all cookie header values
8669 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8670 * pointer and size of the last occurrence of the cookie value is returned into
8671 * <value> and <value_l>, and the function returns non-zero if the value was
8672 * found. Otherwise if the cookie was not found, zero is returned and neither
8673 * value nor value_l are touched. The input hdr string should begin at the
8674 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8675 * value may represent a list of values (cookie headers).
8676 */
8677
8678static int
8679find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8680 const char *hdr_name, int hdr_name_len,
8681 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008682 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008683{
8684 struct hdr_ctx ctx;
8685 int found = 0;
8686
8687 ctx.idx = 0;
8688 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008689 char *hdr, *end;
8690
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008691 if (ctx.vlen < cookie_name_l + 1)
8692 continue;
8693
Willy Tarreau4573af92012-04-06 18:20:06 +02008694 hdr = ctx.line + ctx.val;
8695 end = hdr + ctx.vlen;
8696 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8697 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008698 }
8699 return found;
8700}
8701
8702static int
8703pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8704 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8705{
8706 struct http_txn *txn = l7;
8707 struct http_msg *msg = &txn->req;
8708 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008709 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008710 int found = 0;
8711
8712 found = find_cookie_value(msg, txn, "Cookie", 6,
8713 arg_p->data.str.str, arg_p->data.str.len, 1,
8714 &cookie_value, &cookie_value_l);
8715 if (found) {
8716 data->str.str = cookie_value;
8717 data->str.len = cookie_value_l;
8718 }
8719
8720 return found;
8721}
8722
8723
8724static int
8725pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8726 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8727{
8728 struct http_txn *txn = l7;
8729 struct http_msg *msg = &txn->rsp;
8730 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008731 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008732 int found = 0;
8733
8734 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8735 arg_p->data.str.str, arg_p->data.str.len, 1,
8736 &cookie_value, &cookie_value_l);
8737 if (found) {
8738 data->str.str = cookie_value;
8739 data->str.len = cookie_value_l;
8740 }
8741
8742 return found;
8743}
8744
Emeric Brun485479d2010-09-23 18:02:19 +02008745
Willy Tarreau4a568972010-05-12 08:08:50 +02008746/************************************************************************/
8747/* All supported keywords must be declared here. */
8748/************************************************************************/
8749/* Note: must not be declared <const> as its list will be overwritten */
8750static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008751 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008752 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008753 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8754 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008755 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008756}};
8757
Willy Tarreau8797c062007-05-07 00:55:35 +02008758
8759__attribute__((constructor))
8760static void __http_protocol_init(void)
8761{
8762 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008763 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008764}
8765
8766
Willy Tarreau58f10d72006-12-04 02:26:12 +01008767/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008768 * Local variables:
8769 * c-indent-level: 8
8770 * c-basic-offset: 8
8771 * End:
8772 */