blob: a1a1b8a3923dadc9aa0c7f2c7790a58079271eba [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);
Willy Tarreau80587432006-12-24 17:47:20 +0100271}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272
Willy Tarreau53b6c742006-12-17 13:37:46 +0100273/*
274 * We have 26 list of methods (1 per first letter), each of which can have
275 * up to 3 entries (2 valid, 1 null).
276 */
277struct http_method_desc {
278 http_meth_t meth;
279 int len;
280 const char text[8];
281};
282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100283const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100284 ['C' - 'A'] = {
285 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
286 },
287 ['D' - 'A'] = {
288 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
289 },
290 ['G' - 'A'] = {
291 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
292 },
293 ['H' - 'A'] = {
294 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
295 },
296 ['P' - 'A'] = {
297 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
298 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
299 },
300 ['T' - 'A'] = {
301 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
302 },
303 /* rest is empty like this :
304 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
305 */
306};
307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100308/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310 * RFC2616 for those chars.
311 */
312
313const char http_is_spht[256] = {
314 [' '] = 1, ['\t'] = 1,
315};
316
317const char http_is_crlf[256] = {
318 ['\r'] = 1, ['\n'] = 1,
319};
320
321const char http_is_lws[256] = {
322 [' '] = 1, ['\t'] = 1,
323 ['\r'] = 1, ['\n'] = 1,
324};
325
326const char http_is_sep[256] = {
327 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
328 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
329 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
330 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
331 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
332};
333
334const char http_is_ctl[256] = {
335 [0 ... 31] = 1,
336 [127] = 1,
337};
338
339/*
340 * A token is any ASCII char that is neither a separator nor a CTL char.
341 * Do not overwrite values in assignment since gcc-2.95 will not handle
342 * them correctly. Instead, define every non-CTL char's status.
343 */
344const char http_is_token[256] = {
345 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
346 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
347 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
348 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
349 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
350 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
351 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
352 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
353 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
354 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
355 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
356 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
357 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
358 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
359 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
360 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
361 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
362 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
363 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
364 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
365 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
366 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
367 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
368 ['|'] = 1, ['}'] = 0, ['~'] = 1,
369};
370
371
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100372/*
373 * An http ver_token is any ASCII which can be found in an HTTP version,
374 * which includes 'H', 'T', 'P', '/', '.' and any digit.
375 */
376const char http_is_ver_token[256] = {
377 ['.'] = 1, ['/'] = 1,
378 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
379 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
380 ['H'] = 1, ['P'] = 1, ['T'] = 1,
381};
382
383
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100384/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100385 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
386 */
387#if defined(DEBUG_FSM)
388static void http_silent_debug(int line, struct session *s)
389{
390 int size = 0;
391 size += snprintf(trash + size, sizeof(trash) - size,
392 "[%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",
393 line,
394 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
395 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);
396 write(-1, trash, size);
397 size = 0;
398 size += snprintf(trash + size, sizeof(trash) - size,
399 " %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",
400 line,
401 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
402 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);
403
404 write(-1, trash, size);
405}
406#else
407#define http_silent_debug(l,s) do { } while (0)
408#endif
409
410/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100411 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
412 * CRLF. Text length is measured first, so it cannot be NULL.
413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
417int http_header_add_tail(struct buffer *b, struct http_msg *msg,
418 struct hdr_idx *hdr_idx, const char *text)
419{
420 int bytes, len;
421
422 len = strlen(text);
423 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
424 if (!bytes)
425 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100426 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100427 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
428}
429
430/*
431 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
432 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
433 * the buffer is only opened and the space reserved, but nothing is copied.
434 * The header is also automatically added to the index <hdr_idx>, and the end
435 * of headers is automatically adjusted. The number of bytes added is returned
436 * on success, otherwise <0 is returned indicating an error.
437 */
438int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
439 struct hdr_idx *hdr_idx, const char *text, int len)
440{
441 int bytes;
442
443 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
444 if (!bytes)
445 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100446 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100447 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
448}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
450/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100451 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
452 * If so, returns the position of the first non-space character relative to
453 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
454 * to return a pointer to the place after the first space. Returns 0 if the
455 * header name does not match. Checks are case-insensitive.
456 */
457int http_header_match2(const char *hdr, const char *end,
458 const char *name, int len)
459{
460 const char *val;
461
462 if (hdr + len >= end)
463 return 0;
464 if (hdr[len] != ':')
465 return 0;
466 if (strncasecmp(hdr, name, len) != 0)
467 return 0;
468 val = hdr + len + 1;
469 while (val < end && HTTP_IS_SPHT(*val))
470 val++;
471 if ((val >= end) && (len + 2 <= end - hdr))
472 return len + 2; /* we may replace starting from second space */
473 return val - hdr;
474}
475
Willy Tarreau68085d82010-01-18 14:54:04 +0100476/* Find the end of the header value contained between <s> and <e>. See RFC2616,
477 * par 2.2 for more information. Note that it requires a valid header to return
478 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100480char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481{
482 int quoted, qdpair;
483
484 quoted = qdpair = 0;
485 for (; s < e; s++) {
486 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200487 else if (quoted) {
488 if (*s == '\\') qdpair = 1;
489 else if (*s == '"') quoted = 0;
490 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 else if (*s == '"') quoted = 1;
492 else if (*s == ',') return s;
493 }
494 return s;
495}
496
497/* Find the first or next occurrence of header <name> in message buffer <sol>
498 * using headers index <idx>, and return it in the <ctx> structure. This
499 * structure holds everything necessary to use the header and find next
500 * occurrence. If its <idx> member is 0, the header is searched from the
501 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 * 1 when it finds a value, and 0 when there is no more. It is designed to work
503 * with headers defined as comma-separated lists. As a special case, if ctx->val
504 * is NULL when searching for a new values of a header, the current header is
505 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 */
507int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 struct hdr_ctx *ctx)
510{
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 char *eol, *sov;
512 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 cur_idx = ctx->idx;
515 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200516 /* We have previously returned a value, let's search
517 * another one on the same line.
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200520 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100521 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 eol = sol + idx->v[cur_idx].len;
523
524 if (sov >= eol)
525 /* no more values in this header */
526 goto next_hdr;
527
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 /* values remaining for this header, skip the comma but save it
529 * for later use (eg: for header deletion).
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sov++;
532 while (sov < eol && http_is_lws[(unsigned char)*sov])
533 sov++;
534
535 goto return_hdr;
536 }
537
538 /* first request for this header */
539 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 while (cur_idx) {
543 eol = sol + idx->v[cur_idx].len;
544
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200545 if (len == 0) {
546 /* No argument was passed, we want any header.
547 * To achieve this, we simply build a fake request. */
548 while (sol + len < eol && sol[len] != ':')
549 len++;
550 name = sol;
551 }
552
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 if ((len < eol - sol) &&
554 (sol[len] == ':') &&
555 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100556 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 sov = sol + len + 1;
558 while (sov < eol && http_is_lws[(unsigned char)*sov])
559 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100560
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 ctx->prev = old_idx;
563 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 ctx->idx = cur_idx;
565 ctx->val = sov - sol;
566
567 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200568 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200569 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 eol--;
571 ctx->tws++;
572 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->vlen = eol - sov;
574 return 1;
575 }
576 next_hdr:
577 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100578 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 cur_idx = idx->v[cur_idx].next;
580 }
581 return 0;
582}
583
584int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100585 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200586 struct hdr_ctx *ctx)
587{
588 return http_find_header2(name, strlen(name), sol, idx, ctx);
589}
590
Willy Tarreau68085d82010-01-18 14:54:04 +0100591/* Remove one value of a header. This only works on a <ctx> returned by one of
592 * the http_find_header functions. The value is removed, as well as surrounding
593 * commas if any. If the removed value was alone, the whole header is removed.
594 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
595 * message <msg>. The new index is returned. If it is zero, it means there is
596 * no more header, so any processing may stop. The ctx is always left in a form
597 * that can be handled by http_find_header2() to find next occurrence.
598 */
599int http_remove_header2(struct http_msg *msg, struct buffer *buf,
600 struct hdr_idx *idx, struct hdr_ctx *ctx)
601{
602 int cur_idx = ctx->idx;
603 char *sol = ctx->line;
604 struct hdr_idx_elem *hdr;
605 int delta, skip_comma;
606
607 if (!cur_idx)
608 return 0;
609
610 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 /* This was the only value of the header, we must now remove it entirely. */
613 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
614 http_msg_move_end(msg, delta);
615 idx->used--;
616 hdr->len = 0; /* unused entry */
617 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100618 if (idx->tail == ctx->idx)
619 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 ctx->idx = ctx->prev; /* walk back to the end of previous header */
621 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
622 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200623 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 return ctx->idx;
625 }
626
627 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200628 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
629 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 */
631
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 NULL, 0);
636 hdr->len += delta;
637 http_msg_move_end(msg, delta);
638 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200639 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 return ctx->idx;
641}
642
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100643/* This function handles a server error at the stream interface level. The
644 * stream interface is assumed to be already in a closed state. An optional
645 * message is copied into the input buffer, and an HTTP status code stored.
646 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100647 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100649static void http_server_error(struct session *t, struct stream_interface *si,
650 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100652 buffer_auto_read(si->ob);
653 buffer_abort(si->ob);
654 buffer_auto_close(si->ob);
655 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200656 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100657 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100658 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100659 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100660 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661 }
662 if (!(t->flags & SN_ERR_MASK))
663 t->flags |= err;
664 if (!(t->flags & SN_FINST_MASK))
665 t->flags |= finst;
666}
667
Willy Tarreau80587432006-12-24 17:47:20 +0100668/* This function returns the appropriate error location for the given session
669 * and message.
670 */
671
672struct chunk *error_message(struct session *s, int msgnum)
673{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200674 if (s->be->errmsg[msgnum].str)
675 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100676 else if (s->fe->errmsg[msgnum].str)
677 return &s->fe->errmsg[msgnum];
678 else
679 return &http_err_chunks[msgnum];
680}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682/*
683 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
684 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
685 */
686static http_meth_t find_http_meth(const char *str, const int len)
687{
688 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100689 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100690
691 m = ((unsigned)*str - 'A');
692
693 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100694 for (h = http_methods[m]; h->len > 0; h++) {
695 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 };
700 return HTTP_METH_OTHER;
701 }
702 return HTTP_METH_NONE;
703
704}
705
Willy Tarreau21d2af32008-02-14 20:25:24 +0100706/* Parse the URI from the given transaction (which is assumed to be in request
707 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
708 * It is returned otherwise.
709 */
710static char *
711http_get_path(struct http_txn *txn)
712{
713 char *ptr, *end;
714
Willy Tarreau962c3f42010-01-10 00:15:35 +0100715 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100716 end = ptr + txn->req.sl.rq.u_l;
717
718 if (ptr >= end)
719 return NULL;
720
721 /* RFC2616, par. 5.1.2 :
722 * Request-URI = "*" | absuri | abspath | authority
723 */
724
725 if (*ptr == '*')
726 return NULL;
727
728 if (isalpha((unsigned char)*ptr)) {
729 /* this is a scheme as described by RFC3986, par. 3.1 */
730 ptr++;
731 while (ptr < end &&
732 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
733 ptr++;
734 /* skip '://' */
735 if (ptr == end || *ptr++ != ':')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 if (ptr == end || *ptr++ != '/')
740 return NULL;
741 }
742 /* skip [user[:passwd]@]host[:[port]] */
743
744 while (ptr < end && *ptr != '/')
745 ptr++;
746
747 if (ptr == end)
748 return NULL;
749
750 /* OK, we got the '/' ! */
751 return ptr;
752}
753
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754/* Returns a 302 for a redirectable request. This may only be called just after
755 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
756 * left unchanged and will follow normal proxy processing.
757 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
761 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 char *path;
764 int len;
765
766 /* 1: create the response header */
767 rdr.len = strlen(HTTP_302);
768 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100769 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 memcpy(rdr.str, HTTP_302, rdr.len);
771
Willy Tarreau827aee92011-03-10 16:55:02 +0100772 srv = target_srv(&s->target);
773
Willy Tarreauefb453c2008-10-26 20:49:47 +0100774 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 return;
777
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100778 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
780 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
781 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100782 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783
784 /* 3: add the request URI */
785 txn = &s->txn;
786 path = http_get_path(txn);
787 if (!path)
788 return;
789
Willy Tarreau962c3f42010-01-10 00:15:35 +0100790 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200791 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 return;
793
794 memcpy(rdr.str + rdr.len, path, len);
795 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100796
797 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
798 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
799 rdr.len += 29;
800 } else {
801 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
802 rdr.len += 23;
803 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804
805 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100806 si->shutr(si);
807 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 si->err_type = SI_ET_NONE;
809 si->err_loc = NULL;
810 si->state = SI_ST_CLO;
811
812 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100813 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814
815 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (srv)
817 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818}
819
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100820/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821 * that the server side is closed. Note that err_type is actually a
822 * bitmask, where almost only aborts may be cumulated with other
823 * values. We consider that aborted operations are more important
824 * than timeouts or errors due to the fact that nobody else in the
825 * logs might explain incomplete retries. All others should avoid
826 * being cumulated. It should normally not be possible to have multiple
827 * aborts at once, but just in case, the first one in sequence is reported.
828 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100831 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832
833 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100834 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
835 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
838 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
841 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
844 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
847 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
850 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
853 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854}
855
Willy Tarreau42250582007-04-01 01:30:43 +0200856extern const char sess_term_cond[8];
857extern const char sess_fin_state[8];
858extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200859struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200860struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862/*
863 * Capture headers from message starting at <som> according to header list
864 * <cap_hdr>, and fill the <idx> structure appropriately.
865 */
866void capture_headers(char *som, struct hdr_idx *idx,
867 char **cap, struct cap_hdr *cap_hdr)
868{
869 char *eol, *sol, *col, *sov;
870 int cur_idx;
871 struct cap_hdr *h;
872 int len;
873
874 sol = som + hdr_idx_first_pos(idx);
875 cur_idx = hdr_idx_first_idx(idx);
876
877 while (cur_idx) {
878 eol = sol + idx->v[cur_idx].len;
879
880 col = sol;
881 while (col < eol && *col != ':')
882 col++;
883
884 sov = col + 1;
885 while (sov < eol && http_is_lws[(unsigned char)*sov])
886 sov++;
887
888 for (h = cap_hdr; h; h = h->next) {
889 if ((h->namelen == col - sol) &&
890 (strncasecmp(sol, h->name, h->namelen) == 0)) {
891 if (cap[h->index] == NULL)
892 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200893 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100894
895 if (cap[h->index] == NULL) {
896 Alert("HTTP capture : out of memory.\n");
897 continue;
898 }
899
900 len = eol - sov;
901 if (len > h->len)
902 len = h->len;
903
904 memcpy(cap[h->index], sov, len);
905 cap[h->index][len]=0;
906 }
907 }
908 sol = eol + idx->v[cur_idx].cr + 1;
909 cur_idx = idx->v[cur_idx].next;
910 }
911}
912
913
Willy Tarreau42250582007-04-01 01:30:43 +0200914/* either we find an LF at <ptr> or we jump to <bad>.
915 */
916#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
917
918/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
919 * otherwise to <http_msg_ood> with <state> set to <st>.
920 */
921#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
922 ptr++; \
923 if (likely(ptr < end)) \
924 goto good; \
925 else { \
926 state = (st); \
927 goto http_msg_ood; \
928 } \
929 } while (0)
930
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100933 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100934 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
935 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
936 * will give undefined results.
937 * Note that it is upon the caller's responsibility to ensure that ptr < end,
938 * and that msg->sol points to the beginning of the response.
939 * If a complete line is found (which implies that at least one CR or LF is
940 * found before <end>, the updated <ptr> is returned, otherwise NULL is
941 * returned indicating an incomplete line (which does not mean that parts have
942 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
943 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
944 * upon next call.
945 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
948 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100950 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100951const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
952 unsigned int state, const char *ptr, const char *end,
953 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100954{
Willy Tarreau8973c702007-01-21 23:58:29 +0100955 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200957 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100958 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
960
961 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100962 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
964 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100965 state = HTTP_MSG_ERROR;
966 break;
967
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100971 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 goto http_msg_rpcode;
973 }
974 if (likely(HTTP_IS_SPHT(*ptr)))
975 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
976 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100977 state = HTTP_MSG_ERROR;
978 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100979
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200981 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 if (likely(!HTTP_IS_LWS(*ptr)))
983 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
984
985 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100986 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
988 }
989
990 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100991 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 http_msg_rsp_reason:
993 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100994 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 msg->sl.st.r_l = 0;
996 goto http_msg_rpline_eol;
997
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200999 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001001 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 goto http_msg_rpreason;
1003 }
1004 if (likely(HTTP_IS_SPHT(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1006 /* so it's a CR/LF, so there is no reason phrase */
1007 goto http_msg_rsp_reason;
1008
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001010 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 if (likely(!HTTP_IS_CRLF(*ptr)))
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001013 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 http_msg_rpline_eol:
1015 /* We have seen the end of line. Note that we do not
1016 * necessarily have the \n yet, but at least we know that we
1017 * have EITHER \r OR \n, otherwise the response would not be
1018 * complete. We can then record the response length and return
1019 * to the caller which will be able to register it.
1020 */
1021 msg->sl.st.l = ptr - msg->sol;
1022 return ptr;
1023
1024#ifdef DEBUG_FULL
1025 default:
1026 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1027 exit(1);
1028#endif
1029 }
1030
1031 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001032 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 if (ret_state)
1034 *ret_state = state;
1035 if (ret_ptr)
1036 *ret_ptr = (char *)ptr;
1037 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001038}
1039
Willy Tarreau8973c702007-01-21 23:58:29 +01001040/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001041 * This function parses a request line between <ptr> and <end>, starting with
1042 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1043 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1044 * will give undefined results.
1045 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1046 * and that msg->sol points to the beginning of the request.
1047 * If a complete line is found (which implies that at least one CR or LF is
1048 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1049 * returned indicating an incomplete line (which does not mean that parts have
1050 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1051 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1052 * upon next call.
1053 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001054 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001055 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1056 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001059const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1060 unsigned int state, const char *ptr, const char *end,
1061 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001064 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001065 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 if (likely(HTTP_IS_TOKEN(*ptr)))
1067 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001070 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (likely(HTTP_IS_CRLF(*ptr))) {
1075 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001076 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001078 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001080 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001082 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 msg->sl.rq.v_l = 0;
1084 goto http_msg_rqline_eol;
1085 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001086 state = HTTP_MSG_ERROR;
1087 break;
1088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001090 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001092 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 goto http_msg_rquri;
1094 }
1095 if (likely(HTTP_IS_SPHT(*ptr)))
1096 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1097 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1098 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001101 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001102 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001106 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001110 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001111 /* non-ASCII chars are forbidden unless option
1112 * accept-invalid-http-request is enabled in the frontend.
1113 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001114 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001115 if (msg->err_pos < -1)
1116 goto invalid_char;
1117 if (msg->err_pos == -1)
1118 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1120 }
1121
1122 if (likely(HTTP_IS_CRLF(*ptr))) {
1123 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1124 goto http_msg_req09_uri_e;
1125 }
1126
1127 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001128 invalid_char:
1129 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001130 state = HTTP_MSG_ERROR;
1131 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001134 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001135 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001136 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 goto http_msg_rqver;
1138 }
1139 if (likely(HTTP_IS_SPHT(*ptr)))
1140 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1141 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1142 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001145 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001146 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001148
1149 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001150 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001151 http_msg_rqline_eol:
1152 /* We have seen the end of line. Note that we do not
1153 * necessarily have the \n yet, but at least we know that we
1154 * have EITHER \r OR \n, otherwise the request would not be
1155 * complete. We can then record the request length and return
1156 * to the caller which will be able to register it.
1157 */
1158 msg->sl.rq.l = ptr - msg->sol;
1159 return ptr;
1160 }
1161
1162 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001163 state = HTTP_MSG_ERROR;
1164 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166#ifdef DEBUG_FULL
1167 default:
1168 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1169 exit(1);
1170#endif
1171 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001172
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001174 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 if (ret_state)
1176 *ret_state = state;
1177 if (ret_ptr)
1178 *ret_ptr = (char *)ptr;
1179 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001182/*
1183 * Returns the data from Authorization header. Function may be called more
1184 * than once so data is stored in txn->auth_data. When no header is found
1185 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1186 * searching again for something we are unable to find anyway.
1187 */
1188
1189char get_http_auth_buff[BUFSIZE];
1190
1191int
1192get_http_auth(struct session *s)
1193{
1194
1195 struct http_txn *txn = &s->txn;
1196 struct chunk auth_method;
1197 struct hdr_ctx ctx;
1198 char *h, *p;
1199 int len;
1200
1201#ifdef DEBUG_AUTH
1202 printf("Auth for session %p: %d\n", s, txn->auth.method);
1203#endif
1204
1205 if (txn->auth.method == HTTP_AUTH_WRONG)
1206 return 0;
1207
1208 if (txn->auth.method)
1209 return 1;
1210
1211 txn->auth.method = HTTP_AUTH_WRONG;
1212
1213 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001214
1215 if (txn->flags & TX_USE_PX_CONN) {
1216 h = "Proxy-Authorization";
1217 len = strlen(h);
1218 } else {
1219 h = "Authorization";
1220 len = strlen(h);
1221 }
1222
1223 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001224 return 0;
1225
1226 h = ctx.line + ctx.val;
1227
1228 p = memchr(h, ' ', ctx.vlen);
1229 if (!p || p == h)
1230 return 0;
1231
1232 chunk_initlen(&auth_method, h, 0, p-h);
1233 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1234
1235 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1236
1237 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1238 get_http_auth_buff, BUFSIZE - 1);
1239
1240 if (len < 0)
1241 return 0;
1242
1243
1244 get_http_auth_buff[len] = '\0';
1245
1246 p = strchr(get_http_auth_buff, ':');
1247
1248 if (!p)
1249 return 0;
1250
1251 txn->auth.user = get_http_auth_buff;
1252 *p = '\0';
1253 txn->auth.pass = p+1;
1254
1255 txn->auth.method = HTTP_AUTH_BASIC;
1256 return 1;
1257 }
1258
1259 return 0;
1260}
1261
Willy Tarreau58f10d72006-12-04 02:26:12 +01001262
Willy Tarreau8973c702007-01-21 23:58:29 +01001263/*
1264 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001265 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001266 * when data are missing and recalled at the exact same location with no
1267 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001268 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001269 * fields. Note that msg->som and msg->sol will be initialized after completing
1270 * the first state, so that none of the msg pointers has to be initialized
1271 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1274{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001275 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001278 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 ptr = buf->lr;
1280 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001282 if (unlikely(ptr >= end))
1283 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001286 /*
1287 * First, states that are specific to the response only.
1288 * We check them first so that request and headers are
1289 * closer to each other (accessed more often).
1290 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001292 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001294 /* we have a start of message, but we have to check
1295 * first if we need to remove some CRLF. We can only
1296 * do this when send_max=0.
1297 */
1298 char *beg = buf->w + buf->send_max;
1299 if (beg >= buf->data + buf->size)
1300 beg -= buf->size;
1301 if (unlikely(ptr != beg)) {
1302 if (buf->send_max)
1303 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001304 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001308 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 hdr_idx_init(idx);
1310 state = HTTP_MSG_RPVER;
1311 goto http_msg_rpver;
1312 }
1313
1314 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1315 goto http_msg_invalid;
1316
1317 if (unlikely(*ptr == '\n'))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1320 /* stop here */
1321
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001323 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 EXPECT_LF_HERE(ptr, http_msg_invalid);
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1326 /* stop here */
1327
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 case HTTP_MSG_RPVER_SP:
1331 case HTTP_MSG_RPCODE:
1332 case HTTP_MSG_RPCODE_SP:
1333 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001334 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001335 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 if (unlikely(!ptr))
1337 return;
1338
1339 /* we have a full response and we know that we have either a CR
1340 * or an LF at <ptr>.
1341 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001342 //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 +01001343 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1344
1345 msg->sol = ptr;
1346 if (likely(*ptr == '\r'))
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1348 goto http_msg_rpline_end;
1349
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001351 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 /* msg->sol must point to the first of CR or LF. */
1353 EXPECT_LF_HERE(ptr, http_msg_invalid);
1354 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1355 /* stop here */
1356
1357 /*
1358 * Second, states that are specific to the request only
1359 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001361 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001363 /* we have a start of message, but we have to check
1364 * first if we need to remove some CRLF. We can only
1365 * do this when send_max=0.
1366 */
1367 char *beg = buf->w + buf->send_max;
1368 if (beg >= buf->data + buf->size)
1369 beg -= buf->size;
1370 if (likely(ptr != beg)) {
1371 if (buf->send_max)
1372 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001373 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001377 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001378 /* we will need this when keep-alive will be supported
1379 hdr_idx_init(idx);
1380 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 state = HTTP_MSG_RQMETH;
1382 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1386 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(*ptr == '\n'))
1389 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 EXPECT_LF_HERE(ptr, http_msg_invalid);
1396 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001400 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH_SP:
1402 case HTTP_MSG_RQURI:
1403 case HTTP_MSG_RQURI_SP:
1404 case HTTP_MSG_RQVER:
1405 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001406 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (unlikely(!ptr))
1408 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 /* we have a full request and we know that we have either a CR
1411 * or an LF at <ptr>.
1412 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001413 //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 +01001414 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 msg->sol = ptr;
1417 if (likely(*ptr == '\r'))
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 /* check for HTTP/0.9 request : no version information available.
1424 * msg->sol must point to the first of CR or LF.
1425 */
1426 if (unlikely(msg->sl.rq.v_l == 0))
1427 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 EXPECT_LF_HERE(ptr, http_msg_invalid);
1430 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * Common states below
1435 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001437 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 msg->sol = ptr;
1439 if (likely(!HTTP_IS_CRLF(*ptr))) {
1440 goto http_msg_hdr_name;
1441 }
1442
1443 if (likely(*ptr == '\r'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1445 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001448 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 /* assumes msg->sol points to the first char */
1450 if (likely(HTTP_IS_TOKEN(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 if (likely(*ptr == ':')) {
1454 msg->col = ptr - buf->data;
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1456 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001458 if (likely(msg->err_pos < -1) || *ptr == '\n')
1459 goto http_msg_invalid;
1460
1461 if (msg->err_pos == -1) /* capture error pointer */
1462 msg->err_pos = ptr - buf->data; /* >= 0 now */
1463
1464 /* and we still accept this non-token character */
1465 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001468 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 /* assumes msg->sol points to the first char and msg->col to the colon */
1470 if (likely(HTTP_IS_SPHT(*ptr)))
1471 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 /* header value can be basically anything except CR/LF */
1474 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (likely(!HTTP_IS_CRLF(*ptr))) {
1477 goto http_msg_hdr_val;
1478 }
1479
1480 if (likely(*ptr == '\r'))
1481 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1482 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 EXPECT_LF_HERE(ptr, http_msg_invalid);
1487 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001490 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 if (likely(HTTP_IS_SPHT(*ptr))) {
1492 /* replace HT,CR,LF with spaces */
1493 for (; buf->data+msg->sov < ptr; msg->sov++)
1494 buf->data[msg->sov] = ' ';
1495 goto http_msg_hdr_l1_sp;
1496 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001497 /* we had a header consisting only in spaces ! */
1498 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 goto http_msg_complete_header;
1500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001502 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 /* assumes msg->sol points to the first char, msg->col to the
1504 * colon, and msg->sov points to the first character of the
1505 * value.
1506 */
1507 if (likely(!HTTP_IS_CRLF(*ptr)))
1508 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 msg->eol = ptr;
1511 /* Note: we could also copy eol into ->eoh so that we have the
1512 * real header end in case it ends with lots of LWS, but is this
1513 * really needed ?
1514 */
1515 if (likely(*ptr == '\r'))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1517 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001520 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 EXPECT_LF_HERE(ptr, http_msg_invalid);
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1527 /* LWS: replace HT,CR,LF with spaces */
1528 for (; msg->eol < ptr; msg->eol++)
1529 *msg->eol = ' ';
1530 goto http_msg_hdr_val;
1531 }
1532 http_msg_complete_header:
1533 /*
1534 * It was a new header, so the last one is finished.
1535 * Assumes msg->sol points to the first char, msg->col to the
1536 * colon, msg->sov points to the first character of the value
1537 * and msg->eol to the first CR or LF so we know how the line
1538 * ends. We insert last header into the index.
1539 */
1540 /*
1541 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1542 write(2, msg->sol, msg->eol-msg->sol);
1543 fprintf(stderr,"\n");
1544 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1547 idx, idx->tail) < 0))
1548 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 msg->sol = ptr;
1551 if (likely(!HTTP_IS_CRLF(*ptr))) {
1552 goto http_msg_hdr_name;
1553 }
1554
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1557 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 /* Assumes msg->sol points to the first of either CR or LF */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 ptr++;
1564 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001565 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001567 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001568 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 return;
1570#ifdef DEBUG_FULL
1571 default:
1572 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1573 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001574#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 }
1576 http_msg_ood:
1577 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001578 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 buf->lr = ptr;
1580 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 http_msg_invalid:
1583 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001584 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001585 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 return;
1587}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001588
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1590 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1591 * nothing is done and 1 is returned.
1592 */
1593static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1594{
1595 int delta;
1596 char *cur_end;
1597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601 cur_end = msg->sol + msg->sl.rq.l;
1602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
1606 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
1611 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
1614 cur_end = (char *)http_parse_reqline(msg, req->data,
1615 HTTP_MSG_RQMETH,
1616 msg->sol, cur_end + 1,
1617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
1629 * and "keep-alive" values. If a buffer is provided and we already know that
1630 * some headers may safely be removed, we remove them now. The <to_del> flags
1631 * are used for that :
1632 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1633 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1634 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1635 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1636 * harmless combinations may be removed. Do not call that after changes have
1637 * been processed. If unused, the buffer can be NULL, and no data will be
1638 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001639 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001640void 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 +01001641{
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001643 const char *hdr_val = "Connection";
1644 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001645
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001647 return;
1648
Willy Tarreau88d349d2010-01-25 12:15:43 +01001649 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1650 hdr_val = "Proxy-Connection";
1651 hdr_len = 16;
1652 }
1653
Willy Tarreau5b154472009-12-21 20:11:07 +01001654 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001656 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1658 txn->flags |= TX_HDR_CONN_KAL;
1659 if ((to_del & 2) && buf)
1660 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1661 else
1662 txn->flags |= TX_CON_KAL_SET;
1663 }
1664 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1665 txn->flags |= TX_HDR_CONN_CLO;
1666 if ((to_del & 1) && buf)
1667 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1668 else
1669 txn->flags |= TX_CON_CLO_SET;
1670 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001671 }
1672
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001673 txn->flags |= TX_HDR_CONN_PRS;
1674 return;
1675}
Willy Tarreau5b154472009-12-21 20:11:07 +01001676
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001677/* Apply desired changes on the Connection: header. Values may be removed and/or
1678 * added depending on the <wanted> flags, which are exclusively composed of
1679 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1680 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1681 */
1682void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1683{
1684 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685 const char *hdr_val = "Connection";
1686 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001687
1688 ctx.idx = 0;
1689
Willy Tarreau88d349d2010-01-25 12:15:43 +01001690
1691 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1692 hdr_val = "Proxy-Connection";
1693 hdr_len = 16;
1694 }
1695
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001696 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001697 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1699 if (wanted & TX_CON_KAL_SET)
1700 txn->flags |= TX_CON_KAL_SET;
1701 else
1702 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001703 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1705 if (wanted & TX_CON_CLO_SET)
1706 txn->flags |= TX_CON_CLO_SET;
1707 else
1708 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001709 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001710 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001711
1712 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1713 return;
1714
1715 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1716 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001717 hdr_val = "Connection: close";
1718 hdr_len = 17;
1719 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1720 hdr_val = "Proxy-Connection: close";
1721 hdr_len = 23;
1722 }
1723 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001724 }
1725
1726 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1727 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001728 hdr_val = "Connection: keep-alive";
1729 hdr_len = 22;
1730 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1731 hdr_val = "Proxy-Connection: keep-alive";
1732 hdr_len = 28;
1733 }
1734 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001735 }
1736 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001737}
1738
Willy Tarreaud98cf932009-12-27 22:54:55 +01001739/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1740 * first byte of body, and increments msg->sov by the number of bytes parsed,
1741 * so that we know we can forward between ->som and ->sov. Note that due to
1742 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1743 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749 char *ptr = buf->lr;
1750 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001751 unsigned int chunk = 0;
1752
1753 /* The chunk size is in the following form, though we are only
1754 * interested in the size and CRLF :
1755 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1756 */
1757 while (1) {
1758 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001760 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001762 if (c < 0) /* not a hex digit anymore */
1763 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 if (++ptr >= end)
1765 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001766 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001767 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 chunk = (chunk << 4) + c;
1769 }
1770
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771 /* empty size not allowed */
1772 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001773 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774
1775 while (http_is_spht[(unsigned char)*ptr]) {
1776 if (++ptr >= end)
1777 ptr = buf->data;
1778 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001779 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 }
1781
Willy Tarreaud98cf932009-12-27 22:54:55 +01001782 /* Up to there, we know that at least one byte is present at *ptr. Check
1783 * for the end of chunk size.
1784 */
1785 while (1) {
1786 if (likely(HTTP_IS_CRLF(*ptr))) {
1787 /* we now have a CR or an LF at ptr */
1788 if (likely(*ptr == '\r')) {
1789 if (++ptr >= end)
1790 ptr = buf->data;
1791 if (ptr == buf->r)
1792 return 0;
1793 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001794
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001796 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 if (++ptr >= end)
1798 ptr = buf->data;
1799 /* done */
1800 break;
1801 }
1802 else if (*ptr == ';') {
1803 /* chunk extension, ends at next CRLF */
1804 if (++ptr >= end)
1805 ptr = buf->data;
1806 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001807 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808
1809 while (!HTTP_IS_CRLF(*ptr)) {
1810 if (++ptr >= end)
1811 ptr = buf->data;
1812 if (ptr == buf->r)
1813 return 0;
1814 }
1815 /* we have a CRLF now, loop above */
1816 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001817 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001819 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
1821
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 /* OK we found our CRLF and now <ptr> points to the next byte,
1823 * which may or may not be present. We save that into ->lr and
1824 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 msg->sov += ptr - buf->lr;
1827 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01001828 msg->chunk_len = chunk;
1829 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001832 error:
1833 msg->err_pos = ptr - buf->data;
1834 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835}
1836
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837/* This function skips trailers in the buffer <buf> associated with HTTP
1838 * message <msg>. The first visited position is buf->lr. If the end of
1839 * the trailers is found, it is automatically scheduled to be forwarded,
1840 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1841 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001842 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1843 * zero. If a parse error is encountered, the function returns < 0 and does not
1844 * change anything except maybe buf->lr and msg->sov. Note that the message
1845 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1846 * which implies that all non-trailers data have already been scheduled for
1847 * forwarding, and that the difference between msg->som and msg->sov exactly
1848 * matches the length of trailers already parsed and not forwarded. It is also
1849 * important to note that this function is designed to be able to parse wrapped
1850 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 */
1852int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1853{
1854 /* we have buf->lr which points to next line. Look for CRLF. */
1855 while (1) {
1856 char *p1 = NULL, *p2 = NULL;
1857 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001858 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859
1860 /* scan current line and stop at LF or CRLF */
1861 while (1) {
1862 if (ptr == buf->r)
1863 return 0;
1864
1865 if (*ptr == '\n') {
1866 if (!p1)
1867 p1 = ptr;
1868 p2 = ptr;
1869 break;
1870 }
1871
1872 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001873 if (p1) {
1874 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001876 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 p1 = ptr;
1878 }
1879
1880 ptr++;
1881 if (ptr >= buf->data + buf->size)
1882 ptr = buf->data;
1883 }
1884
1885 /* after LF; point to beginning of next line */
1886 p2++;
1887 if (p2 >= buf->data + buf->size)
1888 p2 = buf->data;
1889
Willy Tarreau638cd022010-01-03 07:42:04 +01001890 bytes = p2 - buf->lr;
1891 if (bytes < 0)
1892 bytes += buf->size;
1893
1894 /* schedule this line for forwarding */
1895 msg->sov += bytes;
1896 if (msg->sov >= buf->size)
1897 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898
Willy Tarreau638cd022010-01-03 07:42:04 +01001899 if (p1 == buf->lr) {
1900 /* LF/CRLF at beginning of line => end of trailers at p2.
1901 * Everything was scheduled for forwarding, there's nothing
1902 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001903 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001904 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 msg->msg_state = HTTP_MSG_DONE;
1906 return 1;
1907 }
1908 /* OK, next line then */
1909 buf->lr = p2;
1910 }
1911}
1912
1913/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1914 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1915 * ->som, buf->lr in order to include this part into the next forwarding phase.
1916 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1917 * not enough data are available, the function does not change anything and
1918 * returns zero. If a parse error is encountered, the function returns < 0 and
1919 * does not change anything. Note: this function is designed to parse wrapped
1920 * CRLF at the end of the buffer.
1921 */
1922int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1923{
1924 char *ptr;
1925 int bytes;
1926
1927 /* NB: we'll check data availabilty at the end. It's not a
1928 * problem because whatever we match first will be checked
1929 * against the correct length.
1930 */
1931 bytes = 1;
1932 ptr = buf->lr;
1933 if (*ptr == '\r') {
1934 bytes++;
1935 ptr++;
1936 if (ptr >= buf->data + buf->size)
1937 ptr = buf->data;
1938 }
1939
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01001940 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 return 0;
1942
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001943 if (*ptr != '\n') {
1944 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947
1948 ptr++;
1949 if (ptr >= buf->data + buf->size)
1950 ptr = buf->data;
1951 buf->lr = ptr;
1952 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1953 msg->sov = ptr - buf->data;
1954 msg->som = msg->sov - bytes;
1955 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1956 return 1;
1957}
Willy Tarreau5b154472009-12-21 20:11:07 +01001958
Willy Tarreau83e3af02009-12-28 17:39:57 +01001959void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1960{
1961 char *end = buf->data + buf->size;
1962 int off = buf->data + buf->size - buf->w;
1963
1964 /* two possible cases :
1965 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001966 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001967 */
1968 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01001969 int block1 = buf->l;
1970 int block2 = 0;
1971 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001972 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01001973 block1 = buf->data + buf->size - buf->w;
1974 block2 = buf->r - buf->data;
1975 }
1976 if (block2)
1977 memcpy(swap_buffer, buf->data, block2);
1978 memmove(buf->data, buf->w, block1);
1979 if (block2)
1980 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001981 }
1982
1983 /* adjust all known pointers */
1984 buf->w = buf->data;
1985 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
1986 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
1987 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1988 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1989
1990 /* adjust relative pointers */
1991 msg->som = 0;
1992 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
1993 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
1994 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
1995
Willy Tarreau83e3af02009-12-28 17:39:57 +01001996 if (msg->err_pos >= 0) {
1997 msg->err_pos += off;
1998 if (msg->err_pos >= buf->size)
1999 msg->err_pos -= buf->size;
2000 }
2001
2002 buf->flags &= ~BF_FULL;
2003 if (buf->l >= buffer_max_len(buf))
2004 buf->flags |= BF_FULL;
2005}
2006
Willy Tarreaud787e662009-07-07 10:14:51 +02002007/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2008 * processing can continue on next analysers, or zero if it either needs more
2009 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2010 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2011 * when it has nothing left to do, and may remove any analyser when it wants to
2012 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002013 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002014int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002015{
Willy Tarreau59234e92008-11-30 23:51:27 +01002016 /*
2017 * We will parse the partial (or complete) lines.
2018 * We will check the request syntax, and also join multi-line
2019 * headers. An index of all the lines will be elaborated while
2020 * parsing.
2021 *
2022 * For the parsing, we use a 28 states FSM.
2023 *
2024 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002025 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002026 * req->data + msg->eoh = end of processed headers / start of current one
2027 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002028 * req->lr = first non-visited byte
2029 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002030 *
2031 * At end of parsing, we may perform a capture of the error (if any), and
2032 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2033 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2034 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002036
Willy Tarreau59234e92008-11-30 23:51:27 +01002037 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002038 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002039 struct http_txn *txn = &s->txn;
2040 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002041 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002042
Willy Tarreau6bf17362009-02-24 10:48:35 +01002043 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2044 now_ms, __FUNCTION__,
2045 s,
2046 req,
2047 req->rex, req->wex,
2048 req->flags,
2049 req->l,
2050 req->analysers);
2051
Willy Tarreau52a0c602009-08-16 22:45:38 +02002052 /* we're speaking HTTP here, so let's speak HTTP to the client */
2053 s->srv_error = http_return_srv_error;
2054
Willy Tarreau83e3af02009-12-28 17:39:57 +01002055 /* There's a protected area at the end of the buffer for rewriting
2056 * purposes. We don't want to start to parse the request if the
2057 * protected area is affected, because we may have to move processed
2058 * data later, which is much more complicated.
2059 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002060 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002061 if ((txn->flags & TX_NOT_FIRST) &&
2062 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002063 req->r < req->lr ||
2064 req->r > req->data + req->size - global.tune.maxrewrite)) {
2065 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002066 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2067 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002068 /* some data has still not left the buffer, wake us once that's done */
2069 buffer_dont_connect(req);
2070 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2071 return 0;
2072 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002073 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002074 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002075 }
2076
Willy Tarreau065e8332010-01-08 00:30:20 +01002077 /* Note that we have the same problem with the response ; we
2078 * may want to send a redirect, error or anything which requires
2079 * some spare space. So we'll ensure that we have at least
2080 * maxrewrite bytes available in the response buffer before
2081 * processing that one. This will only affect pipelined
2082 * keep-alive requests.
2083 */
2084 if ((txn->flags & TX_NOT_FIRST) &&
2085 unlikely((s->rep->flags & BF_FULL) ||
2086 s->rep->r < s->rep->lr ||
2087 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2088 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002089 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2090 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002091 /* don't let a connection request be initiated */
2092 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002093 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002094 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002095 return 0;
2096 }
2097 }
2098
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002099 if (likely(req->lr < req->r))
2100 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002101 }
2102
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 /* 1: we might have to print this header in debug mode */
2104 if (unlikely((global.mode & MODE_DEBUG) &&
2105 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002106 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002107 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002108 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109
Willy Tarreau663308b2010-06-07 14:06:08 +02002110 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 eol = sol + msg->sl.rq.l;
2112 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 sol += hdr_idx_first_pos(&txn->hdr_idx);
2115 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 while (cur_idx) {
2118 eol = sol + txn->hdr_idx.v[cur_idx].len;
2119 debug_hdr("clihdr", s, sol, eol);
2120 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2121 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002122 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 }
2124
Willy Tarreau58f10d72006-12-04 02:26:12 +01002125
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 /*
2127 * Now we quickly check if we have found a full valid request.
2128 * If not so, we check the FD and buffer states before leaving.
2129 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002130 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002131 * requests are checked first. When waiting for a second request
2132 * on a keep-alive session, if we encounter and error, close, t/o,
2133 * we note the error in the session flags but don't set any state.
2134 * Since the error will be noted there, it will not be counted by
2135 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002136 * Last, we may increase some tracked counters' http request errors on
2137 * the cases that are deliberately the client's fault. For instance,
2138 * a timeout or connection reset is not counted as an error. However
2139 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002140 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002141
Willy Tarreau655dce92009-11-08 13:10:58 +01002142 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002143 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002145 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002146 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002147 session_inc_http_req_ctr(s);
2148 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002149 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002151 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002152
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 /* 1: Since we are in header mode, if there's no space
2154 * left for headers, we won't be able to free more
2155 * later, so the session will never terminate. We
2156 * must terminate it now.
2157 */
2158 if (unlikely(req->flags & BF_FULL)) {
2159 /* FIXME: check if URI is set and return Status
2160 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002161 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002162 session_inc_http_req_ctr(s);
2163 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002164 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002165 if (msg->err_pos < 0)
2166 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 goto return_bad_req;
2168 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 /* 2: have we encountered a read error ? */
2171 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002172 if (!(s->flags & SN_ERR_MASK))
2173 s->flags |= SN_ERR_CLICL;
2174
Willy Tarreaufcffa692010-01-10 14:21:19 +01002175 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002176 goto failed_keep_alive;
2177
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002179 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002180 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002181 session_inc_http_err_ctr(s);
2182 }
2183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 msg->msg_state = HTTP_MSG_ERROR;
2185 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002186
Willy Tarreauda7ff642010-06-23 11:44:09 +02002187 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002188 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002189 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002190 if (s->listener->counters)
2191 s->listener->counters->failed_req++;
2192
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 if (!(s->flags & SN_FINST_MASK))
2194 s->flags |= SN_FINST_R;
2195 return 0;
2196 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 /* 3: has the read timeout expired ? */
2199 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002200 if (!(s->flags & SN_ERR_MASK))
2201 s->flags |= SN_ERR_CLITO;
2202
Willy Tarreaufcffa692010-01-10 14:21:19 +01002203 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002204 goto failed_keep_alive;
2205
Willy Tarreau59234e92008-11-30 23:51:27 +01002206 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002207 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002208 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002209 session_inc_http_err_ctr(s);
2210 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 txn->status = 408;
2212 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2213 msg->msg_state = HTTP_MSG_ERROR;
2214 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002215
Willy Tarreauda7ff642010-06-23 11:44:09 +02002216 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002217 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002218 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002219 if (s->listener->counters)
2220 s->listener->counters->failed_req++;
2221
Willy Tarreau59234e92008-11-30 23:51:27 +01002222 if (!(s->flags & SN_FINST_MASK))
2223 s->flags |= SN_FINST_R;
2224 return 0;
2225 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002226
Willy Tarreau59234e92008-11-30 23:51:27 +01002227 /* 4: have we encountered a close ? */
2228 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002229 if (!(s->flags & SN_ERR_MASK))
2230 s->flags |= SN_ERR_CLICL;
2231
Willy Tarreaufcffa692010-01-10 14:21:19 +01002232 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002233 goto failed_keep_alive;
2234
Willy Tarreau4076a152009-04-02 15:18:36 +02002235 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002236 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 txn->status = 400;
2238 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2239 msg->msg_state = HTTP_MSG_ERROR;
2240 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002241
Willy Tarreauda7ff642010-06-23 11:44:09 +02002242 session_inc_http_err_ctr(s);
2243 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002244 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002245 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002246 if (s->listener->counters)
2247 s->listener->counters->failed_req++;
2248
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 if (!(s->flags & SN_FINST_MASK))
2250 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002251 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002252 }
2253
Willy Tarreau520d95e2009-09-19 21:04:57 +02002254 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002255 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002256 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002257#ifdef TCP_QUICKACK
2258 if (s->listener->options & LI_O_NOQUICKACK) {
2259 /* We need more data, we have to re-enable quick-ack in case we
2260 * previously disabled it, otherwise we might cause the client
2261 * to delay next data.
2262 */
2263 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2264 }
2265#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002266
Willy Tarreaufcffa692010-01-10 14:21:19 +01002267 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2268 /* If the client starts to talk, let's fall back to
2269 * request timeout processing.
2270 */
2271 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002272 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002273 }
2274
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002276 if (!tick_isset(req->analyse_exp)) {
2277 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2278 (txn->flags & TX_WAIT_NEXT_RQ) &&
2279 tick_isset(s->be->timeout.httpka))
2280 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2281 else
2282 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 /* we're not ready yet */
2286 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002287
2288 failed_keep_alive:
2289 /* Here we process low-level errors for keep-alive requests. In
2290 * short, if the request is not the first one and it experiences
2291 * a timeout, read error or shutdown, we just silently close so
2292 * that the client can try again.
2293 */
2294 txn->status = 0;
2295 msg->msg_state = HTTP_MSG_RQBEFORE;
2296 req->analysers = 0;
2297 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002298 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002299 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002300 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002301 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302
Willy Tarreaud787e662009-07-07 10:14:51 +02002303 /* OK now we have a complete HTTP request with indexed headers. Let's
2304 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002305 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2306 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2307 * points to the CRLF of the request line. req->lr points to the first
2308 * byte after the last LF. msg->col and msg->sov point to the first
2309 * byte of data. msg->eol cannot be trusted because it may have been
2310 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002311 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002312
Willy Tarreauda7ff642010-06-23 11:44:09 +02002313 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002314 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2315
Willy Tarreaub16a5742010-01-10 14:46:16 +01002316 if (txn->flags & TX_WAIT_NEXT_RQ) {
2317 /* kill the pending keep-alive timeout */
2318 txn->flags &= ~TX_WAIT_NEXT_RQ;
2319 req->analyse_exp = TICK_ETERNITY;
2320 }
2321
2322
Willy Tarreaud787e662009-07-07 10:14:51 +02002323 /* Maybe we found in invalid header name while we were configured not
2324 * to block on that, so we have to capture it now.
2325 */
2326 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002327 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002328
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 /*
2330 * 1: identify the method
2331 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002332 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002333
2334 /* we can make use of server redirect on GET and HEAD */
2335 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2336 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002337
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 /*
2339 * 2: check if the URI matches the monitor_uri.
2340 * We have to do this for every request which gets in, because
2341 * the monitor-uri is defined by the frontend.
2342 */
2343 if (unlikely((s->fe->monitor_uri_len != 0) &&
2344 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002345 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 s->fe->monitor_uri,
2347 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002350 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002354 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002357 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2358 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 ret = acl_pass(ret);
2361 if (cond->pol == ACL_COND_UNLESS)
2362 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 if (ret) {
2365 /* we fail this request, let's return 503 service unavail */
2366 txn->status = 503;
2367 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2368 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002369 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002370 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002371
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 /* nothing to fail, let's reply normaly */
2373 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002374 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 goto return_prx_cond;
2376 }
2377
2378 /*
2379 * 3: Maybe we have to copy the original REQURI for the logs ?
2380 * Note: we cannot log anymore if the request has been
2381 * classified as invalid.
2382 */
2383 if (unlikely(s->logs.logwait & LW_REQ)) {
2384 /* we have a complete HTTP request that we must log */
2385 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2386 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 if (urilen >= REQURI_LEN)
2389 urilen = REQURI_LEN - 1;
2390 memcpy(txn->uri, &req->data[msg->som], urilen);
2391 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 if (!(s->logs.logwait &= ~LW_REQ))
2394 s->do_log(s);
2395 } else {
2396 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002397 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 }
Willy Tarreau06619262006-12-17 08:37:22 +01002399
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002401 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2402 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002403
Willy Tarreau5b154472009-12-21 20:11:07 +01002404 /* ... and check if the request is HTTP/1.1 or above */
2405 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002406 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2407 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2408 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002409 txn->flags |= TX_REQ_VER_11;
2410
2411 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002412 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002413
Willy Tarreau88d349d2010-01-25 12:15:43 +01002414 /* if the frontend has "option http-use-proxy-header", we'll check if
2415 * we have what looks like a proxied connection instead of a connection,
2416 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2417 * Note that this is *not* RFC-compliant, however browsers and proxies
2418 * happen to do that despite being non-standard :-(
2419 * We consider that a request not beginning with either '/' or '*' is
2420 * a proxied connection, which covers both "scheme://location" and
2421 * CONNECT ip:port.
2422 */
2423 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2424 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2425 txn->flags |= TX_USE_PX_CONN;
2426
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002427 /* transfer length unknown*/
2428 txn->flags &= ~TX_REQ_XFER_LEN;
2429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002431 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002432 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002434
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002435 /* 6: determine the transfer-length.
2436 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2437 * the presence of a message-body in a REQUEST and its transfer length
2438 * must be determined that way (in order of precedence) :
2439 * 1. The presence of a message-body in a request is signaled by the
2440 * inclusion of a Content-Length or Transfer-Encoding header field
2441 * in the request's header fields. When a request message contains
2442 * both a message-body of non-zero length and a method that does
2443 * not define any semantics for that request message-body, then an
2444 * origin server SHOULD either ignore the message-body or respond
2445 * with an appropriate error message (e.g., 413). A proxy or
2446 * gateway, when presented the same request, SHOULD either forward
2447 * the request inbound with the message- body or ignore the
2448 * message-body when determining a response.
2449 *
2450 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2451 * and the "chunked" transfer-coding (Section 6.2) is used, the
2452 * transfer-length is defined by the use of this transfer-coding.
2453 * If a Transfer-Encoding header field is present and the "chunked"
2454 * transfer-coding is not present, the transfer-length is defined
2455 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002456 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002457 * 3. If a Content-Length header field is present, its decimal value in
2458 * OCTETs represents both the entity-length and the transfer-length.
2459 * If a message is received with both a Transfer-Encoding header
2460 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002461 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002462 * 4. By the server closing the connection. (Closing the connection
2463 * cannot be used to indicate the end of a request body, since that
2464 * would leave no possibility for the server to send back a response.)
2465 *
2466 * Whenever a transfer-coding is applied to a message-body, the set of
2467 * transfer-codings MUST include "chunked", unless the message indicates
2468 * it is terminated by closing the connection. When the "chunked"
2469 * transfer-coding is used, it MUST be the last transfer-coding applied
2470 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002471 */
2472
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002473 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002475 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002476 while ((txn->flags & TX_REQ_VER_11) &&
2477 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002478 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2479 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2480 else if (txn->flags & TX_REQ_TE_CHNK) {
2481 /* bad transfer-encoding (chunked followed by something else) */
2482 use_close_only = 1;
2483 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2484 break;
2485 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002486 }
2487
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002489 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2491 signed long long cl;
2492
Willy Tarreauad14f752011-09-02 20:33:27 +02002493 if (!ctx.vlen) {
2494 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002496 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497
Willy Tarreauad14f752011-09-02 20:33:27 +02002498 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2499 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002501 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002502
Willy Tarreauad14f752011-09-02 20:33:27 +02002503 if (cl < 0) {
2504 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002505 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002506 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002507
Willy Tarreauad14f752011-09-02 20:33:27 +02002508 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) {
2509 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002510 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002511 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002512
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002513 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002514 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002515 }
2516
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002517 /* bodyless requests have a known length */
2518 if (!use_close_only)
2519 txn->flags |= TX_REQ_XFER_LEN;
2520
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002522 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002523 req->analyse_exp = TICK_ETERNITY;
2524 return 1;
2525
2526 return_bad_req:
2527 /* We centralize bad requests processing here */
2528 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2529 /* we detected a parsing error. We want to archive this request
2530 * in the dedicated proxy area for later troubleshooting.
2531 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002532 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002533 }
2534
2535 txn->req.msg_state = HTTP_MSG_ERROR;
2536 txn->status = 400;
2537 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002538
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002539 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002540 if (s->listener->counters)
2541 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002542
2543 return_prx_cond:
2544 if (!(s->flags & SN_ERR_MASK))
2545 s->flags |= SN_ERR_PRXCOND;
2546 if (!(s->flags & SN_FINST_MASK))
2547 s->flags |= SN_FINST_R;
2548
2549 req->analysers = 0;
2550 req->analyse_exp = TICK_ETERNITY;
2551 return 0;
2552}
2553
Cyril Bonté70be45d2010-10-12 00:14:35 +02002554/* We reached the stats page through a POST request.
2555 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002556 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002557 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002558int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002559{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002560 struct proxy *px = NULL;
2561 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002562
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002563 char key[LINESIZE];
2564 int action = ST_ADM_ACTION_NONE;
2565 int reprocess = 0;
2566
2567 int total_servers = 0;
2568 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569
2570 char *first_param, *cur_param, *next_param, *end_params;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002571 char *st_cur_param, *st_next_param;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002572
2573 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002574 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575
2576 cur_param = next_param = end_params;
2577
Cyril Bonté23b39d92011-02-10 22:54:44 +01002578 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002580 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 return 1;
2582 }
2583 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002584 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002585 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002586 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587 }
2588
2589 *end_params = '\0';
2590
Willy Tarreau295a8372011-03-10 11:25:07 +01002591 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002592
2593 /*
2594 * Parse the parameters in reverse order to only store the last value.
2595 * From the html form, the backend and the action are at the end.
2596 */
2597 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002598 char *value;
2599 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002600
2601 cur_param--;
2602 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002603 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002605 poffset = (cur_param != first_param ? 1 : 0);
2606 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2607 if ((plen > 0) && (plen <= sizeof(key))) {
2608 strncpy(key, cur_param + poffset, plen);
2609 key[plen - 1] = '\0';
2610 } else {
2611 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2612 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002613 }
2614
2615 /* Parse the value */
2616 value = key;
2617 while (*value != '\0' && *value != '=') {
2618 value++;
2619 }
2620 if (*value == '=') {
2621 /* Ok, a value is found, we can mark the end of the key */
2622 *value++ = '\0';
2623 }
2624
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002625 if (!url_decode(key) || !url_decode(value))
2626 break;
2627
Cyril Bonté70be45d2010-10-12 00:14:35 +02002628 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002629 if (!px && (strcmp(key, "b") == 0)) {
2630 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2631 /* the backend name is unknown or ambiguous (duplicate names) */
2632 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2633 goto out;
2634 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002635 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002636 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002637 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002638 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002639 }
2640 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002641 action = ST_ADM_ACTION_ENABLE;
2642 }
2643 else {
2644 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2645 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646 }
2647 }
2648 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002649 if (!(px && action)) {
2650 /*
2651 * Indicates that we'll need to reprocess the parameters
2652 * as soon as backend and action are known
2653 */
2654 if (!reprocess) {
2655 st_cur_param = cur_param;
2656 st_next_param = next_param;
2657 }
2658 reprocess = 1;
2659 }
2660 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002661 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002662 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002663 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002664 /* Not already in maintenance, we can change the server state */
2665 sv->state |= SRV_MAINTAIN;
2666 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002667 altered_servers++;
2668 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002669 }
2670 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002671 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002672 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673 /* Already in maintenance, we can change the server state */
2674 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002675 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002676 altered_servers++;
2677 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678 }
2679 break;
2680 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002681 } else {
2682 /* the server name is unknown or ambiguous (duplicate names) */
2683 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002684 }
2685 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002686 if (reprocess && px && action) {
2687 /* Now, we know the backend and the action chosen by the user.
2688 * We can safely restart from the first server parameter
2689 * to reprocess them
2690 */
2691 cur_param = st_cur_param;
2692 next_param = st_next_param;
2693 reprocess = 0;
2694 goto reprocess_servers;
2695 }
2696
Cyril Bonté70be45d2010-10-12 00:14:35 +02002697 next_param = cur_param;
2698 }
2699 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002700
2701 if (total_servers == 0) {
2702 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2703 }
2704 else if (altered_servers == 0) {
2705 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2706 }
2707 else if (altered_servers == total_servers) {
2708 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2709 }
2710 else {
2711 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2712 }
2713 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002714 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002715}
2716
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002717/* returns a pointer to the first rule which forbids access (deny or http_auth),
2718 * or NULL if everything's OK.
2719 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002720static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002721http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2722{
Willy Tarreauff011f22011-01-06 17:51:27 +01002723 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002724
Willy Tarreauff011f22011-01-06 17:51:27 +01002725 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002726 int ret = 1;
2727
Willy Tarreauff011f22011-01-06 17:51:27 +01002728 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002729 continue;
2730
2731 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002732 if (rule->cond) {
2733 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734 ret = acl_pass(ret);
2735
Willy Tarreauff011f22011-01-06 17:51:27 +01002736 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002737 ret = !ret;
2738 }
2739
2740 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002741 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002742 return NULL; /* no problem */
2743 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002744 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002745 }
2746 }
2747 return NULL;
2748}
2749
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750/* This stream analyser runs all HTTP request processing which is common to
2751 * frontends and backends, which means blocking ACLs, filters, connection-close,
2752 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 * either needs more data or wants to immediately abort the request (eg: deny,
2755 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002756 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002758{
Willy Tarreaud787e662009-07-07 10:14:51 +02002759 struct http_txn *txn = &s->txn;
2760 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002762 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002764 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002765 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002766
Willy Tarreau655dce92009-11-08 13:10:58 +01002767 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002768 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002769 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002770 return 0;
2771 }
2772
Willy Tarreau3a816292009-07-07 10:55:49 +02002773 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002774 req->analyse_exp = TICK_ETERNITY;
2775
2776 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2777 now_ms, __FUNCTION__,
2778 s,
2779 req,
2780 req->rex, req->wex,
2781 req->flags,
2782 req->l,
2783 req->analysers);
2784
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002785 /* first check whether we have some ACLs set to block this request */
2786 list_for_each_entry(cond, &px->block_cond, list) {
2787 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002788
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 ret = acl_pass(ret);
2790 if (cond->pol == ACL_COND_UNLESS)
2791 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002792
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 if (ret) {
2794 txn->status = 403;
2795 /* let's log the request time */
2796 s->logs.tv_request = now;
2797 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002799 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002801 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002802
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002803 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002804 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002805
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002806 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002807 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002808 do_stats = stats_check_uri(s->rep->prod, txn, px);
2809 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002810 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 +01002811 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002812 else
2813 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002814
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002815 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002816 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002817 txn->status = 403;
2818 s->logs.tv_request = now;
2819 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002820 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002821 s->fe->fe_counters.denied_req++;
2822 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2823 s->be->be_counters.denied_req++;
2824 if (s->listener->counters)
2825 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002826 goto return_prx_cond;
2827 }
2828
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002829 /* try headers filters */
2830 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002831 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002832 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002833
Willy Tarreau59234e92008-11-30 23:51:27 +01002834 /* has the request been denied ? */
2835 if (txn->flags & TX_CLDENY) {
2836 /* no need to go further */
2837 txn->status = 403;
2838 /* let's log the request time */
2839 s->logs.tv_request = now;
2840 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002841 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002842 goto return_prx_cond;
2843 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002844
2845 /* When a connection is tarpitted, we use the tarpit timeout,
2846 * which may be the same as the connect timeout if unspecified.
2847 * If unset, then set it to zero because we really want it to
2848 * eventually expire. We build the tarpit as an analyser.
2849 */
2850 if (txn->flags & TX_CLTARPIT) {
2851 buffer_erase(s->req);
2852 /* wipe the request out so that we can drop the connection early
2853 * if the client closes first.
2854 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002855 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002856 req->analysers = 0; /* remove switching rules etc... */
2857 req->analysers |= AN_REQ_HTTP_TARPIT;
2858 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2859 if (!req->analyse_exp)
2860 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002861 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002862 return 1;
2863 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002864 }
Willy Tarreau06619262006-12-17 08:37:22 +01002865
Willy Tarreau5b154472009-12-21 20:11:07 +01002866 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2867 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002868 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2869 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002870 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2871 * avoid to redo the same work if FE and BE have the same settings (common).
2872 * The method consists in checking if options changed between the two calls
2873 * (implying that either one is non-null, or one of them is non-null and we
2874 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002875 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002876
Willy Tarreaudc008c52010-02-01 16:20:08 +01002877 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2878 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2879 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2880 (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 +01002881 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002882
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002883 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2884 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002885 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002886 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2887 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002888 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002889 tmp = TX_CON_WANT_CLO;
2890
Willy Tarreau5b154472009-12-21 20:11:07 +01002891 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2892 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002893
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2895 /* parse the Connection header and possibly clean it */
2896 int to_del = 0;
2897 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002898 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2899 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002900 to_del |= 2; /* remove "keep-alive" */
2901 if (!(txn->flags & TX_REQ_VER_11))
2902 to_del |= 1; /* remove "close" */
2903 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002904 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002905
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906 /* check if client or config asks for explicit close in KAL/SCL */
2907 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2908 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2909 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2910 (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 +01002911 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002912 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2913 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002914 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2915 }
Willy Tarreau78599912009-10-17 20:12:21 +02002916
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002917 /* we can be blocked here because the request needs to be authenticated,
2918 * either to pass or to access stats.
2919 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002920 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002921 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002922 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002923
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002924 if (!realm)
2925 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2926
Willy Tarreau844a7e72010-01-31 21:46:18 +01002927 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002928 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2929 txn->status = 401;
2930 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002931 /* on 401 we still count one error, because normal browsing
2932 * won't significantly increase the counter but brute force
2933 * attempts will.
2934 */
2935 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002936 goto return_prx_cond;
2937 }
2938
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002939 /* add request headers from the rule sets in the same order */
2940 list_for_each_entry(wl, &px->req_add, list) {
2941 if (wl->cond) {
2942 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2943 ret = acl_pass(ret);
2944 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2945 ret = !ret;
2946 if (!ret)
2947 continue;
2948 }
2949
2950 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2951 goto return_bad_req;
2952 }
2953
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002954 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002955 struct stats_admin_rule *stats_admin_rule;
2956
2957 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002958 * FIXME!!! that one is rather dangerous, we want to
2959 * make it follow standard rules (eg: clear req->analysers).
2960 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002961
Cyril Bonté474be412010-10-12 00:14:36 +02002962 /* now check whether we have some admin rules for this request */
2963 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2964 int ret = 1;
2965
2966 if (stats_admin_rule->cond) {
2967 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2968 ret = acl_pass(ret);
2969 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2970 ret = !ret;
2971 }
2972
2973 if (ret) {
2974 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002975 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002976 break;
2977 }
2978 }
2979
Cyril Bonté70be45d2010-10-12 00:14:35 +02002980 /* Was the status page requested with a POST ? */
2981 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002982 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002983 if (msg->msg_state < HTTP_MSG_100_SENT) {
2984 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2985 * send an HTTP/1.1 100 Continue intermediate response.
2986 */
2987 if (txn->flags & TX_REQ_VER_11) {
2988 struct hdr_ctx ctx;
2989 ctx.idx = 0;
2990 /* Expect is allowed in 1.1, look for it */
2991 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
2992 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2993 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2994 }
2995 }
2996 msg->msg_state = HTTP_MSG_100_SENT;
2997 s->logs.tv_request = now; /* update the request timer to reflect full request */
2998 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002999 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003000 /* we need more data */
3001 req->analysers |= an_bit;
3002 buffer_dont_connect(req);
3003 return 0;
3004 }
Cyril Bonté474be412010-10-12 00:14:36 +02003005 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003006 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003007 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003008 }
3009
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003010 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003011 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003012 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003013 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003014 s->rep->prod->applet.private = s;
3015 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003016 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003017 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3018 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003019
3020 return 0;
3021
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003022 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003024 /* check whether we have some ACLs set to redirect this request */
3025 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003026 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003027
Willy Tarreauf285f542010-01-03 20:03:03 +01003028 if (rule->cond) {
3029 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3030 ret = acl_pass(ret);
3031 if (rule->cond->pol == ACL_COND_UNLESS)
3032 ret = !ret;
3033 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003034
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003036 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 /* build redirect message */
3040 switch(rule->code) {
3041 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 msg_fmt = HTTP_303;
3043 break;
3044 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 msg_fmt = HTTP_301;
3046 break;
3047 case 302:
3048 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 msg_fmt = HTTP_302;
3050 break;
3051 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003053 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003055
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003056 switch(rule->type) {
3057 case REDIRECT_TYPE_PREFIX: {
3058 const char *path;
3059 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003060
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 path = http_get_path(txn);
3062 /* build message using path */
3063 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003064 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3066 int qs = 0;
3067 while (qs < pathlen) {
3068 if (path[qs] == '?') {
3069 pathlen = qs;
3070 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003072 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003074 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 } else {
3076 path = "/";
3077 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003078 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003079
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003080 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003081 goto return_bad_req;
3082
3083 /* add prefix. Note that if prefix == "/", we don't want to
3084 * add anything, otherwise it makes it hard for the user to
3085 * configure a self-redirection.
3086 */
3087 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003088 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3089 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 }
3091
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092 /* add path */
3093 memcpy(rdr.str + rdr.len, path, pathlen);
3094 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003095
3096 /* append a slash at the end of the location is needed and missing */
3097 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3098 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3099 if (rdr.len > rdr.size - 5)
3100 goto return_bad_req;
3101 rdr.str[rdr.len] = '/';
3102 rdr.len++;
3103 }
3104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 break;
3106 }
3107 case REDIRECT_TYPE_LOCATION:
3108 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003109 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003111
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112 /* add location */
3113 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3114 rdr.len += rule->rdr_len;
3115 break;
3116 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003117
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118 if (rule->cookie_len) {
3119 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3120 rdr.len += 14;
3121 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3122 rdr.len += rule->cookie_len;
3123 memcpy(rdr.str + rdr.len, "\r\n", 2);
3124 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003125 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003126
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003127 /* add end of headers and the keep-alive/close status.
3128 * We may choose to set keep-alive if the Location begins
3129 * with a slash, because the client will come back to the
3130 * same server.
3131 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003132 txn->status = rule->code;
3133 /* let's log the request time */
3134 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003135
3136 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3137 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003138 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003139 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3140 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3141 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003142 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003143 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3144 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3145 rdr.len += 30;
3146 } else {
3147 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3148 rdr.len += 24;
3149 }
Willy Tarreau75661452010-01-10 10:35:01 +01003150 }
3151 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3152 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003153 buffer_write(req->prod->ob, rdr.str, rdr.len);
3154 /* "eat" the request */
3155 buffer_ignore(req, msg->sov - msg->som);
3156 msg->som = msg->sov;
3157 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003158 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3159 txn->req.msg_state = HTTP_MSG_CLOSED;
3160 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003161 break;
3162 } else {
3163 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003164 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3165 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3166 rdr.len += 29;
3167 } else {
3168 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3169 rdr.len += 23;
3170 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003171 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003172 goto return_prx_cond;
3173 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003174 }
3175 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003176
Willy Tarreau2be39392010-01-03 17:24:51 +01003177 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3178 * If this happens, then the data will not come immediately, so we must
3179 * send all what we have without waiting. Note that due to the small gain
3180 * in waiting for the body of the request, it's easier to simply put the
3181 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3182 * itself once used.
3183 */
3184 req->flags |= BF_SEND_DONTWAIT;
3185
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003186 /* that's OK for us now, let's move on to next analysers */
3187 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003189 return_bad_req:
3190 /* We centralize bad requests processing here */
3191 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3192 /* we detected a parsing error. We want to archive this request
3193 * in the dedicated proxy area for later troubleshooting.
3194 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003195 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003196 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 txn->req.msg_state = HTTP_MSG_ERROR;
3199 txn->status = 400;
3200 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003201
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003202 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003203 if (s->listener->counters)
3204 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003206 return_prx_cond:
3207 if (!(s->flags & SN_ERR_MASK))
3208 s->flags |= SN_ERR_PRXCOND;
3209 if (!(s->flags & SN_FINST_MASK))
3210 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 req->analysers = 0;
3213 req->analyse_exp = TICK_ETERNITY;
3214 return 0;
3215}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003216
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003217/* This function performs all the processing enabled for the current request.
3218 * It returns 1 if the processing can continue on next analysers, or zero if it
3219 * needs more data, encounters an error, or wants to immediately abort the
3220 * request. It relies on buffers flags, and updates s->req->analysers.
3221 */
3222int http_process_request(struct session *s, struct buffer *req, int an_bit)
3223{
3224 struct http_txn *txn = &s->txn;
3225 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003226
Willy Tarreau655dce92009-11-08 13:10:58 +01003227 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003228 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003229 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003230 return 0;
3231 }
3232
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3234 now_ms, __FUNCTION__,
3235 s,
3236 req,
3237 req->rex, req->wex,
3238 req->flags,
3239 req->l,
3240 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003241
Willy Tarreau59234e92008-11-30 23:51:27 +01003242 /*
3243 * Right now, we know that we have processed the entire headers
3244 * and that unwanted requests have been filtered out. We can do
3245 * whatever we want with the remaining request. Also, now we
3246 * may have separate values for ->fe, ->be.
3247 */
Willy Tarreau06619262006-12-17 08:37:22 +01003248
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 /*
3250 * If HTTP PROXY is set we simply get remote server address
3251 * parsing incoming request.
3252 */
3253 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003254 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003256
Willy Tarreau59234e92008-11-30 23:51:27 +01003257 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003258 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 * Note that doing so might move headers in the request, but
3260 * the fields will stay coherent and the URI will not move.
3261 * This should only be performed in the backend.
3262 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003263 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3265 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003266
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003268 * 8: the appsession cookie was looked up very early in 1.2,
3269 * so let's do the same now.
3270 */
3271
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003272 /* It needs to look into the URI unless persistence must be ignored */
3273 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003274 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003275 }
3276
3277 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 * 9: add X-Forwarded-For if either the frontend or the backend
3279 * asks for it.
3280 */
3281 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003282 struct hdr_ctx ctx = { .idx = 0 };
3283
3284 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003285 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 +02003286 /* The header is set to be added only if none is present
3287 * and we found it, so don't do anything.
3288 */
3289 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003290 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 /* Add an X-Forwarded-For header unless the source IP is
3292 * in the 'except' network range.
3293 */
3294 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003295 (((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 +01003296 != s->fe->except_net.s_addr) &&
3297 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003298 (((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 +01003299 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003300 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003302 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003303
3304 /* Note: we rely on the backend to get the header name to be used for
3305 * x-forwarded-for, because the header is really meant for the backends.
3306 * However, if the backend did not specify any option, we have to rely
3307 * on the frontend's header name.
3308 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 if (s->be->fwdfor_hdr_len) {
3310 len = s->be->fwdfor_hdr_len;
3311 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003312 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003313 len = s->fe->fwdfor_hdr_len;
3314 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003315 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003317
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003318 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003319 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003320 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003321 }
3322 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003323 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 /* FIXME: for the sake of completeness, we should also support
3325 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003326 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003327 int len;
3328 char pn[INET6_ADDRSTRLEN];
3329 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003330 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003332
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 /* Note: we rely on the backend to get the header name to be used for
3334 * x-forwarded-for, because the header is really meant for the backends.
3335 * However, if the backend did not specify any option, we have to rely
3336 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003337 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 if (s->be->fwdfor_hdr_len) {
3339 len = s->be->fwdfor_hdr_len;
3340 memcpy(trash, s->be->fwdfor_hdr_name, len);
3341 } else {
3342 len = s->fe->fwdfor_hdr_len;
3343 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003344 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003346
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003348 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 goto return_bad_req;
3350 }
3351 }
3352
3353 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003354 * 10: add X-Original-To if either the frontend or the backend
3355 * asks for it.
3356 */
3357 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3358
3359 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003360 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003361 /* Add an X-Original-To header unless the destination IP is
3362 * in the 'except' network range.
3363 */
3364 if (!(s->flags & SN_FRT_ADDR_SET))
3365 get_frt_addr(s);
3366
Willy Tarreau6471afb2011-09-23 10:54:59 +02003367 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003368 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003369 (((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 +02003370 != s->fe->except_to.s_addr) &&
3371 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003372 (((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 +02003373 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003374 int len;
3375 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003376 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003377
3378 /* Note: we rely on the backend to get the header name to be used for
3379 * x-original-to, because the header is really meant for the backends.
3380 * However, if the backend did not specify any option, we have to rely
3381 * on the frontend's header name.
3382 */
3383 if (s->be->orgto_hdr_len) {
3384 len = s->be->orgto_hdr_len;
3385 memcpy(trash, s->be->orgto_hdr_name, len);
3386 } else {
3387 len = s->fe->orgto_hdr_len;
3388 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003389 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003390 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3391
3392 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003393 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003394 goto return_bad_req;
3395 }
3396 }
3397 }
3398
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003399 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3400 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003401 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003402 unsigned int want_flags = 0;
3403
3404 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003405 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3406 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3407 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003408 want_flags |= TX_CON_CLO_SET;
3409 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003410 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3411 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3412 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003413 want_flags |= TX_CON_KAL_SET;
3414 }
3415
3416 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3417 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003418 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003419
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003420
Willy Tarreau522d6c02009-12-06 18:49:18 +01003421 /* If we have no server assigned yet and we're balancing on url_param
3422 * with a POST request, we may be interested in checking the body for
3423 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 */
3425 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3426 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003427 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003428 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003429 buffer_dont_connect(req);
3430 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003431 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003432
Willy Tarreau5e205522011-12-17 16:34:27 +01003433 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003434 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003435#ifdef TCP_QUICKACK
3436 /* We expect some data from the client. Unless we know for sure
3437 * we already have a full request, we have to re-enable quick-ack
3438 * in case we previously disabled it, otherwise we might cause
3439 * the client to delay further data.
3440 */
3441 if ((s->listener->options & LI_O_NOQUICKACK) &&
3442 ((txn->flags & TX_REQ_TE_CHNK) ||
3443 (msg->body_len > req->l - txn->req.eoh - 2)))
3444 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3445#endif
3446 }
Willy Tarreau03945942009-12-22 16:50:27 +01003447
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 /*************************************************************
3449 * OK, that's finished for the headers. We have done what we *
3450 * could. Let's switch to the DATA state. *
3451 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003452 req->analyse_exp = TICK_ETERNITY;
3453 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003454
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003456 /* OK let's go on with the BODY now */
3457 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003458
Willy Tarreau59234e92008-11-30 23:51:27 +01003459 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003460 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003461 /* we detected a parsing error. We want to archive this request
3462 * in the dedicated proxy area for later troubleshooting.
3463 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003464 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003465 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003466
Willy Tarreau59234e92008-11-30 23:51:27 +01003467 txn->req.msg_state = HTTP_MSG_ERROR;
3468 txn->status = 400;
3469 req->analysers = 0;
3470 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003471
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003472 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003473 if (s->listener->counters)
3474 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003475
Willy Tarreau59234e92008-11-30 23:51:27 +01003476 if (!(s->flags & SN_ERR_MASK))
3477 s->flags |= SN_ERR_PRXCOND;
3478 if (!(s->flags & SN_FINST_MASK))
3479 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003480 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003481}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003482
Willy Tarreau60b85b02008-11-30 23:28:40 +01003483/* This function is an analyser which processes the HTTP tarpit. It always
3484 * returns zero, at the beginning because it prevents any other processing
3485 * from occurring, and at the end because it terminates the request.
3486 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003487int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003488{
3489 struct http_txn *txn = &s->txn;
3490
3491 /* This connection is being tarpitted. The CLIENT side has
3492 * already set the connect expiration date to the right
3493 * timeout. We just have to check that the client is still
3494 * there and that the timeout has not expired.
3495 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003496 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003497 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3498 !tick_is_expired(req->analyse_exp, now_ms))
3499 return 0;
3500
3501 /* We will set the queue timer to the time spent, just for
3502 * logging purposes. We fake a 500 server error, so that the
3503 * attacker will not suspect his connection has been tarpitted.
3504 * It will not cause trouble to the logs because we can exclude
3505 * the tarpitted connections by filtering on the 'PT' status flags.
3506 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003507 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3508
3509 txn->status = 500;
3510 if (req->flags != BF_READ_ERROR)
3511 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3512
3513 req->analysers = 0;
3514 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003515
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003516 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003517 if (s->listener->counters)
3518 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003519
Willy Tarreau60b85b02008-11-30 23:28:40 +01003520 if (!(s->flags & SN_ERR_MASK))
3521 s->flags |= SN_ERR_PRXCOND;
3522 if (!(s->flags & SN_FINST_MASK))
3523 s->flags |= SN_FINST_T;
3524 return 0;
3525}
3526
Willy Tarreaud34af782008-11-30 23:36:37 +01003527/* This function is an analyser which processes the HTTP request body. It looks
3528 * for parameters to be used for the load balancing algorithm (url_param). It
3529 * must only be called after the standard HTTP request processing has occurred,
3530 * because it expects the request to be parsed. It returns zero if it needs to
3531 * read more data, or 1 once it has completed its analysis.
3532 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003533int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003534{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003535 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003536 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003537 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003538
3539 /* We have to parse the HTTP request body to find any required data.
3540 * "balance url_param check_post" should have been the only way to get
3541 * into this. We were brought here after HTTP header analysis, so all
3542 * related structures are ready.
3543 */
3544
Willy Tarreau522d6c02009-12-06 18:49:18 +01003545 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3546 goto missing_data;
3547
3548 if (msg->msg_state < HTTP_MSG_100_SENT) {
3549 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3550 * send an HTTP/1.1 100 Continue intermediate response.
3551 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003552 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553 struct hdr_ctx ctx;
3554 ctx.idx = 0;
3555 /* Expect is allowed in 1.1, look for it */
3556 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3557 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3558 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3559 }
3560 }
3561 msg->msg_state = HTTP_MSG_100_SENT;
3562 }
3563
3564 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003565 /* we have msg->col and msg->sov which both point to the first
3566 * byte of message body. msg->som still points to the beginning
3567 * of the message. We must save the body in req->lr because it
3568 * survives buffer re-alignments.
3569 */
3570 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003571 if (txn->flags & TX_REQ_TE_CHNK)
3572 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3573 else
3574 msg->msg_state = HTTP_MSG_DATA;
3575 }
3576
3577 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003578 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003579 * set ->sov and ->lr to point to the body and switch to DATA or
3580 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003581 */
3582 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003583
Willy Tarreau115acb92009-12-26 13:56:06 +01003584 if (!ret)
3585 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003586 else if (ret < 0) {
3587 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003588 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003589 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003590 }
3591
Willy Tarreaud98cf932009-12-27 22:54:55 +01003592 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003593 * We have the first non-header byte in msg->col, which is either the
3594 * beginning of the chunk size or of the data. The first data byte is in
3595 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3596 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003597 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003598
Willy Tarreau124d9912011-03-01 20:30:48 +01003599 if (msg->body_len < limit)
3600 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601
Willy Tarreau7c96f672009-12-27 22:47:25 +01003602 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603 goto http_end;
3604
3605 missing_data:
3606 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003607 if (req->flags & BF_FULL) {
3608 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003609 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003610 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003611
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3613 txn->status = 408;
3614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003615
3616 if (!(s->flags & SN_ERR_MASK))
3617 s->flags |= SN_ERR_CLITO;
3618 if (!(s->flags & SN_FINST_MASK))
3619 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003621 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622
3623 /* we get here if we need to wait for more data */
3624 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003625 /* Not enough data. We'll re-use the http-request
3626 * timeout here. Ideally, we should set the timeout
3627 * relative to the accept() date. We just set the
3628 * request timeout once at the beginning of the
3629 * request.
3630 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003631 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003632 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003633 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003634 return 0;
3635 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636
3637 http_end:
3638 /* The situation will not evolve, so let's give up on the analysis. */
3639 s->logs.tv_request = now; /* update the request timer to reflect full request */
3640 req->analysers &= ~an_bit;
3641 req->analyse_exp = TICK_ETERNITY;
3642 return 1;
3643
3644 return_bad_req: /* let's centralize all bad requests */
3645 txn->req.msg_state = HTTP_MSG_ERROR;
3646 txn->status = 400;
3647 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3648
Willy Tarreau79ebac62010-06-07 13:47:49 +02003649 if (!(s->flags & SN_ERR_MASK))
3650 s->flags |= SN_ERR_PRXCOND;
3651 if (!(s->flags & SN_FINST_MASK))
3652 s->flags |= SN_FINST_R;
3653
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654 return_err_msg:
3655 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003656 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657 if (s->listener->counters)
3658 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003659 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003660}
3661
Mark Lamourinec2247f02012-01-04 13:02:01 -05003662int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3663
3664 struct hdr_ctx ctx;
3665
Mark Lamourinec2247f02012-01-04 13:02:01 -05003666 char *hdr_name = be->server_id_hdr_name;
3667 int hdr_name_len = be->server_id_hdr_len;
3668
3669 char *hdr_val;
3670
William Lallemandd9e90662012-01-30 17:27:17 +01003671 ctx.idx = 0;
3672
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3674 /* remove any existing values from the header */
3675 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3676 }
3677
3678 /* Add the new header requested with the server value */
3679 hdr_val = trash;
3680 memcpy(hdr_val, hdr_name, hdr_name_len);
3681 hdr_val += hdr_name_len;
3682 *hdr_val++ = ':';
3683 *hdr_val++ = ' ';
3684 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3685 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3686
3687 return 0;
3688}
3689
Willy Tarreau610ecce2010-01-04 21:15:02 +01003690/* Terminate current transaction and prepare a new one. This is very tricky
3691 * right now but it works.
3692 */
3693void http_end_txn_clean_session(struct session *s)
3694{
3695 /* FIXME: We need a more portable way of releasing a backend's and a
3696 * server's connections. We need a safer way to reinitialize buffer
3697 * flags. We also need a more accurate method for computing per-request
3698 * data.
3699 */
3700 http_silent_debug(__LINE__, s);
3701
3702 s->req->cons->flags |= SI_FL_NOLINGER;
3703 s->req->cons->shutr(s->req->cons);
3704 s->req->cons->shutw(s->req->cons);
3705
3706 http_silent_debug(__LINE__, s);
3707
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003708 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003710 if (unlikely(s->srv_conn))
3711 sess_change_server(s, NULL);
3712 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003713
3714 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3715 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003716 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717
3718 if (s->txn.status) {
3719 int n;
3720
3721 n = s->txn.status / 100;
3722 if (n < 1 || n > 5)
3723 n = 0;
3724
3725 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003726 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
Willy Tarreau24657792010-02-26 10:30:28 +01003728 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003730 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 }
3732
3733 /* don't count other requests' data */
3734 s->logs.bytes_in -= s->req->l - s->req->send_max;
3735 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3736
3737 /* let's do a final log if we need it */
3738 if (s->logs.logwait &&
3739 !(s->flags & SN_MONITOR) &&
3740 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3741 s->do_log(s);
3742 }
3743
3744 s->logs.accept_date = date; /* user-visible date for logging */
3745 s->logs.tv_accept = now; /* corrected date for internal use */
3746 tv_zero(&s->logs.tv_request);
3747 s->logs.t_queue = -1;
3748 s->logs.t_connect = -1;
3749 s->logs.t_data = -1;
3750 s->logs.t_close = 0;
3751 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3752 s->logs.srv_queue_size = 0; /* we will get this number soon */
3753
3754 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3755 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3756
3757 if (s->pend_pos)
3758 pendconn_free(s->pend_pos);
3759
Willy Tarreau827aee92011-03-10 16:55:02 +01003760 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761 if (s->flags & SN_CURR_SESS) {
3762 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003763 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003765 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3766 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 }
3768
Willy Tarreau9e000c62011-03-10 14:03:36 +01003769 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770
3771 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3772 s->req->cons->fd = -1; /* just to help with debugging */
3773 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003774 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775 s->req->cons->err_loc = NULL;
3776 s->req->cons->exp = TICK_ETERNITY;
3777 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003778 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3779 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 +02003780 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 +01003781 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3782 s->txn.meth = 0;
3783 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003784 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003785 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 s->req->cons->flags |= SI_FL_INDEP_STR;
3787
Willy Tarreau96e31212011-05-30 18:10:30 +02003788 if (s->fe->options2 & PR_O2_NODELAY) {
3789 s->req->flags |= BF_NEVER_WAIT;
3790 s->rep->flags |= BF_NEVER_WAIT;
3791 }
3792
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 /* if the request buffer is not empty, it means we're
3794 * about to process another request, so send pending
3795 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003796 * Just don't do this if the buffer is close to be full,
3797 * because the request will wait for it to flush a little
3798 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003800 if (s->req->l > s->req->send_max) {
3801 if (s->rep->send_max &&
3802 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003803 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3804 s->rep->flags |= BF_EXPECT_MORE;
3805 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003806
3807 /* we're removing the analysers, we MUST re-enable events detection */
3808 buffer_auto_read(s->req);
3809 buffer_auto_close(s->req);
3810 buffer_auto_read(s->rep);
3811 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812
3813 /* make ->lr point to the first non-forwarded byte */
3814 s->req->lr = s->req->w + s->req->send_max;
3815 if (s->req->lr >= s->req->data + s->req->size)
3816 s->req->lr -= s->req->size;
3817 s->rep->lr = s->rep->w + s->rep->send_max;
3818 if (s->rep->lr >= s->rep->data + s->rep->size)
3819 s->rep->lr -= s->req->size;
3820
Willy Tarreau342b11c2010-11-24 16:22:09 +01003821 s->req->analysers = s->listener->analysers;
3822 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823 s->rep->analysers = 0;
3824
3825 http_silent_debug(__LINE__, s);
3826}
3827
3828
3829/* This function updates the request state machine according to the response
3830 * state machine and buffer flags. It returns 1 if it changes anything (flag
3831 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3832 * it is only used to find when a request/response couple is complete. Both
3833 * this function and its equivalent should loop until both return zero. It
3834 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3835 */
3836int http_sync_req_state(struct session *s)
3837{
3838 struct buffer *buf = s->req;
3839 struct http_txn *txn = &s->txn;
3840 unsigned int old_flags = buf->flags;
3841 unsigned int old_state = txn->req.msg_state;
3842
3843 http_silent_debug(__LINE__, s);
3844 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3845 return 0;
3846
3847 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003848 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003849 * We can shut the read side unless we want to abort_on_close,
3850 * or we have a POST request. The issue with POST requests is
3851 * that some browsers still send a CRLF after the request, and
3852 * this CRLF must be read so that it does not remain in the kernel
3853 * buffers, otherwise a close could cause an RST on some systems
3854 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003855 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003856 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003857 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858
3859 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3860 goto wait_other_side;
3861
3862 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3863 /* The server has not finished to respond, so we
3864 * don't want to move in order not to upset it.
3865 */
3866 goto wait_other_side;
3867 }
3868
3869 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3870 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003871 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003872 txn->req.msg_state = HTTP_MSG_TUNNEL;
3873 goto wait_other_side;
3874 }
3875
3876 /* When we get here, it means that both the request and the
3877 * response have finished receiving. Depending on the connection
3878 * mode, we'll have to wait for the last bytes to leave in either
3879 * direction, and sometimes for a close to be effective.
3880 */
3881
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003882 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3883 /* Server-close mode : queue a connection close to the server */
3884 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003886 }
3887 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3888 /* Option forceclose is set, or either side wants to close,
3889 * let's enforce it now that we're not expecting any new
3890 * data to come. The caller knows the session is complete
3891 * once both states are CLOSED.
3892 */
3893 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894 buffer_shutr_now(buf);
3895 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003897 }
3898 else {
3899 /* The last possible modes are keep-alive and tunnel. Since tunnel
3900 * mode does not set the body analyser, we can't reach this place
3901 * in tunnel mode, so we're left with keep-alive only.
3902 * This mode is currently not implemented, we switch to tunnel mode.
3903 */
3904 buffer_auto_read(buf);
3905 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906 }
3907
3908 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3909 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003910 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3911
Willy Tarreau610ecce2010-01-04 21:15:02 +01003912 if (!(buf->flags & BF_OUT_EMPTY)) {
3913 txn->req.msg_state = HTTP_MSG_CLOSING;
3914 goto http_msg_closing;
3915 }
3916 else {
3917 txn->req.msg_state = HTTP_MSG_CLOSED;
3918 goto http_msg_closed;
3919 }
3920 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003921 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922 }
3923
3924 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3925 http_msg_closing:
3926 /* nothing else to forward, just waiting for the output buffer
3927 * to be empty and for the shutw_now to take effect.
3928 */
3929 if (buf->flags & BF_OUT_EMPTY) {
3930 txn->req.msg_state = HTTP_MSG_CLOSED;
3931 goto http_msg_closed;
3932 }
3933 else if (buf->flags & BF_SHUTW) {
3934 txn->req.msg_state = HTTP_MSG_ERROR;
3935 goto wait_other_side;
3936 }
3937 }
3938
3939 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3940 http_msg_closed:
3941 goto wait_other_side;
3942 }
3943
3944 wait_other_side:
3945 http_silent_debug(__LINE__, s);
3946 return txn->req.msg_state != old_state || buf->flags != old_flags;
3947}
3948
3949
3950/* This function updates the response state machine according to the request
3951 * state machine and buffer flags. It returns 1 if it changes anything (flag
3952 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3953 * it is only used to find when a request/response couple is complete. Both
3954 * this function and its equivalent should loop until both return zero. It
3955 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3956 */
3957int http_sync_res_state(struct session *s)
3958{
3959 struct buffer *buf = s->rep;
3960 struct http_txn *txn = &s->txn;
3961 unsigned int old_flags = buf->flags;
3962 unsigned int old_state = txn->rsp.msg_state;
3963
3964 http_silent_debug(__LINE__, s);
3965 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3966 return 0;
3967
3968 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3969 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003970 * still monitor the server connection for a possible close
3971 * while the request is being uploaded, so we don't disable
3972 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003974 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975
3976 if (txn->req.msg_state == HTTP_MSG_ERROR)
3977 goto wait_other_side;
3978
3979 if (txn->req.msg_state < HTTP_MSG_DONE) {
3980 /* The client seems to still be sending data, probably
3981 * because we got an error response during an upload.
3982 * We have the choice of either breaking the connection
3983 * or letting it pass through. Let's do the later.
3984 */
3985 goto wait_other_side;
3986 }
3987
3988 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3989 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003990 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3992 goto wait_other_side;
3993 }
3994
3995 /* When we get here, it means that both the request and the
3996 * response have finished receiving. Depending on the connection
3997 * mode, we'll have to wait for the last bytes to leave in either
3998 * direction, and sometimes for a close to be effective.
3999 */
4000
4001 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4002 /* Server-close mode : shut read and wait for the request
4003 * side to close its output buffer. The caller will detect
4004 * when we're in DONE and the other is in CLOSED and will
4005 * catch that for the final cleanup.
4006 */
4007 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4008 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004010 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4011 /* Option forceclose is set, or either side wants to close,
4012 * let's enforce it now that we're not expecting any new
4013 * data to come. The caller knows the session is complete
4014 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004016 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4017 buffer_shutr_now(buf);
4018 buffer_shutw_now(buf);
4019 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 }
4021 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004022 /* The last possible modes are keep-alive and tunnel. Since tunnel
4023 * mode does not set the body analyser, we can't reach this place
4024 * in tunnel mode, so we're left with keep-alive only.
4025 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004027 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 }
4030
4031 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4032 /* if we've just closed an output, let's switch */
4033 if (!(buf->flags & BF_OUT_EMPTY)) {
4034 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4035 goto http_msg_closing;
4036 }
4037 else {
4038 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4039 goto http_msg_closed;
4040 }
4041 }
4042 goto wait_other_side;
4043 }
4044
4045 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4046 http_msg_closing:
4047 /* nothing else to forward, just waiting for the output buffer
4048 * to be empty and for the shutw_now to take effect.
4049 */
4050 if (buf->flags & BF_OUT_EMPTY) {
4051 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4052 goto http_msg_closed;
4053 }
4054 else if (buf->flags & BF_SHUTW) {
4055 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004056 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004057 if (target_srv(&s->target))
4058 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 goto wait_other_side;
4060 }
4061 }
4062
4063 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4064 http_msg_closed:
4065 /* drop any pending data */
4066 buffer_ignore(buf, buf->l - buf->send_max);
4067 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004068 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 goto wait_other_side;
4070 }
4071
4072 wait_other_side:
4073 http_silent_debug(__LINE__, s);
4074 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4075}
4076
4077
4078/* Resync the request and response state machines. Return 1 if either state
4079 * changes.
4080 */
4081int http_resync_states(struct session *s)
4082{
4083 struct http_txn *txn = &s->txn;
4084 int old_req_state = txn->req.msg_state;
4085 int old_res_state = txn->rsp.msg_state;
4086
4087 http_silent_debug(__LINE__, s);
4088 http_sync_req_state(s);
4089 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004090 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 if (!http_sync_res_state(s))
4092 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004093 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004094 if (!http_sync_req_state(s))
4095 break;
4096 }
4097 http_silent_debug(__LINE__, s);
4098 /* OK, both state machines agree on a compatible state.
4099 * There are a few cases we're interested in :
4100 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4101 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4102 * directions, so let's simply disable both analysers.
4103 * - HTTP_MSG_CLOSED on the response only means we must abort the
4104 * request.
4105 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4106 * with server-close mode means we've completed one request and we
4107 * must re-initialize the server connection.
4108 */
4109
4110 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4111 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4112 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4113 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4114 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004115 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004116 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004118 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004119 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004121 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4122 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004123 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004124 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004125 s->rep->analysers = 0;
4126 buffer_auto_close(s->rep);
4127 buffer_auto_read(s->rep);
4128 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 buffer_abort(s->req);
4130 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004131 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 }
4134 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4135 txn->rsp.msg_state == HTTP_MSG_DONE &&
4136 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4137 /* server-close: terminate this server connection and
4138 * reinitialize a fresh-new transaction.
4139 */
4140 http_end_txn_clean_session(s);
4141 }
4142
4143 http_silent_debug(__LINE__, s);
4144 return txn->req.msg_state != old_req_state ||
4145 txn->rsp.msg_state != old_res_state;
4146}
4147
Willy Tarreaud98cf932009-12-27 22:54:55 +01004148/* This function is an analyser which forwards request body (including chunk
4149 * sizes if any). It is called as soon as we must forward, even if we forward
4150 * zero byte. The only situation where it must not be called is when we're in
4151 * tunnel mode and we want to forward till the close. It's used both to forward
4152 * remaining data and to resync after end of body. It expects the msg_state to
4153 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4154 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004155 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004156 * bytes of pending data + the headers if not already done (between som and sov).
4157 * It eventually adjusts som to match sov after the data in between have been sent.
4158 */
4159int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4160{
4161 struct http_txn *txn = &s->txn;
4162 struct http_msg *msg = &s->txn.req;
4163
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004164 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4165 return 0;
4166
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004167 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4168 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004169 /* Output closed while we were sending data. We must abort and
4170 * wake the other side up.
4171 */
4172 msg->msg_state = HTTP_MSG_ERROR;
4173 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004174 return 1;
4175 }
4176
Willy Tarreau4fe41902010-06-07 22:27:41 +02004177 /* in most states, we should abort in case of early close */
4178 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004179
4180 /* Note that we don't have to send 100-continue back because we don't
4181 * need the data to complete our job, and it's up to the server to
4182 * decide whether to return 100, 417 or anything else in return of
4183 * an "Expect: 100-continue" header.
4184 */
4185
4186 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4187 /* we have msg->col and msg->sov which both point to the first
4188 * byte of message body. msg->som still points to the beginning
4189 * of the message. We must save the body in req->lr because it
4190 * survives buffer re-alignments.
4191 */
4192 req->lr = req->data + msg->sov;
4193 if (txn->flags & TX_REQ_TE_CHNK)
4194 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4195 else {
4196 msg->msg_state = HTTP_MSG_DATA;
4197 }
4198 }
4199
Willy Tarreaud98cf932009-12-27 22:54:55 +01004200 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004201 int bytes;
4202
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004204 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004205 bytes = msg->sov - msg->som;
4206 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004207 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004208 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4209 bytes += req->size;
4210 msg->chunk_len += (unsigned int)bytes;
4211 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004212 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004213
Willy Tarreaucaabe412010-01-03 23:08:28 +01004214 if (msg->msg_state == HTTP_MSG_DATA) {
4215 /* must still forward */
4216 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004217 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004218
4219 /* nothing left to forward */
4220 if (txn->flags & TX_REQ_TE_CHNK)
4221 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004223 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004224 }
4225 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004226 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004227 * set ->sov and ->lr to point to the body and switch to DATA or
4228 * TRAILERS state.
4229 */
4230 int ret = http_parse_chunk_size(req, msg);
4231
4232 if (!ret)
4233 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004234 else if (ret < 0) {
4235 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004236 if (msg->err_pos >= 0)
4237 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004239 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004240 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004242 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4243 /* we want the CRLF after the data */
4244 int ret;
4245
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004246 req->lr = req->w + req->send_max;
4247 if (req->lr >= req->data + req->size)
4248 req->lr -= req->size;
4249
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250 ret = http_skip_chunk_crlf(req, msg);
4251
4252 if (ret == 0)
4253 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004254 else if (ret < 0) {
4255 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004256 if (msg->err_pos >= 0)
4257 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004259 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004260 /* we're in MSG_CHUNK_SIZE now */
4261 }
4262 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4263 int ret = http_forward_trailers(req, msg);
4264
4265 if (ret == 0)
4266 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004267 else if (ret < 0) {
4268 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004269 if (msg->err_pos >= 0)
4270 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004271 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004272 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004273 /* we're in HTTP_MSG_DONE now */
4274 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004276 int old_state = msg->msg_state;
4277
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004279 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004280 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4281 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4282 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004283 if (http_resync_states(s)) {
4284 /* some state changes occurred, maybe the analyser
4285 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004286 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004287 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4288 if (req->flags & BF_SHUTW) {
4289 /* request errors are most likely due to
4290 * the server aborting the transfer.
4291 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004292 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004293 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004294 if (msg->err_pos >= 0)
4295 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004297 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004299 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004300
4301 /* If "option abortonclose" is set on the backend, we
4302 * want to monitor the client's connection and forward
4303 * any shutdown notification to the server, which will
4304 * decide whether to close or to go on processing the
4305 * request.
4306 */
4307 if (s->be->options & PR_O_ABRT_CLOSE) {
4308 buffer_auto_read(req);
4309 buffer_auto_close(req);
4310 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004311 else if (s->txn.meth == HTTP_METH_POST) {
4312 /* POST requests may require to read extra CRLF
4313 * sent by broken browsers and which could cause
4314 * an RST to be sent upon close on some systems
4315 * (eg: Linux).
4316 */
4317 buffer_auto_read(req);
4318 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004319
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004321 }
4322 }
4323
Willy Tarreaud98cf932009-12-27 22:54:55 +01004324 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004325 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004326 if (req->flags & BF_SHUTR) {
4327 if (!(s->flags & SN_ERR_MASK))
4328 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004329 if (!(s->flags & SN_FINST_MASK)) {
4330 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4331 s->flags |= SN_FINST_H;
4332 else
4333 s->flags |= SN_FINST_D;
4334 }
4335
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004336 s->fe->fe_counters.cli_aborts++;
4337 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004338 if (target_srv(&s->target))
4339 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004340
4341 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004342 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004343
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004344 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004345 if (req->flags & BF_SHUTW)
4346 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004348 /* When TE: chunked is used, we need to get there again to parse remaining
4349 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4350 */
4351 if (txn->flags & TX_REQ_TE_CHNK)
4352 buffer_dont_close(req);
4353
Willy Tarreau5c620922011-05-11 19:56:11 +02004354 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004355 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4356 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004357 * modes are already handled by the stream sock layer. We must not do
4358 * this in content-length mode because it could present the MSG_MORE
4359 * flag with the last block of forwarded data, which would cause an
4360 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004361 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004362 if (txn->flags & TX_REQ_TE_CHNK)
4363 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004364
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004366 return 0;
4367
Willy Tarreaud98cf932009-12-27 22:54:55 +01004368 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004369 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004370 if (s->listener->counters)
4371 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004372 return_bad_req_stats_ok:
4373 txn->req.msg_state = HTTP_MSG_ERROR;
4374 if (txn->status) {
4375 /* Note: we don't send any error if some data were already sent */
4376 stream_int_retnclose(req->prod, NULL);
4377 } else {
4378 txn->status = 400;
4379 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4380 }
4381 req->analysers = 0;
4382 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004383
4384 if (!(s->flags & SN_ERR_MASK))
4385 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004386 if (!(s->flags & SN_FINST_MASK)) {
4387 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4388 s->flags |= SN_FINST_H;
4389 else
4390 s->flags |= SN_FINST_D;
4391 }
4392 return 0;
4393
4394 aborted_xfer:
4395 txn->req.msg_state = HTTP_MSG_ERROR;
4396 if (txn->status) {
4397 /* Note: we don't send any error if some data were already sent */
4398 stream_int_retnclose(req->prod, NULL);
4399 } else {
4400 txn->status = 502;
4401 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4402 }
4403 req->analysers = 0;
4404 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4405
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004406 s->fe->fe_counters.srv_aborts++;
4407 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004408 if (target_srv(&s->target))
4409 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004410
4411 if (!(s->flags & SN_ERR_MASK))
4412 s->flags |= SN_ERR_SRVCL;
4413 if (!(s->flags & SN_FINST_MASK)) {
4414 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4415 s->flags |= SN_FINST_H;
4416 else
4417 s->flags |= SN_FINST_D;
4418 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004419 return 0;
4420}
4421
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004422/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4423 * processing can continue on next analysers, or zero if it either needs more
4424 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4425 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4426 * when it has nothing left to do, and may remove any analyser when it wants to
4427 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004428 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004430{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004431 struct http_txn *txn = &s->txn;
4432 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004433 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004434 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004436 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004437
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004438 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 +02004439 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004441 rep,
4442 rep->rex, rep->wex,
4443 rep->flags,
4444 rep->l,
4445 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004446
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 /*
4448 * Now parse the partial (or complete) lines.
4449 * We will check the response syntax, and also join multi-line
4450 * headers. An index of all the lines will be elaborated while
4451 * parsing.
4452 *
4453 * For the parsing, we use a 28 states FSM.
4454 *
4455 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004456 * rep->data + msg->som = beginning of response
4457 * rep->data + msg->eoh = end of processed headers / start of current one
4458 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 * rep->lr = first non-visited byte
4460 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004461 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004462 */
4463
Willy Tarreau83e3af02009-12-28 17:39:57 +01004464 /* There's a protected area at the end of the buffer for rewriting
4465 * purposes. We don't want to start to parse the request if the
4466 * protected area is affected, because we may have to move processed
4467 * data later, which is much more complicated.
4468 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004469 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4470 if (unlikely((rep->flags & BF_FULL) ||
4471 rep->r < rep->lr ||
4472 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4473 if (rep->send_max) {
4474 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004475 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4476 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004477 buffer_dont_close(rep);
4478 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4479 return 0;
4480 }
4481 if (rep->l <= rep->size - global.tune.maxrewrite)
4482 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004483 }
4484
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004485 if (likely(rep->lr < rep->r))
4486 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004487 }
4488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 /* 1: we might have to print this header in debug mode */
4490 if (unlikely((global.mode & MODE_DEBUG) &&
4491 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004492 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004493 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004495
Willy Tarreau663308b2010-06-07 14:06:08 +02004496 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004497 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500 sol += hdr_idx_first_pos(&txn->hdr_idx);
4501 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004502
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503 while (cur_idx) {
4504 eol = sol + txn->hdr_idx.v[cur_idx].len;
4505 debug_hdr("srvhdr", s, sol, eol);
4506 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4507 cur_idx = txn->hdr_idx.v[cur_idx].next;
4508 }
4509 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004510
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 /*
4512 * Now we quickly check if we have found a full valid response.
4513 * If not so, we check the FD and buffer states before leaving.
4514 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004515 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 * responses are checked first.
4517 *
4518 * Depending on whether the client is still there or not, we
4519 * may send an error response back or not. Note that normally
4520 * we should only check for HTTP status there, and check I/O
4521 * errors somewhere else.
4522 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004523
Willy Tarreau655dce92009-11-08 13:10:58 +01004524 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004525 /* Invalid response */
4526 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4527 /* we detected a parsing error. We want to archive this response
4528 * in the dedicated proxy area for later troubleshooting.
4529 */
4530 hdr_response_bad:
4531 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004532 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004533
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004534 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004535 if (target_srv(&s->target)) {
4536 target_srv(&s->target)->counters.failed_resp++;
4537 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004538 }
Willy Tarreau64648412010-03-05 10:41:54 +01004539 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004540 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 rep->analysers = 0;
4542 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004543 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004544 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4546
4547 if (!(s->flags & SN_ERR_MASK))
4548 s->flags |= SN_ERR_PRXCOND;
4549 if (!(s->flags & SN_FINST_MASK))
4550 s->flags |= SN_FINST_H;
4551
4552 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004553 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004554
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 /* too large response does not fit in buffer. */
4556 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004557 if (msg->err_pos < 0)
4558 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004559 goto hdr_response_bad;
4560 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004561
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 /* read error */
4563 else if (rep->flags & BF_READ_ERROR) {
4564 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004565 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004566
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004567 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004568 if (target_srv(&s->target)) {
4569 target_srv(&s->target)->counters.failed_resp++;
4570 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004571 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004572
Willy Tarreau90deb182010-01-07 00:20:41 +01004573 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 rep->analysers = 0;
4575 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004576 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004577 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004578 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004579
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580 if (!(s->flags & SN_ERR_MASK))
4581 s->flags |= SN_ERR_SRVCL;
4582 if (!(s->flags & SN_FINST_MASK))
4583 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004584 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004587 /* read timeout : return a 504 to the client. */
4588 else if (rep->flags & BF_READ_TIMEOUT) {
4589 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004590 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004591
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004592 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004593 if (target_srv(&s->target)) {
4594 target_srv(&s->target)->counters.failed_resp++;
4595 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004596 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004597
Willy Tarreau90deb182010-01-07 00:20:41 +01004598 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 rep->analysers = 0;
4600 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004601 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004602 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004603 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004604
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004605 if (!(s->flags & SN_ERR_MASK))
4606 s->flags |= SN_ERR_SRVTO;
4607 if (!(s->flags & SN_FINST_MASK))
4608 s->flags |= SN_FINST_H;
4609 return 0;
4610 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004611
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004612 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004614 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004615 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004616
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004617 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004618 if (target_srv(&s->target)) {
4619 target_srv(&s->target)->counters.failed_resp++;
4620 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004621 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004622
Willy Tarreau90deb182010-01-07 00:20:41 +01004623 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 rep->analysers = 0;
4625 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004626 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004627 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004629
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004630 if (!(s->flags & SN_ERR_MASK))
4631 s->flags |= SN_ERR_SRVCL;
4632 if (!(s->flags & SN_FINST_MASK))
4633 s->flags |= SN_FINST_H;
4634 return 0;
4635 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 /* write error to client (we don't send any message then) */
4638 else if (rep->flags & BF_WRITE_ERROR) {
4639 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004640 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004641
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004642 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004644 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645
4646 if (!(s->flags & SN_ERR_MASK))
4647 s->flags |= SN_ERR_CLICL;
4648 if (!(s->flags & SN_FINST_MASK))
4649 s->flags |= SN_FINST_H;
4650
4651 /* process_session() will take care of the error */
4652 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655 buffer_dont_close(rep);
4656 return 0;
4657 }
4658
4659 /* More interesting part now : we know that we have a complete
4660 * response which at least looks like HTTP. We have an indicator
4661 * of each header's length, so we can parse them quickly.
4662 */
4663
4664 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004665 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667 /*
4668 * 1: get the status code
4669 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004670 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671 if (n < 1 || n > 5)
4672 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004673 /* when the client triggers a 4xx from the server, it's most often due
4674 * to a missing object or permission. These events should be tracked
4675 * because if they happen often, it may indicate a brute force or a
4676 * vulnerability scan.
4677 */
4678 if (n == 4)
4679 session_inc_http_err_ctr(s);
4680
Willy Tarreau827aee92011-03-10 16:55:02 +01004681 if (target_srv(&s->target))
4682 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683
Willy Tarreau5b154472009-12-21 20:11:07 +01004684 /* check if the response is HTTP/1.1 or above */
4685 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004686 ((msg->sol[5] > '1') ||
4687 ((msg->sol[5] == '1') &&
4688 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004689 txn->flags |= TX_RES_VER_11;
4690
4691 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004692 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 +01004693
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004694 /* transfer length unknown*/
4695 txn->flags &= ~TX_RES_XFER_LEN;
4696
Willy Tarreau962c3f42010-01-10 00:15:35 +01004697 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004698
Willy Tarreau39650402010-03-15 19:44:39 +01004699 /* Adjust server's health based on status code. Note: status codes 501
4700 * and 505 are triggered on demand by client request, so we must not
4701 * count them as server failures.
4702 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004703 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004704 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004705 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004706 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004707 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004708 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004709
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004710 /*
4711 * 2: check for cacheability.
4712 */
4713
4714 switch (txn->status) {
4715 case 200:
4716 case 203:
4717 case 206:
4718 case 300:
4719 case 301:
4720 case 410:
4721 /* RFC2616 @13.4:
4722 * "A response received with a status code of
4723 * 200, 203, 206, 300, 301 or 410 MAY be stored
4724 * by a cache (...) unless a cache-control
4725 * directive prohibits caching."
4726 *
4727 * RFC2616 @9.5: POST method :
4728 * "Responses to this method are not cacheable,
4729 * unless the response includes appropriate
4730 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004731 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004732 if (likely(txn->meth != HTTP_METH_POST) &&
4733 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4734 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4735 break;
4736 default:
4737 break;
4738 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004739
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740 /*
4741 * 3: we may need to capture headers
4742 */
4743 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004744 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004745 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 txn->rsp.cap, s->fe->rsp_cap);
4747
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004748 /* 4: determine the transfer-length.
4749 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4750 * the presence of a message-body in a RESPONSE and its transfer length
4751 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004752 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004753 * All responses to the HEAD request method MUST NOT include a
4754 * message-body, even though the presence of entity-header fields
4755 * might lead one to believe they do. All 1xx (informational), 204
4756 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4757 * message-body. All other responses do include a message-body,
4758 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004759 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004760 * 1. Any response which "MUST NOT" include a message-body (such as the
4761 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4762 * always terminated by the first empty line after the header fields,
4763 * regardless of the entity-header fields present in the message.
4764 *
4765 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4766 * the "chunked" transfer-coding (Section 6.2) is used, the
4767 * transfer-length is defined by the use of this transfer-coding.
4768 * If a Transfer-Encoding header field is present and the "chunked"
4769 * transfer-coding is not present, the transfer-length is defined by
4770 * the sender closing the connection.
4771 *
4772 * 3. If a Content-Length header field is present, its decimal value in
4773 * OCTETs represents both the entity-length and the transfer-length.
4774 * If a message is received with both a Transfer-Encoding header
4775 * field and a Content-Length header field, the latter MUST be ignored.
4776 *
4777 * 4. If the message uses the media type "multipart/byteranges", and
4778 * the transfer-length is not otherwise specified, then this self-
4779 * delimiting media type defines the transfer-length. This media
4780 * type MUST NOT be used unless the sender knows that the recipient
4781 * can parse it; the presence in a request of a Range header with
4782 * multiple byte-range specifiers from a 1.1 client implies that the
4783 * client can parse multipart/byteranges responses.
4784 *
4785 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004786 */
4787
4788 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004789 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004790 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004791 * FIXME: should we parse anyway and return an error on chunked encoding ?
4792 */
4793 if (txn->meth == HTTP_METH_HEAD ||
4794 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004795 txn->status == 204 || txn->status == 304) {
4796 txn->flags |= TX_RES_XFER_LEN;
4797 goto skip_content_length;
4798 }
4799
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004800 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004801 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004802 while ((txn->flags & TX_RES_VER_11) &&
4803 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004804 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4805 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4806 else if (txn->flags & TX_RES_TE_CHNK) {
4807 /* bad transfer-encoding (chunked followed by something else) */
4808 use_close_only = 1;
4809 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4810 break;
4811 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 }
4813
4814 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4815 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004816 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4818 signed long long cl;
4819
Willy Tarreauad14f752011-09-02 20:33:27 +02004820 if (!ctx.vlen) {
4821 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004823 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004824
Willy Tarreauad14f752011-09-02 20:33:27 +02004825 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4826 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004827 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004828 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004829
Willy Tarreauad14f752011-09-02 20:33:27 +02004830 if (cl < 0) {
4831 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004832 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004833 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004834
Willy Tarreauad14f752011-09-02 20:33:27 +02004835 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
4836 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004837 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004838 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004839
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004840 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004841 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004842 }
4843
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004844 /* FIXME: we should also implement the multipart/byterange method.
4845 * For now on, we resort to close mode in this case (unknown length).
4846 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004847skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004848
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004849 /* end of job, return OK */
4850 rep->analysers &= ~an_bit;
4851 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004852 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004853 return 1;
4854}
4855
4856/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004857 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4858 * and updates t->rep->analysers. It might make sense to explode it into several
4859 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004860 */
4861int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4862{
4863 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 struct http_msg *msg = &txn->rsp;
4865 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004866 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867
4868 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4869 now_ms, __FUNCTION__,
4870 t,
4871 rep,
4872 rep->rex, rep->wex,
4873 rep->flags,
4874 rep->l,
4875 rep->analysers);
4876
Willy Tarreau655dce92009-11-08 13:10:58 +01004877 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004878 return 0;
4879
4880 rep->analysers &= ~an_bit;
4881 rep->analyse_exp = TICK_ETERNITY;
4882
Willy Tarreau5b154472009-12-21 20:11:07 +01004883 /* Now we have to check if we need to modify the Connection header.
4884 * This is more difficult on the response than it is on the request,
4885 * because we can have two different HTTP versions and we don't know
4886 * how the client will interprete a response. For instance, let's say
4887 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4888 * HTTP/1.1 response without any header. Maybe it will bound itself to
4889 * HTTP/1.0 because it only knows about it, and will consider the lack
4890 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4891 * the lack of header as a keep-alive. Thus we will use two flags
4892 * indicating how a request MAY be understood by the client. In case
4893 * of multiple possibilities, we'll fix the header to be explicit. If
4894 * ambiguous cases such as both close and keepalive are seen, then we
4895 * will fall back to explicit close. Note that we won't take risks with
4896 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004897 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004898 */
4899
Willy Tarreaudc008c52010-02-01 16:20:08 +01004900 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4901 txn->status == 101)) {
4902 /* Either we've established an explicit tunnel, or we're
4903 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004904 * to understand the next protocols. We have to switch to tunnel
4905 * mode, so that we transfer the request and responses then let
4906 * this protocol pass unmodified. When we later implement specific
4907 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004908 * header which contains information about that protocol for
4909 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004910 */
4911 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4912 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004913 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4914 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4915 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004916 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004917
Willy Tarreau60466522010-01-18 19:08:45 +01004918 /* on unknown transfer length, we must close */
4919 if (!(txn->flags & TX_RES_XFER_LEN) &&
4920 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4921 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004922
Willy Tarreau60466522010-01-18 19:08:45 +01004923 /* now adjust header transformations depending on current state */
4924 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4925 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4926 to_del |= 2; /* remove "keep-alive" on any response */
4927 if (!(txn->flags & TX_RES_VER_11))
4928 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004929 }
Willy Tarreau60466522010-01-18 19:08:45 +01004930 else { /* SCL / KAL */
4931 to_del |= 1; /* remove "close" on any response */
4932 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4933 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004934 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004935
Willy Tarreau60466522010-01-18 19:08:45 +01004936 /* Parse and remove some headers from the connection header */
4937 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004938
Willy Tarreau60466522010-01-18 19:08:45 +01004939 /* Some keep-alive responses are converted to Server-close if
4940 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004941 */
Willy Tarreau60466522010-01-18 19:08:45 +01004942 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4943 if ((txn->flags & TX_HDR_CONN_CLO) ||
4944 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4945 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004946 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004947 }
4948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004949 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004950 /*
4951 * 3: we will have to evaluate the filters.
4952 * As opposed to version 1.2, now they will be evaluated in the
4953 * filters order and not in the header order. This means that
4954 * each filter has to be validated among all headers.
4955 *
4956 * Filters are tried with ->be first, then with ->fe if it is
4957 * different from ->be.
4958 */
4959
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004960 cur_proxy = t->be;
4961 while (1) {
4962 struct proxy *rule_set = cur_proxy;
4963
4964 /* try headers filters */
4965 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004966 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004967 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004968 if (target_srv(&t->target)) {
4969 target_srv(&t->target)->counters.failed_resp++;
4970 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004971 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004972 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004974 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004976 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004977 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004978 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979 if (!(t->flags & SN_ERR_MASK))
4980 t->flags |= SN_ERR_PRXCOND;
4981 if (!(t->flags & SN_FINST_MASK))
4982 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004983 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004984 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004985 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004986
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004987 /* has the response been denied ? */
4988 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004989 if (target_srv(&t->target))
4990 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004991
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004992 t->be->be_counters.denied_resp++;
4993 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004994 if (t->listener->counters)
4995 t->listener->counters->denied_resp++;
4996
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004997 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004998 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004999
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005000 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005001 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005002 if (txn->status < 200)
5003 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005004 if (wl->cond) {
5005 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5006 ret = acl_pass(ret);
5007 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5008 ret = !ret;
5009 if (!ret)
5010 continue;
5011 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005012 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005013 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005014 }
5015
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005016 /* check whether we're already working on the frontend */
5017 if (cur_proxy == t->fe)
5018 break;
5019 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005020 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005021
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005022 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005023 * We may be facing a 100-continue response, in which case this
5024 * is not the right response, and we're waiting for the next one.
5025 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005026 * next one.
5027 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005028 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005029 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005030 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005031 msg->msg_state = HTTP_MSG_RPBEFORE;
5032 txn->status = 0;
5033 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5034 return 1;
5035 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005036 else if (unlikely(txn->status < 200))
5037 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005038
5039 /* we don't have any 1xx status code now */
5040
5041 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005042 * 4: check for server cookie.
5043 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005044 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5045 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005046 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005047
Willy Tarreaubaaee002006-06-26 02:48:02 +02005048
Willy Tarreaua15645d2007-03-18 16:22:39 +01005049 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005050 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005051 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005052 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005054
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 /*
5056 * 6: add server cookie in the response if needed
5057 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005058 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005059 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005060 (!(t->flags & SN_DIRECT) ||
5061 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5062 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5063 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5064 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005065 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5066 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005068 /* the server is known, it's not the one the client requested, or the
5069 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005070 * insert a set-cookie here, except if we want to insert only on POST
5071 * requests and this one isn't. Note that servers which don't have cookies
5072 * (eg: some backup servers) will return a full cookie removal request.
5073 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005074 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005075 len = sprintf(trash,
5076 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5077 t->be->cookie_name);
5078 }
5079 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005080 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005081
5082 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5083 /* emit last_date, which is mandatory */
5084 trash[len++] = COOKIE_DELIM_DATE;
5085 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5086 if (t->be->cookie_maxlife) {
5087 /* emit first_date, which is either the original one or
5088 * the current date.
5089 */
5090 trash[len++] = COOKIE_DELIM_DATE;
5091 s30tob64(txn->cookie_first_date ?
5092 txn->cookie_first_date >> 2 :
5093 (date.tv_sec+3) >> 2, trash + len);
5094 len += 5;
5095 }
5096 }
5097 len += sprintf(trash + len, "; path=/");
5098 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005099
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005100 if (t->be->cookie_domain)
5101 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005104 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005106
Willy Tarreauf1348312010-10-07 15:54:11 +02005107 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005108 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005109 /* the server did not change, only the date was updated */
5110 txn->flags |= TX_SCK_UPDATED;
5111 else
5112 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005113
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 /* Here, we will tell an eventual cache on the client side that we don't
5115 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5116 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5117 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5118 */
5119 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5122
5123 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005124 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005126 }
5127 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005128
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129 /*
5130 * 7: check if result will be cacheable with a cookie.
5131 * We'll block the response if security checks have caught
5132 * nasty things such as a cacheable cookie.
5133 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005134 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5135 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005136 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005137
5138 /* we're in presence of a cacheable response containing
5139 * a set-cookie header. We'll block it as requested by
5140 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005141 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005142 if (target_srv(&t->target))
5143 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005144
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005145 t->be->be_counters.denied_resp++;
5146 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005147 if (t->listener->counters)
5148 t->listener->counters->denied_resp++;
5149
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005151 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152 send_log(t->be, LOG_ALERT,
5153 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005154 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005155 goto return_srv_prx_502;
5156 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005157
5158 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005159 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160 */
Willy Tarreau60466522010-01-18 19:08:45 +01005161 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5162 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5163 unsigned int want_flags = 0;
5164
5165 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5166 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5167 /* we want a keep-alive response here. Keep-alive header
5168 * required if either side is not 1.1.
5169 */
5170 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5171 want_flags |= TX_CON_KAL_SET;
5172 }
5173 else {
5174 /* we want a close response here. Close header required if
5175 * the server is 1.1, regardless of the client.
5176 */
5177 if (txn->flags & TX_RES_VER_11)
5178 want_flags |= TX_CON_CLO_SET;
5179 }
5180
5181 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5182 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005183 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005185 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005186 if ((txn->flags & TX_RES_XFER_LEN) ||
5187 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005188 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005189
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /*************************************************************
5191 * OK, that's finished for the headers. We have done what we *
5192 * could. Let's switch to the DATA state. *
5193 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005196
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005197 /* if the user wants to log as soon as possible, without counting
5198 * bytes from the server, then this is the right moment. We have
5199 * to temporarily assign bytes_out to log what we currently have.
5200 */
5201 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5202 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5203 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005204 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205 t->logs.bytes_out = 0;
5206 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /* Note: we must not try to cheat by jumping directly to DATA,
5209 * otherwise we would not let the client side wake up.
5210 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005211
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005212 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005213 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005214 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005216
Willy Tarreaud98cf932009-12-27 22:54:55 +01005217/* This function is an analyser which forwards response body (including chunk
5218 * sizes if any). It is called as soon as we must forward, even if we forward
5219 * zero byte. The only situation where it must not be called is when we're in
5220 * tunnel mode and we want to forward till the close. It's used both to forward
5221 * remaining data and to resync after end of body. It expects the msg_state to
5222 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5223 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005224 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005225 * bytes of pending data + the headers if not already done (between som and sov).
5226 * It eventually adjusts som to match sov after the data in between have been sent.
5227 */
5228int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5229{
5230 struct http_txn *txn = &s->txn;
5231 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005232 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005233
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005234 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5235 return 0;
5236
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005237 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005238 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005239 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005240 /* Output closed while we were sending data. We must abort and
5241 * wake the other side up.
5242 */
5243 msg->msg_state = HTTP_MSG_ERROR;
5244 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005245 return 1;
5246 }
5247
Willy Tarreau4fe41902010-06-07 22:27:41 +02005248 /* in most states, we should abort in case of early close */
5249 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005250
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5252 /* we have msg->col and msg->sov which both point to the first
5253 * byte of message body. msg->som still points to the beginning
5254 * of the message. We must save the body in req->lr because it
5255 * survives buffer re-alignments.
5256 */
5257 res->lr = res->data + msg->sov;
5258 if (txn->flags & TX_RES_TE_CHNK)
5259 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5260 else {
5261 msg->msg_state = HTTP_MSG_DATA;
5262 }
5263 }
5264
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005266 int bytes;
5267
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005269 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005270 bytes = msg->sov - msg->som;
5271 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005272 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005273 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5274 bytes += res->size;
5275 msg->chunk_len += (unsigned int)bytes;
5276 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005277 }
5278
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005279
Willy Tarreaucaabe412010-01-03 23:08:28 +01005280 if (msg->msg_state == HTTP_MSG_DATA) {
5281 /* must still forward */
5282 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005283 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005284
5285 /* nothing left to forward */
5286 if (txn->flags & TX_RES_TE_CHNK)
5287 msg->msg_state = HTTP_MSG_DATA_CRLF;
5288 else
5289 msg->msg_state = HTTP_MSG_DONE;
5290 }
5291 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005292 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5294 */
5295 int ret = http_parse_chunk_size(res, msg);
5296
5297 if (!ret)
5298 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005299 else if (ret < 0) {
5300 if (msg->err_pos >= 0)
5301 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005302 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005303 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005304 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005306 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5307 /* we want the CRLF after the data */
5308 int ret;
5309
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005310 res->lr = res->w + res->send_max;
5311 if (res->lr >= res->data + res->size)
5312 res->lr -= res->size;
5313
Willy Tarreaud98cf932009-12-27 22:54:55 +01005314 ret = http_skip_chunk_crlf(res, msg);
5315
5316 if (!ret)
5317 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005318 else if (ret < 0) {
5319 if (msg->err_pos >= 0)
5320 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005322 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 /* we're in MSG_CHUNK_SIZE now */
5324 }
5325 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5326 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 if (ret == 0)
5329 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005330 else if (ret < 0) {
5331 if (msg->err_pos >= 0)
5332 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005333 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005334 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005335 /* we're in HTTP_MSG_DONE now */
5336 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005338 int old_state = msg->msg_state;
5339
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005341 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005342 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5343 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5344 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 if (http_resync_states(s)) {
5346 http_silent_debug(__LINE__, s);
5347 /* some state changes occurred, maybe the analyser
5348 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005349 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005350 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5351 if (res->flags & BF_SHUTW) {
5352 /* response errors are most likely due to
5353 * the client aborting the transfer.
5354 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005355 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005356 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005357 if (msg->err_pos >= 0)
5358 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005360 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005361 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005362 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005364 }
5365 }
5366
Willy Tarreaud98cf932009-12-27 22:54:55 +01005367 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005368 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005369 if (res->flags & BF_SHUTR) {
5370 if (!(s->flags & SN_ERR_MASK))
5371 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005372 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005373 if (target_srv(&s->target))
5374 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005375 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005376 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005377
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005378 if (res->flags & BF_SHUTW)
5379 goto aborted_xfer;
5380
Willy Tarreau40dba092010-03-04 18:14:51 +01005381 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 if (!s->req->analysers)
5383 goto return_bad_res;
5384
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005385 /* forward any pending data */
5386 bytes = msg->sov - msg->som;
5387 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005388 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005389 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5390 bytes += res->size;
5391 msg->chunk_len += (unsigned int)bytes;
5392 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 }
5394
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005395 /* When TE: chunked is used, we need to get there again to parse remaining
5396 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5397 * Similarly, with keep-alive on the client side, we don't want to forward a
5398 * close.
5399 */
5400 if ((txn->flags & TX_RES_TE_CHNK) ||
5401 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5402 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5403 buffer_dont_close(res);
5404
Willy Tarreau5c620922011-05-11 19:56:11 +02005405 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005406 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5407 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005408 * modes are already handled by the stream sock layer. We must not do
5409 * this in content-length mode because it could present the MSG_MORE
5410 * flag with the last block of forwarded data, which would cause an
5411 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005412 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005413 if (txn->flags & TX_RES_TE_CHNK)
5414 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005415
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418 return 0;
5419
Willy Tarreau40dba092010-03-04 18:14:51 +01005420 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005421 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005422 if (target_srv(&s->target))
5423 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005424
5425 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005427 /* don't send any error message as we're in the body */
5428 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005430 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005431 if (target_srv(&s->target))
5432 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433
5434 if (!(s->flags & SN_ERR_MASK))
5435 s->flags |= SN_ERR_PRXCOND;
5436 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005437 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005438 return 0;
5439
5440 aborted_xfer:
5441 txn->rsp.msg_state = HTTP_MSG_ERROR;
5442 /* don't send any error message as we're in the body */
5443 stream_int_retnclose(res->cons, NULL);
5444 res->analysers = 0;
5445 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5446
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005447 s->fe->fe_counters.cli_aborts++;
5448 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005449 if (target_srv(&s->target))
5450 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005451
5452 if (!(s->flags & SN_ERR_MASK))
5453 s->flags |= SN_ERR_CLICL;
5454 if (!(s->flags & SN_FINST_MASK))
5455 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 return 0;
5457}
5458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005459/* Iterate the same filter through all request headers.
5460 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005461 * Since it can manage the switch to another backend, it updates the per-proxy
5462 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005465{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005466 char term;
5467 char *cur_ptr, *cur_end, *cur_next;
5468 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005469 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005470 struct hdr_idx_elem *cur_hdr;
5471 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005473 last_hdr = 0;
5474
Willy Tarreau962c3f42010-01-10 00:15:35 +01005475 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 old_idx = 0;
5477
5478 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005479 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005481 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005482 (exp->action == ACT_ALLOW ||
5483 exp->action == ACT_DENY ||
5484 exp->action == ACT_TARPIT))
5485 return 0;
5486
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005487 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488 if (!cur_idx)
5489 break;
5490
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005491 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 cur_ptr = cur_next;
5493 cur_end = cur_ptr + cur_hdr->len;
5494 cur_next = cur_end + cur_hdr->cr + 1;
5495
5496 /* Now we have one header between cur_ptr and cur_end,
5497 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005498 */
5499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005500 /* The annoying part is that pattern matching needs
5501 * that we modify the contents to null-terminate all
5502 * strings before testing them.
5503 */
5504
5505 term = *cur_end;
5506 *cur_end = '\0';
5507
5508 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5509 switch (exp->action) {
5510 case ACT_SETBE:
5511 /* It is not possible to jump a second time.
5512 * FIXME: should we return an HTTP/500 here so that
5513 * the admin knows there's a problem ?
5514 */
5515 if (t->be != t->fe)
5516 break;
5517
5518 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005519 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005520 last_hdr = 1;
5521 break;
5522
5523 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005524 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005525 last_hdr = 1;
5526 break;
5527
5528 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005529 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005531
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005532 t->fe->fe_counters.denied_req++;
5533 if (t->fe != t->be)
5534 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005536 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 break;
5539
5540 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005541 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005542 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005543
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005544 t->fe->fe_counters.denied_req++;
5545 if (t->fe != t->be)
5546 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005547 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005548 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005550 break;
5551
5552 case ACT_REPLACE:
5553 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5554 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5555 /* FIXME: if the user adds a newline in the replacement, the
5556 * index will not be recalculated for now, and the new line
5557 * will not be counted as a new header.
5558 */
5559
5560 cur_end += delta;
5561 cur_next += delta;
5562 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005563 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 break;
5565
5566 case ACT_REMOVE:
5567 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5568 cur_next += delta;
5569
Willy Tarreaufa355d42009-11-29 18:12:29 +01005570 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005571 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5572 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005573 cur_hdr->len = 0;
5574 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005575 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005576 break;
5577
5578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005579 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005580 if (cur_end)
5581 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 /* keep the link from this header to next one in case of later
5584 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 old_idx = cur_idx;
5587 }
5588 return 0;
5589}
5590
5591
5592/* Apply the filter to the request line.
5593 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5594 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595 * Since it can manage the switch to another backend, it updates the per-proxy
5596 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005597 */
5598int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5599{
5600 char term;
5601 char *cur_ptr, *cur_end;
5602 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005603 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005605
Willy Tarreau58f10d72006-12-04 02:26:12 +01005606
Willy Tarreau3d300592007-03-18 18:34:41 +01005607 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005609 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005610 (exp->action == ACT_ALLOW ||
5611 exp->action == ACT_DENY ||
5612 exp->action == ACT_TARPIT))
5613 return 0;
5614 else if (exp->action == ACT_REMOVE)
5615 return 0;
5616
5617 done = 0;
5618
Willy Tarreau962c3f42010-01-10 00:15:35 +01005619 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005620 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621
5622 /* Now we have the request line between cur_ptr and cur_end */
5623
5624 /* The annoying part is that pattern matching needs
5625 * that we modify the contents to null-terminate all
5626 * strings before testing them.
5627 */
5628
5629 term = *cur_end;
5630 *cur_end = '\0';
5631
5632 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5633 switch (exp->action) {
5634 case ACT_SETBE:
5635 /* It is not possible to jump a second time.
5636 * FIXME: should we return an HTTP/500 here so that
5637 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005638 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 if (t->be != t->fe)
5640 break;
5641
5642 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005643 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644 done = 1;
5645 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005648 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 done = 1;
5650 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005653 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005654
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005655 t->fe->fe_counters.denied_req++;
5656 if (t->fe != t->be)
5657 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005659 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 done = 1;
5662 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005665 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005666
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005667 t->fe->fe_counters.denied_req++;
5668 if (t->fe != t->be)
5669 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005670 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005671 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005672
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 done = 1;
5674 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 case ACT_REPLACE:
5677 *cur_end = term; /* restore the string terminator */
5678 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5679 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5680 /* FIXME: if the user adds a newline in the replacement, the
5681 * index will not be recalculated for now, and the new line
5682 * will not be counted as a new header.
5683 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005684
Willy Tarreaufa355d42009-11-29 18:12:29 +01005685 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005687 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 HTTP_MSG_RQMETH,
5689 cur_ptr, cur_end + 1,
5690 NULL, NULL);
5691 if (unlikely(!cur_end))
5692 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005694 /* we have a full request and we know that we have either a CR
5695 * or an LF at <ptr>.
5696 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005697 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5698 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 /* there is no point trying this regex on headers */
5700 return 1;
5701 }
5702 }
5703 *cur_end = term; /* restore the string terminator */
5704 return done;
5705}
Willy Tarreau97de6242006-12-27 17:18:38 +01005706
Willy Tarreau58f10d72006-12-04 02:26:12 +01005707
Willy Tarreau58f10d72006-12-04 02:26:12 +01005708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005710 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005712 * unparsable request. Since it can manage the switch to another backend, it
5713 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005715int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005717 struct http_txn *txn = &s->txn;
5718 struct hdr_exp *exp;
5719
5720 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 /*
5724 * The interleaving of transformations and verdicts
5725 * makes it difficult to decide to continue or stop
5726 * the evaluation.
5727 */
5728
Willy Tarreau6c123b12010-01-28 20:22:06 +01005729 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5730 break;
5731
Willy Tarreau3d300592007-03-18 18:34:41 +01005732 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005734 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005736
5737 /* if this filter had a condition, evaluate it now and skip to
5738 * next filter if the condition does not match.
5739 */
5740 if (exp->cond) {
5741 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5742 ret = acl_pass(ret);
5743 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5744 ret = !ret;
5745
5746 if (!ret)
5747 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 }
5749
5750 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005751 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752 if (unlikely(ret < 0))
5753 return -1;
5754
5755 if (likely(ret == 0)) {
5756 /* The filter did not match the request, it can be
5757 * iterated through all headers.
5758 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005759 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005761 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005762 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005763}
5764
5765
Willy Tarreaua15645d2007-03-18 16:22:39 +01005766
Willy Tarreau58f10d72006-12-04 02:26:12 +01005767/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005768 * Try to retrieve the server associated to the appsession.
5769 * If the server is found, it's assigned to the session.
5770 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005771void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772 struct http_txn *txn = &t->txn;
5773 appsess *asession = NULL;
5774 char *sessid_temp = NULL;
5775
Cyril Bontéb21570a2009-11-29 20:04:48 +01005776 if (len > t->be->appsession_len) {
5777 len = t->be->appsession_len;
5778 }
5779
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005780 if (t->be->options2 & PR_O2_AS_REQL) {
5781 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005782 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005783 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005784 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005785 }
5786
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005787 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005788 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5789 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5790 return;
5791 }
5792
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005793 memcpy(txn->sessid, buf, len);
5794 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005795 }
5796
5797 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5798 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5799 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5800 return;
5801 }
5802
Cyril Bontéb21570a2009-11-29 20:04:48 +01005803 memcpy(sessid_temp, buf, len);
5804 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005805
5806 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5807 /* free previously allocated memory */
5808 pool_free2(apools.sessid, sessid_temp);
5809
5810 if (asession != NULL) {
5811 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5812 if (!(t->be->options2 & PR_O2_AS_REQL))
5813 asession->request_count++;
5814
5815 if (asession->serverid != NULL) {
5816 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005817
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005818 while (srv) {
5819 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005820 if ((srv->state & SRV_RUNNING) ||
5821 (t->be->options & PR_O_PERSIST) ||
5822 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005823 /* we found the server and it's usable */
5824 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005825 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005826 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005827 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005828
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005829 break;
5830 } else {
5831 txn->flags &= ~TX_CK_MASK;
5832 txn->flags |= TX_CK_DOWN;
5833 }
5834 }
5835 srv = srv->next;
5836 }
5837 }
5838 }
5839}
5840
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005841/* Find the end of a cookie value contained between <s> and <e>. It works the
5842 * same way as with headers above except that the semi-colon also ends a token.
5843 * See RFC2965 for more information. Note that it requires a valid header to
5844 * return a valid result.
5845 */
5846char *find_cookie_value_end(char *s, const char *e)
5847{
5848 int quoted, qdpair;
5849
5850 quoted = qdpair = 0;
5851 for (; s < e; s++) {
5852 if (qdpair) qdpair = 0;
5853 else if (quoted) {
5854 if (*s == '\\') qdpair = 1;
5855 else if (*s == '"') quoted = 0;
5856 }
5857 else if (*s == '"') quoted = 1;
5858 else if (*s == ',' || *s == ';') return s;
5859 }
5860 return s;
5861}
5862
5863/* Delete a value in a header between delimiters <from> and <next> in buffer
5864 * <buf>. The number of characters displaced is returned, and the pointer to
5865 * the first delimiter is updated if required. The function tries as much as
5866 * possible to respect the following principles :
5867 * - replace <from> delimiter by the <next> one unless <from> points to a
5868 * colon, in which case <next> is simply removed
5869 * - set exactly one space character after the new first delimiter, unless
5870 * there are not enough characters in the block being moved to do so.
5871 * - remove unneeded spaces before the previous delimiter and after the new
5872 * one.
5873 *
5874 * It is the caller's responsibility to ensure that :
5875 * - <from> points to a valid delimiter or the colon ;
5876 * - <next> points to a valid delimiter or the final CR/LF ;
5877 * - there are non-space chars before <from> ;
5878 * - there is a CR/LF at or after <next>.
5879 */
5880int del_hdr_value(struct buffer *buf, char **from, char *next)
5881{
5882 char *prev = *from;
5883
5884 if (*prev == ':') {
5885 /* We're removing the first value, preserve the colon and add a
5886 * space if possible.
5887 */
5888 if (!http_is_crlf[(unsigned char)*next])
5889 next++;
5890 prev++;
5891 if (prev < next)
5892 *prev++ = ' ';
5893
5894 while (http_is_spht[(unsigned char)*next])
5895 next++;
5896 } else {
5897 /* Remove useless spaces before the old delimiter. */
5898 while (http_is_spht[(unsigned char)*(prev-1)])
5899 prev--;
5900 *from = prev;
5901
5902 /* copy the delimiter and if possible a space if we're
5903 * not at the end of the line.
5904 */
5905 if (!http_is_crlf[(unsigned char)*next]) {
5906 *prev++ = *next++;
5907 if (prev + 1 < next)
5908 *prev++ = ' ';
5909 while (http_is_spht[(unsigned char)*next])
5910 next++;
5911 }
5912 }
5913 return buffer_replace2(buf, prev, next, NULL, 0);
5914}
5915
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005916/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005917 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918 * desirable to call it only when needed. This code is quite complex because
5919 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5920 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921 */
5922void manage_client_side_cookies(struct session *t, struct buffer *req)
5923{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005924 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005925 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005926 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005927 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5928 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005930 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005931 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005932 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005934 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005936 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005937
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005938 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005939 hdr_beg = hdr_next;
5940 hdr_end = hdr_beg + cur_hdr->len;
5941 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005942
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005943 /* We have one full header between hdr_beg and hdr_end, and the
5944 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 * "Cookie:" headers.
5946 */
5947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005948 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005949 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005950 old_idx = cur_idx;
5951 continue;
5952 }
5953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005954 del_from = NULL; /* nothing to be deleted */
5955 preserve_hdr = 0; /* assume we may kill the whole header */
5956
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957 /* Now look for cookies. Conforming to RFC2109, we have to support
5958 * attributes whose name begin with a '$', and associate them with
5959 * the right cookie, if we want to delete this cookie.
5960 * So there are 3 cases for each cookie read :
5961 * 1) it's a special attribute, beginning with a '$' : ignore it.
5962 * 2) it's a server id cookie that we *MAY* want to delete : save
5963 * some pointers on it (last semi-colon, beginning of cookie...)
5964 * 3) it's an application cookie : we *MAY* have to delete a previous
5965 * "special" cookie.
5966 * At the end of loop, if a "special" cookie remains, we may have to
5967 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005968 * *MUST* delete it.
5969 *
5970 * Note: RFC2965 is unclear about the processing of spaces around
5971 * the equal sign in the ATTR=VALUE form. A careful inspection of
5972 * the RFC explicitly allows spaces before it, and not within the
5973 * tokens (attrs or values). An inspection of RFC2109 allows that
5974 * too but section 10.1.3 lets one think that spaces may be allowed
5975 * after the equal sign too, resulting in some (rare) buggy
5976 * implementations trying to do that. So let's do what servers do.
5977 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5978 * allowed quoted strings in values, with any possible character
5979 * after a backslash, including control chars and delimitors, which
5980 * causes parsing to become ambiguous. Browsers also allow spaces
5981 * within values even without quotes.
5982 *
5983 * We have to keep multiple pointers in order to support cookie
5984 * removal at the beginning, middle or end of header without
5985 * corrupting the header. All of these headers are valid :
5986 *
5987 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5988 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5989 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5990 * | | | | | | | | |
5991 * | | | | | | | | hdr_end <--+
5992 * | | | | | | | +--> next
5993 * | | | | | | +----> val_end
5994 * | | | | | +-----------> val_beg
5995 * | | | | +--------------> equal
5996 * | | | +----------------> att_end
5997 * | | +---------------------> att_beg
5998 * | +--------------------------> prev
5999 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006000 */
6001
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006002 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6003 /* Iterate through all cookies on this line */
6004
6005 /* find att_beg */
6006 att_beg = prev + 1;
6007 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6008 att_beg++;
6009
6010 /* find att_end : this is the first character after the last non
6011 * space before the equal. It may be equal to hdr_end.
6012 */
6013 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006014
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006015 while (equal < hdr_end) {
6016 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006017 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006018 if (http_is_spht[(unsigned char)*equal++])
6019 continue;
6020 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021 }
6022
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006023 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6024 * is between <att_beg> and <equal>, both may be identical.
6025 */
6026
6027 /* look for end of cookie if there is an equal sign */
6028 if (equal < hdr_end && *equal == '=') {
6029 /* look for the beginning of the value */
6030 val_beg = equal + 1;
6031 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6032 val_beg++;
6033
6034 /* find the end of the value, respecting quotes */
6035 next = find_cookie_value_end(val_beg, hdr_end);
6036
6037 /* make val_end point to the first white space or delimitor after the value */
6038 val_end = next;
6039 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6040 val_end--;
6041 } else {
6042 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006043 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006044
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006045 /* We have nothing to do with attributes beginning with '$'. However,
6046 * they will automatically be removed if a header before them is removed,
6047 * since they're supposed to be linked together.
6048 */
6049 if (*att_beg == '$')
6050 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 /* Ignore cookies with no equal sign */
6053 if (equal == next) {
6054 /* This is not our cookie, so we must preserve it. But if we already
6055 * scheduled another cookie for removal, we cannot remove the
6056 * complete header, but we can remove the previous block itself.
6057 */
6058 preserve_hdr = 1;
6059 if (del_from != NULL) {
6060 int delta = del_hdr_value(req, &del_from, prev);
6061 val_end += delta;
6062 next += delta;
6063 hdr_end += delta;
6064 hdr_next += delta;
6065 cur_hdr->len += delta;
6066 http_msg_move_end(&txn->req, delta);
6067 prev = del_from;
6068 del_from = NULL;
6069 }
6070 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 /* if there are spaces around the equal sign, we need to
6074 * strip them otherwise we'll get trouble for cookie captures,
6075 * or even for rewrites. Since this happens extremely rarely,
6076 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6079 int stripped_before = 0;
6080 int stripped_after = 0;
6081
6082 if (att_end != equal) {
6083 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6084 equal += stripped_before;
6085 val_beg += stripped_before;
6086 }
6087
6088 if (val_beg > equal + 1) {
6089 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6090 val_beg += stripped_after;
6091 stripped_before += stripped_after;
6092 }
6093
6094 val_end += stripped_before;
6095 next += stripped_before;
6096 hdr_end += stripped_before;
6097 hdr_next += stripped_before;
6098 cur_hdr->len += stripped_before;
6099 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006101 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 /* First, let's see if we want to capture this cookie. We check
6104 * that we don't already have a client side cookie, because we
6105 * can only capture one. Also as an optimisation, we ignore
6106 * cookies shorter than the declared name.
6107 */
6108 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6109 (val_end - att_beg >= t->fe->capture_namelen) &&
6110 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6111 int log_len = val_end - att_beg;
6112
6113 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6114 Alert("HTTP logging : out of memory.\n");
6115 } else {
6116 if (log_len > t->fe->capture_len)
6117 log_len = t->fe->capture_len;
6118 memcpy(txn->cli_cookie, att_beg, log_len);
6119 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreaubca99692010-10-06 19:25:55 +02006123 /* Persistence cookies in passive, rewrite or insert mode have the
6124 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006126 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006128 * For cookies in prefix mode, the form is :
6129 *
6130 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006131 */
6132 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6133 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6134 struct server *srv = t->be->srv;
6135 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6138 * have the server ID between val_beg and delim, and the original cookie between
6139 * delim+1 and val_end. Otherwise, delim==val_end :
6140 *
6141 * Cookie: NAME=SRV; # in all but prefix modes
6142 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6143 * | || || | |+-> next
6144 * | || || | +--> val_end
6145 * | || || +---------> delim
6146 * | || |+------------> val_beg
6147 * | || +-------------> att_end = equal
6148 * | |+-----------------> att_beg
6149 * | +------------------> prev
6150 * +-------------------------> hdr_beg
6151 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006152
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006153 if (t->be->options & PR_O_COOK_PFX) {
6154 for (delim = val_beg; delim < val_end; delim++)
6155 if (*delim == COOKIE_DELIM)
6156 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006157 } else {
6158 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006159 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 /* Now check if the cookie contains a date field, which would
6161 * appear after a vertical bar ('|') just after the server name
6162 * and before the delimiter.
6163 */
6164 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6165 if (vbar1) {
6166 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006167 * right is the last seen date. It is a base64 encoded
6168 * 30-bit value representing the UNIX date since the
6169 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006170 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006171 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006172 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006173 if (val_end - vbar1 >= 5) {
6174 val = b64tos30(vbar1);
6175 if (val > 0)
6176 txn->cookie_last_date = val << 2;
6177 }
6178 /* look for a second vertical bar */
6179 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6180 if (vbar1 && (val_end - vbar1 > 5)) {
6181 val = b64tos30(vbar1 + 1);
6182 if (val > 0)
6183 txn->cookie_first_date = val << 2;
6184 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006185 }
6186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006187
Willy Tarreauf64d1412010-10-07 20:06:11 +02006188 /* if the cookie has an expiration date and the proxy wants to check
6189 * it, then we do that now. We first check if the cookie is too old,
6190 * then only if it has expired. We detect strict overflow because the
6191 * time resolution here is not great (4 seconds). Cookies with dates
6192 * in the future are ignored if their offset is beyond one day. This
6193 * allows an admin to fix timezone issues without expiring everyone
6194 * and at the same time avoids keeping unwanted side effects for too
6195 * long.
6196 */
6197 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006198 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6199 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006200 txn->flags &= ~TX_CK_MASK;
6201 txn->flags |= TX_CK_OLD;
6202 delim = val_beg; // let's pretend we have not found the cookie
6203 txn->cookie_first_date = 0;
6204 txn->cookie_last_date = 0;
6205 }
6206 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006207 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6208 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006209 txn->flags &= ~TX_CK_MASK;
6210 txn->flags |= TX_CK_EXPIRED;
6211 delim = val_beg; // let's pretend we have not found the cookie
6212 txn->cookie_first_date = 0;
6213 txn->cookie_last_date = 0;
6214 }
6215
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 /* Here, we'll look for the first running server which supports the cookie.
6217 * This allows to share a same cookie between several servers, for example
6218 * to dedicate backup servers to specific servers only.
6219 * However, to prevent clients from sticking to cookie-less backup server
6220 * when they have incidentely learned an empty cookie, we simply ignore
6221 * empty cookies and mark them as invalid.
6222 * The same behaviour is applied when persistence must be ignored.
6223 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006224 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 while (srv) {
6228 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6229 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6230 if ((srv->state & SRV_RUNNING) ||
6231 (t->be->options & PR_O_PERSIST) ||
6232 (t->flags & SN_FORCE_PRST)) {
6233 /* we found the server and we can use it */
6234 txn->flags &= ~TX_CK_MASK;
6235 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6236 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006237 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 break;
6239 } else {
6240 /* we found a server, but it's down,
6241 * mark it as such and go on in case
6242 * another one is available.
6243 */
6244 txn->flags &= ~TX_CK_MASK;
6245 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 srv = srv->next;
6249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreauf64d1412010-10-07 20:06:11 +02006251 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006252 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006254 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6255 txn->flags |= TX_CK_UNUSED;
6256 else
6257 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006259
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 /* depending on the cookie mode, we may have to either :
6261 * - delete the complete cookie if we're in insert+indirect mode, so that
6262 * the server never sees it ;
6263 * - remove the server id from the cookie value, and tag the cookie as an
6264 * application cookie so that it does not get accidentely removed later,
6265 * if we're in cookie prefix mode
6266 */
6267 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6268 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6271 val_end += delta;
6272 next += delta;
6273 hdr_end += delta;
6274 hdr_next += delta;
6275 cur_hdr->len += delta;
6276 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 del_from = NULL;
6279 preserve_hdr = 1; /* we want to keep this cookie */
6280 }
6281 else if (del_from == NULL &&
6282 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6283 del_from = prev;
6284 }
6285 } else {
6286 /* This is not our cookie, so we must preserve it. But if we already
6287 * scheduled another cookie for removal, we cannot remove the
6288 * complete header, but we can remove the previous block itself.
6289 */
6290 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 if (del_from != NULL) {
6293 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006294 if (att_beg >= del_from)
6295 att_beg += delta;
6296 if (att_end >= del_from)
6297 att_end += delta;
6298 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 val_end += delta;
6300 next += delta;
6301 hdr_end += delta;
6302 hdr_next += delta;
6303 cur_hdr->len += delta;
6304 http_msg_move_end(&txn->req, delta);
6305 prev = del_from;
6306 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006308 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 /* Look for the appsession cookie unless persistence must be ignored */
6311 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6312 int cmp_len, value_len;
6313 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 if (t->be->options2 & PR_O2_AS_PFX) {
6316 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6317 value_begin = att_beg + t->be->appsession_name_len;
6318 value_len = val_end - att_beg - t->be->appsession_name_len;
6319 } else {
6320 cmp_len = att_end - att_beg;
6321 value_begin = val_beg;
6322 value_len = val_end - val_beg;
6323 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006324
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 /* let's see if the cookie is our appcookie */
6326 if (cmp_len == t->be->appsession_name_len &&
6327 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6328 manage_client_side_appsession(t, value_begin, value_len);
6329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330 }
6331
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 /* continue with next cookie on this header line */
6333 att_beg = next;
6334 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 /* There are no more cookies on this line.
6337 * We may still have one (or several) marked for deletion at the
6338 * end of the line. We must do this now in two ways :
6339 * - if some cookies must be preserved, we only delete from the
6340 * mark to the end of line ;
6341 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006345 if (preserve_hdr) {
6346 delta = del_hdr_value(req, &del_from, hdr_end);
6347 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 cur_hdr->len += delta;
6349 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351
6352 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006353 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6354 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006356 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006357 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006358 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006359 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360 }
6361
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006362 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365}
6366
6367
Willy Tarreaua15645d2007-03-18 16:22:39 +01006368/* Iterate the same filter through all response headers contained in <rtr>.
6369 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6370 */
6371int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6372{
6373 char term;
6374 char *cur_ptr, *cur_end, *cur_next;
6375 int cur_idx, old_idx, last_hdr;
6376 struct http_txn *txn = &t->txn;
6377 struct hdr_idx_elem *cur_hdr;
6378 int len, delta;
6379
6380 last_hdr = 0;
6381
Willy Tarreau962c3f42010-01-10 00:15:35 +01006382 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006383 old_idx = 0;
6384
6385 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006386 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006387 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006388 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006389 (exp->action == ACT_ALLOW ||
6390 exp->action == ACT_DENY))
6391 return 0;
6392
6393 cur_idx = txn->hdr_idx.v[old_idx].next;
6394 if (!cur_idx)
6395 break;
6396
6397 cur_hdr = &txn->hdr_idx.v[cur_idx];
6398 cur_ptr = cur_next;
6399 cur_end = cur_ptr + cur_hdr->len;
6400 cur_next = cur_end + cur_hdr->cr + 1;
6401
6402 /* Now we have one header between cur_ptr and cur_end,
6403 * and the next header starts at cur_next.
6404 */
6405
6406 /* The annoying part is that pattern matching needs
6407 * that we modify the contents to null-terminate all
6408 * strings before testing them.
6409 */
6410
6411 term = *cur_end;
6412 *cur_end = '\0';
6413
6414 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6415 switch (exp->action) {
6416 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006417 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418 last_hdr = 1;
6419 break;
6420
6421 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006422 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 last_hdr = 1;
6424 break;
6425
6426 case ACT_REPLACE:
6427 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6428 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6429 /* FIXME: if the user adds a newline in the replacement, the
6430 * index will not be recalculated for now, and the new line
6431 * will not be counted as a new header.
6432 */
6433
6434 cur_end += delta;
6435 cur_next += delta;
6436 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006437 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438 break;
6439
6440 case ACT_REMOVE:
6441 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6442 cur_next += delta;
6443
Willy Tarreaufa355d42009-11-29 18:12:29 +01006444 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006445 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6446 txn->hdr_idx.used--;
6447 cur_hdr->len = 0;
6448 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006449 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006450 break;
6451
6452 }
6453 }
6454 if (cur_end)
6455 *cur_end = term; /* restore the string terminator */
6456
6457 /* keep the link from this header to next one in case of later
6458 * removal of next header.
6459 */
6460 old_idx = cur_idx;
6461 }
6462 return 0;
6463}
6464
6465
6466/* Apply the filter to the status line in the response buffer <rtr>.
6467 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6468 * or -1 if a replacement resulted in an invalid status line.
6469 */
6470int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6471{
6472 char term;
6473 char *cur_ptr, *cur_end;
6474 int done;
6475 struct http_txn *txn = &t->txn;
6476 int len, delta;
6477
6478
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006481 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006482 (exp->action == ACT_ALLOW ||
6483 exp->action == ACT_DENY))
6484 return 0;
6485 else if (exp->action == ACT_REMOVE)
6486 return 0;
6487
6488 done = 0;
6489
Willy Tarreau962c3f42010-01-10 00:15:35 +01006490 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006491 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492
6493 /* Now we have the status line between cur_ptr and cur_end */
6494
6495 /* The annoying part is that pattern matching needs
6496 * that we modify the contents to null-terminate all
6497 * strings before testing them.
6498 */
6499
6500 term = *cur_end;
6501 *cur_end = '\0';
6502
6503 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6504 switch (exp->action) {
6505 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006506 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507 done = 1;
6508 break;
6509
6510 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006511 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006512 done = 1;
6513 break;
6514
6515 case ACT_REPLACE:
6516 *cur_end = term; /* restore the string terminator */
6517 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6518 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6519 /* FIXME: if the user adds a newline in the replacement, the
6520 * index will not be recalculated for now, and the new line
6521 * will not be counted as a new header.
6522 */
6523
Willy Tarreaufa355d42009-11-29 18:12:29 +01006524 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006527 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528 cur_ptr, cur_end + 1,
6529 NULL, NULL);
6530 if (unlikely(!cur_end))
6531 return -1;
6532
6533 /* we have a full respnse and we know that we have either a CR
6534 * or an LF at <ptr>.
6535 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006536 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006537 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 /* there is no point trying this regex on headers */
6539 return 1;
6540 }
6541 }
6542 *cur_end = term; /* restore the string terminator */
6543 return done;
6544}
6545
6546
6547
6548/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006549 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6551 * unparsable response.
6552 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006553int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006555 struct http_txn *txn = &s->txn;
6556 struct hdr_exp *exp;
6557
6558 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559 int ret;
6560
6561 /*
6562 * The interleaving of transformations and verdicts
6563 * makes it difficult to decide to continue or stop
6564 * the evaluation.
6565 */
6566
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006567 if (txn->flags & TX_SVDENY)
6568 break;
6569
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006571 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6572 exp->action == ACT_PASS)) {
6573 exp = exp->next;
6574 continue;
6575 }
6576
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006577 /* if this filter had a condition, evaluate it now and skip to
6578 * next filter if the condition does not match.
6579 */
6580 if (exp->cond) {
6581 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6582 ret = acl_pass(ret);
6583 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6584 ret = !ret;
6585 if (!ret)
6586 continue;
6587 }
6588
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006590 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591 if (unlikely(ret < 0))
6592 return -1;
6593
6594 if (likely(ret == 0)) {
6595 /* The filter did not match the response, it can be
6596 * iterated through all headers.
6597 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006598 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006599 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 }
6601 return 0;
6602}
6603
6604
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006606 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006607 * desirable to call it only when needed. This function is also used when we
6608 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006610void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611{
6612 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006613 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006614 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006616 char *hdr_beg, *hdr_end, *hdr_next;
6617 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 /* Iterate through the headers.
6620 * we start with the start line.
6621 */
6622 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006623 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624
6625 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6626 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006627 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628
6629 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 hdr_beg = hdr_next;
6631 hdr_end = hdr_beg + cur_hdr->len;
6632 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634 /* We have one full header between hdr_beg and hdr_end, and the
6635 * next header starts at hdr_next. We're only interested in
6636 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637 */
6638
Willy Tarreau24581ba2010-08-31 22:39:35 +02006639 is_cookie2 = 0;
6640 prev = hdr_beg + 10;
6641 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006642 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006643 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6644 if (!val) {
6645 old_idx = cur_idx;
6646 continue;
6647 }
6648 is_cookie2 = 1;
6649 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 }
6651
Willy Tarreau24581ba2010-08-31 22:39:35 +02006652 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6653 * <prev> points to the colon.
6654 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006655 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656
Willy Tarreau24581ba2010-08-31 22:39:35 +02006657 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6658 * check-cache is enabled) and we are not interested in checking
6659 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006660 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006661 if (t->be->cookie_name == NULL &&
6662 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006663 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664 return;
6665
Willy Tarreau24581ba2010-08-31 22:39:35 +02006666 /* OK so now we know we have to process this response cookie.
6667 * The format of the Set-Cookie header is slightly different
6668 * from the format of the Cookie header in that it does not
6669 * support the comma as a cookie delimiter (thus the header
6670 * cannot be folded) because the Expires attribute described in
6671 * the original Netscape's spec may contain an unquoted date
6672 * with a comma inside. We have to live with this because
6673 * many browsers don't support Max-Age and some browsers don't
6674 * support quoted strings. However the Set-Cookie2 header is
6675 * clean.
6676 *
6677 * We have to keep multiple pointers in order to support cookie
6678 * removal at the beginning, middle or end of header without
6679 * corrupting the header (in case of set-cookie2). A special
6680 * pointer, <scav> points to the beginning of the set-cookie-av
6681 * fields after the first semi-colon. The <next> pointer points
6682 * either to the end of line (set-cookie) or next unquoted comma
6683 * (set-cookie2). All of these headers are valid :
6684 *
6685 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6686 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6687 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6688 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6689 * | | | | | | | | | |
6690 * | | | | | | | | +-> next hdr_end <--+
6691 * | | | | | | | +------------> scav
6692 * | | | | | | +--------------> val_end
6693 * | | | | | +--------------------> val_beg
6694 * | | | | +----------------------> equal
6695 * | | | +------------------------> att_end
6696 * | | +----------------------------> att_beg
6697 * | +------------------------------> prev
6698 * +-----------------------------------------> hdr_beg
6699 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700
Willy Tarreau24581ba2010-08-31 22:39:35 +02006701 for (; prev < hdr_end; prev = next) {
6702 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703
Willy Tarreau24581ba2010-08-31 22:39:35 +02006704 /* find att_beg */
6705 att_beg = prev + 1;
6706 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6707 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708
Willy Tarreau24581ba2010-08-31 22:39:35 +02006709 /* find att_end : this is the first character after the last non
6710 * space before the equal. It may be equal to hdr_end.
6711 */
6712 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau24581ba2010-08-31 22:39:35 +02006714 while (equal < hdr_end) {
6715 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6716 break;
6717 if (http_is_spht[(unsigned char)*equal++])
6718 continue;
6719 att_end = equal;
6720 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721
Willy Tarreau24581ba2010-08-31 22:39:35 +02006722 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6723 * is between <att_beg> and <equal>, both may be identical.
6724 */
6725
6726 /* look for end of cookie if there is an equal sign */
6727 if (equal < hdr_end && *equal == '=') {
6728 /* look for the beginning of the value */
6729 val_beg = equal + 1;
6730 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6731 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 /* find the end of the value, respecting quotes */
6734 next = find_cookie_value_end(val_beg, hdr_end);
6735
6736 /* make val_end point to the first white space or delimitor after the value */
6737 val_end = next;
6738 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6739 val_end--;
6740 } else {
6741 /* <equal> points to next comma, semi-colon or EOL */
6742 val_beg = val_end = next = equal;
6743 }
6744
6745 if (next < hdr_end) {
6746 /* Set-Cookie2 supports multiple cookies, and <next> points to
6747 * a colon or semi-colon before the end. So skip all attr-value
6748 * pairs and look for the next comma. For Set-Cookie, since
6749 * commas are permitted in values, skip to the end.
6750 */
6751 if (is_cookie2)
6752 next = find_hdr_value_end(next, hdr_end);
6753 else
6754 next = hdr_end;
6755 }
6756
6757 /* Now everything is as on the diagram above */
6758
6759 /* Ignore cookies with no equal sign */
6760 if (equal == val_end)
6761 continue;
6762
6763 /* If there are spaces around the equal sign, we need to
6764 * strip them otherwise we'll get trouble for cookie captures,
6765 * or even for rewrites. Since this happens extremely rarely,
6766 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6769 int stripped_before = 0;
6770 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771
Willy Tarreau24581ba2010-08-31 22:39:35 +02006772 if (att_end != equal) {
6773 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6774 equal += stripped_before;
6775 val_beg += stripped_before;
6776 }
6777
6778 if (val_beg > equal + 1) {
6779 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6780 val_beg += stripped_after;
6781 stripped_before += stripped_after;
6782 }
6783
6784 val_end += stripped_before;
6785 next += stripped_before;
6786 hdr_end += stripped_before;
6787 hdr_next += stripped_before;
6788 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006789 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006790 }
6791
6792 /* First, let's see if we want to capture this cookie. We check
6793 * that we don't already have a server side cookie, because we
6794 * can only capture one. Also as an optimisation, we ignore
6795 * cookies shorter than the declared name.
6796 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006797 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006798 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 (val_end - att_beg >= t->fe->capture_namelen) &&
6800 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6801 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006802 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 Alert("HTTP logging : out of memory.\n");
6804 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006805 else {
6806 if (log_len > t->fe->capture_len)
6807 log_len = t->fe->capture_len;
6808 memcpy(txn->srv_cookie, att_beg, log_len);
6809 txn->srv_cookie[log_len] = 0;
6810 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 }
6812
Willy Tarreau827aee92011-03-10 16:55:02 +01006813 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006815 if (!(t->flags & SN_IGNORE_PRST) &&
6816 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6817 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006818 /* assume passive cookie by default */
6819 txn->flags &= ~TX_SCK_MASK;
6820 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821
6822 /* If the cookie is in insert mode on a known server, we'll delete
6823 * this occurrence because we'll insert another one later.
6824 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 * a direct access.
6826 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006827 if (t->be->options2 & PR_O2_COOK_PSV) {
6828 /* The "preserve" flag was set, we don't want to touch the
6829 * server's cookie.
6830 */
6831 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006832 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006833 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 /* this cookie must be deleted */
6835 if (*prev == ':' && next == hdr_end) {
6836 /* whole header */
6837 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6838 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6839 txn->hdr_idx.used--;
6840 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006841 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006842 hdr_next += delta;
6843 http_msg_move_end(&txn->rsp, delta);
6844 /* note: while both invalid now, <next> and <hdr_end>
6845 * are still equal, so the for() will stop as expected.
6846 */
6847 } else {
6848 /* just remove the value */
6849 int delta = del_hdr_value(res, &prev, next);
6850 next = prev;
6851 hdr_end += delta;
6852 hdr_next += delta;
6853 cur_hdr->len += delta;
6854 http_msg_move_end(&txn->rsp, delta);
6855 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006856 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006857 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006858 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006860 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 * with this server since we know it.
6863 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006864 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006865 next += delta;
6866 hdr_end += delta;
6867 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006869 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870
Willy Tarreauf1348312010-10-07 15:54:11 +02006871 txn->flags &= ~TX_SCK_MASK;
6872 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006874 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006878 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006879 next += delta;
6880 hdr_end += delta;
6881 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006883 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006884
Willy Tarreau827aee92011-03-10 16:55:02 +01006885 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006886 txn->flags &= ~TX_SCK_MASK;
6887 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 }
6889 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006890 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6891 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006892 int cmp_len, value_len;
6893 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Cyril Bontéb21570a2009-11-29 20:04:48 +01006895 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6897 value_begin = att_beg + t->be->appsession_name_len;
6898 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006899 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 cmp_len = att_end - att_beg;
6901 value_begin = val_beg;
6902 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006903 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006904
Cyril Bonté17530c32010-04-06 21:11:10 +02006905 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6907 /* free a possibly previously allocated memory */
6908 pool_free2(apools.sessid, txn->sessid);
6909
Cyril Bontéb21570a2009-11-29 20:04:48 +01006910 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006911 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006912 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6913 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6914 return;
6915 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006916 memcpy(txn->sessid, value_begin, value_len);
6917 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006919 }
6920 /* that's done for this cookie, check the next one on the same
6921 * line when next != hdr_end (only if is_cookie2).
6922 */
6923 }
6924 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006925 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006926 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006927
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006928 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006929 appsess *asession = NULL;
6930 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006931 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006933 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006934 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6935 Alert("Not enough Memory process_srv():asession:calloc().\n");
6936 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6937 return;
6938 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006939 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6940
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6942 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6943 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006944 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945 return;
6946 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006947 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006948 asession->sessid[t->be->appsession_len] = 0;
6949
Willy Tarreau827aee92011-03-10 16:55:02 +01006950 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006952 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006954 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006955 return;
6956 }
6957 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006958 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006959
6960 asession->request_count = 0;
6961 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6962 }
6963
6964 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6965 asession->request_count++;
6966 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967}
6968
6969
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970/*
6971 * Check if response is cacheable or not. Updates t->flags.
6972 */
6973void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6974{
6975 struct http_txn *txn = &t->txn;
6976 char *p1, *p2;
6977
6978 char *cur_ptr, *cur_end, *cur_next;
6979 int cur_idx;
6980
Willy Tarreau5df51872007-11-25 16:20:08 +01006981 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982 return;
6983
6984 /* Iterate through the headers.
6985 * we start with the start line.
6986 */
6987 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006988 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989
6990 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6991 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006992 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993
6994 cur_hdr = &txn->hdr_idx.v[cur_idx];
6995 cur_ptr = cur_next;
6996 cur_end = cur_ptr + cur_hdr->len;
6997 cur_next = cur_end + cur_hdr->cr + 1;
6998
6999 /* We have one full header between cur_ptr and cur_end, and the
7000 * next header starts at cur_next. We're only interested in
7001 * "Cookie:" headers.
7002 */
7003
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007004 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7005 if (val) {
7006 if ((cur_end - (cur_ptr + val) >= 8) &&
7007 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7008 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7009 return;
7010 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 }
7012
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007013 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7014 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015 continue;
7016
7017 /* OK, right now we know we have a cache-control header at cur_ptr */
7018
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007019 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020
7021 if (p1 >= cur_end) /* no more info */
7022 continue;
7023
7024 /* p1 is at the beginning of the value */
7025 p2 = p1;
7026
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007027 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007028 p2++;
7029
7030 /* we have a complete value between p1 and p2 */
7031 if (p2 < cur_end && *p2 == '=') {
7032 /* we have something of the form no-cache="set-cookie" */
7033 if ((cur_end - p1 >= 21) &&
7034 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7035 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007036 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 continue;
7038 }
7039
7040 /* OK, so we know that either p2 points to the end of string or to a comma */
7041 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7042 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7043 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7044 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007045 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007046 return;
7047 }
7048
7049 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007050 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051 continue;
7052 }
7053 }
7054}
7055
7056
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057/*
7058 * Try to retrieve a known appsession in the URI, then the associated server.
7059 * If the server is found, it's assigned to the session.
7060 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007063 char *end_params, *first_param, *cur_param, *next_param;
7064 char separator;
7065 int value_len;
7066
7067 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007069 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007070 (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 +01007071 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073
Cyril Bontéb21570a2009-11-29 20:04:48 +01007074 first_param = NULL;
7075 switch (mode) {
7076 case PR_O2_AS_M_PP:
7077 first_param = memchr(begin, ';', len);
7078 break;
7079 case PR_O2_AS_M_QS:
7080 first_param = memchr(begin, '?', len);
7081 break;
7082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083
Cyril Bontéb21570a2009-11-29 20:04:48 +01007084 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Cyril Bontéb21570a2009-11-29 20:04:48 +01007088 switch (mode) {
7089 case PR_O2_AS_M_PP:
7090 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7091 end_params = (char *) begin + len;
7092 }
7093 separator = ';';
7094 break;
7095 case PR_O2_AS_M_QS:
7096 end_params = (char *) begin + len;
7097 separator = '&';
7098 break;
7099 default:
7100 /* unknown mode, shouldn't happen */
7101 return;
7102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103
Cyril Bontéb21570a2009-11-29 20:04:48 +01007104 cur_param = next_param = end_params;
7105 while (cur_param > first_param) {
7106 cur_param--;
7107 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7108 /* let's see if this is the appsession parameter */
7109 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7110 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7111 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7112 /* Cool... it's the right one */
7113 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7114 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7115 if (value_len > 0) {
7116 manage_client_side_appsession(t, cur_param, value_len);
7117 }
7118 break;
7119 }
7120 next_param = cur_param;
7121 }
7122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007124 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007125 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007126#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127}
7128
Willy Tarreaub2513902006-12-17 14:52:38 +01007129/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007130 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007131 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007132 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007133 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007134 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007135 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007136 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007137 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007138int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007139{
7140 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007142
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007143 if (!uri_auth)
7144 return 0;
7145
Cyril Bonté70be45d2010-10-12 00:14:35 +02007146 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007147 return 0;
7148
Willy Tarreau295a8372011-03-10 11:25:07 +01007149 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007150 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007153 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007154 return 0;
7155
Willy Tarreau962c3f42010-01-10 00:15:35 +01007156 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007157
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007158 /* the URI is in h */
7159 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007160 return 0;
7161
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007162 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007163 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007164 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007165 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007166 break;
7167 }
7168 h++;
7169 }
7170
7171 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007172 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7173 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007174 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007175 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007176 break;
7177 }
7178 h++;
7179 }
7180 }
7181
Willy Tarreau962c3f42010-01-10 00:15:35 +01007182 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7183 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007184 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007185 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007186 break;
7187 }
7188 h++;
7189 }
7190
Cyril Bonté70be45d2010-10-12 00:14:35 +02007191 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7192 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7193 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007194 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007195 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007196 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7197 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7198 si->applet.ctx.stats.st_code = i;
7199 break;
7200 }
7201 }
7202 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007203 break;
7204 }
7205 h++;
7206 }
7207
Willy Tarreau295a8372011-03-10 11:25:07 +01007208 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007209
Willy Tarreaub2513902006-12-17 14:52:38 +01007210 return 1;
7211}
7212
Willy Tarreau4076a152009-04-02 15:18:36 +02007213/*
7214 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007215 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7216 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007217 */
7218void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7219 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007220 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007221{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007222 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7223 int len1 = buf->size - msg->som;
7224 es->len = buf->r - (buf->data + msg->som) + buf->size;
7225 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7226 if (es->len > len1 && len1 < sizeof(es->buf))
7227 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7228 }
7229 else {
7230 es->len = buf->r - (buf->data + msg->som);
7231 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7232 }
7233
Willy Tarreau4076a152009-04-02 15:18:36 +02007234 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007235 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007236 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007237 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007238 else
7239 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7240
Willy Tarreau4076a152009-04-02 15:18:36 +02007241 es->when = date; // user-visible date
7242 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007243 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007244 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007245 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007246 es->state = state;
7247 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007248 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007249}
Willy Tarreaub2513902006-12-17 14:52:38 +01007250
Willy Tarreau294c4732011-12-16 21:35:50 +01007251/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7252 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7253 * performed over the whole headers. Otherwise it must contain a valid header
7254 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7255 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7256 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7257 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7258 * -1.
7259 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007260 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007261unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7262 struct hdr_idx *idx, int occ,
7263 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007264{
Willy Tarreau294c4732011-12-16 21:35:50 +01007265 struct hdr_ctx local_ctx;
7266 char *ptr_hist[MAX_HDR_HISTORY];
7267 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007268 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007269 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007270
Willy Tarreau294c4732011-12-16 21:35:50 +01007271 if (!ctx) {
7272 local_ctx.idx = 0;
7273 ctx = &local_ctx;
7274 }
7275
Willy Tarreaubce70882009-09-07 11:51:47 +02007276 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007277 /* search from the beginning */
7278 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007279 occ--;
7280 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007281 *vptr = ctx->line + ctx->val;
7282 *vlen = ctx->vlen;
7283 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007284 }
7285 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007286 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007287 }
7288
7289 /* negative occurrence, we scan all the list then walk back */
7290 if (-occ > MAX_HDR_HISTORY)
7291 return 0;
7292
Willy Tarreau294c4732011-12-16 21:35:50 +01007293 found = hist_ptr = 0;
7294 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7295 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7296 len_hist[hist_ptr] = ctx->vlen;
7297 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007298 hist_ptr = 0;
7299 found++;
7300 }
7301 if (-occ > found)
7302 return 0;
7303 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7304 * find occurrence -occ, so we have to check [hist_ptr+occ].
7305 */
7306 hist_ptr += occ;
7307 if (hist_ptr >= MAX_HDR_HISTORY)
7308 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007309 *vptr = ptr_hist[hist_ptr];
7310 *vlen = len_hist[hist_ptr];
7311 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007312}
7313
Willy Tarreaubaaee002006-06-26 02:48:02 +02007314/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 * Print a debug line with a header
7316 */
7317void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7318{
7319 int len, max;
7320 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007321 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 max = end - start;
7323 UBOUND(max, sizeof(trash) - len - 1);
7324 len += strlcpy2(trash + len, start, max + 1);
7325 trash[len++] = '\n';
7326 write(1, trash, len);
7327}
7328
Willy Tarreau0937bc42009-12-22 15:03:09 +01007329/*
7330 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7331 * the required fields are properly allocated and that we only need to (re)init
7332 * them. This should be used before processing any new request.
7333 */
7334void http_init_txn(struct session *s)
7335{
7336 struct http_txn *txn = &s->txn;
7337 struct proxy *fe = s->fe;
7338
7339 txn->flags = 0;
7340 txn->status = -1;
7341
Willy Tarreauf64d1412010-10-07 20:06:11 +02007342 txn->cookie_first_date = 0;
7343 txn->cookie_last_date = 0;
7344
Willy Tarreau0937bc42009-12-22 15:03:09 +01007345 txn->req.sol = txn->req.eol = NULL;
7346 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7347 txn->rsp.sol = txn->rsp.eol = NULL;
7348 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007349 txn->req.chunk_len = 0LL;
7350 txn->req.body_len = 0LL;
7351 txn->rsp.chunk_len = 0LL;
7352 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007353 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7354 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007355
7356 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007357
7358 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7359 if (fe->options2 & PR_O2_REQBUG_OK)
7360 txn->req.err_pos = -1; /* let buggy requests pass */
7361
Willy Tarreau46023632010-01-07 22:51:47 +01007362 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007363 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7364
Willy Tarreau46023632010-01-07 22:51:47 +01007365 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007366 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7367
7368 if (txn->hdr_idx.v)
7369 hdr_idx_init(&txn->hdr_idx);
7370}
7371
7372/* to be used at the end of a transaction */
7373void http_end_txn(struct session *s)
7374{
7375 struct http_txn *txn = &s->txn;
7376
7377 /* these ones will have been dynamically allocated */
7378 pool_free2(pool2_requri, txn->uri);
7379 pool_free2(pool2_capture, txn->cli_cookie);
7380 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007381 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007382
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007383 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007384 txn->uri = NULL;
7385 txn->srv_cookie = NULL;
7386 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007387
7388 if (txn->req.cap) {
7389 struct cap_hdr *h;
7390 for (h = s->fe->req_cap; h; h = h->next)
7391 pool_free2(h->pool, txn->req.cap[h->index]);
7392 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7393 }
7394
7395 if (txn->rsp.cap) {
7396 struct cap_hdr *h;
7397 for (h = s->fe->rsp_cap; h; h = h->next)
7398 pool_free2(h->pool, txn->rsp.cap[h->index]);
7399 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7400 }
7401
Willy Tarreau0937bc42009-12-22 15:03:09 +01007402}
7403
7404/* to be used at the end of a transaction to prepare a new one */
7405void http_reset_txn(struct session *s)
7406{
7407 http_end_txn(s);
7408 http_init_txn(s);
7409
7410 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007411 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007412 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007413 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007414 /* re-init store persistence */
7415 s->store_count = 0;
7416
Willy Tarreau0937bc42009-12-22 15:03:09 +01007417 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007418
7419 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7420
Willy Tarreau739cfba2010-01-25 23:11:14 +01007421 /* We must trim any excess data from the response buffer, because we
7422 * may have blocked an invalid response from a server that we don't
7423 * want to accidentely forward once we disable the analysers, nor do
7424 * we want those data to come along with next response. A typical
7425 * example of such data would be from a buggy server responding to
7426 * a HEAD with some data, or sending more than the advertised
7427 * content-length.
7428 */
7429 if (unlikely(s->rep->l > s->rep->send_max)) {
7430 s->rep->l = s->rep->send_max;
7431 s->rep->r = s->rep->w + s->rep->l;
7432 if (s->rep->r >= s->rep->data + s->rep->size)
7433 s->rep->r -= s->rep->size;
7434 }
7435
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007437 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007438
Willy Tarreaud04e8582010-05-31 12:31:35 +02007439 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007440 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007441
7442 s->req->rex = TICK_ETERNITY;
7443 s->req->wex = TICK_ETERNITY;
7444 s->req->analyse_exp = TICK_ETERNITY;
7445 s->rep->rex = TICK_ETERNITY;
7446 s->rep->wex = TICK_ETERNITY;
7447 s->rep->analyse_exp = TICK_ETERNITY;
7448}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007449
Willy Tarreauff011f22011-01-06 17:51:27 +01007450void free_http_req_rules(struct list *r) {
7451 struct http_req_rule *tr, *pr;
7452
7453 list_for_each_entry_safe(pr, tr, r, list) {
7454 LIST_DEL(&pr->list);
7455 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7456 free(pr->http_auth.realm);
7457
7458 free(pr);
7459 }
7460}
7461
7462struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7463{
7464 struct http_req_rule *rule;
7465 int cur_arg;
7466
7467 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7468 if (!rule) {
7469 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7470 return NULL;
7471 }
7472
7473 if (!*args[0]) {
7474 goto req_error_parsing;
7475 } else if (!strcmp(args[0], "allow")) {
7476 rule->action = HTTP_REQ_ACT_ALLOW;
7477 cur_arg = 1;
7478 } else if (!strcmp(args[0], "deny")) {
7479 rule->action = HTTP_REQ_ACT_DENY;
7480 cur_arg = 1;
7481 } else if (!strcmp(args[0], "auth")) {
7482 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7483 cur_arg = 1;
7484
7485 while(*args[cur_arg]) {
7486 if (!strcmp(args[cur_arg], "realm")) {
7487 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7488 cur_arg+=2;
7489 continue;
7490 } else
7491 break;
7492 }
7493 } else {
7494req_error_parsing:
7495 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7496 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7497 return NULL;
7498 }
7499
7500 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7501 struct acl_cond *cond;
7502
7503 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7504 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7505 file, linenum, args[0]);
7506 return NULL;
7507 }
7508 rule->cond = cond;
7509 }
7510 else if (*args[cur_arg]) {
7511 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7512 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7513 file, linenum, args[0], args[cur_arg]);
7514 return NULL;
7515 }
7516
7517 return rule;
7518}
7519
Willy Tarreau8797c062007-05-07 00:55:35 +02007520/************************************************************************/
7521/* The code below is dedicated to ACL parsing and matching */
7522/************************************************************************/
7523
7524
7525
7526
7527/* 1. Check on METHOD
7528 * We use the pre-parsed method if it is known, and store its number as an
7529 * integer. If it is unknown, we use the pointer and the length.
7530 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007531static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007532{
7533 int len, meth;
7534
Willy Tarreauae8b7962007-06-09 23:10:04 +02007535 len = strlen(*text);
7536 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007537
7538 pattern->val.i = meth;
7539 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007540 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007541 if (!pattern->ptr.str)
7542 return 0;
7543 pattern->len = len;
7544 }
7545 return 1;
7546}
7547
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007548/* This function fetches the method of current HTTP request and stores
7549 * it in the global pattern struct as a chunk. There are two possibilities :
7550 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7551 * in <len> and <ptr> is NULL ;
7552 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7553 * <len> to its length.
7554 * This is intended to be used with acl_match_meth() only.
7555 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007556static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007557acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7558 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007559{
7560 int meth;
7561 struct http_txn *txn = l7;
7562
Willy Tarreaub6866442008-07-14 23:54:42 +02007563 if (!txn)
7564 return 0;
7565
Willy Tarreau655dce92009-11-08 13:10:58 +01007566 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007567 return 0;
7568
Willy Tarreau8797c062007-05-07 00:55:35 +02007569 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007570 temp_pattern.data.str.len = meth;
7571 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007572 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007573 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7574 /* ensure the indexes are not affected */
7575 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007576 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7577 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007578 }
7579 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7580 return 1;
7581}
7582
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007583/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007584static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7585{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007586 int icase;
7587
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007588
7589 if (temp_pattern.data.str.str == NULL) {
7590 /* well-known method */
7591 if (temp_pattern.data.str.len == pattern->val.i)
7592 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007593 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007594 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007595
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007596 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7597 if (pattern->val.i != HTTP_METH_OTHER)
7598 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007599
7600 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007601 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007602 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007603
7604 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007605 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7606 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007607 return ACL_PAT_FAIL;
7608 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007609}
7610
7611/* 2. Check on Request/Status Version
7612 * We simply compare strings here.
7613 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007614static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007615{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007616 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007617 if (!pattern->ptr.str)
7618 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007619 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007620 return 1;
7621}
7622
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007623static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007624acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7625 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007626{
7627 struct http_txn *txn = l7;
7628 char *ptr;
7629 int len;
7630
Willy Tarreaub6866442008-07-14 23:54:42 +02007631 if (!txn)
7632 return 0;
7633
Willy Tarreau655dce92009-11-08 13:10:58 +01007634 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007635 return 0;
7636
Willy Tarreau8797c062007-05-07 00:55:35 +02007637 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007638 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007639
7640 while ((len-- > 0) && (*ptr++ != '/'));
7641 if (len <= 0)
7642 return 0;
7643
Willy Tarreau664092c2011-12-16 19:11:42 +01007644 temp_pattern.data.str.str = ptr;
7645 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007646
7647 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7648 return 1;
7649}
7650
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007651static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007652acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7653 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007654{
7655 struct http_txn *txn = l7;
7656 char *ptr;
7657 int len;
7658
Willy Tarreaub6866442008-07-14 23:54:42 +02007659 if (!txn)
7660 return 0;
7661
Willy Tarreau655dce92009-11-08 13:10:58 +01007662 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007663 return 0;
7664
Willy Tarreau8797c062007-05-07 00:55:35 +02007665 len = txn->rsp.sl.st.v_l;
7666 ptr = txn->rsp.sol;
7667
7668 while ((len-- > 0) && (*ptr++ != '/'));
7669 if (len <= 0)
7670 return 0;
7671
Willy Tarreau664092c2011-12-16 19:11:42 +01007672 temp_pattern.data.str.str = ptr;
7673 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007674
7675 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7676 return 1;
7677}
7678
7679/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007680static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007681acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7682 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007683{
7684 struct http_txn *txn = l7;
7685 char *ptr;
7686 int len;
7687
Willy Tarreaub6866442008-07-14 23:54:42 +02007688 if (!txn)
7689 return 0;
7690
Willy Tarreau655dce92009-11-08 13:10:58 +01007691 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007692 return 0;
7693
Willy Tarreau8797c062007-05-07 00:55:35 +02007694 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007695 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007696
Willy Tarreaua5e37562011-12-16 17:06:15 +01007697 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007698 test->flags = ACL_TEST_F_VOL_1ST;
7699 return 1;
7700}
7701
7702/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007703static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007704acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7705 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007706{
7707 struct http_txn *txn = l7;
7708
Willy Tarreaub6866442008-07-14 23:54:42 +02007709 if (!txn)
7710 return 0;
7711
Willy Tarreau655dce92009-11-08 13:10:58 +01007712 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007713 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007714
Willy Tarreauc11416f2007-06-17 16:58:38 +02007715 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7716 /* ensure the indexes are not affected */
7717 return 0;
7718
Willy Tarreau664092c2011-12-16 19:11:42 +01007719 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7720 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007721
Willy Tarreauf3d25982007-05-08 22:45:09 +02007722 /* we do not need to set READ_ONLY because the data is in a buffer */
7723 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007724 return 1;
7725}
7726
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007727static int
7728acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7729 struct acl_expr *expr, struct acl_test *test)
7730{
7731 struct http_txn *txn = l7;
7732
Willy Tarreaub6866442008-07-14 23:54:42 +02007733 if (!txn)
7734 return 0;
7735
Willy Tarreau655dce92009-11-08 13:10:58 +01007736 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007737 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007738
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007739 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7740 /* ensure the indexes are not affected */
7741 return 0;
7742
7743 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007744 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 +01007745 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7746 return 0;
7747 temp_pattern.type = PATTERN_TYPE_IP;
7748 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007749
7750 /*
7751 * If we are parsing url in frontend space, we prepare backend stage
7752 * to not parse again the same url ! optimization lazyness...
7753 */
7754 if (px->options & PR_O_HTTP_PROXY)
7755 l4->flags |= SN_ADDR_SET;
7756
Willy Tarreauf4362b32011-12-16 17:49:52 +01007757 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007758 return 1;
7759}
7760
7761static int
7762acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7763 struct acl_expr *expr, struct acl_test *test)
7764{
7765 struct http_txn *txn = l7;
7766
Willy Tarreaub6866442008-07-14 23:54:42 +02007767 if (!txn)
7768 return 0;
7769
Willy Tarreau655dce92009-11-08 13:10:58 +01007770 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007771 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007772
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007773 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7774 /* ensure the indexes are not affected */
7775 return 0;
7776
7777 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007778 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 +01007779 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007780
7781 if (px->options & PR_O_HTTP_PROXY)
7782 l4->flags |= SN_ADDR_SET;
7783
7784 test->flags = ACL_TEST_F_READ_ONLY;
7785 return 1;
7786}
7787
Willy Tarreauc11416f2007-06-17 16:58:38 +02007788/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7789 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7790 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007791static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007792acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007793 struct acl_expr *expr, struct acl_test *test)
7794{
7795 struct http_txn *txn = l7;
7796 struct hdr_idx *idx = &txn->hdr_idx;
7797 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007798
Willy Tarreaub6866442008-07-14 23:54:42 +02007799 if (!txn)
7800 return 0;
7801
Willy Tarreau33a7e692007-06-10 19:45:56 +02007802 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7803 /* search for header from the beginning */
7804 ctx->idx = 0;
7805
Willy Tarreau33a7e692007-06-10 19:45:56 +02007806 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7807 test->flags |= ACL_TEST_F_FETCH_MORE;
7808 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007809 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7810 temp_pattern.data.str.len = ctx->vlen;
7811
Willy Tarreau33a7e692007-06-10 19:45:56 +02007812 return 1;
7813 }
7814
7815 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7816 test->flags |= ACL_TEST_F_VOL_HDR;
7817 return 0;
7818}
7819
Willy Tarreau33a7e692007-06-10 19:45:56 +02007820static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7822 struct acl_expr *expr, struct acl_test *test)
7823{
7824 struct http_txn *txn = l7;
7825
Willy Tarreaub6866442008-07-14 23:54:42 +02007826 if (!txn)
7827 return 0;
7828
Willy Tarreau655dce92009-11-08 13:10:58 +01007829 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007830 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007831
Willy Tarreauc11416f2007-06-17 16:58:38 +02007832 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7833 /* ensure the indexes are not affected */
7834 return 0;
7835
7836 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7837}
7838
7839static int
7840acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7841 struct acl_expr *expr, struct acl_test *test)
7842{
7843 struct http_txn *txn = l7;
7844
Willy Tarreaub6866442008-07-14 23:54:42 +02007845 if (!txn)
7846 return 0;
7847
Willy Tarreau655dce92009-11-08 13:10:58 +01007848 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007849 return 0;
7850
7851 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7852}
7853
7854/* 6. Check on HTTP header count. The number of occurrences is returned.
7855 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7856 */
7857static int
7858acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007859 struct acl_expr *expr, struct acl_test *test)
7860{
7861 struct http_txn *txn = l7;
7862 struct hdr_idx *idx = &txn->hdr_idx;
7863 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007864 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007865
Willy Tarreaub6866442008-07-14 23:54:42 +02007866 if (!txn)
7867 return 0;
7868
Willy Tarreau33a7e692007-06-10 19:45:56 +02007869 ctx.idx = 0;
7870 cnt = 0;
7871 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7872 cnt++;
7873
Willy Tarreaua5e37562011-12-16 17:06:15 +01007874 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007875 test->flags = ACL_TEST_F_VOL_HDR;
7876 return 1;
7877}
7878
Willy Tarreauc11416f2007-06-17 16:58:38 +02007879static int
7880acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7881 struct acl_expr *expr, struct acl_test *test)
7882{
7883 struct http_txn *txn = l7;
7884
Willy Tarreaub6866442008-07-14 23:54:42 +02007885 if (!txn)
7886 return 0;
7887
Willy Tarreau655dce92009-11-08 13:10:58 +01007888 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007889 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007890
Willy Tarreauc11416f2007-06-17 16:58:38 +02007891 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7892 /* ensure the indexes are not affected */
7893 return 0;
7894
7895 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7896}
7897
7898static int
7899acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7900 struct acl_expr *expr, struct acl_test *test)
7901{
7902 struct http_txn *txn = l7;
7903
Willy Tarreaub6866442008-07-14 23:54:42 +02007904 if (!txn)
7905 return 0;
7906
Willy Tarreau655dce92009-11-08 13:10:58 +01007907 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007908 return 0;
7909
7910 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7911}
7912
Willy Tarreau33a7e692007-06-10 19:45:56 +02007913/* 7. Check on HTTP header's integer value. The integer value is returned.
7914 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007915 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007916 */
7917static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007918acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007919 struct acl_expr *expr, struct acl_test *test)
7920{
7921 struct http_txn *txn = l7;
7922 struct hdr_idx *idx = &txn->hdr_idx;
7923 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924
Willy Tarreaub6866442008-07-14 23:54:42 +02007925 if (!txn)
7926 return 0;
7927
Willy Tarreau33a7e692007-06-10 19:45:56 +02007928 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7929 /* search for header from the beginning */
7930 ctx->idx = 0;
7931
Willy Tarreau33a7e692007-06-10 19:45:56 +02007932 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7933 test->flags |= ACL_TEST_F_FETCH_MORE;
7934 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007935 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007936 return 1;
7937 }
7938
7939 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7940 test->flags |= ACL_TEST_F_VOL_HDR;
7941 return 0;
7942}
7943
Willy Tarreauc11416f2007-06-17 16:58:38 +02007944static int
7945acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7946 struct acl_expr *expr, struct acl_test *test)
7947{
7948 struct http_txn *txn = l7;
7949
Willy Tarreaub6866442008-07-14 23:54:42 +02007950 if (!txn)
7951 return 0;
7952
Willy Tarreau655dce92009-11-08 13:10:58 +01007953 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007954 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007955
Willy Tarreauc11416f2007-06-17 16:58:38 +02007956 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7957 /* ensure the indexes are not affected */
7958 return 0;
7959
7960 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7961}
7962
7963static int
7964acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7965 struct acl_expr *expr, struct acl_test *test)
7966{
7967 struct http_txn *txn = l7;
7968
Willy Tarreaub6866442008-07-14 23:54:42 +02007969 if (!txn)
7970 return 0;
7971
Willy Tarreau655dce92009-11-08 13:10:58 +01007972 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007973 return 0;
7974
7975 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7976}
7977
Willy Tarreau106f9792009-09-19 07:54:16 +02007978/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7979 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7980 */
7981static int
7982acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7983 struct acl_expr *expr, struct acl_test *test)
7984{
7985 struct http_txn *txn = l7;
7986 struct hdr_idx *idx = &txn->hdr_idx;
7987 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7988
7989 if (!txn)
7990 return 0;
7991
7992 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7993 /* search for header from the beginning */
7994 ctx->idx = 0;
7995
Willy Tarreauf4362b32011-12-16 17:49:52 +01007996 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007997 test->flags |= ACL_TEST_F_FETCH_MORE;
7998 test->flags |= ACL_TEST_F_VOL_HDR;
7999 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008000 temp_pattern.type = PATTERN_TYPE_IP;
8001 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8002 return 1;
8003 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008004 }
8005
8006 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8007 test->flags |= ACL_TEST_F_VOL_HDR;
8008 return 0;
8009}
8010
8011static int
8012acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8013 struct acl_expr *expr, struct acl_test *test)
8014{
8015 struct http_txn *txn = l7;
8016
8017 if (!txn)
8018 return 0;
8019
Willy Tarreau655dce92009-11-08 13:10:58 +01008020 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008021 return 0;
8022
8023 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8024 /* ensure the indexes are not affected */
8025 return 0;
8026
8027 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8028}
8029
8030static int
8031acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8032 struct acl_expr *expr, struct acl_test *test)
8033{
8034 struct http_txn *txn = l7;
8035
8036 if (!txn)
8037 return 0;
8038
Willy Tarreau655dce92009-11-08 13:10:58 +01008039 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008040 return 0;
8041
8042 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8043}
8044
Willy Tarreau737b0c12007-06-10 21:28:46 +02008045/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8046 * the first '/' after the possible hostname, and ends before the possible '?'.
8047 */
8048static int
8049acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8050 struct acl_expr *expr, struct acl_test *test)
8051{
8052 struct http_txn *txn = l7;
8053 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008054
Willy Tarreaub6866442008-07-14 23:54:42 +02008055 if (!txn)
8056 return 0;
8057
Willy Tarreau655dce92009-11-08 13:10:58 +01008058 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008059 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008060
Willy Tarreauc11416f2007-06-17 16:58:38 +02008061 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8062 /* ensure the indexes are not affected */
8063 return 0;
8064
Willy Tarreau962c3f42010-01-10 00:15:35 +01008065 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008066 ptr = http_get_path(txn);
8067 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008068 return 0;
8069
8070 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008071 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008072
8073 while (ptr < end && *ptr != '?')
8074 ptr++;
8075
Willy Tarreau664092c2011-12-16 19:11:42 +01008076 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008077
8078 /* we do not need to set READ_ONLY because the data is in a buffer */
8079 test->flags = ACL_TEST_F_VOL_1ST;
8080 return 1;
8081}
8082
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008083static int
8084acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8085 struct acl_expr *expr, struct acl_test *test)
8086{
8087 struct buffer *req = s->req;
8088 struct http_txn *txn = &s->txn;
8089 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008090
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008091 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8092 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8093 */
8094
8095 if (!s || !req)
8096 return 0;
8097
Willy Tarreau655dce92009-11-08 13:10:58 +01008098 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008099 /* Already decoded as OK */
8100 test->flags |= ACL_TEST_F_SET_RES_PASS;
8101 return 1;
8102 }
8103
8104 /* Try to decode HTTP request */
8105 if (likely(req->lr < req->r))
8106 http_msg_analyzer(req, msg, &txn->hdr_idx);
8107
Willy Tarreau655dce92009-11-08 13:10:58 +01008108 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008109 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8110 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8111 return 1;
8112 }
8113 /* wait for final state */
8114 test->flags |= ACL_TEST_F_MAY_CHANGE;
8115 return 0;
8116 }
8117
8118 /* OK we got a valid HTTP request. We have some minor preparation to
8119 * perform so that further checks can rely on HTTP tests.
8120 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008121 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008122 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8123 s->flags |= SN_REDIRECTABLE;
8124
8125 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8126 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8127 return 1;
8128 }
8129
8130 test->flags |= ACL_TEST_F_SET_RES_PASS;
8131 return 1;
8132}
8133
Willy Tarreau7f18e522010-10-22 20:04:13 +02008134/* return a valid test if the current request is the first one on the connection */
8135static int
8136acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8137 struct acl_expr *expr, struct acl_test *test)
8138{
8139 if (!s)
8140 return 0;
8141
8142 if (s->txn.flags & TX_NOT_FIRST)
8143 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8144 else
8145 test->flags |= ACL_TEST_F_SET_RES_PASS;
8146
8147 return 1;
8148}
8149
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008150static int
8151acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8152 struct acl_expr *expr, struct acl_test *test)
8153{
8154
8155 if (!s)
8156 return 0;
8157
8158 if (!get_http_auth(s))
8159 return 0;
8160
8161 test->ctx.a[0] = expr->arg.ul;
8162 test->ctx.a[1] = s->txn.auth.user;
8163 test->ctx.a[2] = s->txn.auth.pass;
8164
8165 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8166
8167 return 1;
8168}
Willy Tarreau8797c062007-05-07 00:55:35 +02008169
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008170/* Try to find the next occurrence of a cookie name in a cookie header value.
8171 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8172 * the cookie value is returned into *value and *value_l, and the function
8173 * returns a pointer to the next pointer to search from if the value was found.
8174 * Otherwise if the cookie was not found, NULL is returned and neither value
8175 * nor value_l are touched. The input <hdr> string should first point to the
8176 * header's value, and the <hdr_end> pointer must point to the first character
8177 * not part of the value. <list> must be non-zero if value may represent a list
8178 * of values (cookie headers). This makes it faster to abort parsing when no
8179 * list is expected.
8180 */
8181static char *
8182extract_cookie_value(char *hdr, const char *hdr_end,
8183 char *cookie_name, size_t cookie_name_l, int list,
8184 char **value, size_t *value_l)
8185{
8186 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8187 char *next;
8188
8189 /* we search at least a cookie name followed by an equal, and more
8190 * generally something like this :
8191 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8192 */
8193 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8194 /* Iterate through all cookies on this line */
8195
8196 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8197 att_beg++;
8198
8199 /* find att_end : this is the first character after the last non
8200 * space before the equal. It may be equal to hdr_end.
8201 */
8202 equal = att_end = att_beg;
8203
8204 while (equal < hdr_end) {
8205 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8206 break;
8207 if (http_is_spht[(unsigned char)*equal++])
8208 continue;
8209 att_end = equal;
8210 }
8211
8212 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8213 * is between <att_beg> and <equal>, both may be identical.
8214 */
8215
8216 /* look for end of cookie if there is an equal sign */
8217 if (equal < hdr_end && *equal == '=') {
8218 /* look for the beginning of the value */
8219 val_beg = equal + 1;
8220 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8221 val_beg++;
8222
8223 /* find the end of the value, respecting quotes */
8224 next = find_cookie_value_end(val_beg, hdr_end);
8225
8226 /* make val_end point to the first white space or delimitor after the value */
8227 val_end = next;
8228 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8229 val_end--;
8230 } else {
8231 val_beg = val_end = next = equal;
8232 }
8233
8234 /* We have nothing to do with attributes beginning with '$'. However,
8235 * they will automatically be removed if a header before them is removed,
8236 * since they're supposed to be linked together.
8237 */
8238 if (*att_beg == '$')
8239 continue;
8240
8241 /* Ignore cookies with no equal sign */
8242 if (equal == next)
8243 continue;
8244
8245 /* Now we have the cookie name between att_beg and att_end, and
8246 * its value between val_beg and val_end.
8247 */
8248
8249 if (att_end - att_beg == cookie_name_l &&
8250 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8251 /* let's return this value and indicate where to go on from */
8252 *value = val_beg;
8253 *value_l = val_end - val_beg;
8254 return next + 1;
8255 }
8256
8257 /* Set-Cookie headers only have the name in the first attr=value part */
8258 if (!list)
8259 break;
8260 }
8261
8262 return NULL;
8263}
8264
8265/* Iterate over all cookies present in a request. The context is stored in
8266 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8267 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8268 * non-null, then multiple cookies may be parsed on the same line.
8269 * The cookie name is in expr->arg and the name length in expr->arg_len.
8270 */
8271static int
8272acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8273 const char *hdr_name, int hdr_name_len, int multi,
8274 struct acl_expr *expr, struct acl_test *test)
8275{
8276 struct http_txn *txn = l7;
8277 struct hdr_idx *idx = &txn->hdr_idx;
8278 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8279
8280 if (!txn)
8281 return 0;
8282
8283 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8284 /* search for the header from the beginning, we must first initialize
8285 * the search parameters.
8286 */
8287 test->ctx.a[0] = NULL;
8288 ctx->idx = 0;
8289 }
8290
8291 while (1) {
8292 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8293 if (!test->ctx.a[0]) {
8294 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8295 goto out;
8296
8297 if (ctx->vlen < expr->arg_len + 1)
8298 continue;
8299
8300 test->ctx.a[0] = ctx->line + ctx->val;
8301 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8302 }
8303
8304 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8305 expr->arg.str, expr->arg_len, multi,
8306 &temp_pattern.data.str.str,
8307 &temp_pattern.data.str.len);
8308 if (test->ctx.a[0]) {
8309 /* one value was returned into temp_pattern.data.str.{str,len} */
8310 test->flags |= ACL_TEST_F_FETCH_MORE;
8311 test->flags |= ACL_TEST_F_VOL_HDR;
8312 return 1;
8313 }
8314 }
8315
8316 out:
8317 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8318 test->flags |= ACL_TEST_F_VOL_HDR;
8319 return 0;
8320}
8321
8322static int
8323acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8324 struct acl_expr *expr, struct acl_test *test)
8325{
8326 struct http_txn *txn = l7;
8327
8328 if (!txn)
8329 return 0;
8330
8331 if (txn->req.msg_state < HTTP_MSG_BODY)
8332 return 0;
8333
8334 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8335 /* ensure the indexes are not affected */
8336 return 0;
8337
8338 /* The Cookie header allows multiple cookies on the same line */
8339 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8340}
8341
8342static int
8343acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8344 struct acl_expr *expr, struct acl_test *test)
8345{
8346 struct http_txn *txn = l7;
8347
8348 if (!txn)
8349 return 0;
8350
8351 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8352 return 0;
8353
8354 /* The Set-Cookie header allows only one cookie on the same line */
8355 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8356}
8357
8358/* Iterate over all cookies present in a request to count how many occurrences
8359 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8360 * multiple cookies may be parsed on the same line.
8361 */
8362static int
8363acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8364 const char *hdr_name, int hdr_name_len, int multi,
8365 struct acl_expr *expr, struct acl_test *test)
8366{
8367 struct http_txn *txn = l7;
8368 struct hdr_idx *idx = &txn->hdr_idx;
8369 struct hdr_ctx ctx;
8370 int cnt;
8371 char *val_beg, *val_end;
8372
8373 if (!txn)
8374 return 0;
8375
8376 val_beg = NULL;
8377 ctx.idx = 0;
8378 cnt = 0;
8379
8380 while (1) {
8381 /* Note: val_beg == NULL every time we need to fetch a new header */
8382 if (!val_beg) {
8383 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8384 break;
8385
8386 if (ctx.vlen < expr->arg_len + 1)
8387 continue;
8388
8389 val_beg = ctx.line + ctx.val;
8390 val_end = val_beg + ctx.vlen;
8391 }
8392
8393 while ((val_beg = extract_cookie_value(val_beg, val_end,
8394 expr->arg.str, expr->arg_len, multi,
8395 &temp_pattern.data.str.str,
8396 &temp_pattern.data.str.len))) {
8397 cnt++;
8398 }
8399 }
8400
8401 temp_pattern.data.integer = cnt;
8402 test->flags |= ACL_TEST_F_VOL_HDR;
8403 return 1;
8404}
8405
8406static int
8407acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8408 struct acl_expr *expr, struct acl_test *test)
8409{
8410 struct http_txn *txn = l7;
8411
8412 if (!txn)
8413 return 0;
8414
8415 if (txn->req.msg_state < HTTP_MSG_BODY)
8416 return 0;
8417
8418 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8419 /* ensure the indexes are not affected */
8420 return 0;
8421
8422 /* The Cookie header allows multiple cookies on the same line */
8423 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8424}
8425
8426static int
8427acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8428 struct acl_expr *expr, struct acl_test *test)
8429{
8430 struct http_txn *txn = l7;
8431
8432 if (!txn)
8433 return 0;
8434
8435 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8436 return 0;
8437
8438 /* The Set-Cookie header allows only one cookie on the same line */
8439 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8440}
8441
Willy Tarreau8797c062007-05-07 00:55:35 +02008442/************************************************************************/
8443/* All supported keywords must be declared here. */
8444/************************************************************************/
8445
8446/* Note: must not be declared <const> as its list will be overwritten */
8447static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008448 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8449
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008450 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008451 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8452 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008453 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008454
Willy Tarreauc4262962010-05-10 23:42:40 +02008455 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008456 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8457 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8458 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8459 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8460 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8461 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008462 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008463 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008464 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008465
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008466 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008467 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008468 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8469 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8470 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8471 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8472 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8473 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8474 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008475 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008476 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008477 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008478
Willy Tarreauc4262962010-05-10 23:42:40 +02008479 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008480 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8481 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8482 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8483 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8484 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8485 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8486 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008487 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008488 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008489 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008490
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008491 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8492 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8493 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8494 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8495 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8496 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8497 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8498 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8499 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8500
8501 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8502 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8503 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8504 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8505 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8506 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8507 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8508 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8509 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8510
Willy Tarreauc4262962010-05-10 23:42:40 +02008511 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008512 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8513 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8514 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8515 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8516 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8517 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008518 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008519
Willy Tarreau7f18e522010-10-22 20:04:13 +02008520 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8521 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8522 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008523 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008524}};
8525
Willy Tarreau4a568972010-05-12 08:08:50 +02008526/************************************************************************/
8527/* The code below is dedicated to pattern fetching and matching */
8528/************************************************************************/
8529
Willy Tarreaue428fb72011-12-16 21:50:30 +01008530/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008531static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008532pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8533 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008534{
8535 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008536
Willy Tarreaue428fb72011-12-16 21:50:30 +01008537 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8538 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008539}
8540
David Cournapeau16023ee2010-12-23 20:55:41 +09008541/*
8542 * Given a path string and its length, find the position of beginning of the
8543 * query string. Returns NULL if no query string is found in the path.
8544 *
8545 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8546 *
8547 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8548 */
8549static inline char *find_query_string(char *path, size_t path_l)
8550{
8551 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008552
David Cournapeau16023ee2010-12-23 20:55:41 +09008553 p = memchr(path, '?', path_l);
8554 return p ? p + 1 : NULL;
8555}
8556
8557static inline int is_param_delimiter(char c)
8558{
8559 return c == '&' || c == ';';
8560}
8561
8562/*
8563 * Given a url parameter, find the starting position of the first occurence,
8564 * or NULL if the parameter is not found.
8565 *
8566 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8567 * the function will return query_string+8.
8568 */
8569static char*
8570find_url_param_pos(char* query_string, size_t query_string_l,
8571 char* url_param_name, size_t url_param_name_l)
8572{
8573 char *pos, *last;
8574
8575 pos = query_string;
8576 last = query_string + query_string_l - url_param_name_l - 1;
8577
8578 while (pos <= last) {
8579 if (pos[url_param_name_l] == '=') {
8580 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8581 return pos;
8582 pos += url_param_name_l + 1;
8583 }
8584 while (pos <= last && !is_param_delimiter(*pos))
8585 pos++;
8586 pos++;
8587 }
8588 return NULL;
8589}
8590
8591/*
8592 * Given a url parameter name, returns its value and size into *value and
8593 * *value_l respectively, and returns non-zero. If the parameter is not found,
8594 * zero is returned and value/value_l are not touched.
8595 */
8596static int
8597find_url_param_value(char* path, size_t path_l,
8598 char* url_param_name, size_t url_param_name_l,
8599 char** value, size_t* value_l)
8600{
8601 char *query_string, *qs_end;
8602 char *arg_start;
8603 char *value_start, *value_end;
8604
8605 query_string = find_query_string(path, path_l);
8606 if (!query_string)
8607 return 0;
8608
8609 qs_end = path + path_l;
8610 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8611 url_param_name, url_param_name_l);
8612 if (!arg_start)
8613 return 0;
8614
8615 value_start = arg_start + url_param_name_l + 1;
8616 value_end = value_start;
8617
8618 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8619 value_end++;
8620
8621 *value = value_start;
8622 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008623 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008624}
8625
8626static int
8627pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8628 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8629{
8630 struct http_txn *txn = l7;
8631 struct http_msg *msg = &txn->req;
8632 char *url_param_value;
8633 size_t url_param_value_l;
8634
8635 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8636 arg_p->data.str.str, arg_p->data.str.len,
8637 &url_param_value, &url_param_value_l))
8638 return 0;
8639
8640 data->str.str = url_param_value;
8641 data->str.len = url_param_value_l;
8642 return 1;
8643}
8644
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008645/* Try to find in request or response message is in <msg> and whose transaction
8646 * is in <txn> the last occurrence of a cookie name in all cookie header values
8647 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8648 * pointer and size of the last occurrence of the cookie value is returned into
8649 * <value> and <value_l>, and the function returns non-zero if the value was
8650 * found. Otherwise if the cookie was not found, zero is returned and neither
8651 * value nor value_l are touched. The input hdr string should begin at the
8652 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8653 * value may represent a list of values (cookie headers).
8654 */
8655
8656static int
8657find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8658 const char *hdr_name, int hdr_name_len,
8659 char *cookie_name, size_t cookie_name_l, int list,
8660 char **value, size_t *value_l)
8661{
8662 struct hdr_ctx ctx;
8663 int found = 0;
8664
8665 ctx.idx = 0;
8666 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008667 char *hdr, *end;
8668
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008669 if (ctx.vlen < cookie_name_l + 1)
8670 continue;
8671
Willy Tarreau4573af92012-04-06 18:20:06 +02008672 hdr = ctx.line + ctx.val;
8673 end = hdr + ctx.vlen;
8674 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8675 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008676 }
8677 return found;
8678}
8679
8680static int
8681pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8682 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8683{
8684 struct http_txn *txn = l7;
8685 struct http_msg *msg = &txn->req;
8686 char *cookie_value;
8687 size_t cookie_value_l;
8688 int found = 0;
8689
8690 found = find_cookie_value(msg, txn, "Cookie", 6,
8691 arg_p->data.str.str, arg_p->data.str.len, 1,
8692 &cookie_value, &cookie_value_l);
8693 if (found) {
8694 data->str.str = cookie_value;
8695 data->str.len = cookie_value_l;
8696 }
8697
8698 return found;
8699}
8700
8701
8702static int
8703pattern_fetch_set_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->rsp;
8708 char *cookie_value;
8709 size_t cookie_value_l;
8710 int found = 0;
8711
8712 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
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
Emeric Brun485479d2010-09-23 18:02:19 +02008723
Willy Tarreau4a568972010-05-12 08:08:50 +02008724/************************************************************************/
8725/* All supported keywords must be declared here. */
8726/************************************************************************/
8727/* Note: must not be declared <const> as its list will be overwritten */
8728static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008729 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008730 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008731 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8732 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008733 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008734}};
8735
Willy Tarreau8797c062007-05-07 00:55:35 +02008736
8737__attribute__((constructor))
8738static void __http_protocol_init(void)
8739{
8740 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008741 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008742}
8743
8744
Willy Tarreau58f10d72006-12-04 02:26:12 +01008745/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008746 * Local variables:
8747 * c-indent-level: 8
8748 * c-basic-offset: 8
8749 * End:
8750 */