blob: 4dcefca2da435d51bafef57919a4032df2db4083 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 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 Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%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",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 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);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %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",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 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);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 else if (*s == '"') quoted = 1;
483 else if (*s == ',') return s;
484 }
485 return s;
486}
487
488/* Find the first or next occurrence of header <name> in message buffer <sol>
489 * using headers index <idx>, and return it in the <ctx> structure. This
490 * structure holds everything necessary to use the header and find next
491 * occurrence. If its <idx> member is 0, the header is searched from the
492 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100493 * 1 when it finds a value, and 0 when there is no more. It is designed to work
494 * with headers defined as comma-separated lists. As a special case, if ctx->val
495 * is NULL when searching for a new values of a header, the current header is
496 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 eol = sol + idx->v[cur_idx].len;
514
515 if (sov >= eol)
516 /* no more values in this header */
517 goto next_hdr;
518
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 sov++;
523 while (sov < eol && http_is_lws[(unsigned char)*sov])
524 sov++;
525
526 goto return_hdr;
527 }
528
529 /* first request for this header */
530 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 if (len == 0) {
537 /* No argument was passed, we want any header.
538 * To achieve this, we simply build a fake request. */
539 while (sol + len < eol && sol[len] != ':')
540 len++;
541 name = sol;
542 }
543
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 ctx->idx = cur_idx;
556 ctx->val = sov - sol;
557
558 eol = find_hdr_value_end(sov, eol);
559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
604 ctx->idx = ctx->prev; /* walk back to the end of previous header */
605 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
606 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
607 ctx->vlen = 0;
608 return ctx->idx;
609 }
610
611 /* This was not the only value of this header. We have to remove between
612 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
613 * of the list, we remove the last separator.
614 */
615
616 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
617 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
618 sol + ctx->val + ctx->vlen + skip_comma,
619 NULL, 0);
620 hdr->len += delta;
621 http_msg_move_end(msg, delta);
622 ctx->val = ctx->del;
623 ctx->vlen = 0;
624 return ctx->idx;
625}
626
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100627/* This function handles a server error at the stream interface level. The
628 * stream interface is assumed to be already in a closed state. An optional
629 * message is copied into the input buffer, and an HTTP status code stored.
630 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100631 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100633static void http_server_error(struct session *t, struct stream_interface *si,
634 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100636 buffer_auto_read(si->ob);
637 buffer_abort(si->ob);
638 buffer_auto_close(si->ob);
639 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200640 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100641 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100642 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100643 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
646 if (!(t->flags & SN_ERR_MASK))
647 t->flags |= err;
648 if (!(t->flags & SN_FINST_MASK))
649 t->flags |= finst;
650}
651
Willy Tarreau80587432006-12-24 17:47:20 +0100652/* This function returns the appropriate error location for the given session
653 * and message.
654 */
655
656struct chunk *error_message(struct session *s, int msgnum)
657{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200658 if (s->be->errmsg[msgnum].str)
659 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100660 else if (s->fe->errmsg[msgnum].str)
661 return &s->fe->errmsg[msgnum];
662 else
663 return &http_err_chunks[msgnum];
664}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665
Willy Tarreau53b6c742006-12-17 13:37:46 +0100666/*
667 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
668 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
669 */
670static http_meth_t find_http_meth(const char *str, const int len)
671{
672 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100673 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100674
675 m = ((unsigned)*str - 'A');
676
677 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 for (h = http_methods[m]; h->len > 0; h++) {
679 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100681 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683 };
684 return HTTP_METH_OTHER;
685 }
686 return HTTP_METH_NONE;
687
688}
689
Willy Tarreau21d2af32008-02-14 20:25:24 +0100690/* Parse the URI from the given transaction (which is assumed to be in request
691 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
692 * It is returned otherwise.
693 */
694static char *
695http_get_path(struct http_txn *txn)
696{
697 char *ptr, *end;
698
Willy Tarreau962c3f42010-01-10 00:15:35 +0100699 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100700 end = ptr + txn->req.sl.rq.u_l;
701
702 if (ptr >= end)
703 return NULL;
704
705 /* RFC2616, par. 5.1.2 :
706 * Request-URI = "*" | absuri | abspath | authority
707 */
708
709 if (*ptr == '*')
710 return NULL;
711
712 if (isalpha((unsigned char)*ptr)) {
713 /* this is a scheme as described by RFC3986, par. 3.1 */
714 ptr++;
715 while (ptr < end &&
716 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
717 ptr++;
718 /* skip '://' */
719 if (ptr == end || *ptr++ != ':')
720 return NULL;
721 if (ptr == end || *ptr++ != '/')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 }
726 /* skip [user[:passwd]@]host[:[port]] */
727
728 while (ptr < end && *ptr != '/')
729 ptr++;
730
731 if (ptr == end)
732 return NULL;
733
734 /* OK, we got the '/' ! */
735 return ptr;
736}
737
Willy Tarreauefb453c2008-10-26 20:49:47 +0100738/* Returns a 302 for a redirectable request. This may only be called just after
739 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
740 * left unchanged and will follow normal proxy processing.
741 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743{
744 struct http_txn *txn;
745 struct chunk rdr;
746 char *path;
747 int len;
748
749 /* 1: create the response header */
750 rdr.len = strlen(HTTP_302);
751 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100752 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753 memcpy(rdr.str, HTTP_302, rdr.len);
754
755 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200756 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
760 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
762 rdr.len += s->srv->rdr_len;
763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
797 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100798 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
842 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
843 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100849 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
856 char *svid;
857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
868 if (s->cli_addr.ss_family == AF_INET)
869 inet_ntop(AF_INET,
870 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
871 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200872 else if (s->cli_addr.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 inet_ntop(AF_INET6,
874 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
875 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200876 else
877 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200878
879 get_gmtime(s->logs.accept_date.tv_sec, &tm);
880
881 /* FIXME: let's limit ourselves to frontend logging for now. */
882 tolog = fe->to_log;
883
884 h = tmpline;
885
886 w = snprintf(h, sizeof(tmpline),
887 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
888 pn,
889 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
890 tm.tm_hour, tm.tm_min, tm.tm_sec);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 4)
896 goto trunc;
897
898 *(h++) = ' ';
899 *(h++) = '\"';
900 uri = txn->uri ? txn->uri : "<BADREQ>";
901 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
902 '#', url_encode_map, uri);
903 *(h++) = '\"';
904
905 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 if (h >= tmpline + sizeof(tmpline) - 9)
911 goto trunc;
912 memcpy(h, " \"-\" \"-\"", 8);
913 h += 8;
914
915 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
916 " %d %03d",
Emeric Brun5bd86a82010-10-22 17:23:04 +0200917 s->cli_addr.ss_family == AF_UNIX ? s->listener->luid :
918 ntohs((s->cli_addr.ss_family == AF_INET) ?
919 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
920 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200921 (int)s->logs.accept_date.tv_usec/1000);
922 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
923 goto trunc;
924 h += w;
925
926 w = strlen(fe->id);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, fe->id, w);
932 h += w;
933 *(h++) = '\"';
934
935 w = strlen(be->id);
936 if (h >= tmpline + sizeof(tmpline) - 4 - w)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 memcpy(h, be->id, w);
941 h += w;
942 *(h++) = '\"';
943
944 svid = (tolog & LW_SVID) ?
945 (s->data_source != DATA_SRC_STATS) ?
946 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
947
948 w = strlen(svid);
949 if (h >= tmpline + sizeof(tmpline) - 4 - w)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 memcpy(h, svid, w);
954 h += w;
955 *(h++) = '\"';
956
957 t_request = -1;
958 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
959 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
960 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
961 " %d %ld %ld %ld %ld",
962 t_request,
963 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
964 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
965 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
966 s->logs.t_close);
967 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
968 goto trunc;
969 h += w;
970
971 if (h >= tmpline + sizeof(tmpline) - 8)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
976 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
977 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
978 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
979 *(h++) = '\"';
980
981 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
982 " %d %d %d %d %d %ld %ld",
983 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200984 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200985 s->logs.srv_queue_size, s->logs.prx_queue_size);
986
987 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
988 goto trunc;
989 h += w;
990
991 if (txn->cli_cookie) {
992 w = strlen(txn->cli_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->cli_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if (txn->srv_cookie) {
1008 w = strlen(txn->srv_cookie);
1009 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 memcpy(h, txn->srv_cookie, w);
1014 h += w;
1015 *(h++) = '\"';
1016 } else {
1017 if (h >= tmpline + sizeof(tmpline) - 5)
1018 goto trunc;
1019 memcpy(h, " \"-\"", 4);
1020 h += 4;
1021 }
1022
1023 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1024 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1025 if (h >= sizeof (tmpline) + tmpline - 4)
1026 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001027 if (txn->req.cap[hdr] != NULL) {
1028 *(h++) = ' ';
1029 *(h++) = '\"';
1030 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1031 '#', hdr_encode_map, txn->req.cap[hdr]);
1032 *(h++) = '\"';
1033 } else {
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001037 }
1038 }
1039
1040 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1041 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1042 if (h >= sizeof (tmpline) + tmpline - 4)
1043 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001044 if (txn->rsp.cap[hdr] != NULL) {
1045 *(h++) = ' ';
1046 *(h++) = '\"';
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 *(h++) = '\"';
1050 } else {
1051 memcpy(h, " \"-\"", 4);
1052 h += 4;
1053 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001054 }
1055 }
1056
1057trunc:
1058 *h = '\0';
1059
1060 level = LOG_INFO;
1061 if (err && (fe->options2 & PR_O2_LOGERRORS))
1062 level = LOG_ERR;
1063
1064 send_log(prx_log, level, "%s\n", tmpline);
1065
1066 s->logs.logwait = 0;
1067}
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/*
1070 * send a log for the session when we have enough info about it.
1071 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001073void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001074{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001075 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001076 struct proxy *fe = s->fe;
1077 struct proxy *be = s->be;
1078 struct proxy *prx_log;
1079 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001080 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 char *uri, *h;
1082 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001084 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001085 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 int hdr;
1087
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 /* if we don't want to log normal traffic, return now */
1089 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001090 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001091 txn->status >= 500;
1092 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1093 return;
1094
Willy Tarreau42250582007-04-01 01:30:43 +02001095 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1096 return;
1097 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098
Emeric Brun3a058f32009-06-30 18:26:00 +02001099 if (prx_log->options2 & PR_O2_CLFLOG)
1100 return http_sess_clflog(s);
1101
Willy Tarreau42250582007-04-01 01:30:43 +02001102 if (s->cli_addr.ss_family == AF_INET)
1103 inet_ntop(AF_INET,
1104 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1105 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +02001106 else if (s->cli_addr.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001107 inet_ntop(AF_INET6,
1108 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1109 pn, sizeof(pn));
1110
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001111 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001112
1113 /* FIXME: let's limit ourselves to frontend logging for now. */
1114 tolog = fe->to_log;
1115
1116 h = tmpline;
1117 if (fe->to_log & LW_REQHDR &&
1118 txn->req.cap &&
1119 (h < tmpline + sizeof(tmpline) - 10)) {
1120 *(h++) = ' ';
1121 *(h++) = '{';
1122 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1123 if (hdr)
1124 *(h++) = '|';
1125 if (txn->req.cap[hdr] != NULL)
1126 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1127 '#', hdr_encode_map, txn->req.cap[hdr]);
1128 }
1129 *(h++) = '}';
1130 }
1131
1132 if (fe->to_log & LW_RSPHDR &&
1133 txn->rsp.cap &&
1134 (h < tmpline + sizeof(tmpline) - 7)) {
1135 *(h++) = ' ';
1136 *(h++) = '{';
1137 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1138 if (hdr)
1139 *(h++) = '|';
1140 if (txn->rsp.cap[hdr] != NULL)
1141 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1142 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1143 }
1144 *(h++) = '}';
1145 }
1146
1147 if (h < tmpline + sizeof(tmpline) - 4) {
1148 *(h++) = ' ';
1149 *(h++) = '"';
1150 uri = txn->uri ? txn->uri : "<BADREQ>";
1151 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1152 '#', url_encode_map, uri);
1153 *(h++) = '"';
1154 }
1155 *h = '\0';
1156
1157 svid = (tolog & LW_SVID) ?
1158 (s->data_source != DATA_SRC_STATS) ?
1159 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1160
Willy Tarreau70089872008-06-13 21:12:51 +02001161 t_request = -1;
1162 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1163 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1164
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001165 level = LOG_INFO;
1166 if (err && (fe->options2 & PR_O2_LOGERRORS))
1167 level = LOG_ERR;
1168
1169 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001170 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001171 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1172 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Emeric Brun5bd86a82010-10-22 17:23:04 +02001173 (s->cli_addr.ss_family == AF_UNIX) ? "unix" : pn,
1174 (s->cli_addr.ss_family == AF_UNIX) ? s->listener->luid :
1175 ntohs((s->cli_addr.ss_family == AF_INET) ?
1176 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
1177 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001178 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001179 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001180 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001181 t_request,
1182 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001183 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1184 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1185 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1186 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001187 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001188 txn->cli_cookie ? txn->cli_cookie : "-",
1189 txn->srv_cookie ? txn->srv_cookie : "-",
1190 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1191 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1192 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1193 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1194 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001195 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001196 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001197 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1198
1199 s->logs.logwait = 0;
1200}
1201
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202
1203/*
1204 * Capture headers from message starting at <som> according to header list
1205 * <cap_hdr>, and fill the <idx> structure appropriately.
1206 */
1207void capture_headers(char *som, struct hdr_idx *idx,
1208 char **cap, struct cap_hdr *cap_hdr)
1209{
1210 char *eol, *sol, *col, *sov;
1211 int cur_idx;
1212 struct cap_hdr *h;
1213 int len;
1214
1215 sol = som + hdr_idx_first_pos(idx);
1216 cur_idx = hdr_idx_first_idx(idx);
1217
1218 while (cur_idx) {
1219 eol = sol + idx->v[cur_idx].len;
1220
1221 col = sol;
1222 while (col < eol && *col != ':')
1223 col++;
1224
1225 sov = col + 1;
1226 while (sov < eol && http_is_lws[(unsigned char)*sov])
1227 sov++;
1228
1229 for (h = cap_hdr; h; h = h->next) {
1230 if ((h->namelen == col - sol) &&
1231 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1232 if (cap[h->index] == NULL)
1233 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001234 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001235
1236 if (cap[h->index] == NULL) {
1237 Alert("HTTP capture : out of memory.\n");
1238 continue;
1239 }
1240
1241 len = eol - sov;
1242 if (len > h->len)
1243 len = h->len;
1244
1245 memcpy(cap[h->index], sov, len);
1246 cap[h->index][len]=0;
1247 }
1248 }
1249 sol = eol + idx->v[cur_idx].cr + 1;
1250 cur_idx = idx->v[cur_idx].next;
1251 }
1252}
1253
1254
Willy Tarreau42250582007-04-01 01:30:43 +02001255/* either we find an LF at <ptr> or we jump to <bad>.
1256 */
1257#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1258
1259/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1260 * otherwise to <http_msg_ood> with <state> set to <st>.
1261 */
1262#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1263 ptr++; \
1264 if (likely(ptr < end)) \
1265 goto good; \
1266 else { \
1267 state = (st); \
1268 goto http_msg_ood; \
1269 } \
1270 } while (0)
1271
1272
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001274 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1276 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1277 * will give undefined results.
1278 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1279 * and that msg->sol points to the beginning of the response.
1280 * If a complete line is found (which implies that at least one CR or LF is
1281 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1282 * returned indicating an incomplete line (which does not mean that parts have
1283 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1284 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1285 * upon next call.
1286 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001287 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1289 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001290 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001292const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1293 unsigned int state, const char *ptr, const char *end,
1294 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001295{
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001298 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001299 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1301
1302 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001303 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1305 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001306 state = HTTP_MSG_ERROR;
1307 break;
1308
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001310 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001312 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 goto http_msg_rpcode;
1314 }
1315 if (likely(HTTP_IS_SPHT(*ptr)))
1316 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1317 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001318 state = HTTP_MSG_ERROR;
1319 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001320
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 if (likely(!HTTP_IS_LWS(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1325
1326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1329 }
1330
1331 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001332 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 http_msg_rsp_reason:
1334 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001335 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 msg->sl.st.r_l = 0;
1337 goto http_msg_rpline_eol;
1338
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001342 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 goto http_msg_rpreason;
1344 }
1345 if (likely(HTTP_IS_SPHT(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1347 /* so it's a CR/LF, so there is no reason phrase */
1348 goto http_msg_rsp_reason;
1349
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001351 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (likely(!HTTP_IS_CRLF(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001354 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 http_msg_rpline_eol:
1356 /* We have seen the end of line. Note that we do not
1357 * necessarily have the \n yet, but at least we know that we
1358 * have EITHER \r OR \n, otherwise the response would not be
1359 * complete. We can then record the response length and return
1360 * to the caller which will be able to register it.
1361 */
1362 msg->sl.st.l = ptr - msg->sol;
1363 return ptr;
1364
1365#ifdef DEBUG_FULL
1366 default:
1367 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1368 exit(1);
1369#endif
1370 }
1371
1372 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001373 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 if (ret_state)
1375 *ret_state = state;
1376 if (ret_ptr)
1377 *ret_ptr = (char *)ptr;
1378 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379}
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 * This function parses a request line between <ptr> and <end>, starting with
1383 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1384 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1385 * will give undefined results.
1386 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1387 * and that msg->sol points to the beginning of the request.
1388 * If a complete line is found (which implies that at least one CR or LF is
1389 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1390 * returned indicating an incomplete line (which does not mean that parts have
1391 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1392 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1393 * upon next call.
1394 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1397 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001398 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001400const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1401 unsigned int state, const char *ptr, const char *end,
1402 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001406 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (likely(HTTP_IS_TOKEN(*ptr)))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 if (likely(HTTP_IS_CRLF(*ptr))) {
1416 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001417 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001419 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001423 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->sl.rq.v_l = 0;
1425 goto http_msg_rqline_eol;
1426 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001427 state = HTTP_MSG_ERROR;
1428 break;
1429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001433 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 goto http_msg_rquri;
1435 }
1436 if (likely(HTTP_IS_SPHT(*ptr)))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1438 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1439 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001442 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_LWS(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001447 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1452 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001457 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 goto http_msg_rqver;
1459 }
1460 if (likely(HTTP_IS_SPHT(*ptr)))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1462 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1463 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001467 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001469
1470 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001471 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001472 http_msg_rqline_eol:
1473 /* We have seen the end of line. Note that we do not
1474 * necessarily have the \n yet, but at least we know that we
1475 * have EITHER \r OR \n, otherwise the request would not be
1476 * complete. We can then record the request length and return
1477 * to the caller which will be able to register it.
1478 */
1479 msg->sl.rq.l = ptr - msg->sol;
1480 return ptr;
1481 }
1482
1483 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001484 state = HTTP_MSG_ERROR;
1485 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487#ifdef DEBUG_FULL
1488 default:
1489 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1490 exit(1);
1491#endif
1492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001495 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (ret_state)
1497 *ret_state = state;
1498 if (ret_ptr)
1499 *ret_ptr = (char *)ptr;
1500 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001503/*
1504 * Returns the data from Authorization header. Function may be called more
1505 * than once so data is stored in txn->auth_data. When no header is found
1506 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1507 * searching again for something we are unable to find anyway.
1508 */
1509
1510char get_http_auth_buff[BUFSIZE];
1511
1512int
1513get_http_auth(struct session *s)
1514{
1515
1516 struct http_txn *txn = &s->txn;
1517 struct chunk auth_method;
1518 struct hdr_ctx ctx;
1519 char *h, *p;
1520 int len;
1521
1522#ifdef DEBUG_AUTH
1523 printf("Auth for session %p: %d\n", s, txn->auth.method);
1524#endif
1525
1526 if (txn->auth.method == HTTP_AUTH_WRONG)
1527 return 0;
1528
1529 if (txn->auth.method)
1530 return 1;
1531
1532 txn->auth.method = HTTP_AUTH_WRONG;
1533
1534 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001535
1536 if (txn->flags & TX_USE_PX_CONN) {
1537 h = "Proxy-Authorization";
1538 len = strlen(h);
1539 } else {
1540 h = "Authorization";
1541 len = strlen(h);
1542 }
1543
1544 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001545 return 0;
1546
1547 h = ctx.line + ctx.val;
1548
1549 p = memchr(h, ' ', ctx.vlen);
1550 if (!p || p == h)
1551 return 0;
1552
1553 chunk_initlen(&auth_method, h, 0, p-h);
1554 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1555
1556 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1557
1558 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1559 get_http_auth_buff, BUFSIZE - 1);
1560
1561 if (len < 0)
1562 return 0;
1563
1564
1565 get_http_auth_buff[len] = '\0';
1566
1567 p = strchr(get_http_auth_buff, ':');
1568
1569 if (!p)
1570 return 0;
1571
1572 txn->auth.user = get_http_auth_buff;
1573 *p = '\0';
1574 txn->auth.pass = p+1;
1575
1576 txn->auth.method = HTTP_AUTH_BASIC;
1577 return 1;
1578 }
1579
1580 return 0;
1581}
1582
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583
Willy Tarreau8973c702007-01-21 23:58:29 +01001584/*
1585 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001586 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 * when data are missing and recalled at the exact same location with no
1588 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001589 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001590 * fields. Note that msg->som and msg->sol will be initialized after completing
1591 * the first state, so that none of the msg pointers has to be initialized
1592 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1595{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001596 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001599 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 ptr = buf->lr;
1601 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (unlikely(ptr >= end))
1604 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 /*
1608 * First, states that are specific to the response only.
1609 * We check them first so that request and headers are
1610 * closer to each other (accessed more often).
1611 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001613 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001615 /* we have a start of message, but we have to check
1616 * first if we need to remove some CRLF. We can only
1617 * do this when send_max=0.
1618 */
1619 char *beg = buf->w + buf->send_max;
1620 if (beg >= buf->data + buf->size)
1621 beg -= buf->size;
1622 if (unlikely(ptr != beg)) {
1623 if (buf->send_max)
1624 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001625 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001626 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001627 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001629 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001630 hdr_idx_init(idx);
1631 state = HTTP_MSG_RPVER;
1632 goto http_msg_rpver;
1633 }
1634
1635 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1636 goto http_msg_invalid;
1637
1638 if (unlikely(*ptr == '\n'))
1639 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1640 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1641 /* stop here */
1642
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 EXPECT_LF_HERE(ptr, http_msg_invalid);
1646 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1647 /* stop here */
1648
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 case HTTP_MSG_RPVER_SP:
1652 case HTTP_MSG_RPCODE:
1653 case HTTP_MSG_RPCODE_SP:
1654 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001655 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001657 if (unlikely(!ptr))
1658 return;
1659
1660 /* we have a full response and we know that we have either a CR
1661 * or an LF at <ptr>.
1662 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001663 //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 +01001664 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1665
1666 msg->sol = ptr;
1667 if (likely(*ptr == '\r'))
1668 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1669 goto http_msg_rpline_end;
1670
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 /* msg->sol must point to the first of CR or LF. */
1674 EXPECT_LF_HERE(ptr, http_msg_invalid);
1675 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1676 /* stop here */
1677
1678 /*
1679 * Second, states that are specific to the request only
1680 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001684 /* we have a start of message, but we have to check
1685 * first if we need to remove some CRLF. We can only
1686 * do this when send_max=0.
1687 */
1688 char *beg = buf->w + buf->send_max;
1689 if (beg >= buf->data + buf->size)
1690 beg -= buf->size;
1691 if (likely(ptr != beg)) {
1692 if (buf->send_max)
1693 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001694 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001695 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001697 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001698 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001699 /* we will need this when keep-alive will be supported
1700 hdr_idx_init(idx);
1701 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001702 state = HTTP_MSG_RQMETH;
1703 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1707 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (unlikely(*ptr == '\n'))
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1711 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001712 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001715 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 EXPECT_LF_HERE(ptr, http_msg_invalid);
1717 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_RQMETH_SP:
1723 case HTTP_MSG_RQURI:
1724 case HTTP_MSG_RQURI_SP:
1725 case HTTP_MSG_RQVER:
1726 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 if (unlikely(!ptr))
1729 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 /* we have a full request and we know that we have either a CR
1732 * or an LF at <ptr>.
1733 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 //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 +01001735 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 msg->sol = ptr;
1738 if (likely(*ptr == '\r'))
1739 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 /* check for HTTP/0.9 request : no version information available.
1745 * msg->sol must point to the first of CR or LF.
1746 */
1747 if (unlikely(msg->sl.rq.v_l == 0))
1748 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 EXPECT_LF_HERE(ptr, http_msg_invalid);
1751 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001752 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001753
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /*
1755 * Common states below
1756 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001758 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001769 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 /* assumes msg->sol points to the first char */
1771 if (likely(HTTP_IS_TOKEN(*ptr)))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 if (likely(*ptr == ':')) {
1775 msg->col = ptr - buf->data;
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001778
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001779 if (likely(msg->err_pos < -1) || *ptr == '\n')
1780 goto http_msg_invalid;
1781
1782 if (msg->err_pos == -1) /* capture error pointer */
1783 msg->err_pos = ptr - buf->data; /* >= 0 now */
1784
1785 /* and we still accept this non-token character */
1786 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char and msg->col to the colon */
1791 if (likely(HTTP_IS_SPHT(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 /* header value can be basically anything except CR/LF */
1795 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_val;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1803 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 EXPECT_LF_HERE(ptr, http_msg_invalid);
1808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001811 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 if (likely(HTTP_IS_SPHT(*ptr))) {
1813 /* replace HT,CR,LF with spaces */
1814 for (; buf->data+msg->sov < ptr; msg->sov++)
1815 buf->data[msg->sov] = ' ';
1816 goto http_msg_hdr_l1_sp;
1817 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001818 /* we had a header consisting only in spaces ! */
1819 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 goto http_msg_complete_header;
1821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 /* assumes msg->sol points to the first char, msg->col to the
1825 * colon, and msg->sov points to the first character of the
1826 * value.
1827 */
1828 if (likely(!HTTP_IS_CRLF(*ptr)))
1829 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 msg->eol = ptr;
1832 /* Note: we could also copy eol into ->eoh so that we have the
1833 * real header end in case it ends with lots of LWS, but is this
1834 * really needed ?
1835 */
1836 if (likely(*ptr == '\r'))
1837 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1838 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001841 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 EXPECT_LF_HERE(ptr, http_msg_invalid);
1843 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001846 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1848 /* LWS: replace HT,CR,LF with spaces */
1849 for (; msg->eol < ptr; msg->eol++)
1850 *msg->eol = ' ';
1851 goto http_msg_hdr_val;
1852 }
1853 http_msg_complete_header:
1854 /*
1855 * It was a new header, so the last one is finished.
1856 * Assumes msg->sol points to the first char, msg->col to the
1857 * colon, msg->sov points to the first character of the value
1858 * and msg->eol to the first CR or LF so we know how the line
1859 * ends. We insert last header into the index.
1860 */
1861 /*
1862 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1863 write(2, msg->sol, msg->eol-msg->sol);
1864 fprintf(stderr,"\n");
1865 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1868 idx, idx->tail) < 0))
1869 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 msg->sol = ptr;
1872 if (likely(!HTTP_IS_CRLF(*ptr))) {
1873 goto http_msg_hdr_name;
1874 }
1875
1876 if (likely(*ptr == '\r'))
1877 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1878 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001881 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 /* Assumes msg->sol points to the first of either CR or LF */
1883 EXPECT_LF_HERE(ptr, http_msg_invalid);
1884 ptr++;
1885 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001886 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001888 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001889 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 return;
1891#ifdef DEBUG_FULL
1892 default:
1893 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1894 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 }
1897 http_msg_ood:
1898 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 buf->lr = ptr;
1901 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 http_msg_invalid:
1904 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001905 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001906 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 return;
1908}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001909
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001910/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1911 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1912 * nothing is done and 1 is returned.
1913 */
1914static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1915{
1916 int delta;
1917 char *cur_end;
1918
1919 if (msg->sl.rq.v_l != 0)
1920 return 1;
1921
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001922 cur_end = msg->sol + msg->sl.rq.l;
1923 delta = 0;
1924
1925 if (msg->sl.rq.u_l == 0) {
1926 /* if no URI was set, add "/" */
1927 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1928 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001929 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930 }
1931 /* add HTTP version */
1932 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001933 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001934 cur_end += delta;
1935 cur_end = (char *)http_parse_reqline(msg, req->data,
1936 HTTP_MSG_RQMETH,
1937 msg->sol, cur_end + 1,
1938 NULL, NULL);
1939 if (unlikely(!cur_end))
1940 return 0;
1941
1942 /* we have a full HTTP/1.0 request now and we know that
1943 * we have either a CR or an LF at <ptr>.
1944 */
1945 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1946 return 1;
1947}
1948
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001949/* Parse the Connection: header of an HTTP request, looking for both "close"
1950 * and "keep-alive" values. If a buffer is provided and we already know that
1951 * some headers may safely be removed, we remove them now. The <to_del> flags
1952 * are used for that :
1953 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1954 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1955 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1956 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1957 * harmless combinations may be removed. Do not call that after changes have
1958 * been processed. If unused, the buffer can be NULL, and no data will be
1959 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001960 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001961void 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 +01001962{
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001964 const char *hdr_val = "Connection";
1965 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001966
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001967 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001968 return;
1969
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection";
1972 hdr_len = 16;
1973 }
1974
Willy Tarreau5b154472009-12-21 20:11:07 +01001975 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001976 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1979 txn->flags |= TX_HDR_CONN_KAL;
1980 if ((to_del & 2) && buf)
1981 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1982 else
1983 txn->flags |= TX_CON_KAL_SET;
1984 }
1985 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1986 txn->flags |= TX_HDR_CONN_CLO;
1987 if ((to_del & 1) && buf)
1988 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1989 else
1990 txn->flags |= TX_CON_CLO_SET;
1991 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001992 }
1993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 txn->flags |= TX_HDR_CONN_PRS;
1995 return;
1996}
Willy Tarreau5b154472009-12-21 20:11:07 +01001997
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998/* Apply desired changes on the Connection: header. Values may be removed and/or
1999 * added depending on the <wanted> flags, which are exclusively composed of
2000 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2001 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2002 */
2003void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2004{
2005 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002006 const char *hdr_val = "Connection";
2007 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002008
2009 ctx.idx = 0;
2010
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011
2012 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2013 hdr_val = "Proxy-Connection";
2014 hdr_len = 16;
2015 }
2016
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002017 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002018 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2020 if (wanted & TX_CON_KAL_SET)
2021 txn->flags |= TX_CON_KAL_SET;
2022 else
2023 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002024 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2026 if (wanted & TX_CON_CLO_SET)
2027 txn->flags |= TX_CON_CLO_SET;
2028 else
2029 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002030 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002031 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032
2033 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2034 return;
2035
2036 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2037 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002038 hdr_val = "Connection: close";
2039 hdr_len = 17;
2040 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2041 hdr_val = "Proxy-Connection: close";
2042 hdr_len = 23;
2043 }
2044 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002045 }
2046
2047 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2048 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002049 hdr_val = "Connection: keep-alive";
2050 hdr_len = 22;
2051 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2052 hdr_val = "Proxy-Connection: keep-alive";
2053 hdr_len = 28;
2054 }
2055 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056 }
2057 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002058}
2059
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2061 * first byte of body, and increments msg->sov by the number of bytes parsed,
2062 * so that we know we can forward between ->som and ->sov. Note that due to
2063 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2064 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002067 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002069{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 char *ptr = buf->lr;
2071 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002072 unsigned int chunk = 0;
2073
2074 /* The chunk size is in the following form, though we are only
2075 * interested in the size and CRLF :
2076 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2077 */
2078 while (1) {
2079 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 if (c < 0) /* not a hex digit anymore */
2084 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 if (++ptr >= end)
2086 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002087 if (chunk & 0xF000000) /* overflow will occur */
2088 return -1;
2089 chunk = (chunk << 4) + c;
2090 }
2091
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 /* empty size not allowed */
2093 if (ptr == buf->lr)
2094 return -1;
2095
2096 while (http_is_spht[(unsigned char)*ptr]) {
2097 if (++ptr >= end)
2098 ptr = buf->data;
2099 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002100 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002101 }
2102
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 /* Up to there, we know that at least one byte is present at *ptr. Check
2104 * for the end of chunk size.
2105 */
2106 while (1) {
2107 if (likely(HTTP_IS_CRLF(*ptr))) {
2108 /* we now have a CR or an LF at ptr */
2109 if (likely(*ptr == '\r')) {
2110 if (++ptr >= end)
2111 ptr = buf->data;
2112 if (ptr == buf->r)
2113 return 0;
2114 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002115
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 if (*ptr != '\n')
2117 return -1;
2118 if (++ptr >= end)
2119 ptr = buf->data;
2120 /* done */
2121 break;
2122 }
2123 else if (*ptr == ';') {
2124 /* chunk extension, ends at next CRLF */
2125 if (++ptr >= end)
2126 ptr = buf->data;
2127 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002129
2130 while (!HTTP_IS_CRLF(*ptr)) {
2131 if (++ptr >= end)
2132 ptr = buf->data;
2133 if (ptr == buf->r)
2134 return 0;
2135 }
2136 /* we have a CRLF now, loop above */
2137 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002138 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002140 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002141 }
2142
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 /* OK we found our CRLF and now <ptr> points to the next byte,
2144 * which may or may not be present. We save that into ->lr and
2145 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002146 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->sov += ptr - buf->lr;
2148 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002149 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002151 return 1;
2152}
2153
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154/* This function skips trailers in the buffer <buf> associated with HTTP
2155 * message <msg>. The first visited position is buf->lr. If the end of
2156 * the trailers is found, it is automatically scheduled to be forwarded,
2157 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2158 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002159 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2160 * zero. If a parse error is encountered, the function returns < 0 and does not
2161 * change anything except maybe buf->lr and msg->sov. Note that the message
2162 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2163 * which implies that all non-trailers data have already been scheduled for
2164 * forwarding, and that the difference between msg->som and msg->sov exactly
2165 * matches the length of trailers already parsed and not forwarded. It is also
2166 * important to note that this function is designed to be able to parse wrapped
2167 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168 */
2169int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2170{
2171 /* we have buf->lr which points to next line. Look for CRLF. */
2172 while (1) {
2173 char *p1 = NULL, *p2 = NULL;
2174 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002175 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176
2177 /* scan current line and stop at LF or CRLF */
2178 while (1) {
2179 if (ptr == buf->r)
2180 return 0;
2181
2182 if (*ptr == '\n') {
2183 if (!p1)
2184 p1 = ptr;
2185 p2 = ptr;
2186 break;
2187 }
2188
2189 if (*ptr == '\r') {
2190 if (p1)
2191 return -1;
2192 p1 = ptr;
2193 }
2194
2195 ptr++;
2196 if (ptr >= buf->data + buf->size)
2197 ptr = buf->data;
2198 }
2199
2200 /* after LF; point to beginning of next line */
2201 p2++;
2202 if (p2 >= buf->data + buf->size)
2203 p2 = buf->data;
2204
Willy Tarreau638cd022010-01-03 07:42:04 +01002205 bytes = p2 - buf->lr;
2206 if (bytes < 0)
2207 bytes += buf->size;
2208
2209 /* schedule this line for forwarding */
2210 msg->sov += bytes;
2211 if (msg->sov >= buf->size)
2212 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002213
Willy Tarreau638cd022010-01-03 07:42:04 +01002214 if (p1 == buf->lr) {
2215 /* LF/CRLF at beginning of line => end of trailers at p2.
2216 * Everything was scheduled for forwarding, there's nothing
2217 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002218 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002219 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002220 msg->msg_state = HTTP_MSG_DONE;
2221 return 1;
2222 }
2223 /* OK, next line then */
2224 buf->lr = p2;
2225 }
2226}
2227
2228/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2229 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2230 * ->som, buf->lr in order to include this part into the next forwarding phase.
2231 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2232 * not enough data are available, the function does not change anything and
2233 * returns zero. If a parse error is encountered, the function returns < 0 and
2234 * does not change anything. Note: this function is designed to parse wrapped
2235 * CRLF at the end of the buffer.
2236 */
2237int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2238{
2239 char *ptr;
2240 int bytes;
2241
2242 /* NB: we'll check data availabilty at the end. It's not a
2243 * problem because whatever we match first will be checked
2244 * against the correct length.
2245 */
2246 bytes = 1;
2247 ptr = buf->lr;
2248 if (*ptr == '\r') {
2249 bytes++;
2250 ptr++;
2251 if (ptr >= buf->data + buf->size)
2252 ptr = buf->data;
2253 }
2254
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002255 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002256 return 0;
2257
2258 if (*ptr != '\n')
2259 return -1;
2260
2261 ptr++;
2262 if (ptr >= buf->data + buf->size)
2263 ptr = buf->data;
2264 buf->lr = ptr;
2265 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2266 msg->sov = ptr - buf->data;
2267 msg->som = msg->sov - bytes;
2268 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2269 return 1;
2270}
Willy Tarreau5b154472009-12-21 20:11:07 +01002271
Willy Tarreau83e3af02009-12-28 17:39:57 +01002272void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2273{
2274 char *end = buf->data + buf->size;
2275 int off = buf->data + buf->size - buf->w;
2276
2277 /* two possible cases :
2278 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002279 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002280 */
2281 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002282 int block1 = buf->l;
2283 int block2 = 0;
2284 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002285 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002286 block1 = buf->data + buf->size - buf->w;
2287 block2 = buf->r - buf->data;
2288 }
2289 if (block2)
2290 memcpy(swap_buffer, buf->data, block2);
2291 memmove(buf->data, buf->w, block1);
2292 if (block2)
2293 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002294 }
2295
2296 /* adjust all known pointers */
2297 buf->w = buf->data;
2298 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2299 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2300 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2301 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2302
2303 /* adjust relative pointers */
2304 msg->som = 0;
2305 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2306 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2307 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2308
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 if (msg->err_pos >= 0) {
2310 msg->err_pos += off;
2311 if (msg->err_pos >= buf->size)
2312 msg->err_pos -= buf->size;
2313 }
2314
2315 buf->flags &= ~BF_FULL;
2316 if (buf->l >= buffer_max_len(buf))
2317 buf->flags |= BF_FULL;
2318}
2319
Willy Tarreaud787e662009-07-07 10:14:51 +02002320/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2321 * processing can continue on next analysers, or zero if it either needs more
2322 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2323 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2324 * when it has nothing left to do, and may remove any analyser when it wants to
2325 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002326 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002327int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002328{
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 /*
2330 * We will parse the partial (or complete) lines.
2331 * We will check the request syntax, and also join multi-line
2332 * headers. An index of all the lines will be elaborated while
2333 * parsing.
2334 *
2335 * For the parsing, we use a 28 states FSM.
2336 *
2337 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002338 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002339 * req->data + msg->eoh = end of processed headers / start of current one
2340 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 * req->lr = first non-visited byte
2342 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002343 *
2344 * At end of parsing, we may perform a capture of the error (if any), and
2345 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2346 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2347 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002351 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002354 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002355
Willy Tarreau6bf17362009-02-24 10:48:35 +01002356 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2357 now_ms, __FUNCTION__,
2358 s,
2359 req,
2360 req->rex, req->wex,
2361 req->flags,
2362 req->l,
2363 req->analysers);
2364
Willy Tarreau52a0c602009-08-16 22:45:38 +02002365 /* we're speaking HTTP here, so let's speak HTTP to the client */
2366 s->srv_error = http_return_srv_error;
2367
Willy Tarreau83e3af02009-12-28 17:39:57 +01002368 /* There's a protected area at the end of the buffer for rewriting
2369 * purposes. We don't want to start to parse the request if the
2370 * protected area is affected, because we may have to move processed
2371 * data later, which is much more complicated.
2372 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002373 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002374 if ((txn->flags & TX_NOT_FIRST) &&
2375 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002376 req->r < req->lr ||
2377 req->r > req->data + req->size - global.tune.maxrewrite)) {
2378 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002379 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2380 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002381 /* some data has still not left the buffer, wake us once that's done */
2382 buffer_dont_connect(req);
2383 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2384 return 0;
2385 }
2386 if (req->l <= req->size - global.tune.maxrewrite)
2387 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002388 }
2389
Willy Tarreau065e8332010-01-08 00:30:20 +01002390 /* Note that we have the same problem with the response ; we
2391 * may want to send a redirect, error or anything which requires
2392 * some spare space. So we'll ensure that we have at least
2393 * maxrewrite bytes available in the response buffer before
2394 * processing that one. This will only affect pipelined
2395 * keep-alive requests.
2396 */
2397 if ((txn->flags & TX_NOT_FIRST) &&
2398 unlikely((s->rep->flags & BF_FULL) ||
2399 s->rep->r < s->rep->lr ||
2400 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2401 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002402 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2403 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002404 /* don't let a connection request be initiated */
2405 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002406 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002407 return 0;
2408 }
2409 }
2410
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002411 if (likely(req->lr < req->r))
2412 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002413 }
2414
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 /* 1: we might have to print this header in debug mode */
2416 if (unlikely((global.mode & MODE_DEBUG) &&
2417 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002418 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002419 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002420 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002421
Willy Tarreau663308b2010-06-07 14:06:08 +02002422 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 eol = sol + msg->sl.rq.l;
2424 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 sol += hdr_idx_first_pos(&txn->hdr_idx);
2427 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002428
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 while (cur_idx) {
2430 eol = sol + txn->hdr_idx.v[cur_idx].len;
2431 debug_hdr("clihdr", s, sol, eol);
2432 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2433 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002434 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 }
2436
Willy Tarreau58f10d72006-12-04 02:26:12 +01002437
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 /*
2439 * Now we quickly check if we have found a full valid request.
2440 * If not so, we check the FD and buffer states before leaving.
2441 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002442 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002443 * requests are checked first. When waiting for a second request
2444 * on a keep-alive session, if we encounter and error, close, t/o,
2445 * we note the error in the session flags but don't set any state.
2446 * Since the error will be noted there, it will not be counted by
2447 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002448 * Last, we may increase some tracked counters' http request errors on
2449 * the cases that are deliberately the client's fault. For instance,
2450 * a timeout or connection reset is not counted as an error. However
2451 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Willy Tarreau655dce92009-11-08 13:10:58 +01002454 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002455 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002457 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002458 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002459 session_inc_http_req_ctr(s);
2460 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002461 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002462 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002464
Willy Tarreau59234e92008-11-30 23:51:27 +01002465 /* 1: Since we are in header mode, if there's no space
2466 * left for headers, we won't be able to free more
2467 * later, so the session will never terminate. We
2468 * must terminate it now.
2469 */
2470 if (unlikely(req->flags & BF_FULL)) {
2471 /* FIXME: check if URI is set and return Status
2472 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002473 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002474 session_inc_http_req_ctr(s);
2475 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002476 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002477 goto return_bad_req;
2478 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 /* 2: have we encountered a read error ? */
2481 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002482 if (!(s->flags & SN_ERR_MASK))
2483 s->flags |= SN_ERR_CLICL;
2484
Willy Tarreaufcffa692010-01-10 14:21:19 +01002485 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002486 goto failed_keep_alive;
2487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002489 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002490 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002491 session_inc_http_err_ctr(s);
2492 }
2493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 msg->msg_state = HTTP_MSG_ERROR;
2495 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002496
Willy Tarreauda7ff642010-06-23 11:44:09 +02002497 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002498 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002499 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002500 if (s->listener->counters)
2501 s->listener->counters->failed_req++;
2502
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 if (!(s->flags & SN_FINST_MASK))
2504 s->flags |= SN_FINST_R;
2505 return 0;
2506 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* 3: has the read timeout expired ? */
2509 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002510 if (!(s->flags & SN_ERR_MASK))
2511 s->flags |= SN_ERR_CLITO;
2512
Willy Tarreaufcffa692010-01-10 14:21:19 +01002513 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002514 goto failed_keep_alive;
2515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002517 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002518 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002519 session_inc_http_err_ctr(s);
2520 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 txn->status = 408;
2522 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2523 msg->msg_state = HTTP_MSG_ERROR;
2524 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525
Willy Tarreauda7ff642010-06-23 11:44:09 +02002526 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002527 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002528 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002529 if (s->listener->counters)
2530 s->listener->counters->failed_req++;
2531
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 if (!(s->flags & SN_FINST_MASK))
2533 s->flags |= SN_FINST_R;
2534 return 0;
2535 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 /* 4: have we encountered a close ? */
2538 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002539 if (!(s->flags & SN_ERR_MASK))
2540 s->flags |= SN_ERR_CLICL;
2541
Willy Tarreaufcffa692010-01-10 14:21:19 +01002542 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002543 goto failed_keep_alive;
2544
Willy Tarreau4076a152009-04-02 15:18:36 +02002545 if (msg->err_pos >= 0)
2546 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 txn->status = 400;
2548 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2549 msg->msg_state = HTTP_MSG_ERROR;
2550 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002551
Willy Tarreauda7ff642010-06-23 11:44:09 +02002552 session_inc_http_err_ctr(s);
2553 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002554 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002555 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002556 if (s->listener->counters)
2557 s->listener->counters->failed_req++;
2558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 if (!(s->flags & SN_FINST_MASK))
2560 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002561 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562 }
2563
Willy Tarreau520d95e2009-09-19 21:04:57 +02002564 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002565 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002566 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002567
Willy Tarreaufcffa692010-01-10 14:21:19 +01002568 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2569 /* If the client starts to talk, let's fall back to
2570 * request timeout processing.
2571 */
2572 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002573 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002574 }
2575
Willy Tarreau59234e92008-11-30 23:51:27 +01002576 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002577 if (!tick_isset(req->analyse_exp)) {
2578 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2579 (txn->flags & TX_WAIT_NEXT_RQ) &&
2580 tick_isset(s->be->timeout.httpka))
2581 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2582 else
2583 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2584 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /* we're not ready yet */
2587 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002588
2589 failed_keep_alive:
2590 /* Here we process low-level errors for keep-alive requests. In
2591 * short, if the request is not the first one and it experiences
2592 * a timeout, read error or shutdown, we just silently close so
2593 * that the client can try again.
2594 */
2595 txn->status = 0;
2596 msg->msg_state = HTTP_MSG_RQBEFORE;
2597 req->analysers = 0;
2598 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002599 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002600 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002601 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002602 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002603
Willy Tarreaud787e662009-07-07 10:14:51 +02002604 /* OK now we have a complete HTTP request with indexed headers. Let's
2605 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002606 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2607 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2608 * points to the CRLF of the request line. req->lr points to the first
2609 * byte after the last LF. msg->col and msg->sov point to the first
2610 * byte of data. msg->eol cannot be trusted because it may have been
2611 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002612 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002613
Willy Tarreauda7ff642010-06-23 11:44:09 +02002614 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002615 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2616
Willy Tarreaub16a5742010-01-10 14:46:16 +01002617 if (txn->flags & TX_WAIT_NEXT_RQ) {
2618 /* kill the pending keep-alive timeout */
2619 txn->flags &= ~TX_WAIT_NEXT_RQ;
2620 req->analyse_exp = TICK_ETERNITY;
2621 }
2622
2623
Willy Tarreaud787e662009-07-07 10:14:51 +02002624 /* Maybe we found in invalid header name while we were configured not
2625 * to block on that, so we have to capture it now.
2626 */
2627 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002628 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2629
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 /*
2631 * 1: identify the method
2632 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002633 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002634
2635 /* we can make use of server redirect on GET and HEAD */
2636 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2637 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /*
2640 * 2: check if the URI matches the monitor_uri.
2641 * We have to do this for every request which gets in, because
2642 * the monitor-uri is defined by the frontend.
2643 */
2644 if (unlikely((s->fe->monitor_uri_len != 0) &&
2645 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002646 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 s->fe->monitor_uri,
2648 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002649 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002655
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002657 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2658 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 ret = acl_pass(ret);
2661 if (cond->pol == ACL_COND_UNLESS)
2662 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 if (ret) {
2665 /* we fail this request, let's return 503 service unavail */
2666 txn->status = 503;
2667 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2668 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002669 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002670 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 /* nothing to fail, let's reply normaly */
2673 txn->status = 200;
2674 stream_int_retnclose(req->prod, &http_200_chunk);
2675 goto return_prx_cond;
2676 }
2677
2678 /*
2679 * 3: Maybe we have to copy the original REQURI for the logs ?
2680 * Note: we cannot log anymore if the request has been
2681 * classified as invalid.
2682 */
2683 if (unlikely(s->logs.logwait & LW_REQ)) {
2684 /* we have a complete HTTP request that we must log */
2685 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2686 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 if (urilen >= REQURI_LEN)
2689 urilen = REQURI_LEN - 1;
2690 memcpy(txn->uri, &req->data[msg->som], urilen);
2691 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 if (!(s->logs.logwait &= ~LW_REQ))
2694 s->do_log(s);
2695 } else {
2696 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002697 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 }
Willy Tarreau06619262006-12-17 08:37:22 +01002699
Willy Tarreau59234e92008-11-30 23:51:27 +01002700 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002701 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2702 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002703
Willy Tarreau5b154472009-12-21 20:11:07 +01002704 /* ... and check if the request is HTTP/1.1 or above */
2705 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002706 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2707 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2708 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002709 txn->flags |= TX_REQ_VER_11;
2710
2711 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002712 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002713
Willy Tarreau88d349d2010-01-25 12:15:43 +01002714 /* if the frontend has "option http-use-proxy-header", we'll check if
2715 * we have what looks like a proxied connection instead of a connection,
2716 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2717 * Note that this is *not* RFC-compliant, however browsers and proxies
2718 * happen to do that despite being non-standard :-(
2719 * We consider that a request not beginning with either '/' or '*' is
2720 * a proxied connection, which covers both "scheme://location" and
2721 * CONNECT ip:port.
2722 */
2723 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2724 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2725 txn->flags |= TX_USE_PX_CONN;
2726
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002727 /* transfer length unknown*/
2728 txn->flags &= ~TX_REQ_XFER_LEN;
2729
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 /* 5: we may need to capture headers */
2731 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002732 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002734
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002735 /* 6: determine the transfer-length.
2736 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2737 * the presence of a message-body in a REQUEST and its transfer length
2738 * must be determined that way (in order of precedence) :
2739 * 1. The presence of a message-body in a request is signaled by the
2740 * inclusion of a Content-Length or Transfer-Encoding header field
2741 * in the request's header fields. When a request message contains
2742 * both a message-body of non-zero length and a method that does
2743 * not define any semantics for that request message-body, then an
2744 * origin server SHOULD either ignore the message-body or respond
2745 * with an appropriate error message (e.g., 413). A proxy or
2746 * gateway, when presented the same request, SHOULD either forward
2747 * the request inbound with the message- body or ignore the
2748 * message-body when determining a response.
2749 *
2750 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2751 * and the "chunked" transfer-coding (Section 6.2) is used, the
2752 * transfer-length is defined by the use of this transfer-coding.
2753 * If a Transfer-Encoding header field is present and the "chunked"
2754 * transfer-coding is not present, the transfer-length is defined
2755 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002757 * 3. If a Content-Length header field is present, its decimal value in
2758 * OCTETs represents both the entity-length and the transfer-length.
2759 * If a message is received with both a Transfer-Encoding header
2760 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002761 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002762 * 4. By the server closing the connection. (Closing the connection
2763 * cannot be used to indicate the end of a request body, since that
2764 * would leave no possibility for the server to send back a response.)
2765 *
2766 * Whenever a transfer-coding is applied to a message-body, the set of
2767 * transfer-codings MUST include "chunked", unless the message indicates
2768 * it is terminated by closing the connection. When the "chunked"
2769 * transfer-coding is used, it MUST be the last transfer-coding applied
2770 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002771 */
2772
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002773 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002774 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002775 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002776 while ((txn->flags & TX_REQ_VER_11) &&
2777 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002778 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2779 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2780 else if (txn->flags & TX_REQ_TE_CHNK) {
2781 /* bad transfer-encoding (chunked followed by something else) */
2782 use_close_only = 1;
2783 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2784 break;
2785 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002786 }
2787
Willy Tarreau32b47f42009-10-18 20:55:02 +02002788 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002789 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002790 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2791 signed long long cl;
2792
2793 if (!ctx.vlen)
2794 goto return_bad_req;
2795
2796 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2797 goto return_bad_req; /* parse failure */
2798
2799 if (cl < 0)
2800 goto return_bad_req;
2801
2802 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2803 goto return_bad_req; /* already specified, was different */
2804
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002805 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002806 msg->hdr_content_len = cl;
2807 }
2808
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002809 /* bodyless requests have a known length */
2810 if (!use_close_only)
2811 txn->flags |= TX_REQ_XFER_LEN;
2812
Willy Tarreaud787e662009-07-07 10:14:51 +02002813 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002814 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 req->analyse_exp = TICK_ETERNITY;
2816 return 1;
2817
2818 return_bad_req:
2819 /* We centralize bad requests processing here */
2820 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2821 /* we detected a parsing error. We want to archive this request
2822 * in the dedicated proxy area for later troubleshooting.
2823 */
2824 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2825 }
2826
2827 txn->req.msg_state = HTTP_MSG_ERROR;
2828 txn->status = 400;
2829 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002830
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002831 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002832 if (s->listener->counters)
2833 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002834
2835 return_prx_cond:
2836 if (!(s->flags & SN_ERR_MASK))
2837 s->flags |= SN_ERR_PRXCOND;
2838 if (!(s->flags & SN_FINST_MASK))
2839 s->flags |= SN_FINST_R;
2840
2841 req->analysers = 0;
2842 req->analyse_exp = TICK_ETERNITY;
2843 return 0;
2844}
2845
Cyril Bonté70be45d2010-10-12 00:14:35 +02002846/* We reached the stats page through a POST request.
2847 * Parse the posted data and enable/disable servers if necessary.
2848 * Returns 0 if request was parsed.
2849 * Returns 1 if there was a problem parsing the posted data.
2850 */
2851int http_process_req_stat_post(struct session *s, struct buffer *req)
2852{
2853 struct http_txn *txn = &s->txn;
2854 struct proxy *px;
2855 struct server *sv;
2856
2857 char *backend = NULL;
2858 int action = 0;
2859
2860 char *first_param, *cur_param, *next_param, *end_params;
2861
2862 first_param = req->data + txn->req.eoh + 2;
2863 end_params = first_param + txn->req.hdr_content_len;
2864
2865 cur_param = next_param = end_params;
2866
2867 if (end_params >= req->data + req->size) {
2868 /* Prevent buffer overflow */
2869 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2870 return 1;
2871 }
2872 else if (end_params > req->data + req->l) {
2873 /* This condition also rejects a request with Expect: 100-continue */
2874 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2875 return 1;
2876 }
2877
2878 *end_params = '\0';
2879
2880 s->data_ctx.stats.st_code = STAT_STATUS_NONE;
2881
2882 /*
2883 * Parse the parameters in reverse order to only store the last value.
2884 * From the html form, the backend and the action are at the end.
2885 */
2886 while (cur_param > first_param) {
2887 char *key, *value;
2888
2889 cur_param--;
2890 if ((*cur_param == '&') || (cur_param == first_param)) {
2891 /* Parse the key */
2892 key = cur_param;
2893 if (cur_param != first_param) {
2894 /* delimit the string for the next loop */
2895 *key++ = '\0';
2896 }
2897
2898 /* Parse the value */
2899 value = key;
2900 while (*value != '\0' && *value != '=') {
2901 value++;
2902 }
2903 if (*value == '=') {
2904 /* Ok, a value is found, we can mark the end of the key */
2905 *value++ = '\0';
2906 }
2907
2908 /* Now we can check the key to see what to do */
2909 if (!backend && strcmp(key, "b") == 0) {
2910 backend = value;
2911 }
2912 else if (!action && strcmp(key, "action") == 0) {
2913 if (strcmp(value, "disable") == 0) {
2914 action = 1;
2915 }
2916 else if (strcmp(value, "enable") == 0) {
2917 action = 2;
2918 } else {
2919 /* unknown action, no need to continue */
2920 break;
2921 }
2922 }
2923 else if (strcmp(key, "s") == 0) {
2924 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2925 switch (action) {
2926 case 1:
2927 if (! (sv->state & SRV_MAINTAIN)) {
2928 /* Not already in maintenance, we can change the server state */
2929 sv->state |= SRV_MAINTAIN;
2930 set_server_down(sv);
2931 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2932 }
2933 break;
2934 case 2:
2935 if ((sv->state & SRV_MAINTAIN)) {
2936 /* Already in maintenance, we can change the server state */
2937 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002938 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002939 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2940 }
2941 break;
2942 }
2943 }
2944 }
2945 next_param = cur_param;
2946 }
2947 }
2948 return 0;
2949}
2950
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002951/* This stream analyser runs all HTTP request processing which is common to
2952 * frontends and backends, which means blocking ACLs, filters, connection-close,
2953 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002954 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002955 * either needs more data or wants to immediately abort the request (eg: deny,
2956 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002957 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002958int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002959{
Willy Tarreaud787e662009-07-07 10:14:51 +02002960 struct http_txn *txn = &s->txn;
2961 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002963 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002964 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002965 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002966 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002967
Willy Tarreau655dce92009-11-08 13:10:58 +01002968 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002969 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002970 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002971 return 0;
2972 }
2973
Willy Tarreau3a816292009-07-07 10:55:49 +02002974 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002975 req->analyse_exp = TICK_ETERNITY;
2976
2977 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2978 now_ms, __FUNCTION__,
2979 s,
2980 req,
2981 req->rex, req->wex,
2982 req->flags,
2983 req->l,
2984 req->analysers);
2985
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002986 /* first check whether we have some ACLs set to block this request */
2987 list_for_each_entry(cond, &px->block_cond, list) {
2988 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002989
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002990 ret = acl_pass(ret);
2991 if (cond->pol == ACL_COND_UNLESS)
2992 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002994 if (ret) {
2995 txn->status = 403;
2996 /* let's log the request time */
2997 s->logs.tv_request = now;
2998 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002999 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003000 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003001 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003003
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003004 do_stats = stats_check_uri(s, px);
3005
3006 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003007 int ret = 1;
3008
3009 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
3010 continue;
3011
3012 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003013 if (req_acl->cond) {
3014 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
3015 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01003016
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003017 if (req_acl->cond->pol == ACL_COND_UNLESS)
3018 ret = !ret;
3019 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003020
3021 if (ret) {
3022 req_acl_final = req_acl;
3023 break;
3024 }
3025 }
3026
3027 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
3028 txn->status = 403;
3029 s->logs.tv_request = now;
3030 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003031 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003032 goto return_prx_cond;
3033 }
3034
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 /* try headers filters */
3036 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003037 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003038 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003039
Willy Tarreau59234e92008-11-30 23:51:27 +01003040 /* has the request been denied ? */
3041 if (txn->flags & TX_CLDENY) {
3042 /* no need to go further */
3043 txn->status = 403;
3044 /* let's log the request time */
3045 s->logs.tv_request = now;
3046 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003047 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 goto return_prx_cond;
3049 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003050
3051 /* When a connection is tarpitted, we use the tarpit timeout,
3052 * which may be the same as the connect timeout if unspecified.
3053 * If unset, then set it to zero because we really want it to
3054 * eventually expire. We build the tarpit as an analyser.
3055 */
3056 if (txn->flags & TX_CLTARPIT) {
3057 buffer_erase(s->req);
3058 /* wipe the request out so that we can drop the connection early
3059 * if the client closes first.
3060 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003061 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003062 req->analysers = 0; /* remove switching rules etc... */
3063 req->analysers |= AN_REQ_HTTP_TARPIT;
3064 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3065 if (!req->analyse_exp)
3066 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003067 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003068 return 1;
3069 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003070 }
Willy Tarreau06619262006-12-17 08:37:22 +01003071
Willy Tarreau5b154472009-12-21 20:11:07 +01003072 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3073 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003074 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3075 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003076 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3077 * avoid to redo the same work if FE and BE have the same settings (common).
3078 * The method consists in checking if options changed between the two calls
3079 * (implying that either one is non-null, or one of them is non-null and we
3080 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003081 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003082
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003083 del_cl = del_ka = 0;
3084
Willy Tarreaudc008c52010-02-01 16:20:08 +01003085 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3086 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3087 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3088 (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 +01003089 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003090
Willy Tarreau5b154472009-12-21 20:11:07 +01003091 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
3092 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003093 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3094 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003095 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003096 tmp = TX_CON_WANT_CLO;
3097
Willy Tarreau5b154472009-12-21 20:11:07 +01003098 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3099 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003100
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003101 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3102 /* parse the Connection header and possibly clean it */
3103 int to_del = 0;
3104 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003105 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3106 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003107 to_del |= 2; /* remove "keep-alive" */
3108 if (!(txn->flags & TX_REQ_VER_11))
3109 to_del |= 1; /* remove "close" */
3110 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003111 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003112
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003113 /* check if client or config asks for explicit close in KAL/SCL */
3114 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3115 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3116 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3117 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Willy Tarreau22a95342010-09-29 14:31:41 +02003118 (((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) && /* httpclose without pretend-ka... */
3119 1/*!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)*/) || /* ... +any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003120 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3121 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003122 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3123 }
Willy Tarreau78599912009-10-17 20:12:21 +02003124
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003126 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003127 if (wl->cond) {
3128 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3129 ret = acl_pass(ret);
3130 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3131 ret = !ret;
3132 if (!ret)
3133 continue;
3134 }
3135
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003136 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003137 goto return_bad_req;
3138 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003139
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003140 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3141 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003142 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003143
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003144 if (!realm)
3145 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3146
Willy Tarreau844a7e72010-01-31 21:46:18 +01003147 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003148 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3149 txn->status = 401;
3150 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003151 /* on 401 we still count one error, because normal browsing
3152 * won't significantly increase the counter but brute force
3153 * attempts will.
3154 */
3155 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003156 goto return_prx_cond;
3157 }
3158
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003159 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003160 struct stats_admin_rule *stats_admin_rule;
3161
3162 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003163 * FIXME!!! that one is rather dangerous, we want to
3164 * make it follow standard rules (eg: clear req->analysers).
3165 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003166
Cyril Bonté474be412010-10-12 00:14:36 +02003167 /* now check whether we have some admin rules for this request */
3168 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3169 int ret = 1;
3170
3171 if (stats_admin_rule->cond) {
3172 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3173 ret = acl_pass(ret);
3174 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3175 ret = !ret;
3176 }
3177
3178 if (ret) {
3179 /* no rule, or the rule matches */
3180 s->data_ctx.stats.flags |= STAT_ADMIN;
3181 break;
3182 }
3183 }
3184
Cyril Bonté70be45d2010-10-12 00:14:35 +02003185 /* Was the status page requested with a POST ? */
3186 if (txn->meth == HTTP_METH_POST) {
Cyril Bonté474be412010-10-12 00:14:36 +02003187 if (s->data_ctx.stats.flags & STAT_ADMIN) {
3188 http_process_req_stat_post(s, req);
3189 } else {
3190 s->data_ctx.stats.st_code = STAT_STATUS_DENY;
3191 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003192 }
3193
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003194 s->logs.tv_request = now;
3195 s->data_source = DATA_SRC_STATS;
3196 s->data_state = DATA_ST_INIT;
3197 s->task->nice = -32; /* small boost for HTTP statistics */
3198 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3199 s->rep->prod->private = s;
3200 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3201 req->analysers = 0;
3202
3203 return 0;
3204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003205 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003207 /* check whether we have some ACLs set to redirect this request */
3208 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003209 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003210
Willy Tarreauf285f542010-01-03 20:03:03 +01003211 if (rule->cond) {
3212 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3213 ret = acl_pass(ret);
3214 if (rule->cond->pol == ACL_COND_UNLESS)
3215 ret = !ret;
3216 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003217
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003219 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003220 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003221
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003222 /* build redirect message */
3223 switch(rule->code) {
3224 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003225 msg_fmt = HTTP_303;
3226 break;
3227 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 msg_fmt = HTTP_301;
3229 break;
3230 case 302:
3231 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003232 msg_fmt = HTTP_302;
3233 break;
3234 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003235
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003236 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003237 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003238
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003239 switch(rule->type) {
3240 case REDIRECT_TYPE_PREFIX: {
3241 const char *path;
3242 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003243
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003244 path = http_get_path(txn);
3245 /* build message using path */
3246 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003247 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003248 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3249 int qs = 0;
3250 while (qs < pathlen) {
3251 if (path[qs] == '?') {
3252 pathlen = qs;
3253 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003254 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003255 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003256 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003257 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003258 } else {
3259 path = "/";
3260 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003261 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003262
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003263 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003264 goto return_bad_req;
3265
3266 /* add prefix. Note that if prefix == "/", we don't want to
3267 * add anything, otherwise it makes it hard for the user to
3268 * configure a self-redirection.
3269 */
3270 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003271 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3272 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003273 }
3274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275 /* add path */
3276 memcpy(rdr.str + rdr.len, path, pathlen);
3277 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003278
3279 /* append a slash at the end of the location is needed and missing */
3280 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3281 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3282 if (rdr.len > rdr.size - 5)
3283 goto return_bad_req;
3284 rdr.str[rdr.len] = '/';
3285 rdr.len++;
3286 }
3287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003288 break;
3289 }
3290 case REDIRECT_TYPE_LOCATION:
3291 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003292 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003293 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003294
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295 /* add location */
3296 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3297 rdr.len += rule->rdr_len;
3298 break;
3299 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003300
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 if (rule->cookie_len) {
3302 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3303 rdr.len += 14;
3304 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3305 rdr.len += rule->cookie_len;
3306 memcpy(rdr.str + rdr.len, "\r\n", 2);
3307 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003308 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003309
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003310 /* add end of headers and the keep-alive/close status.
3311 * We may choose to set keep-alive if the Location begins
3312 * with a slash, because the client will come back to the
3313 * same server.
3314 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003315 txn->status = rule->code;
3316 /* let's log the request time */
3317 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003318
3319 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3320 (txn->flags & TX_REQ_XFER_LEN) &&
3321 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3322 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3323 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3324 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003325 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003326 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3327 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3328 rdr.len += 30;
3329 } else {
3330 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3331 rdr.len += 24;
3332 }
Willy Tarreau75661452010-01-10 10:35:01 +01003333 }
3334 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3335 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003336 buffer_write(req->prod->ob, rdr.str, rdr.len);
3337 /* "eat" the request */
3338 buffer_ignore(req, msg->sov - msg->som);
3339 msg->som = msg->sov;
3340 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003341 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3342 txn->req.msg_state = HTTP_MSG_CLOSED;
3343 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003344 break;
3345 } else {
3346 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003347 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3348 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3349 rdr.len += 29;
3350 } else {
3351 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3352 rdr.len += 23;
3353 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003354 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003355 goto return_prx_cond;
3356 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003357 }
3358 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003359
Willy Tarreau2be39392010-01-03 17:24:51 +01003360 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3361 * If this happens, then the data will not come immediately, so we must
3362 * send all what we have without waiting. Note that due to the small gain
3363 * in waiting for the body of the request, it's easier to simply put the
3364 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3365 * itself once used.
3366 */
3367 req->flags |= BF_SEND_DONTWAIT;
3368
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003369 /* that's OK for us now, let's move on to next analysers */
3370 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003371
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003372 return_bad_req:
3373 /* We centralize bad requests processing here */
3374 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3375 /* we detected a parsing error. We want to archive this request
3376 * in the dedicated proxy area for later troubleshooting.
3377 */
3378 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3379 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003380
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003381 txn->req.msg_state = HTTP_MSG_ERROR;
3382 txn->status = 400;
3383 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003384
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003385 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003386 if (s->listener->counters)
3387 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003388
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003389 return_prx_cond:
3390 if (!(s->flags & SN_ERR_MASK))
3391 s->flags |= SN_ERR_PRXCOND;
3392 if (!(s->flags & SN_FINST_MASK))
3393 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003394
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003395 req->analysers = 0;
3396 req->analyse_exp = TICK_ETERNITY;
3397 return 0;
3398}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003399
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003400/* This function performs all the processing enabled for the current request.
3401 * It returns 1 if the processing can continue on next analysers, or zero if it
3402 * needs more data, encounters an error, or wants to immediately abort the
3403 * request. It relies on buffers flags, and updates s->req->analysers.
3404 */
3405int http_process_request(struct session *s, struct buffer *req, int an_bit)
3406{
3407 struct http_txn *txn = &s->txn;
3408 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003409
Willy Tarreau655dce92009-11-08 13:10:58 +01003410 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003411 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003412 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003413 return 0;
3414 }
3415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003416 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3417 now_ms, __FUNCTION__,
3418 s,
3419 req,
3420 req->rex, req->wex,
3421 req->flags,
3422 req->l,
3423 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003424
Willy Tarreau59234e92008-11-30 23:51:27 +01003425 /*
3426 * Right now, we know that we have processed the entire headers
3427 * and that unwanted requests have been filtered out. We can do
3428 * whatever we want with the remaining request. Also, now we
3429 * may have separate values for ->fe, ->be.
3430 */
Willy Tarreau06619262006-12-17 08:37:22 +01003431
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 /*
3433 * If HTTP PROXY is set we simply get remote server address
3434 * parsing incoming request.
3435 */
3436 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003437 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003439
Willy Tarreau59234e92008-11-30 23:51:27 +01003440 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003441 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 * Note that doing so might move headers in the request, but
3443 * the fields will stay coherent and the URI will not move.
3444 * This should only be performed in the backend.
3445 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003446 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3448 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003449
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003451 * 8: the appsession cookie was looked up very early in 1.2,
3452 * so let's do the same now.
3453 */
3454
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003455 /* It needs to look into the URI unless persistence must be ignored */
3456 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003457 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003458 }
3459
3460 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 * 9: add X-Forwarded-For if either the frontend or the backend
3462 * asks for it.
3463 */
3464 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3465 if (s->cli_addr.ss_family == AF_INET) {
3466 /* Add an X-Forwarded-For header unless the source IP is
3467 * in the 'except' network range.
3468 */
3469 if ((!s->fe->except_mask.s_addr ||
3470 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3471 != s->fe->except_net.s_addr) &&
3472 (!s->be->except_mask.s_addr ||
3473 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3474 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003475 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003476 unsigned char *pn;
3477 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003478
3479 /* Note: we rely on the backend to get the header name to be used for
3480 * x-forwarded-for, because the header is really meant for the backends.
3481 * However, if the backend did not specify any option, we have to rely
3482 * on the frontend's header name.
3483 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003484 if (s->be->fwdfor_hdr_len) {
3485 len = s->be->fwdfor_hdr_len;
3486 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003487 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003488 len = s->fe->fwdfor_hdr_len;
3489 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003490 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003491 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003492
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003493 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003494 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003495 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003496 }
3497 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003498 else if (s->cli_addr.ss_family == AF_INET6) {
3499 /* FIXME: for the sake of completeness, we should also support
3500 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003501 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003502 int len;
3503 char pn[INET6_ADDRSTRLEN];
3504 inet_ntop(AF_INET6,
3505 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3506 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003507
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 /* Note: we rely on the backend to get the header name to be used for
3509 * x-forwarded-for, because the header is really meant for the backends.
3510 * However, if the backend did not specify any option, we have to rely
3511 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003512 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003513 if (s->be->fwdfor_hdr_len) {
3514 len = s->be->fwdfor_hdr_len;
3515 memcpy(trash, s->be->fwdfor_hdr_name, len);
3516 } else {
3517 len = s->fe->fwdfor_hdr_len;
3518 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003519 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003520 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003521
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003523 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003524 goto return_bad_req;
3525 }
3526 }
3527
3528 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003529 * 10: add X-Original-To if either the frontend or the backend
3530 * asks for it.
3531 */
3532 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3533
3534 /* FIXME: don't know if IPv6 can handle that case too. */
3535 if (s->cli_addr.ss_family == AF_INET) {
3536 /* Add an X-Original-To header unless the destination IP is
3537 * in the 'except' network range.
3538 */
3539 if (!(s->flags & SN_FRT_ADDR_SET))
3540 get_frt_addr(s);
3541
Emeric Brun5bd86a82010-10-22 17:23:04 +02003542 if (s->frt_addr.ss_family == AF_INET &&
3543 ((!s->fe->except_mask_to.s_addr ||
3544 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3545 != s->fe->except_to.s_addr) &&
3546 (!s->be->except_mask_to.s_addr ||
3547 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3548 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003549 int len;
3550 unsigned char *pn;
3551 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3552
3553 /* Note: we rely on the backend to get the header name to be used for
3554 * x-original-to, because the header is really meant for the backends.
3555 * However, if the backend did not specify any option, we have to rely
3556 * on the frontend's header name.
3557 */
3558 if (s->be->orgto_hdr_len) {
3559 len = s->be->orgto_hdr_len;
3560 memcpy(trash, s->be->orgto_hdr_name, len);
3561 } else {
3562 len = s->fe->orgto_hdr_len;
3563 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003564 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003565 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3566
3567 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003568 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003569 goto return_bad_req;
3570 }
3571 }
3572 }
3573
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003574 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3575 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02003576 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) ||
3577 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003578 unsigned int want_flags = 0;
3579
3580 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003581 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3582 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3583 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003584 want_flags |= TX_CON_CLO_SET;
3585 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003586 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3587 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3588 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003589 want_flags |= TX_CON_KAL_SET;
3590 }
3591
3592 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3593 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003594 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003595
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003596
Willy Tarreau522d6c02009-12-06 18:49:18 +01003597 /* If we have no server assigned yet and we're balancing on url_param
3598 * with a POST request, we may be interested in checking the body for
3599 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003600 */
3601 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3602 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603 s->be->url_param_post_limit != 0 &&
3604 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003605 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003606 buffer_dont_connect(req);
3607 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003609
Willy Tarreaud98cf932009-12-27 22:54:55 +01003610 if (txn->flags & TX_REQ_XFER_LEN)
3611 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003612
Willy Tarreau59234e92008-11-30 23:51:27 +01003613 /*************************************************************
3614 * OK, that's finished for the headers. We have done what we *
3615 * could. Let's switch to the DATA state. *
3616 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617 req->analyse_exp = TICK_ETERNITY;
3618 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003619
Willy Tarreau59234e92008-11-30 23:51:27 +01003620 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003621 /* OK let's go on with the BODY now */
3622 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003623
Willy Tarreau59234e92008-11-30 23:51:27 +01003624 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003625 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003626 /* we detected a parsing error. We want to archive this request
3627 * in the dedicated proxy area for later troubleshooting.
3628 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003629 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003630 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003631
Willy Tarreau59234e92008-11-30 23:51:27 +01003632 txn->req.msg_state = HTTP_MSG_ERROR;
3633 txn->status = 400;
3634 req->analysers = 0;
3635 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003636
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003637 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003638 if (s->listener->counters)
3639 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003640
Willy Tarreau59234e92008-11-30 23:51:27 +01003641 if (!(s->flags & SN_ERR_MASK))
3642 s->flags |= SN_ERR_PRXCOND;
3643 if (!(s->flags & SN_FINST_MASK))
3644 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003645 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003646}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003647
Willy Tarreau60b85b02008-11-30 23:28:40 +01003648/* This function is an analyser which processes the HTTP tarpit. It always
3649 * returns zero, at the beginning because it prevents any other processing
3650 * from occurring, and at the end because it terminates the request.
3651 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003652int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003653{
3654 struct http_txn *txn = &s->txn;
3655
3656 /* This connection is being tarpitted. The CLIENT side has
3657 * already set the connect expiration date to the right
3658 * timeout. We just have to check that the client is still
3659 * there and that the timeout has not expired.
3660 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003661 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003662 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3663 !tick_is_expired(req->analyse_exp, now_ms))
3664 return 0;
3665
3666 /* We will set the queue timer to the time spent, just for
3667 * logging purposes. We fake a 500 server error, so that the
3668 * attacker will not suspect his connection has been tarpitted.
3669 * It will not cause trouble to the logs because we can exclude
3670 * the tarpitted connections by filtering on the 'PT' status flags.
3671 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003672 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3673
3674 txn->status = 500;
3675 if (req->flags != BF_READ_ERROR)
3676 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3677
3678 req->analysers = 0;
3679 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003680
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003681 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003682 if (s->listener->counters)
3683 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003684
Willy Tarreau60b85b02008-11-30 23:28:40 +01003685 if (!(s->flags & SN_ERR_MASK))
3686 s->flags |= SN_ERR_PRXCOND;
3687 if (!(s->flags & SN_FINST_MASK))
3688 s->flags |= SN_FINST_T;
3689 return 0;
3690}
3691
Willy Tarreaud34af782008-11-30 23:36:37 +01003692/* This function is an analyser which processes the HTTP request body. It looks
3693 * for parameters to be used for the load balancing algorithm (url_param). It
3694 * must only be called after the standard HTTP request processing has occurred,
3695 * because it expects the request to be parsed. It returns zero if it needs to
3696 * read more data, or 1 once it has completed its analysis.
3697 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003698int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003699{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003700 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003701 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003702 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003703
3704 /* We have to parse the HTTP request body to find any required data.
3705 * "balance url_param check_post" should have been the only way to get
3706 * into this. We were brought here after HTTP header analysis, so all
3707 * related structures are ready.
3708 */
3709
Willy Tarreau522d6c02009-12-06 18:49:18 +01003710 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3711 goto missing_data;
3712
3713 if (msg->msg_state < HTTP_MSG_100_SENT) {
3714 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3715 * send an HTTP/1.1 100 Continue intermediate response.
3716 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003717 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003718 struct hdr_ctx ctx;
3719 ctx.idx = 0;
3720 /* Expect is allowed in 1.1, look for it */
3721 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3722 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3723 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3724 }
3725 }
3726 msg->msg_state = HTTP_MSG_100_SENT;
3727 }
3728
3729 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003730 /* we have msg->col and msg->sov which both point to the first
3731 * byte of message body. msg->som still points to the beginning
3732 * of the message. We must save the body in req->lr because it
3733 * survives buffer re-alignments.
3734 */
3735 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003736 if (txn->flags & TX_REQ_TE_CHNK)
3737 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3738 else
3739 msg->msg_state = HTTP_MSG_DATA;
3740 }
3741
3742 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003743 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003744 * set ->sov and ->lr to point to the body and switch to DATA or
3745 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003746 */
3747 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003748
Willy Tarreau115acb92009-12-26 13:56:06 +01003749 if (!ret)
3750 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003751 else if (ret < 0) {
3752 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003753 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003754 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003755 }
3756
Willy Tarreaud98cf932009-12-27 22:54:55 +01003757 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003758 * We have the first non-header byte in msg->col, which is either the
3759 * beginning of the chunk size or of the data. The first data byte is in
3760 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3761 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003762 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003763
3764 if (msg->hdr_content_len < limit)
3765 limit = msg->hdr_content_len;
3766
Willy Tarreau7c96f672009-12-27 22:47:25 +01003767 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003768 goto http_end;
3769
3770 missing_data:
3771 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003772 if (req->flags & BF_FULL) {
3773 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003774 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003775 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003776
Willy Tarreau522d6c02009-12-06 18:49:18 +01003777 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3778 txn->status = 408;
3779 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003780
3781 if (!(s->flags & SN_ERR_MASK))
3782 s->flags |= SN_ERR_CLITO;
3783 if (!(s->flags & SN_FINST_MASK))
3784 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003785 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003786 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003787
3788 /* we get here if we need to wait for more data */
3789 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003790 /* Not enough data. We'll re-use the http-request
3791 * timeout here. Ideally, we should set the timeout
3792 * relative to the accept() date. We just set the
3793 * request timeout once at the beginning of the
3794 * request.
3795 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003796 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003797 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003798 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003799 return 0;
3800 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003801
3802 http_end:
3803 /* The situation will not evolve, so let's give up on the analysis. */
3804 s->logs.tv_request = now; /* update the request timer to reflect full request */
3805 req->analysers &= ~an_bit;
3806 req->analyse_exp = TICK_ETERNITY;
3807 return 1;
3808
3809 return_bad_req: /* let's centralize all bad requests */
3810 txn->req.msg_state = HTTP_MSG_ERROR;
3811 txn->status = 400;
3812 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3813
Willy Tarreau79ebac62010-06-07 13:47:49 +02003814 if (!(s->flags & SN_ERR_MASK))
3815 s->flags |= SN_ERR_PRXCOND;
3816 if (!(s->flags & SN_FINST_MASK))
3817 s->flags |= SN_FINST_R;
3818
Willy Tarreau522d6c02009-12-06 18:49:18 +01003819 return_err_msg:
3820 req->analysers = 0;
3821 s->fe->counters.failed_req++;
3822 if (s->listener->counters)
3823 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003824 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003825}
3826
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827/* Terminate current transaction and prepare a new one. This is very tricky
3828 * right now but it works.
3829 */
3830void http_end_txn_clean_session(struct session *s)
3831{
3832 /* FIXME: We need a more portable way of releasing a backend's and a
3833 * server's connections. We need a safer way to reinitialize buffer
3834 * flags. We also need a more accurate method for computing per-request
3835 * data.
3836 */
3837 http_silent_debug(__LINE__, s);
3838
3839 s->req->cons->flags |= SI_FL_NOLINGER;
3840 s->req->cons->shutr(s->req->cons);
3841 s->req->cons->shutw(s->req->cons);
3842
3843 http_silent_debug(__LINE__, s);
3844
3845 if (s->flags & SN_BE_ASSIGNED)
3846 s->be->beconn--;
3847
3848 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3849 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003850 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003851
3852 if (s->txn.status) {
3853 int n;
3854
3855 n = s->txn.status / 100;
3856 if (n < 1 || n > 5)
3857 n = 0;
3858
3859 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003860 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003861
Willy Tarreau24657792010-02-26 10:30:28 +01003862 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003863 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003864 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865 }
3866
3867 /* don't count other requests' data */
3868 s->logs.bytes_in -= s->req->l - s->req->send_max;
3869 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3870
3871 /* let's do a final log if we need it */
3872 if (s->logs.logwait &&
3873 !(s->flags & SN_MONITOR) &&
3874 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3875 s->do_log(s);
3876 }
3877
3878 s->logs.accept_date = date; /* user-visible date for logging */
3879 s->logs.tv_accept = now; /* corrected date for internal use */
3880 tv_zero(&s->logs.tv_request);
3881 s->logs.t_queue = -1;
3882 s->logs.t_connect = -1;
3883 s->logs.t_data = -1;
3884 s->logs.t_close = 0;
3885 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3886 s->logs.srv_queue_size = 0; /* we will get this number soon */
3887
3888 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3889 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3890
3891 if (s->pend_pos)
3892 pendconn_free(s->pend_pos);
3893
3894 if (s->srv) {
3895 if (s->flags & SN_CURR_SESS) {
3896 s->flags &= ~SN_CURR_SESS;
3897 s->srv->cur_sess--;
3898 }
3899 if (may_dequeue_tasks(s->srv, s->be))
3900 process_srv_queue(s->srv);
3901 }
3902
3903 if (unlikely(s->srv_conn))
3904 sess_change_server(s, NULL);
3905 s->srv = NULL;
3906
3907 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3908 s->req->cons->fd = -1; /* just to help with debugging */
3909 s->req->cons->err_type = SI_ET_NONE;
3910 s->req->cons->err_loc = NULL;
3911 s->req->cons->exp = TICK_ETERNITY;
3912 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003913 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3914 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);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003915 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 +01003916 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3917 s->txn.meth = 0;
3918 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003919 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003920 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003921 s->req->cons->flags |= SI_FL_INDEP_STR;
3922
3923 /* if the request buffer is not empty, it means we're
3924 * about to process another request, so send pending
3925 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003926 * Just don't do this if the buffer is close to be full,
3927 * because the request will wait for it to flush a little
3928 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003930 if (s->req->l > s->req->send_max) {
3931 if (s->rep->send_max &&
3932 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003933 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3934 s->rep->flags |= BF_EXPECT_MORE;
3935 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003936
3937 /* we're removing the analysers, we MUST re-enable events detection */
3938 buffer_auto_read(s->req);
3939 buffer_auto_close(s->req);
3940 buffer_auto_read(s->rep);
3941 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942
3943 /* make ->lr point to the first non-forwarded byte */
3944 s->req->lr = s->req->w + s->req->send_max;
3945 if (s->req->lr >= s->req->data + s->req->size)
3946 s->req->lr -= s->req->size;
3947 s->rep->lr = s->rep->w + s->rep->send_max;
3948 if (s->rep->lr >= s->rep->data + s->rep->size)
3949 s->rep->lr -= s->req->size;
3950
Willy Tarreau342b11c2010-11-24 16:22:09 +01003951 s->req->analysers = s->listener->analysers;
3952 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003953 s->rep->analysers = 0;
3954
3955 http_silent_debug(__LINE__, s);
3956}
3957
3958
3959/* This function updates the request state machine according to the response
3960 * state machine and buffer flags. It returns 1 if it changes anything (flag
3961 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3962 * it is only used to find when a request/response couple is complete. Both
3963 * this function and its equivalent should loop until both return zero. It
3964 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3965 */
3966int http_sync_req_state(struct session *s)
3967{
3968 struct buffer *buf = s->req;
3969 struct http_txn *txn = &s->txn;
3970 unsigned int old_flags = buf->flags;
3971 unsigned int old_state = txn->req.msg_state;
3972
3973 http_silent_debug(__LINE__, s);
3974 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3975 return 0;
3976
3977 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003978 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003979 * We can shut the read side unless we want to abort_on_close,
3980 * or we have a POST request. The issue with POST requests is
3981 * that some browsers still send a CRLF after the request, and
3982 * this CRLF must be read so that it does not remain in the kernel
3983 * buffers, otherwise a close could cause an RST on some systems
3984 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003985 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003986 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003987 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988
3989 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3990 goto wait_other_side;
3991
3992 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3993 /* The server has not finished to respond, so we
3994 * don't want to move in order not to upset it.
3995 */
3996 goto wait_other_side;
3997 }
3998
3999 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4000 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004001 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 txn->req.msg_state = HTTP_MSG_TUNNEL;
4003 goto wait_other_side;
4004 }
4005
4006 /* When we get here, it means that both the request and the
4007 * response have finished receiving. Depending on the connection
4008 * mode, we'll have to wait for the last bytes to leave in either
4009 * direction, and sometimes for a close to be effective.
4010 */
4011
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004012 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4013 /* Server-close mode : queue a connection close to the server */
4014 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004016 }
4017 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4018 /* Option forceclose is set, or either side wants to close,
4019 * let's enforce it now that we're not expecting any new
4020 * data to come. The caller knows the session is complete
4021 * once both states are CLOSED.
4022 */
4023 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024 buffer_shutr_now(buf);
4025 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004027 }
4028 else {
4029 /* The last possible modes are keep-alive and tunnel. Since tunnel
4030 * mode does not set the body analyser, we can't reach this place
4031 * in tunnel mode, so we're left with keep-alive only.
4032 * This mode is currently not implemented, we switch to tunnel mode.
4033 */
4034 buffer_auto_read(buf);
4035 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004036 }
4037
4038 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4039 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004040 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4041
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 if (!(buf->flags & BF_OUT_EMPTY)) {
4043 txn->req.msg_state = HTTP_MSG_CLOSING;
4044 goto http_msg_closing;
4045 }
4046 else {
4047 txn->req.msg_state = HTTP_MSG_CLOSED;
4048 goto http_msg_closed;
4049 }
4050 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004051 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 }
4053
4054 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4055 http_msg_closing:
4056 /* nothing else to forward, just waiting for the output buffer
4057 * to be empty and for the shutw_now to take effect.
4058 */
4059 if (buf->flags & BF_OUT_EMPTY) {
4060 txn->req.msg_state = HTTP_MSG_CLOSED;
4061 goto http_msg_closed;
4062 }
4063 else if (buf->flags & BF_SHUTW) {
4064 txn->req.msg_state = HTTP_MSG_ERROR;
4065 goto wait_other_side;
4066 }
4067 }
4068
4069 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4070 http_msg_closed:
4071 goto wait_other_side;
4072 }
4073
4074 wait_other_side:
4075 http_silent_debug(__LINE__, s);
4076 return txn->req.msg_state != old_state || buf->flags != old_flags;
4077}
4078
4079
4080/* This function updates the response state machine according to the request
4081 * state machine and buffer flags. It returns 1 if it changes anything (flag
4082 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4083 * it is only used to find when a request/response couple is complete. Both
4084 * this function and its equivalent should loop until both return zero. It
4085 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4086 */
4087int http_sync_res_state(struct session *s)
4088{
4089 struct buffer *buf = s->rep;
4090 struct http_txn *txn = &s->txn;
4091 unsigned int old_flags = buf->flags;
4092 unsigned int old_state = txn->rsp.msg_state;
4093
4094 http_silent_debug(__LINE__, s);
4095 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4096 return 0;
4097
4098 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4099 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004100 * still monitor the server connection for a possible close
4101 * while the request is being uploaded, so we don't disable
4102 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004104 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105
4106 if (txn->req.msg_state == HTTP_MSG_ERROR)
4107 goto wait_other_side;
4108
4109 if (txn->req.msg_state < HTTP_MSG_DONE) {
4110 /* The client seems to still be sending data, probably
4111 * because we got an error response during an upload.
4112 * We have the choice of either breaking the connection
4113 * or letting it pass through. Let's do the later.
4114 */
4115 goto wait_other_side;
4116 }
4117
4118 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4119 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004120 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4122 goto wait_other_side;
4123 }
4124
4125 /* When we get here, it means that both the request and the
4126 * response have finished receiving. Depending on the connection
4127 * mode, we'll have to wait for the last bytes to leave in either
4128 * direction, and sometimes for a close to be effective.
4129 */
4130
4131 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4132 /* Server-close mode : shut read and wait for the request
4133 * side to close its output buffer. The caller will detect
4134 * when we're in DONE and the other is in CLOSED and will
4135 * catch that for the final cleanup.
4136 */
4137 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4138 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004140 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4141 /* Option forceclose is set, or either side wants to close,
4142 * let's enforce it now that we're not expecting any new
4143 * data to come. The caller knows the session is complete
4144 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004146 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4147 buffer_shutr_now(buf);
4148 buffer_shutw_now(buf);
4149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 }
4151 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004152 /* The last possible modes are keep-alive and tunnel. Since tunnel
4153 * mode does not set the body analyser, we can't reach this place
4154 * in tunnel mode, so we're left with keep-alive only.
4155 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004157 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004158 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004159 }
4160
4161 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4162 /* if we've just closed an output, let's switch */
4163 if (!(buf->flags & BF_OUT_EMPTY)) {
4164 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4165 goto http_msg_closing;
4166 }
4167 else {
4168 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4169 goto http_msg_closed;
4170 }
4171 }
4172 goto wait_other_side;
4173 }
4174
4175 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4176 http_msg_closing:
4177 /* nothing else to forward, just waiting for the output buffer
4178 * to be empty and for the shutw_now to take effect.
4179 */
4180 if (buf->flags & BF_OUT_EMPTY) {
4181 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4182 goto http_msg_closed;
4183 }
4184 else if (buf->flags & BF_SHUTW) {
4185 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004186 s->be->counters.cli_aborts++;
4187 if (s->srv)
4188 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004189 goto wait_other_side;
4190 }
4191 }
4192
4193 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4194 http_msg_closed:
4195 /* drop any pending data */
4196 buffer_ignore(buf, buf->l - buf->send_max);
4197 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004198 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004199 goto wait_other_side;
4200 }
4201
4202 wait_other_side:
4203 http_silent_debug(__LINE__, s);
4204 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4205}
4206
4207
4208/* Resync the request and response state machines. Return 1 if either state
4209 * changes.
4210 */
4211int http_resync_states(struct session *s)
4212{
4213 struct http_txn *txn = &s->txn;
4214 int old_req_state = txn->req.msg_state;
4215 int old_res_state = txn->rsp.msg_state;
4216
4217 http_silent_debug(__LINE__, s);
4218 http_sync_req_state(s);
4219 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004220 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 if (!http_sync_res_state(s))
4222 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004223 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224 if (!http_sync_req_state(s))
4225 break;
4226 }
4227 http_silent_debug(__LINE__, s);
4228 /* OK, both state machines agree on a compatible state.
4229 * There are a few cases we're interested in :
4230 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4231 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4232 * directions, so let's simply disable both analysers.
4233 * - HTTP_MSG_CLOSED on the response only means we must abort the
4234 * request.
4235 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4236 * with server-close mode means we've completed one request and we
4237 * must re-initialize the server connection.
4238 */
4239
4240 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4241 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4242 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4243 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4244 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004245 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004246 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004248 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004249 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004251 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4252 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004253 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004254 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004255 s->rep->analysers = 0;
4256 buffer_auto_close(s->rep);
4257 buffer_auto_read(s->rep);
4258 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 buffer_abort(s->req);
4260 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004261 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 }
4264 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4265 txn->rsp.msg_state == HTTP_MSG_DONE &&
4266 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4267 /* server-close: terminate this server connection and
4268 * reinitialize a fresh-new transaction.
4269 */
4270 http_end_txn_clean_session(s);
4271 }
4272
4273 http_silent_debug(__LINE__, s);
4274 return txn->req.msg_state != old_req_state ||
4275 txn->rsp.msg_state != old_res_state;
4276}
4277
Willy Tarreaud98cf932009-12-27 22:54:55 +01004278/* This function is an analyser which forwards request body (including chunk
4279 * sizes if any). It is called as soon as we must forward, even if we forward
4280 * zero byte. The only situation where it must not be called is when we're in
4281 * tunnel mode and we want to forward till the close. It's used both to forward
4282 * remaining data and to resync after end of body. It expects the msg_state to
4283 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4284 * read more data, or 1 once we can go on with next request or end the session.
4285 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4286 * bytes of pending data + the headers if not already done (between som and sov).
4287 * It eventually adjusts som to match sov after the data in between have been sent.
4288 */
4289int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4290{
4291 struct http_txn *txn = &s->txn;
4292 struct http_msg *msg = &s->txn.req;
4293
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004294 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4295 return 0;
4296
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004297 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4298 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004299 /* Output closed while we were sending data. We must abort and
4300 * wake the other side up.
4301 */
4302 msg->msg_state = HTTP_MSG_ERROR;
4303 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004304 return 1;
4305 }
4306
Willy Tarreau4fe41902010-06-07 22:27:41 +02004307 /* in most states, we should abort in case of early close */
4308 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004309
4310 /* Note that we don't have to send 100-continue back because we don't
4311 * need the data to complete our job, and it's up to the server to
4312 * decide whether to return 100, 417 or anything else in return of
4313 * an "Expect: 100-continue" header.
4314 */
4315
4316 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4317 /* we have msg->col and msg->sov which both point to the first
4318 * byte of message body. msg->som still points to the beginning
4319 * of the message. We must save the body in req->lr because it
4320 * survives buffer re-alignments.
4321 */
4322 req->lr = req->data + msg->sov;
4323 if (txn->flags & TX_REQ_TE_CHNK)
4324 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4325 else {
4326 msg->msg_state = HTTP_MSG_DATA;
4327 }
4328 }
4329
Willy Tarreaud98cf932009-12-27 22:54:55 +01004330 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004332 /* we may have some data pending */
4333 if (msg->hdr_content_len || msg->som != msg->sov) {
4334 int bytes = msg->sov - msg->som;
4335 if (bytes < 0) /* sov may have wrapped at the end */
4336 bytes += req->size;
4337 buffer_forward(req, bytes + msg->hdr_content_len);
4338 msg->hdr_content_len = 0; /* don't forward that again */
4339 msg->som = msg->sov;
4340 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004341
Willy Tarreaucaabe412010-01-03 23:08:28 +01004342 if (msg->msg_state == HTTP_MSG_DATA) {
4343 /* must still forward */
4344 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004345 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004346
4347 /* nothing left to forward */
4348 if (txn->flags & TX_REQ_TE_CHNK)
4349 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004351 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004352 }
4353 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 /* read the chunk size and assign it to ->hdr_content_len, then
4355 * set ->sov and ->lr to point to the body and switch to DATA or
4356 * TRAILERS state.
4357 */
4358 int ret = http_parse_chunk_size(req, msg);
4359
4360 if (!ret)
4361 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004362 else if (ret < 0) {
4363 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004364 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004365 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004367 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4368 if (msg->msg_state == HTTP_MSG_DATA)
4369 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004370 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004371 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4372 /* we want the CRLF after the data */
4373 int ret;
4374
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004375 req->lr = req->w + req->send_max;
4376 if (req->lr >= req->data + req->size)
4377 req->lr -= req->size;
4378
Willy Tarreaud98cf932009-12-27 22:54:55 +01004379 ret = http_skip_chunk_crlf(req, msg);
4380
4381 if (ret == 0)
4382 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004383 else if (ret < 0) {
4384 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004385 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004386 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004387 /* we're in MSG_CHUNK_SIZE now */
4388 }
4389 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4390 int ret = http_forward_trailers(req, msg);
4391
4392 if (ret == 0)
4393 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004394 else if (ret < 0) {
4395 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004396 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004397 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004398 /* we're in HTTP_MSG_DONE now */
4399 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004400 else {
4401 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004402 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004403 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4404 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4405 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406 if (http_resync_states(s)) {
4407 /* some state changes occurred, maybe the analyser
4408 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004409 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4411 goto return_bad_req;
4412 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004413 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004414
4415 /* If "option abortonclose" is set on the backend, we
4416 * want to monitor the client's connection and forward
4417 * any shutdown notification to the server, which will
4418 * decide whether to close or to go on processing the
4419 * request.
4420 */
4421 if (s->be->options & PR_O_ABRT_CLOSE) {
4422 buffer_auto_read(req);
4423 buffer_auto_close(req);
4424 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004425 else if (s->txn.meth == HTTP_METH_POST) {
4426 /* POST requests may require to read extra CRLF
4427 * sent by broken browsers and which could cause
4428 * an RST to be sent upon close on some systems
4429 * (eg: Linux).
4430 */
4431 buffer_auto_read(req);
4432 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004433
Willy Tarreau610ecce2010-01-04 21:15:02 +01004434 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004435 }
4436 }
4437
Willy Tarreaud98cf932009-12-27 22:54:55 +01004438 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004439 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004440 if (req->flags & BF_SHUTR) {
4441 if (!(s->flags & SN_ERR_MASK))
4442 s->flags |= SN_ERR_CLICL;
4443 if (!(s->flags & SN_FINST_MASK))
4444 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004445 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004446 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004447
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004448 /* waiting for the last bits to leave the buffer */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004449 if (req->flags & BF_SHUTW) {
4450 if (!(s->flags & SN_ERR_MASK))
4451 s->flags |= SN_ERR_SRVCL;
4452 if (!(s->flags & SN_FINST_MASK))
4453 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004454 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004455 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004456
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004457 /* When TE: chunked is used, we need to get there again to parse remaining
4458 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4459 */
4460 if (txn->flags & TX_REQ_TE_CHNK)
4461 buffer_dont_close(req);
4462
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004464 return 0;
4465
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466 return_bad_req: /* let's centralize all bad requests */
4467 txn->req.msg_state = HTTP_MSG_ERROR;
4468 txn->status = 400;
4469 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004470 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004471 req->analysers = 0;
4472 s->fe->counters.failed_req++;
4473 if (s->listener->counters)
4474 s->listener->counters->failed_req++;
4475
4476 if (!(s->flags & SN_ERR_MASK))
4477 s->flags |= SN_ERR_PRXCOND;
4478 if (!(s->flags & SN_FINST_MASK))
4479 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004481 return 0;
4482}
4483
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4485 * processing can continue on next analysers, or zero if it either needs more
4486 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4487 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4488 * when it has nothing left to do, and may remove any analyser when it wants to
4489 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004490 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004492{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 struct http_txn *txn = &s->txn;
4494 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004495 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004496 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004497 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004498 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004499
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004500 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 +02004501 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004503 rep,
4504 rep->rex, rep->wex,
4505 rep->flags,
4506 rep->l,
4507 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004508
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 /*
4510 * Now parse the partial (or complete) lines.
4511 * We will check the response syntax, and also join multi-line
4512 * headers. An index of all the lines will be elaborated while
4513 * parsing.
4514 *
4515 * For the parsing, we use a 28 states FSM.
4516 *
4517 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004518 * rep->data + msg->som = beginning of response
4519 * rep->data + msg->eoh = end of processed headers / start of current one
4520 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 * rep->lr = first non-visited byte
4522 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004523 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 */
4525
Willy Tarreau83e3af02009-12-28 17:39:57 +01004526 /* There's a protected area at the end of the buffer for rewriting
4527 * purposes. We don't want to start to parse the request if the
4528 * protected area is affected, because we may have to move processed
4529 * data later, which is much more complicated.
4530 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004531 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4532 if (unlikely((rep->flags & BF_FULL) ||
4533 rep->r < rep->lr ||
4534 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4535 if (rep->send_max) {
4536 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004537 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4538 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004539 buffer_dont_close(rep);
4540 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4541 return 0;
4542 }
4543 if (rep->l <= rep->size - global.tune.maxrewrite)
4544 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004545 }
4546
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004547 if (likely(rep->lr < rep->r))
4548 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004549 }
4550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 /* 1: we might have to print this header in debug mode */
4552 if (unlikely((global.mode & MODE_DEBUG) &&
4553 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004554 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004555 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004557
Willy Tarreau663308b2010-06-07 14:06:08 +02004558 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004559 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004561
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 sol += hdr_idx_first_pos(&txn->hdr_idx);
4563 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004564
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 while (cur_idx) {
4566 eol = sol + txn->hdr_idx.v[cur_idx].len;
4567 debug_hdr("srvhdr", s, sol, eol);
4568 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4569 cur_idx = txn->hdr_idx.v[cur_idx].next;
4570 }
4571 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 /*
4574 * Now we quickly check if we have found a full valid response.
4575 * If not so, we check the FD and buffer states before leaving.
4576 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004577 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004578 * responses are checked first.
4579 *
4580 * Depending on whether the client is still there or not, we
4581 * may send an error response back or not. Note that normally
4582 * we should only check for HTTP status there, and check I/O
4583 * errors somewhere else.
4584 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585
Willy Tarreau655dce92009-11-08 13:10:58 +01004586 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004587 /* Invalid response */
4588 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4589 /* we detected a parsing error. We want to archive this response
4590 * in the dedicated proxy area for later troubleshooting.
4591 */
4592 hdr_response_bad:
4593 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4594 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4595
4596 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004597 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004599 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4600 }
Willy Tarreau64648412010-03-05 10:41:54 +01004601 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004602 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004603 rep->analysers = 0;
4604 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004605 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004606 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4608
4609 if (!(s->flags & SN_ERR_MASK))
4610 s->flags |= SN_ERR_PRXCOND;
4611 if (!(s->flags & SN_FINST_MASK))
4612 s->flags |= SN_FINST_H;
4613
4614 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004615 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 /* too large response does not fit in buffer. */
4618 else if (rep->flags & BF_FULL) {
4619 goto hdr_response_bad;
4620 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004621
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004622 /* read error */
4623 else if (rep->flags & BF_READ_ERROR) {
4624 if (msg->err_pos >= 0)
4625 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004626
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004628 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004630 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4631 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004632
Willy Tarreau90deb182010-01-07 00:20:41 +01004633 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 rep->analysers = 0;
4635 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004636 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004637 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004639
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 if (!(s->flags & SN_ERR_MASK))
4641 s->flags |= SN_ERR_SRVCL;
4642 if (!(s->flags & SN_FINST_MASK))
4643 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004644 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004645 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004646
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647 /* read timeout : return a 504 to the client. */
4648 else if (rep->flags & BF_READ_TIMEOUT) {
4649 if (msg->err_pos >= 0)
4650 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004651
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004653 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004655 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4656 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004657
Willy Tarreau90deb182010-01-07 00:20:41 +01004658 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659 rep->analysers = 0;
4660 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004661 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004662 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004664
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 if (!(s->flags & SN_ERR_MASK))
4666 s->flags |= SN_ERR_SRVTO;
4667 if (!(s->flags & SN_FINST_MASK))
4668 s->flags |= SN_FINST_H;
4669 return 0;
4670 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004671
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 /* close from server */
4673 else if (rep->flags & BF_SHUTR) {
4674 if (msg->err_pos >= 0)
4675 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004676
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004678 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004680 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4681 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004682
Willy Tarreau90deb182010-01-07 00:20:41 +01004683 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684 rep->analysers = 0;
4685 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004686 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004687 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004688 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004689
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690 if (!(s->flags & SN_ERR_MASK))
4691 s->flags |= SN_ERR_SRVCL;
4692 if (!(s->flags & SN_FINST_MASK))
4693 s->flags |= SN_FINST_H;
4694 return 0;
4695 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004696
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697 /* write error to client (we don't send any message then) */
4698 else if (rep->flags & BF_WRITE_ERROR) {
4699 if (msg->err_pos >= 0)
4700 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004701
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 s->be->counters.failed_resp++;
4703 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004704 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705
4706 if (!(s->flags & SN_ERR_MASK))
4707 s->flags |= SN_ERR_CLICL;
4708 if (!(s->flags & SN_FINST_MASK))
4709 s->flags |= SN_FINST_H;
4710
4711 /* process_session() will take care of the error */
4712 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004713 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 buffer_dont_close(rep);
4716 return 0;
4717 }
4718
4719 /* More interesting part now : we know that we have a complete
4720 * response which at least looks like HTTP. We have an indicator
4721 * of each header's length, so we can parse them quickly.
4722 */
4723
4724 if (unlikely(msg->err_pos >= 0))
4725 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4726
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 /*
4728 * 1: get the status code
4729 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004730 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 if (n < 1 || n > 5)
4732 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004733 /* when the client triggers a 4xx from the server, it's most often due
4734 * to a missing object or permission. These events should be tracked
4735 * because if they happen often, it may indicate a brute force or a
4736 * vulnerability scan.
4737 */
4738 if (n == 4)
4739 session_inc_http_err_ctr(s);
4740
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004741 if (s->srv)
4742 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743
Willy Tarreau5b154472009-12-21 20:11:07 +01004744 /* check if the response is HTTP/1.1 or above */
4745 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004746 ((msg->sol[5] > '1') ||
4747 ((msg->sol[5] == '1') &&
4748 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004749 txn->flags |= TX_RES_VER_11;
4750
4751 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004752 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 +01004753
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004754 /* transfer length unknown*/
4755 txn->flags &= ~TX_RES_XFER_LEN;
4756
Willy Tarreau962c3f42010-01-10 00:15:35 +01004757 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758
Willy Tarreau39650402010-03-15 19:44:39 +01004759 /* Adjust server's health based on status code. Note: status codes 501
4760 * and 505 are triggered on demand by client request, so we must not
4761 * count them as server failures.
4762 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004763 if (s->srv) {
4764 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4765 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4766 else
4767 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4768 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 /*
4771 * 2: check for cacheability.
4772 */
4773
4774 switch (txn->status) {
4775 case 200:
4776 case 203:
4777 case 206:
4778 case 300:
4779 case 301:
4780 case 410:
4781 /* RFC2616 @13.4:
4782 * "A response received with a status code of
4783 * 200, 203, 206, 300, 301 or 410 MAY be stored
4784 * by a cache (...) unless a cache-control
4785 * directive prohibits caching."
4786 *
4787 * RFC2616 @9.5: POST method :
4788 * "Responses to this method are not cacheable,
4789 * unless the response includes appropriate
4790 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004791 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792 if (likely(txn->meth != HTTP_METH_POST) &&
4793 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4794 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4795 break;
4796 default:
4797 break;
4798 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004799
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004800 /*
4801 * 3: we may need to capture headers
4802 */
4803 s->logs.logwait &= ~LW_RESP;
4804 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004805 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004806 txn->rsp.cap, s->fe->rsp_cap);
4807
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 /* 4: determine the transfer-length.
4809 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4810 * the presence of a message-body in a RESPONSE and its transfer length
4811 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004813 * All responses to the HEAD request method MUST NOT include a
4814 * message-body, even though the presence of entity-header fields
4815 * might lead one to believe they do. All 1xx (informational), 204
4816 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4817 * message-body. All other responses do include a message-body,
4818 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004820 * 1. Any response which "MUST NOT" include a message-body (such as the
4821 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4822 * always terminated by the first empty line after the header fields,
4823 * regardless of the entity-header fields present in the message.
4824 *
4825 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4826 * the "chunked" transfer-coding (Section 6.2) is used, the
4827 * transfer-length is defined by the use of this transfer-coding.
4828 * If a Transfer-Encoding header field is present and the "chunked"
4829 * transfer-coding is not present, the transfer-length is defined by
4830 * the sender closing the connection.
4831 *
4832 * 3. If a Content-Length header field is present, its decimal value in
4833 * OCTETs represents both the entity-length and the transfer-length.
4834 * If a message is received with both a Transfer-Encoding header
4835 * field and a Content-Length header field, the latter MUST be ignored.
4836 *
4837 * 4. If the message uses the media type "multipart/byteranges", and
4838 * the transfer-length is not otherwise specified, then this self-
4839 * delimiting media type defines the transfer-length. This media
4840 * type MUST NOT be used unless the sender knows that the recipient
4841 * can parse it; the presence in a request of a Range header with
4842 * multiple byte-range specifiers from a 1.1 client implies that the
4843 * client can parse multipart/byteranges responses.
4844 *
4845 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004846 */
4847
4848 /* Skip parsing if no content length is possible. The response flags
4849 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004850 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004851 * FIXME: should we parse anyway and return an error on chunked encoding ?
4852 */
4853 if (txn->meth == HTTP_METH_HEAD ||
4854 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004855 txn->status == 204 || txn->status == 304) {
4856 txn->flags |= TX_RES_XFER_LEN;
4857 goto skip_content_length;
4858 }
4859
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004860 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004861 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004862 while ((txn->flags & TX_RES_VER_11) &&
4863 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004864 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4865 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4866 else if (txn->flags & TX_RES_TE_CHNK) {
4867 /* bad transfer-encoding (chunked followed by something else) */
4868 use_close_only = 1;
4869 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4870 break;
4871 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004872 }
4873
4874 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4875 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004876 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004877 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4878 signed long long cl;
4879
4880 if (!ctx.vlen)
4881 goto hdr_response_bad;
4882
4883 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4884 goto hdr_response_bad; /* parse failure */
4885
4886 if (cl < 0)
4887 goto hdr_response_bad;
4888
4889 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4890 goto hdr_response_bad; /* already specified, was different */
4891
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004892 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004893 msg->hdr_content_len = cl;
4894 }
4895
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004896 /* FIXME: we should also implement the multipart/byterange method.
4897 * For now on, we resort to close mode in this case (unknown length).
4898 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004899skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004900
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004901 /* end of job, return OK */
4902 rep->analysers &= ~an_bit;
4903 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004904 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905 return 1;
4906}
4907
4908/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004909 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4910 * and updates t->rep->analysers. It might make sense to explode it into several
4911 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004912 */
4913int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4914{
4915 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004916 struct http_msg *msg = &txn->rsp;
4917 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004918 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004919
4920 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4921 now_ms, __FUNCTION__,
4922 t,
4923 rep,
4924 rep->rex, rep->wex,
4925 rep->flags,
4926 rep->l,
4927 rep->analysers);
4928
Willy Tarreau655dce92009-11-08 13:10:58 +01004929 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004930 return 0;
4931
4932 rep->analysers &= ~an_bit;
4933 rep->analyse_exp = TICK_ETERNITY;
4934
Willy Tarreau5b154472009-12-21 20:11:07 +01004935 /* Now we have to check if we need to modify the Connection header.
4936 * This is more difficult on the response than it is on the request,
4937 * because we can have two different HTTP versions and we don't know
4938 * how the client will interprete a response. For instance, let's say
4939 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4940 * HTTP/1.1 response without any header. Maybe it will bound itself to
4941 * HTTP/1.0 because it only knows about it, and will consider the lack
4942 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4943 * the lack of header as a keep-alive. Thus we will use two flags
4944 * indicating how a request MAY be understood by the client. In case
4945 * of multiple possibilities, we'll fix the header to be explicit. If
4946 * ambiguous cases such as both close and keepalive are seen, then we
4947 * will fall back to explicit close. Note that we won't take risks with
4948 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004949 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004950 */
4951
Willy Tarreaudc008c52010-02-01 16:20:08 +01004952 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4953 txn->status == 101)) {
4954 /* Either we've established an explicit tunnel, or we're
4955 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004956 * to understand the next protocols. We have to switch to tunnel
4957 * mode, so that we transfer the request and responses then let
4958 * this protocol pass unmodified. When we later implement specific
4959 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004960 * header which contains information about that protocol for
4961 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004962 */
4963 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4964 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004965 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4966 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4967 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004968 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004969
Willy Tarreau60466522010-01-18 19:08:45 +01004970 /* on unknown transfer length, we must close */
4971 if (!(txn->flags & TX_RES_XFER_LEN) &&
4972 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4973 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004974
Willy Tarreau60466522010-01-18 19:08:45 +01004975 /* now adjust header transformations depending on current state */
4976 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4977 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4978 to_del |= 2; /* remove "keep-alive" on any response */
4979 if (!(txn->flags & TX_RES_VER_11))
4980 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004981 }
Willy Tarreau60466522010-01-18 19:08:45 +01004982 else { /* SCL / KAL */
4983 to_del |= 1; /* remove "close" on any response */
4984 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4985 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004986 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004987
Willy Tarreau60466522010-01-18 19:08:45 +01004988 /* Parse and remove some headers from the connection header */
4989 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004990
Willy Tarreau60466522010-01-18 19:08:45 +01004991 /* Some keep-alive responses are converted to Server-close if
4992 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004993 */
Willy Tarreau60466522010-01-18 19:08:45 +01004994 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4995 if ((txn->flags & TX_HDR_CONN_CLO) ||
4996 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4997 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004998 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004999 }
5000
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005002 /*
5003 * 3: we will have to evaluate the filters.
5004 * As opposed to version 1.2, now they will be evaluated in the
5005 * filters order and not in the header order. This means that
5006 * each filter has to be validated among all headers.
5007 *
5008 * Filters are tried with ->be first, then with ->fe if it is
5009 * different from ->be.
5010 */
5011
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005012 cur_proxy = t->be;
5013 while (1) {
5014 struct proxy *rule_set = cur_proxy;
5015
5016 /* try headers filters */
5017 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005018 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005019 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005020 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005021 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005022 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
5023 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005024 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005025 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005026 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005027 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005028 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005029 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005030 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 if (!(t->flags & SN_ERR_MASK))
5032 t->flags |= SN_ERR_PRXCOND;
5033 if (!(t->flags & SN_FINST_MASK))
5034 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005035 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005036 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005037 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005038
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005039 /* has the response been denied ? */
5040 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01005041 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005042 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005043
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005044 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005045 if (t->listener->counters)
5046 t->listener->counters->denied_resp++;
5047
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005049 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005050
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005052 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005053 if (txn->status < 200)
5054 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005055 if (wl->cond) {
5056 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5057 ret = acl_pass(ret);
5058 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5059 ret = !ret;
5060 if (!ret)
5061 continue;
5062 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005063 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005065 }
5066
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067 /* check whether we're already working on the frontend */
5068 if (cur_proxy == t->fe)
5069 break;
5070 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005072
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005073 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005074 * We may be facing a 100-continue response, in which case this
5075 * is not the right response, and we're waiting for the next one.
5076 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005077 * next one.
5078 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005079 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005080 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005081 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005082 msg->msg_state = HTTP_MSG_RPBEFORE;
5083 txn->status = 0;
5084 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5085 return 1;
5086 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005087 else if (unlikely(txn->status < 200))
5088 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005089
5090 /* we don't have any 1xx status code now */
5091
5092 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 * 4: check for server cookie.
5094 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005095 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5096 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005097 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005098
Willy Tarreaubaaee002006-06-26 02:48:02 +02005099
Willy Tarreaua15645d2007-03-18 16:22:39 +01005100 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005102 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005103 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005104 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005105
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005106 /*
5107 * 6: add server cookie in the response if needed
5108 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005109 if ((t->srv) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005110 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005111 (!(t->flags & SN_DIRECT) ||
5112 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5113 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5114 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5115 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005116 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5117 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005119 /* the server is known, it's not the one the client requested, or the
5120 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 * insert a set-cookie here, except if we want to insert only on POST
5122 * requests and this one isn't. Note that servers which don't have cookies
5123 * (eg: some backup servers) will return a full cookie removal request.
5124 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005125 if (!t->srv->cookie) {
5126 len = sprintf(trash,
5127 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5128 t->be->cookie_name);
5129 }
5130 else {
5131 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, t->srv->cookie);
5132
5133 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5134 /* emit last_date, which is mandatory */
5135 trash[len++] = COOKIE_DELIM_DATE;
5136 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5137 if (t->be->cookie_maxlife) {
5138 /* emit first_date, which is either the original one or
5139 * the current date.
5140 */
5141 trash[len++] = COOKIE_DELIM_DATE;
5142 s30tob64(txn->cookie_first_date ?
5143 txn->cookie_first_date >> 2 :
5144 (date.tv_sec+3) >> 2, trash + len);
5145 len += 5;
5146 }
5147 }
5148 len += sprintf(trash + len, "; path=/");
5149 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005150
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005151 if (t->be->cookie_domain)
5152 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005153
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005154 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005155 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005156 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005157
Willy Tarreauf1348312010-10-07 15:54:11 +02005158 txn->flags &= ~TX_SCK_MASK;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005159 if (t->srv->cookie && (t->flags & SN_DIRECT))
5160 /* the server did not change, only the date was updated */
5161 txn->flags |= TX_SCK_UPDATED;
5162 else
5163 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005164
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 /* Here, we will tell an eventual cache on the client side that we don't
5166 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5167 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5168 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5169 */
5170 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005171
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005172 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5173
5174 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005175 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005177 }
5178 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005179
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005180 /*
5181 * 7: check if result will be cacheable with a cookie.
5182 * We'll block the response if security checks have caught
5183 * nasty things such as a cacheable cookie.
5184 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005185 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5186 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005187 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188
5189 /* we're in presence of a cacheable response containing
5190 * a set-cookie header. We'll block it as requested by
5191 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005192 */
Willy Tarreau8365f932009-03-15 23:11:49 +01005193 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005194 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005195
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005196 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005197 if (t->listener->counters)
5198 t->listener->counters->denied_resp++;
5199
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005200 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5201 t->be->id, t->srv?t->srv->id:"<dispatch>");
5202 send_log(t->be, LOG_ALERT,
5203 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5204 t->be->id, t->srv?t->srv->id:"<dispatch>");
5205 goto return_srv_prx_502;
5206 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207
5208 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005209 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005210 */
Willy Tarreau60466522010-01-18 19:08:45 +01005211 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5212 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5213 unsigned int want_flags = 0;
5214
5215 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5216 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5217 /* we want a keep-alive response here. Keep-alive header
5218 * required if either side is not 1.1.
5219 */
5220 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5221 want_flags |= TX_CON_KAL_SET;
5222 }
5223 else {
5224 /* we want a close response here. Close header required if
5225 * the server is 1.1, regardless of the client.
5226 */
5227 if (txn->flags & TX_RES_VER_11)
5228 want_flags |= TX_CON_CLO_SET;
5229 }
5230
5231 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5232 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005233 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005234
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005235 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005236 if ((txn->flags & TX_RES_XFER_LEN) ||
5237 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005238 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 /*************************************************************
5241 * OK, that's finished for the headers. We have done what we *
5242 * could. Let's switch to the DATA state. *
5243 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005244
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005246
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 /* if the user wants to log as soon as possible, without counting
5248 * bytes from the server, then this is the right moment. We have
5249 * to temporarily assign bytes_out to log what we currently have.
5250 */
5251 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5252 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5253 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005254 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005255 t->logs.bytes_out = 0;
5256 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005257
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005258 /* Note: we must not try to cheat by jumping directly to DATA,
5259 * otherwise we would not let the client side wake up.
5260 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005261
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005262 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005263 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005264 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005266
Willy Tarreaud98cf932009-12-27 22:54:55 +01005267/* This function is an analyser which forwards response body (including chunk
5268 * sizes if any). It is called as soon as we must forward, even if we forward
5269 * zero byte. The only situation where it must not be called is when we're in
5270 * tunnel mode and we want to forward till the close. It's used both to forward
5271 * remaining data and to resync after end of body. It expects the msg_state to
5272 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5273 * read more data, or 1 once we can go on with next request or end the session.
5274 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5275 * bytes of pending data + the headers if not already done (between som and sov).
5276 * It eventually adjusts som to match sov after the data in between have been sent.
5277 */
5278int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5279{
5280 struct http_txn *txn = &s->txn;
5281 struct http_msg *msg = &s->txn.rsp;
5282
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005283 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5284 return 0;
5285
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005286 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005287 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005288 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005289 /* Output closed while we were sending data. We must abort and
5290 * wake the other side up.
5291 */
5292 msg->msg_state = HTTP_MSG_ERROR;
5293 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005294 return 1;
5295 }
5296
Willy Tarreau4fe41902010-06-07 22:27:41 +02005297 /* in most states, we should abort in case of early close */
5298 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005299
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5301 /* we have msg->col and msg->sov which both point to the first
5302 * byte of message body. msg->som still points to the beginning
5303 * of the message. We must save the body in req->lr because it
5304 * survives buffer re-alignments.
5305 */
5306 res->lr = res->data + msg->sov;
5307 if (txn->flags & TX_RES_TE_CHNK)
5308 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5309 else {
5310 msg->msg_state = HTTP_MSG_DATA;
5311 }
5312 }
5313
Willy Tarreaud98cf932009-12-27 22:54:55 +01005314 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005316 /* we may have some data pending */
5317 if (msg->hdr_content_len || msg->som != msg->sov) {
5318 int bytes = msg->sov - msg->som;
5319 if (bytes < 0) /* sov may have wrapped at the end */
5320 bytes += res->size;
5321 buffer_forward(res, bytes + msg->hdr_content_len);
5322 msg->hdr_content_len = 0; /* don't forward that again */
5323 msg->som = msg->sov;
5324 }
5325
Willy Tarreaucaabe412010-01-03 23:08:28 +01005326 if (msg->msg_state == HTTP_MSG_DATA) {
5327 /* must still forward */
5328 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005329 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005330
5331 /* nothing left to forward */
5332 if (txn->flags & TX_RES_TE_CHNK)
5333 msg->msg_state = HTTP_MSG_DATA_CRLF;
5334 else
5335 msg->msg_state = HTTP_MSG_DONE;
5336 }
5337 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005338 /* read the chunk size and assign it to ->hdr_content_len, then
5339 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5340 */
5341 int ret = http_parse_chunk_size(res, msg);
5342
5343 if (!ret)
5344 goto missing_data;
5345 else if (ret < 0)
5346 goto return_bad_res;
5347 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005348 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5349 if (msg->msg_state == HTTP_MSG_DATA)
5350 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005351 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5353 /* we want the CRLF after the data */
5354 int ret;
5355
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005356 res->lr = res->w + res->send_max;
5357 if (res->lr >= res->data + res->size)
5358 res->lr -= res->size;
5359
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 ret = http_skip_chunk_crlf(res, msg);
5361
5362 if (!ret)
5363 goto missing_data;
5364 else if (ret < 0)
5365 goto return_bad_res;
5366 /* we're in MSG_CHUNK_SIZE now */
5367 }
5368 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5369 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005370
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 if (ret == 0)
5372 goto missing_data;
5373 else if (ret < 0)
5374 goto return_bad_res;
5375 /* we're in HTTP_MSG_DONE now */
5376 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 else {
5378 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005379 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005380 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5381 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5382 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 if (http_resync_states(s)) {
5384 http_silent_debug(__LINE__, s);
5385 /* some state changes occurred, maybe the analyser
5386 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005387 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5389 goto return_bad_res;
5390 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005391 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 }
5394 }
5395
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005397 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005398 if (res->flags & BF_SHUTR) {
5399 if (!(s->flags & SN_ERR_MASK))
5400 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005401 s->be->counters.srv_aborts++;
5402 if (s->srv)
5403 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005404 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005405 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005406
Willy Tarreau40dba092010-03-04 18:14:51 +01005407 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 if (!s->req->analysers)
5409 goto return_bad_res;
5410
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 /* forward the chunk size as well as any pending data */
5412 if (msg->hdr_content_len || msg->som != msg->sov) {
5413 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5414 msg->hdr_content_len = 0; /* don't forward that again */
5415 msg->som = msg->sov;
5416 }
5417
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005418 /* When TE: chunked is used, we need to get there again to parse remaining
5419 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5420 * Similarly, with keep-alive on the client side, we don't want to forward a
5421 * close.
5422 */
5423 if ((txn->flags & TX_RES_TE_CHNK) ||
5424 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5425 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5426 buffer_dont_close(res);
5427
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005429 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430 return 0;
5431
Willy Tarreau40dba092010-03-04 18:14:51 +01005432 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005434 /* don't send any error message as we're in the body */
5435 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436 res->analysers = 0;
5437 s->be->counters.failed_resp++;
5438 if (s->srv) {
5439 s->srv->counters.failed_resp++;
5440 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5441 }
5442
5443 if (!(s->flags & SN_ERR_MASK))
5444 s->flags |= SN_ERR_PRXCOND;
5445 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005446 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 return 0;
5449}
5450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451/* Iterate the same filter through all request headers.
5452 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005453 * Since it can manage the switch to another backend, it updates the per-proxy
5454 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005455 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005456int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005457{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005458 char term;
5459 char *cur_ptr, *cur_end, *cur_next;
5460 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005461 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462 struct hdr_idx_elem *cur_hdr;
5463 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005465 last_hdr = 0;
5466
Willy Tarreau962c3f42010-01-10 00:15:35 +01005467 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 old_idx = 0;
5469
5470 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005471 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005472 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005473 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 (exp->action == ACT_ALLOW ||
5475 exp->action == ACT_DENY ||
5476 exp->action == ACT_TARPIT))
5477 return 0;
5478
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 if (!cur_idx)
5481 break;
5482
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005483 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005484 cur_ptr = cur_next;
5485 cur_end = cur_ptr + cur_hdr->len;
5486 cur_next = cur_end + cur_hdr->cr + 1;
5487
5488 /* Now we have one header between cur_ptr and cur_end,
5489 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005490 */
5491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 /* The annoying part is that pattern matching needs
5493 * that we modify the contents to null-terminate all
5494 * strings before testing them.
5495 */
5496
5497 term = *cur_end;
5498 *cur_end = '\0';
5499
5500 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5501 switch (exp->action) {
5502 case ACT_SETBE:
5503 /* It is not possible to jump a second time.
5504 * FIXME: should we return an HTTP/500 here so that
5505 * the admin knows there's a problem ?
5506 */
5507 if (t->be != t->fe)
5508 break;
5509
5510 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005511 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005512 last_hdr = 1;
5513 break;
5514
5515 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005516 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 last_hdr = 1;
5518 break;
5519
5520 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005521 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005522 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005523
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005524 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005525 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005526 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 break;
5529
5530 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005531 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005532 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005533
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005534 t->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_REPLACE:
5541 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5542 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5543 /* FIXME: if the user adds a newline in the replacement, the
5544 * index will not be recalculated for now, and the new line
5545 * will not be counted as a new header.
5546 */
5547
5548 cur_end += delta;
5549 cur_next += delta;
5550 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005551 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005552 break;
5553
5554 case ACT_REMOVE:
5555 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5556 cur_next += delta;
5557
Willy Tarreaufa355d42009-11-29 18:12:29 +01005558 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005559 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5560 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 cur_hdr->len = 0;
5562 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005563 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 break;
5565
5566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 if (cur_end)
5569 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 /* keep the link from this header to next one in case of later
5572 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 old_idx = cur_idx;
5575 }
5576 return 0;
5577}
5578
5579
5580/* Apply the filter to the request line.
5581 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5582 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005583 * Since it can manage the switch to another backend, it updates the per-proxy
5584 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005585 */
5586int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5587{
5588 char term;
5589 char *cur_ptr, *cur_end;
5590 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005591 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593
Willy Tarreau58f10d72006-12-04 02:26:12 +01005594
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005597 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 (exp->action == ACT_ALLOW ||
5599 exp->action == ACT_DENY ||
5600 exp->action == ACT_TARPIT))
5601 return 0;
5602 else if (exp->action == ACT_REMOVE)
5603 return 0;
5604
5605 done = 0;
5606
Willy Tarreau962c3f42010-01-10 00:15:35 +01005607 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005608 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609
5610 /* Now we have the request line between cur_ptr and cur_end */
5611
5612 /* The annoying part is that pattern matching needs
5613 * that we modify the contents to null-terminate all
5614 * strings before testing them.
5615 */
5616
5617 term = *cur_end;
5618 *cur_end = '\0';
5619
5620 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5621 switch (exp->action) {
5622 case ACT_SETBE:
5623 /* It is not possible to jump a second time.
5624 * FIXME: should we return an HTTP/500 here so that
5625 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005626 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 if (t->be != t->fe)
5628 break;
5629
5630 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005631 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005632 done = 1;
5633 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005636 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637 done = 1;
5638 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005641 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005642
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005643 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005644 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005645 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 done = 1;
5648 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005651 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005652
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005653 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005654 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005655 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 done = 1;
5658 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 case ACT_REPLACE:
5661 *cur_end = term; /* restore the string terminator */
5662 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5663 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5664 /* FIXME: if the user adds a newline in the replacement, the
5665 * index will not be recalculated for now, and the new line
5666 * will not be counted as a new header.
5667 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005668
Willy Tarreaufa355d42009-11-29 18:12:29 +01005669 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005670 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005671 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 HTTP_MSG_RQMETH,
5673 cur_ptr, cur_end + 1,
5674 NULL, NULL);
5675 if (unlikely(!cur_end))
5676 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005677
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 /* we have a full request and we know that we have either a CR
5679 * or an LF at <ptr>.
5680 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005681 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5682 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 /* there is no point trying this regex on headers */
5684 return 1;
5685 }
5686 }
5687 *cur_end = term; /* restore the string terminator */
5688 return done;
5689}
Willy Tarreau97de6242006-12-27 17:18:38 +01005690
Willy Tarreau58f10d72006-12-04 02:26:12 +01005691
Willy Tarreau58f10d72006-12-04 02:26:12 +01005692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005693/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005694 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005696 * unparsable request. Since it can manage the switch to another backend, it
5697 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005699int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005701 struct http_txn *txn = &s->txn;
5702 struct hdr_exp *exp;
5703
5704 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 /*
5708 * The interleaving of transformations and verdicts
5709 * makes it difficult to decide to continue or stop
5710 * the evaluation.
5711 */
5712
Willy Tarreau6c123b12010-01-28 20:22:06 +01005713 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5714 break;
5715
Willy Tarreau3d300592007-03-18 18:34:41 +01005716 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005718 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005720
5721 /* if this filter had a condition, evaluate it now and skip to
5722 * next filter if the condition does not match.
5723 */
5724 if (exp->cond) {
5725 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5726 ret = acl_pass(ret);
5727 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5728 ret = !ret;
5729
5730 if (!ret)
5731 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005732 }
5733
5734 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005735 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 if (unlikely(ret < 0))
5737 return -1;
5738
5739 if (likely(ret == 0)) {
5740 /* The filter did not match the request, it can be
5741 * iterated through all headers.
5742 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005743 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005744 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005745 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005747}
5748
5749
Willy Tarreaua15645d2007-03-18 16:22:39 +01005750
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005752 * Try to retrieve the server associated to the appsession.
5753 * If the server is found, it's assigned to the session.
5754 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005755void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005756 struct http_txn *txn = &t->txn;
5757 appsess *asession = NULL;
5758 char *sessid_temp = NULL;
5759
Cyril Bontéb21570a2009-11-29 20:04:48 +01005760 if (len > t->be->appsession_len) {
5761 len = t->be->appsession_len;
5762 }
5763
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005764 if (t->be->options2 & PR_O2_AS_REQL) {
5765 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005766 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005767 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005768 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005769 }
5770
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005771 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5773 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5774 return;
5775 }
5776
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005777 memcpy(txn->sessid, buf, len);
5778 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005779 }
5780
5781 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5782 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5783 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5784 return;
5785 }
5786
Cyril Bontéb21570a2009-11-29 20:04:48 +01005787 memcpy(sessid_temp, buf, len);
5788 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005789
5790 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5791 /* free previously allocated memory */
5792 pool_free2(apools.sessid, sessid_temp);
5793
5794 if (asession != NULL) {
5795 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5796 if (!(t->be->options2 & PR_O2_AS_REQL))
5797 asession->request_count++;
5798
5799 if (asession->serverid != NULL) {
5800 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005801
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005802 while (srv) {
5803 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005804 if ((srv->state & SRV_RUNNING) ||
5805 (t->be->options & PR_O_PERSIST) ||
5806 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005807 /* we found the server and it's usable */
5808 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005809 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005810 t->flags |= SN_DIRECT | SN_ASSIGNED;
5811 t->srv = srv;
5812 break;
5813 } else {
5814 txn->flags &= ~TX_CK_MASK;
5815 txn->flags |= TX_CK_DOWN;
5816 }
5817 }
5818 srv = srv->next;
5819 }
5820 }
5821 }
5822}
5823
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005824/* Find the end of a cookie value contained between <s> and <e>. It works the
5825 * same way as with headers above except that the semi-colon also ends a token.
5826 * See RFC2965 for more information. Note that it requires a valid header to
5827 * return a valid result.
5828 */
5829char *find_cookie_value_end(char *s, const char *e)
5830{
5831 int quoted, qdpair;
5832
5833 quoted = qdpair = 0;
5834 for (; s < e; s++) {
5835 if (qdpair) qdpair = 0;
5836 else if (quoted) {
5837 if (*s == '\\') qdpair = 1;
5838 else if (*s == '"') quoted = 0;
5839 }
5840 else if (*s == '"') quoted = 1;
5841 else if (*s == ',' || *s == ';') return s;
5842 }
5843 return s;
5844}
5845
5846/* Delete a value in a header between delimiters <from> and <next> in buffer
5847 * <buf>. The number of characters displaced is returned, and the pointer to
5848 * the first delimiter is updated if required. The function tries as much as
5849 * possible to respect the following principles :
5850 * - replace <from> delimiter by the <next> one unless <from> points to a
5851 * colon, in which case <next> is simply removed
5852 * - set exactly one space character after the new first delimiter, unless
5853 * there are not enough characters in the block being moved to do so.
5854 * - remove unneeded spaces before the previous delimiter and after the new
5855 * one.
5856 *
5857 * It is the caller's responsibility to ensure that :
5858 * - <from> points to a valid delimiter or the colon ;
5859 * - <next> points to a valid delimiter or the final CR/LF ;
5860 * - there are non-space chars before <from> ;
5861 * - there is a CR/LF at or after <next>.
5862 */
5863int del_hdr_value(struct buffer *buf, char **from, char *next)
5864{
5865 char *prev = *from;
5866
5867 if (*prev == ':') {
5868 /* We're removing the first value, preserve the colon and add a
5869 * space if possible.
5870 */
5871 if (!http_is_crlf[(unsigned char)*next])
5872 next++;
5873 prev++;
5874 if (prev < next)
5875 *prev++ = ' ';
5876
5877 while (http_is_spht[(unsigned char)*next])
5878 next++;
5879 } else {
5880 /* Remove useless spaces before the old delimiter. */
5881 while (http_is_spht[(unsigned char)*(prev-1)])
5882 prev--;
5883 *from = prev;
5884
5885 /* copy the delimiter and if possible a space if we're
5886 * not at the end of the line.
5887 */
5888 if (!http_is_crlf[(unsigned char)*next]) {
5889 *prev++ = *next++;
5890 if (prev + 1 < next)
5891 *prev++ = ' ';
5892 while (http_is_spht[(unsigned char)*next])
5893 next++;
5894 }
5895 }
5896 return buffer_replace2(buf, prev, next, NULL, 0);
5897}
5898
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005899/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005900 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005901 * desirable to call it only when needed. This code is quite complex because
5902 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5903 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904 */
5905void manage_client_side_cookies(struct session *t, struct buffer *req)
5906{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005907 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005908 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005910 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5911 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005914 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005915 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005916
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005917 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005918 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005919 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005920
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005921 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005922 hdr_beg = hdr_next;
5923 hdr_end = hdr_beg + cur_hdr->len;
5924 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005926 /* We have one full header between hdr_beg and hdr_end, and the
5927 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005928 * "Cookie:" headers.
5929 */
5930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005932 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 old_idx = cur_idx;
5934 continue;
5935 }
5936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005937 del_from = NULL; /* nothing to be deleted */
5938 preserve_hdr = 0; /* assume we may kill the whole header */
5939
Willy Tarreau58f10d72006-12-04 02:26:12 +01005940 /* Now look for cookies. Conforming to RFC2109, we have to support
5941 * attributes whose name begin with a '$', and associate them with
5942 * the right cookie, if we want to delete this cookie.
5943 * So there are 3 cases for each cookie read :
5944 * 1) it's a special attribute, beginning with a '$' : ignore it.
5945 * 2) it's a server id cookie that we *MAY* want to delete : save
5946 * some pointers on it (last semi-colon, beginning of cookie...)
5947 * 3) it's an application cookie : we *MAY* have to delete a previous
5948 * "special" cookie.
5949 * At the end of loop, if a "special" cookie remains, we may have to
5950 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005951 * *MUST* delete it.
5952 *
5953 * Note: RFC2965 is unclear about the processing of spaces around
5954 * the equal sign in the ATTR=VALUE form. A careful inspection of
5955 * the RFC explicitly allows spaces before it, and not within the
5956 * tokens (attrs or values). An inspection of RFC2109 allows that
5957 * too but section 10.1.3 lets one think that spaces may be allowed
5958 * after the equal sign too, resulting in some (rare) buggy
5959 * implementations trying to do that. So let's do what servers do.
5960 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5961 * allowed quoted strings in values, with any possible character
5962 * after a backslash, including control chars and delimitors, which
5963 * causes parsing to become ambiguous. Browsers also allow spaces
5964 * within values even without quotes.
5965 *
5966 * We have to keep multiple pointers in order to support cookie
5967 * removal at the beginning, middle or end of header without
5968 * corrupting the header. All of these headers are valid :
5969 *
5970 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5971 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5972 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5973 * | | | | | | | | |
5974 * | | | | | | | | hdr_end <--+
5975 * | | | | | | | +--> next
5976 * | | | | | | +----> val_end
5977 * | | | | | +-----------> val_beg
5978 * | | | | +--------------> equal
5979 * | | | +----------------> att_end
5980 * | | +---------------------> att_beg
5981 * | +--------------------------> prev
5982 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005983 */
5984
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005985 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5986 /* Iterate through all cookies on this line */
5987
5988 /* find att_beg */
5989 att_beg = prev + 1;
5990 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5991 att_beg++;
5992
5993 /* find att_end : this is the first character after the last non
5994 * space before the equal. It may be equal to hdr_end.
5995 */
5996 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005997
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005998 while (equal < hdr_end) {
5999 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006000 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006001 if (http_is_spht[(unsigned char)*equal++])
6002 continue;
6003 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006004 }
6005
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006006 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6007 * is between <att_beg> and <equal>, both may be identical.
6008 */
6009
6010 /* look for end of cookie if there is an equal sign */
6011 if (equal < hdr_end && *equal == '=') {
6012 /* look for the beginning of the value */
6013 val_beg = equal + 1;
6014 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6015 val_beg++;
6016
6017 /* find the end of the value, respecting quotes */
6018 next = find_cookie_value_end(val_beg, hdr_end);
6019
6020 /* make val_end point to the first white space or delimitor after the value */
6021 val_end = next;
6022 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6023 val_end--;
6024 } else {
6025 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006026 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006028 /* We have nothing to do with attributes beginning with '$'. However,
6029 * they will automatically be removed if a header before them is removed,
6030 * since they're supposed to be linked together.
6031 */
6032 if (*att_beg == '$')
6033 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006035 /* Ignore cookies with no equal sign */
6036 if (equal == next) {
6037 /* This is not our cookie, so we must preserve it. But if we already
6038 * scheduled another cookie for removal, we cannot remove the
6039 * complete header, but we can remove the previous block itself.
6040 */
6041 preserve_hdr = 1;
6042 if (del_from != NULL) {
6043 int delta = del_hdr_value(req, &del_from, prev);
6044 val_end += delta;
6045 next += delta;
6046 hdr_end += delta;
6047 hdr_next += delta;
6048 cur_hdr->len += delta;
6049 http_msg_move_end(&txn->req, delta);
6050 prev = del_from;
6051 del_from = NULL;
6052 }
6053 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006055
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006056 /* if there are spaces around the equal sign, we need to
6057 * strip them otherwise we'll get trouble for cookie captures,
6058 * or even for rewrites. Since this happens extremely rarely,
6059 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006061 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6062 int stripped_before = 0;
6063 int stripped_after = 0;
6064
6065 if (att_end != equal) {
6066 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6067 equal += stripped_before;
6068 val_beg += stripped_before;
6069 }
6070
6071 if (val_beg > equal + 1) {
6072 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6073 val_beg += stripped_after;
6074 stripped_before += stripped_after;
6075 }
6076
6077 val_end += stripped_before;
6078 next += stripped_before;
6079 hdr_end += stripped_before;
6080 hdr_next += stripped_before;
6081 cur_hdr->len += stripped_before;
6082 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006084 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006086 /* First, let's see if we want to capture this cookie. We check
6087 * that we don't already have a client side cookie, because we
6088 * can only capture one. Also as an optimisation, we ignore
6089 * cookies shorter than the declared name.
6090 */
6091 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6092 (val_end - att_beg >= t->fe->capture_namelen) &&
6093 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6094 int log_len = val_end - att_beg;
6095
6096 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6097 Alert("HTTP logging : out of memory.\n");
6098 } else {
6099 if (log_len > t->fe->capture_len)
6100 log_len = t->fe->capture_len;
6101 memcpy(txn->cli_cookie, att_beg, log_len);
6102 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105
Willy Tarreaubca99692010-10-06 19:25:55 +02006106 /* Persistence cookies in passive, rewrite or insert mode have the
6107 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006109 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006111 * For cookies in prefix mode, the form is :
6112 *
6113 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 */
6115 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6116 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6117 struct server *srv = t->be->srv;
6118 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006120 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6121 * have the server ID between val_beg and delim, and the original cookie between
6122 * delim+1 and val_end. Otherwise, delim==val_end :
6123 *
6124 * Cookie: NAME=SRV; # in all but prefix modes
6125 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6126 * | || || | |+-> next
6127 * | || || | +--> val_end
6128 * | || || +---------> delim
6129 * | || |+------------> val_beg
6130 * | || +-------------> att_end = equal
6131 * | |+-----------------> att_beg
6132 * | +------------------> prev
6133 * +-------------------------> hdr_beg
6134 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136 if (t->be->options & PR_O_COOK_PFX) {
6137 for (delim = val_beg; delim < val_end; delim++)
6138 if (*delim == COOKIE_DELIM)
6139 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006140 } else {
6141 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006143 /* Now check if the cookie contains a date field, which would
6144 * appear after a vertical bar ('|') just after the server name
6145 * and before the delimiter.
6146 */
6147 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6148 if (vbar1) {
6149 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006150 * right is the last seen date. It is a base64 encoded
6151 * 30-bit value representing the UNIX date since the
6152 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006153 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006154 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006155 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006156 if (val_end - vbar1 >= 5) {
6157 val = b64tos30(vbar1);
6158 if (val > 0)
6159 txn->cookie_last_date = val << 2;
6160 }
6161 /* look for a second vertical bar */
6162 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6163 if (vbar1 && (val_end - vbar1 > 5)) {
6164 val = b64tos30(vbar1 + 1);
6165 if (val > 0)
6166 txn->cookie_first_date = val << 2;
6167 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006168 }
6169 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170
Willy Tarreauf64d1412010-10-07 20:06:11 +02006171 /* if the cookie has an expiration date and the proxy wants to check
6172 * it, then we do that now. We first check if the cookie is too old,
6173 * then only if it has expired. We detect strict overflow because the
6174 * time resolution here is not great (4 seconds). Cookies with dates
6175 * in the future are ignored if their offset is beyond one day. This
6176 * allows an admin to fix timezone issues without expiring everyone
6177 * and at the same time avoids keeping unwanted side effects for too
6178 * long.
6179 */
6180 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006181 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6182 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006183 txn->flags &= ~TX_CK_MASK;
6184 txn->flags |= TX_CK_OLD;
6185 delim = val_beg; // let's pretend we have not found the cookie
6186 txn->cookie_first_date = 0;
6187 txn->cookie_last_date = 0;
6188 }
6189 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006190 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6191 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006192 txn->flags &= ~TX_CK_MASK;
6193 txn->flags |= TX_CK_EXPIRED;
6194 delim = val_beg; // let's pretend we have not found the cookie
6195 txn->cookie_first_date = 0;
6196 txn->cookie_last_date = 0;
6197 }
6198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006199 /* Here, we'll look for the first running server which supports the cookie.
6200 * This allows to share a same cookie between several servers, for example
6201 * to dedicate backup servers to specific servers only.
6202 * However, to prevent clients from sticking to cookie-less backup server
6203 * when they have incidentely learned an empty cookie, we simply ignore
6204 * empty cookies and mark them as invalid.
6205 * The same behaviour is applied when persistence must be ignored.
6206 */
6207 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6208 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006209
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006210 while (srv) {
6211 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6212 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6213 if ((srv->state & SRV_RUNNING) ||
6214 (t->be->options & PR_O_PERSIST) ||
6215 (t->flags & SN_FORCE_PRST)) {
6216 /* we found the server and we can use it */
6217 txn->flags &= ~TX_CK_MASK;
6218 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6219 t->flags |= SN_DIRECT | SN_ASSIGNED;
6220 t->srv = srv;
6221 break;
6222 } else {
6223 /* we found a server, but it's down,
6224 * mark it as such and go on in case
6225 * another one is available.
6226 */
6227 txn->flags &= ~TX_CK_MASK;
6228 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 srv = srv->next;
6232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233
Willy Tarreauf64d1412010-10-07 20:06:11 +02006234 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 /* no server matched this cookie */
6236 txn->flags &= ~TX_CK_MASK;
6237 txn->flags |= TX_CK_INVALID;
6238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 /* depending on the cookie mode, we may have to either :
6241 * - delete the complete cookie if we're in insert+indirect mode, so that
6242 * the server never sees it ;
6243 * - remove the server id from the cookie value, and tag the cookie as an
6244 * application cookie so that it does not get accidentely removed later,
6245 * if we're in cookie prefix mode
6246 */
6247 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6248 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006249
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6251 val_end += delta;
6252 next += delta;
6253 hdr_end += delta;
6254 hdr_next += delta;
6255 cur_hdr->len += delta;
6256 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 del_from = NULL;
6259 preserve_hdr = 1; /* we want to keep this cookie */
6260 }
6261 else if (del_from == NULL &&
6262 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6263 del_from = prev;
6264 }
6265 } else {
6266 /* This is not our cookie, so we must preserve it. But if we already
6267 * scheduled another cookie for removal, we cannot remove the
6268 * complete header, but we can remove the previous block itself.
6269 */
6270 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 if (del_from != NULL) {
6273 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006274 if (att_beg >= del_from)
6275 att_beg += delta;
6276 if (att_end >= del_from)
6277 att_end += delta;
6278 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 val_end += delta;
6280 next += delta;
6281 hdr_end += delta;
6282 hdr_next += delta;
6283 cur_hdr->len += delta;
6284 http_msg_move_end(&txn->req, delta);
6285 prev = del_from;
6286 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 /* Look for the appsession cookie unless persistence must be ignored */
6291 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6292 int cmp_len, value_len;
6293 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006294
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 if (t->be->options2 & PR_O2_AS_PFX) {
6296 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6297 value_begin = att_beg + t->be->appsession_name_len;
6298 value_len = val_end - att_beg - t->be->appsession_name_len;
6299 } else {
6300 cmp_len = att_end - att_beg;
6301 value_begin = val_beg;
6302 value_len = val_end - val_beg;
6303 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006304
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006305 /* let's see if the cookie is our appcookie */
6306 if (cmp_len == t->be->appsession_name_len &&
6307 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6308 manage_client_side_appsession(t, value_begin, value_len);
6309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006310 }
6311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 /* continue with next cookie on this header line */
6313 att_beg = next;
6314 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 /* There are no more cookies on this line.
6317 * We may still have one (or several) marked for deletion at the
6318 * end of the line. We must do this now in two ways :
6319 * - if some cookies must be preserved, we only delete from the
6320 * mark to the end of line ;
6321 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006324 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 if (preserve_hdr) {
6326 delta = del_hdr_value(req, &del_from, hdr_end);
6327 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328 cur_hdr->len += delta;
6329 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331
6332 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006333 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6334 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006336 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006338 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006339 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 }
6341
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345}
6346
6347
Willy Tarreaua15645d2007-03-18 16:22:39 +01006348/* Iterate the same filter through all response headers contained in <rtr>.
6349 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6350 */
6351int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6352{
6353 char term;
6354 char *cur_ptr, *cur_end, *cur_next;
6355 int cur_idx, old_idx, last_hdr;
6356 struct http_txn *txn = &t->txn;
6357 struct hdr_idx_elem *cur_hdr;
6358 int len, delta;
6359
6360 last_hdr = 0;
6361
Willy Tarreau962c3f42010-01-10 00:15:35 +01006362 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006363 old_idx = 0;
6364
6365 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006366 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006367 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006368 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 (exp->action == ACT_ALLOW ||
6370 exp->action == ACT_DENY))
6371 return 0;
6372
6373 cur_idx = txn->hdr_idx.v[old_idx].next;
6374 if (!cur_idx)
6375 break;
6376
6377 cur_hdr = &txn->hdr_idx.v[cur_idx];
6378 cur_ptr = cur_next;
6379 cur_end = cur_ptr + cur_hdr->len;
6380 cur_next = cur_end + cur_hdr->cr + 1;
6381
6382 /* Now we have one header between cur_ptr and cur_end,
6383 * and the next header starts at cur_next.
6384 */
6385
6386 /* The annoying part is that pattern matching needs
6387 * that we modify the contents to null-terminate all
6388 * strings before testing them.
6389 */
6390
6391 term = *cur_end;
6392 *cur_end = '\0';
6393
6394 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6395 switch (exp->action) {
6396 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006397 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006398 last_hdr = 1;
6399 break;
6400
6401 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006402 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403 last_hdr = 1;
6404 break;
6405
6406 case ACT_REPLACE:
6407 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6408 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6409 /* FIXME: if the user adds a newline in the replacement, the
6410 * index will not be recalculated for now, and the new line
6411 * will not be counted as a new header.
6412 */
6413
6414 cur_end += delta;
6415 cur_next += delta;
6416 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006417 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418 break;
6419
6420 case ACT_REMOVE:
6421 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6422 cur_next += delta;
6423
Willy Tarreaufa355d42009-11-29 18:12:29 +01006424 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006425 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6426 txn->hdr_idx.used--;
6427 cur_hdr->len = 0;
6428 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006429 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006430 break;
6431
6432 }
6433 }
6434 if (cur_end)
6435 *cur_end = term; /* restore the string terminator */
6436
6437 /* keep the link from this header to next one in case of later
6438 * removal of next header.
6439 */
6440 old_idx = cur_idx;
6441 }
6442 return 0;
6443}
6444
6445
6446/* Apply the filter to the status line in the response buffer <rtr>.
6447 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6448 * or -1 if a replacement resulted in an invalid status line.
6449 */
6450int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6451{
6452 char term;
6453 char *cur_ptr, *cur_end;
6454 int done;
6455 struct http_txn *txn = &t->txn;
6456 int len, delta;
6457
6458
Willy Tarreau3d300592007-03-18 18:34:41 +01006459 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006461 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006462 (exp->action == ACT_ALLOW ||
6463 exp->action == ACT_DENY))
6464 return 0;
6465 else if (exp->action == ACT_REMOVE)
6466 return 0;
6467
6468 done = 0;
6469
Willy Tarreau962c3f42010-01-10 00:15:35 +01006470 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006471 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006472
6473 /* Now we have the status line between cur_ptr and cur_end */
6474
6475 /* The annoying part is that pattern matching needs
6476 * that we modify the contents to null-terminate all
6477 * strings before testing them.
6478 */
6479
6480 term = *cur_end;
6481 *cur_end = '\0';
6482
6483 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6484 switch (exp->action) {
6485 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006486 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 done = 1;
6488 break;
6489
6490 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006491 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492 done = 1;
6493 break;
6494
6495 case ACT_REPLACE:
6496 *cur_end = term; /* restore the string terminator */
6497 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6498 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6499 /* FIXME: if the user adds a newline in the replacement, the
6500 * index will not be recalculated for now, and the new line
6501 * will not be counted as a new header.
6502 */
6503
Willy Tarreaufa355d42009-11-29 18:12:29 +01006504 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006506 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006507 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508 cur_ptr, cur_end + 1,
6509 NULL, NULL);
6510 if (unlikely(!cur_end))
6511 return -1;
6512
6513 /* we have a full respnse and we know that we have either a CR
6514 * or an LF at <ptr>.
6515 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006516 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006517 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006518 /* there is no point trying this regex on headers */
6519 return 1;
6520 }
6521 }
6522 *cur_end = term; /* restore the string terminator */
6523 return done;
6524}
6525
6526
6527
6528/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006529 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6531 * unparsable response.
6532 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006533int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006535 struct http_txn *txn = &s->txn;
6536 struct hdr_exp *exp;
6537
6538 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539 int ret;
6540
6541 /*
6542 * The interleaving of transformations and verdicts
6543 * makes it difficult to decide to continue or stop
6544 * the evaluation.
6545 */
6546
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006547 if (txn->flags & TX_SVDENY)
6548 break;
6549
Willy Tarreau3d300592007-03-18 18:34:41 +01006550 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6552 exp->action == ACT_PASS)) {
6553 exp = exp->next;
6554 continue;
6555 }
6556
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006557 /* if this filter had a condition, evaluate it now and skip to
6558 * next filter if the condition does not match.
6559 */
6560 if (exp->cond) {
6561 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6562 ret = acl_pass(ret);
6563 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6564 ret = !ret;
6565 if (!ret)
6566 continue;
6567 }
6568
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006570 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006571 if (unlikely(ret < 0))
6572 return -1;
6573
6574 if (likely(ret == 0)) {
6575 /* The filter did not match the response, it can be
6576 * iterated through all headers.
6577 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006578 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 }
6581 return 0;
6582}
6583
6584
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006586 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006587 * desirable to call it only when needed. This function is also used when we
6588 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006590void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591{
6592 struct http_txn *txn = &t->txn;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006593 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006595 char *hdr_beg, *hdr_end, *hdr_next;
6596 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 /* Iterate through the headers.
6599 * we start with the start line.
6600 */
6601 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006602 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603
6604 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6605 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006606 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607
6608 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006609 hdr_beg = hdr_next;
6610 hdr_end = hdr_beg + cur_hdr->len;
6611 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006612
Willy Tarreau24581ba2010-08-31 22:39:35 +02006613 /* We have one full header between hdr_beg and hdr_end, and the
6614 * next header starts at hdr_next. We're only interested in
6615 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 */
6617
Willy Tarreau24581ba2010-08-31 22:39:35 +02006618 is_cookie2 = 0;
6619 prev = hdr_beg + 10;
6620 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006621 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006622 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6623 if (!val) {
6624 old_idx = cur_idx;
6625 continue;
6626 }
6627 is_cookie2 = 1;
6628 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 }
6630
Willy Tarreau24581ba2010-08-31 22:39:35 +02006631 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6632 * <prev> points to the colon.
6633 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006634 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635
Willy Tarreau24581ba2010-08-31 22:39:35 +02006636 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6637 * check-cache is enabled) and we are not interested in checking
6638 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006639 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006640 if (t->be->cookie_name == NULL &&
6641 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006642 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643 return;
6644
Willy Tarreau24581ba2010-08-31 22:39:35 +02006645 /* OK so now we know we have to process this response cookie.
6646 * The format of the Set-Cookie header is slightly different
6647 * from the format of the Cookie header in that it does not
6648 * support the comma as a cookie delimiter (thus the header
6649 * cannot be folded) because the Expires attribute described in
6650 * the original Netscape's spec may contain an unquoted date
6651 * with a comma inside. We have to live with this because
6652 * many browsers don't support Max-Age and some browsers don't
6653 * support quoted strings. However the Set-Cookie2 header is
6654 * clean.
6655 *
6656 * We have to keep multiple pointers in order to support cookie
6657 * removal at the beginning, middle or end of header without
6658 * corrupting the header (in case of set-cookie2). A special
6659 * pointer, <scav> points to the beginning of the set-cookie-av
6660 * fields after the first semi-colon. The <next> pointer points
6661 * either to the end of line (set-cookie) or next unquoted comma
6662 * (set-cookie2). All of these headers are valid :
6663 *
6664 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6665 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6666 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6667 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6668 * | | | | | | | | | |
6669 * | | | | | | | | +-> next hdr_end <--+
6670 * | | | | | | | +------------> scav
6671 * | | | | | | +--------------> val_end
6672 * | | | | | +--------------------> val_beg
6673 * | | | | +----------------------> equal
6674 * | | | +------------------------> att_end
6675 * | | +----------------------------> att_beg
6676 * | +------------------------------> prev
6677 * +-----------------------------------------> hdr_beg
6678 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679
Willy Tarreau24581ba2010-08-31 22:39:35 +02006680 for (; prev < hdr_end; prev = next) {
6681 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006682
Willy Tarreau24581ba2010-08-31 22:39:35 +02006683 /* find att_beg */
6684 att_beg = prev + 1;
6685 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6686 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 /* find att_end : this is the first character after the last non
6689 * space before the equal. It may be equal to hdr_end.
6690 */
6691 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692
Willy Tarreau24581ba2010-08-31 22:39:35 +02006693 while (equal < hdr_end) {
6694 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6695 break;
6696 if (http_is_spht[(unsigned char)*equal++])
6697 continue;
6698 att_end = equal;
6699 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700
Willy Tarreau24581ba2010-08-31 22:39:35 +02006701 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6702 * is between <att_beg> and <equal>, both may be identical.
6703 */
6704
6705 /* look for end of cookie if there is an equal sign */
6706 if (equal < hdr_end && *equal == '=') {
6707 /* look for the beginning of the value */
6708 val_beg = equal + 1;
6709 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6710 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711
Willy Tarreau24581ba2010-08-31 22:39:35 +02006712 /* find the end of the value, respecting quotes */
6713 next = find_cookie_value_end(val_beg, hdr_end);
6714
6715 /* make val_end point to the first white space or delimitor after the value */
6716 val_end = next;
6717 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6718 val_end--;
6719 } else {
6720 /* <equal> points to next comma, semi-colon or EOL */
6721 val_beg = val_end = next = equal;
6722 }
6723
6724 if (next < hdr_end) {
6725 /* Set-Cookie2 supports multiple cookies, and <next> points to
6726 * a colon or semi-colon before the end. So skip all attr-value
6727 * pairs and look for the next comma. For Set-Cookie, since
6728 * commas are permitted in values, skip to the end.
6729 */
6730 if (is_cookie2)
6731 next = find_hdr_value_end(next, hdr_end);
6732 else
6733 next = hdr_end;
6734 }
6735
6736 /* Now everything is as on the diagram above */
6737
6738 /* Ignore cookies with no equal sign */
6739 if (equal == val_end)
6740 continue;
6741
6742 /* If there are spaces around the equal sign, we need to
6743 * strip them otherwise we'll get trouble for cookie captures,
6744 * or even for rewrites. Since this happens extremely rarely,
6745 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006747 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6748 int stripped_before = 0;
6749 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
Willy Tarreau24581ba2010-08-31 22:39:35 +02006751 if (att_end != equal) {
6752 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6753 equal += stripped_before;
6754 val_beg += stripped_before;
6755 }
6756
6757 if (val_beg > equal + 1) {
6758 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6759 val_beg += stripped_after;
6760 stripped_before += stripped_after;
6761 }
6762
6763 val_end += stripped_before;
6764 next += stripped_before;
6765 hdr_end += stripped_before;
6766 hdr_next += stripped_before;
6767 cur_hdr->len += stripped_before;
6768 http_msg_move_end(&txn->req, stripped_before);
6769 }
6770
6771 /* First, let's see if we want to capture this cookie. We check
6772 * that we don't already have a server side cookie, because we
6773 * can only capture one. Also as an optimisation, we ignore
6774 * cookies shorter than the declared name.
6775 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006776 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006777 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006778 (val_end - att_beg >= t->fe->capture_namelen) &&
6779 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6780 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006781 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 Alert("HTTP logging : out of memory.\n");
6783 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006784 else {
6785 if (log_len > t->fe->capture_len)
6786 log_len = t->fe->capture_len;
6787 memcpy(txn->srv_cookie, att_beg, log_len);
6788 txn->srv_cookie[log_len] = 0;
6789 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 }
6791
6792 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 if (!(t->flags & SN_IGNORE_PRST) &&
6794 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6795 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006796 /* assume passive cookie by default */
6797 txn->flags &= ~TX_SCK_MASK;
6798 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799
6800 /* If the cookie is in insert mode on a known server, we'll delete
6801 * this occurrence because we'll insert another one later.
6802 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006803 * a direct access.
6804 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006805 if (t->be->options2 & PR_O2_COOK_PSV) {
6806 /* The "preserve" flag was set, we don't want to touch the
6807 * server's cookie.
6808 */
6809 }
6810 else if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006811 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 /* this cookie must be deleted */
6813 if (*prev == ':' && next == hdr_end) {
6814 /* whole header */
6815 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6816 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6817 txn->hdr_idx.used--;
6818 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006819 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 hdr_next += delta;
6821 http_msg_move_end(&txn->rsp, delta);
6822 /* note: while both invalid now, <next> and <hdr_end>
6823 * are still equal, so the for() will stop as expected.
6824 */
6825 } else {
6826 /* just remove the value */
6827 int delta = del_hdr_value(res, &prev, next);
6828 next = prev;
6829 hdr_end += delta;
6830 hdr_next += delta;
6831 cur_hdr->len += delta;
6832 http_msg_move_end(&txn->rsp, delta);
6833 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006834 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006835 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006836 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 }
6838 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006839 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006840 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 * with this server since we know it.
6842 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 delta = buffer_replace2(res, val_beg, val_end, t->srv->cookie, t->srv->cklen);
6844 next += delta;
6845 hdr_end += delta;
6846 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006848 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849
Willy Tarreauf1348312010-10-07 15:54:11 +02006850 txn->flags &= ~TX_SCK_MASK;
6851 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 }
6853 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006854 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006858 delta = buffer_replace2(res, val_beg, val_beg, t->srv->cookie, t->srv->cklen + 1);
6859 next += delta;
6860 hdr_end += delta;
6861 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006863 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864
Willy Tarreau24581ba2010-08-31 22:39:35 +02006865 val_beg[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006866 txn->flags &= ~TX_SCK_MASK;
6867 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 }
6869 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006870 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6871 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006872 int cmp_len, value_len;
6873 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874
Cyril Bontéb21570a2009-11-29 20:04:48 +01006875 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6877 value_begin = att_beg + t->be->appsession_name_len;
6878 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006879 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006880 cmp_len = att_end - att_beg;
6881 value_begin = val_beg;
6882 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006883 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006884
Cyril Bonté17530c32010-04-06 21:11:10 +02006885 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6887 /* free a possibly previously allocated memory */
6888 pool_free2(apools.sessid, txn->sessid);
6889
Cyril Bontéb21570a2009-11-29 20:04:48 +01006890 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006891 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006892 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6893 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6894 return;
6895 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006896 memcpy(txn->sessid, value_begin, value_len);
6897 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006899 }
6900 /* that's done for this cookie, check the next one on the same
6901 * line when next != hdr_end (only if is_cookie2).
6902 */
6903 }
6904 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006907
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006908 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006909 appsess *asession = NULL;
6910 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006911 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006912 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006913 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006914 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6915 Alert("Not enough Memory process_srv():asession:calloc().\n");
6916 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6917 return;
6918 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006919 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6920
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006921 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6922 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6923 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006924 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006925 return;
6926 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006927 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006928 asession->sessid[t->be->appsession_len] = 0;
6929
Willy Tarreau1fac7532010-01-09 19:23:06 +01006930 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006931 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006932 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006934 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006935 return;
6936 }
6937 asession->serverid[0] = '\0';
6938 memcpy(asession->serverid, t->srv->id, server_id_len);
6939
6940 asession->request_count = 0;
6941 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6942 }
6943
6944 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6945 asession->request_count++;
6946 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947}
6948
6949
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950/*
6951 * Check if response is cacheable or not. Updates t->flags.
6952 */
6953void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6954{
6955 struct http_txn *txn = &t->txn;
6956 char *p1, *p2;
6957
6958 char *cur_ptr, *cur_end, *cur_next;
6959 int cur_idx;
6960
Willy Tarreau5df51872007-11-25 16:20:08 +01006961 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962 return;
6963
6964 /* Iterate through the headers.
6965 * we start with the start line.
6966 */
6967 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006968 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969
6970 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6971 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006972 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973
6974 cur_hdr = &txn->hdr_idx.v[cur_idx];
6975 cur_ptr = cur_next;
6976 cur_end = cur_ptr + cur_hdr->len;
6977 cur_next = cur_end + cur_hdr->cr + 1;
6978
6979 /* We have one full header between cur_ptr and cur_end, and the
6980 * next header starts at cur_next. We're only interested in
6981 * "Cookie:" headers.
6982 */
6983
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006984 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6985 if (val) {
6986 if ((cur_end - (cur_ptr + val) >= 8) &&
6987 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6988 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6989 return;
6990 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 }
6992
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006993 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6994 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 continue;
6996
6997 /* OK, right now we know we have a cache-control header at cur_ptr */
6998
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006999 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000
7001 if (p1 >= cur_end) /* no more info */
7002 continue;
7003
7004 /* p1 is at the beginning of the value */
7005 p2 = p1;
7006
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007007 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008 p2++;
7009
7010 /* we have a complete value between p1 and p2 */
7011 if (p2 < cur_end && *p2 == '=') {
7012 /* we have something of the form no-cache="set-cookie" */
7013 if ((cur_end - p1 >= 21) &&
7014 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7015 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007016 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017 continue;
7018 }
7019
7020 /* OK, so we know that either p2 points to the end of string or to a comma */
7021 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7022 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7023 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7024 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007025 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026 return;
7027 }
7028
7029 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 continue;
7032 }
7033 }
7034}
7035
7036
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037/*
7038 * Try to retrieve a known appsession in the URI, then the associated server.
7039 * If the server is found, it's assigned to the session.
7040 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007042{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007043 char *end_params, *first_param, *cur_param, *next_param;
7044 char separator;
7045 int value_len;
7046
7047 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007049 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007050 (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 +01007051 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007052 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053
Cyril Bontéb21570a2009-11-29 20:04:48 +01007054 first_param = NULL;
7055 switch (mode) {
7056 case PR_O2_AS_M_PP:
7057 first_param = memchr(begin, ';', len);
7058 break;
7059 case PR_O2_AS_M_QS:
7060 first_param = memchr(begin, '?', len);
7061 break;
7062 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063
Cyril Bontéb21570a2009-11-29 20:04:48 +01007064 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007066 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Cyril Bontéb21570a2009-11-29 20:04:48 +01007068 switch (mode) {
7069 case PR_O2_AS_M_PP:
7070 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7071 end_params = (char *) begin + len;
7072 }
7073 separator = ';';
7074 break;
7075 case PR_O2_AS_M_QS:
7076 end_params = (char *) begin + len;
7077 separator = '&';
7078 break;
7079 default:
7080 /* unknown mode, shouldn't happen */
7081 return;
7082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083
Cyril Bontéb21570a2009-11-29 20:04:48 +01007084 cur_param = next_param = end_params;
7085 while (cur_param > first_param) {
7086 cur_param--;
7087 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7088 /* let's see if this is the appsession parameter */
7089 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7090 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7091 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7092 /* Cool... it's the right one */
7093 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7094 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7095 if (value_len > 0) {
7096 manage_client_side_appsession(t, cur_param, value_len);
7097 }
7098 break;
7099 }
7100 next_param = cur_param;
7101 }
7102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007104 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007105 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107}
7108
Willy Tarreaub2513902006-12-17 14:52:38 +01007109/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007110 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007111 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007112 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007113 * It is assumed that the request is either a HEAD, GET, or POST and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007114 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007115 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007116 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007117 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007118int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007119{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007120 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01007121 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007123
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007124 if (!uri_auth)
7125 return 0;
7126
Cyril Bonté70be45d2010-10-12 00:14:35 +02007127 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007128 return 0;
7129
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007130 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
7131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007133 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007134 return 0;
7135
Willy Tarreau962c3f42010-01-10 00:15:35 +01007136 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007138 /* the URI is in h */
7139 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007140 return 0;
7141
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007142 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007143 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007144 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007145 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007146 break;
7147 }
7148 h++;
7149 }
7150
7151 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007152 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7153 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007154 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007155 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007156 break;
7157 }
7158 h++;
7159 }
7160 }
7161
Willy Tarreau962c3f42010-01-10 00:15:35 +01007162 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7163 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007164 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007165 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007166 break;
7167 }
7168 h++;
7169 }
7170
Cyril Bonté70be45d2010-10-12 00:14:35 +02007171 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7172 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7173 if (memcmp(h, ";st=", 4) == 0) {
7174 h += 4;
7175
7176 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
7177 t->data_ctx.stats.st_code = STAT_STATUS_DONE;
7178 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
7179 t->data_ctx.stats.st_code = STAT_STATUS_NONE;
7180 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
7181 t->data_ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007182 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
7183 t->data_ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007184 else
7185 t->data_ctx.stats.st_code = STAT_STATUS_UNKN;
7186 break;
7187 }
7188 h++;
7189 }
7190
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007191 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
7192
Willy Tarreaub2513902006-12-17 14:52:38 +01007193 return 1;
7194}
7195
Willy Tarreau4076a152009-04-02 15:18:36 +02007196/*
7197 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007198 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7199 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007200 */
7201void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7202 struct buffer *buf, struct http_msg *msg,
7203 struct proxy *other_end)
7204{
Willy Tarreau2df8d712009-05-01 11:33:17 +02007205 es->len = buf->r - (buf->data + msg->som);
7206 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02007207 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007208 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02007209 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02007210 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02007211 es->when = date; // user-visible date
7212 es->sid = s->uniq_id;
7213 es->srv = s->srv;
7214 es->oe = other_end;
7215 es->src = s->cli_addr;
7216}
Willy Tarreaub2513902006-12-17 14:52:38 +01007217
Willy Tarreaubce70882009-09-07 11:51:47 +02007218/* return the IP address pointed to by occurrence <occ> of header <hname> in
7219 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7220 * occurrence number corresponding to this value is returned. If <occ> is
7221 * strictly negative, the occurrence number before the end corresponding to
7222 * this value is returned. If <occ> is null, any value is returned, so it is
7223 * not recommended to use it that way. Negative occurrences are limited to
7224 * a small value because it is required to keep them in memory while scanning.
7225 * IP address 0.0.0.0 is returned if no match is found.
7226 */
7227unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7228{
7229 struct hdr_ctx ctx;
7230 unsigned int hdr_hist[MAX_HDR_HISTORY];
7231 unsigned int hist_ptr;
7232 int found = 0;
7233
7234 ctx.idx = 0;
7235 if (occ >= 0) {
7236 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7237 occ--;
7238 if (occ <= 0) {
7239 found = 1;
7240 break;
7241 }
7242 }
7243 if (!found)
7244 return 0;
7245 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7246 }
7247
7248 /* negative occurrence, we scan all the list then walk back */
7249 if (-occ > MAX_HDR_HISTORY)
7250 return 0;
7251
7252 hist_ptr = 0;
7253 hdr_hist[hist_ptr] = 0;
7254 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7255 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7256 if (hist_ptr >= MAX_HDR_HISTORY)
7257 hist_ptr = 0;
7258 found++;
7259 }
7260 if (-occ > found)
7261 return 0;
7262 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7263 * find occurrence -occ, so we have to check [hist_ptr+occ].
7264 */
7265 hist_ptr += occ;
7266 if (hist_ptr >= MAX_HDR_HISTORY)
7267 hist_ptr -= MAX_HDR_HISTORY;
7268 return hdr_hist[hist_ptr];
7269}
7270
Willy Tarreaubaaee002006-06-26 02:48:02 +02007271/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007272 * Print a debug line with a header
7273 */
7274void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7275{
7276 int len, max;
7277 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007278 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279 max = end - start;
7280 UBOUND(max, sizeof(trash) - len - 1);
7281 len += strlcpy2(trash + len, start, max + 1);
7282 trash[len++] = '\n';
7283 write(1, trash, len);
7284}
7285
Willy Tarreau0937bc42009-12-22 15:03:09 +01007286/*
7287 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7288 * the required fields are properly allocated and that we only need to (re)init
7289 * them. This should be used before processing any new request.
7290 */
7291void http_init_txn(struct session *s)
7292{
7293 struct http_txn *txn = &s->txn;
7294 struct proxy *fe = s->fe;
7295
7296 txn->flags = 0;
7297 txn->status = -1;
7298
Willy Tarreauf64d1412010-10-07 20:06:11 +02007299 txn->cookie_first_date = 0;
7300 txn->cookie_last_date = 0;
7301
Willy Tarreau0937bc42009-12-22 15:03:09 +01007302 txn->req.sol = txn->req.eol = NULL;
7303 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7304 txn->rsp.sol = txn->rsp.eol = NULL;
7305 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
7306 txn->req.hdr_content_len = 0LL;
7307 txn->rsp.hdr_content_len = 0LL;
7308 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7309 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007310
7311 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007312
7313 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7314 if (fe->options2 & PR_O2_REQBUG_OK)
7315 txn->req.err_pos = -1; /* let buggy requests pass */
7316
Willy Tarreau46023632010-01-07 22:51:47 +01007317 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007318 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7319
Willy Tarreau46023632010-01-07 22:51:47 +01007320 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007321 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7322
7323 if (txn->hdr_idx.v)
7324 hdr_idx_init(&txn->hdr_idx);
7325}
7326
7327/* to be used at the end of a transaction */
7328void http_end_txn(struct session *s)
7329{
7330 struct http_txn *txn = &s->txn;
7331
7332 /* these ones will have been dynamically allocated */
7333 pool_free2(pool2_requri, txn->uri);
7334 pool_free2(pool2_capture, txn->cli_cookie);
7335 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007336 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007337
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007338 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007339 txn->uri = NULL;
7340 txn->srv_cookie = NULL;
7341 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007342
7343 if (txn->req.cap) {
7344 struct cap_hdr *h;
7345 for (h = s->fe->req_cap; h; h = h->next)
7346 pool_free2(h->pool, txn->req.cap[h->index]);
7347 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7348 }
7349
7350 if (txn->rsp.cap) {
7351 struct cap_hdr *h;
7352 for (h = s->fe->rsp_cap; h; h = h->next)
7353 pool_free2(h->pool, txn->rsp.cap[h->index]);
7354 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7355 }
7356
Willy Tarreau0937bc42009-12-22 15:03:09 +01007357}
7358
7359/* to be used at the end of a transaction to prepare a new one */
7360void http_reset_txn(struct session *s)
7361{
7362 http_end_txn(s);
7363 http_init_txn(s);
7364
7365 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007366 s->logs.logwait = s->fe->to_log;
7367 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007368 /* re-init store persistence */
7369 s->store_count = 0;
7370
Willy Tarreau0937bc42009-12-22 15:03:09 +01007371 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007372
7373 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7374
Willy Tarreau739cfba2010-01-25 23:11:14 +01007375 /* We must trim any excess data from the response buffer, because we
7376 * may have blocked an invalid response from a server that we don't
7377 * want to accidentely forward once we disable the analysers, nor do
7378 * we want those data to come along with next response. A typical
7379 * example of such data would be from a buggy server responding to
7380 * a HEAD with some data, or sending more than the advertised
7381 * content-length.
7382 */
7383 if (unlikely(s->rep->l > s->rep->send_max)) {
7384 s->rep->l = s->rep->send_max;
7385 s->rep->r = s->rep->w + s->rep->l;
7386 if (s->rep->r >= s->rep->data + s->rep->size)
7387 s->rep->r -= s->rep->size;
7388 }
7389
Willy Tarreau0937bc42009-12-22 15:03:09 +01007390 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007391 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007392
Willy Tarreaud04e8582010-05-31 12:31:35 +02007393 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007394 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007395
7396 s->req->rex = TICK_ETERNITY;
7397 s->req->wex = TICK_ETERNITY;
7398 s->req->analyse_exp = TICK_ETERNITY;
7399 s->rep->rex = TICK_ETERNITY;
7400 s->rep->wex = TICK_ETERNITY;
7401 s->rep->analyse_exp = TICK_ETERNITY;
7402}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007403
Willy Tarreau8797c062007-05-07 00:55:35 +02007404/************************************************************************/
7405/* The code below is dedicated to ACL parsing and matching */
7406/************************************************************************/
7407
7408
7409
7410
7411/* 1. Check on METHOD
7412 * We use the pre-parsed method if it is known, and store its number as an
7413 * integer. If it is unknown, we use the pointer and the length.
7414 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007415static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007416{
7417 int len, meth;
7418
Willy Tarreauae8b7962007-06-09 23:10:04 +02007419 len = strlen(*text);
7420 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007421
7422 pattern->val.i = meth;
7423 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007424 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007425 if (!pattern->ptr.str)
7426 return 0;
7427 pattern->len = len;
7428 }
7429 return 1;
7430}
7431
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007432static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007433acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7434 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007435{
7436 int meth;
7437 struct http_txn *txn = l7;
7438
Willy Tarreaub6866442008-07-14 23:54:42 +02007439 if (!txn)
7440 return 0;
7441
Willy Tarreau655dce92009-11-08 13:10:58 +01007442 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007443 return 0;
7444
Willy Tarreau8797c062007-05-07 00:55:35 +02007445 meth = txn->meth;
7446 test->i = meth;
7447 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007448 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7449 /* ensure the indexes are not affected */
7450 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007451 test->len = txn->req.sl.rq.m_l;
7452 test->ptr = txn->req.sol;
7453 }
7454 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7455 return 1;
7456}
7457
7458static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7459{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007460 int icase;
7461
Willy Tarreau8797c062007-05-07 00:55:35 +02007462 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007463 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007464
7465 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007466 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007467
7468 /* Other method, we must compare the strings */
7469 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007470 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007471
7472 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7473 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7474 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007475 return ACL_PAT_FAIL;
7476 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007477}
7478
7479/* 2. Check on Request/Status Version
7480 * We simply compare strings here.
7481 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007482static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007483{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007484 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007485 if (!pattern->ptr.str)
7486 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007487 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007488 return 1;
7489}
7490
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007491static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007492acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7493 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007494{
7495 struct http_txn *txn = l7;
7496 char *ptr;
7497 int len;
7498
Willy Tarreaub6866442008-07-14 23:54:42 +02007499 if (!txn)
7500 return 0;
7501
Willy Tarreau655dce92009-11-08 13:10:58 +01007502 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007503 return 0;
7504
Willy Tarreau8797c062007-05-07 00:55:35 +02007505 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007506 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007507
7508 while ((len-- > 0) && (*ptr++ != '/'));
7509 if (len <= 0)
7510 return 0;
7511
7512 test->ptr = ptr;
7513 test->len = len;
7514
7515 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7516 return 1;
7517}
7518
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007519static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007520acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7521 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007522{
7523 struct http_txn *txn = l7;
7524 char *ptr;
7525 int len;
7526
Willy Tarreaub6866442008-07-14 23:54:42 +02007527 if (!txn)
7528 return 0;
7529
Willy Tarreau655dce92009-11-08 13:10:58 +01007530 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007531 return 0;
7532
Willy Tarreau8797c062007-05-07 00:55:35 +02007533 len = txn->rsp.sl.st.v_l;
7534 ptr = txn->rsp.sol;
7535
7536 while ((len-- > 0) && (*ptr++ != '/'));
7537 if (len <= 0)
7538 return 0;
7539
7540 test->ptr = ptr;
7541 test->len = len;
7542
7543 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7544 return 1;
7545}
7546
7547/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007548static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007549acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7550 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007551{
7552 struct http_txn *txn = l7;
7553 char *ptr;
7554 int len;
7555
Willy Tarreaub6866442008-07-14 23:54:42 +02007556 if (!txn)
7557 return 0;
7558
Willy Tarreau655dce92009-11-08 13:10:58 +01007559 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007560 return 0;
7561
Willy Tarreau8797c062007-05-07 00:55:35 +02007562 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007563 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007564
7565 test->i = __strl2ui(ptr, len);
7566 test->flags = ACL_TEST_F_VOL_1ST;
7567 return 1;
7568}
7569
7570/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007571static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007572acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7573 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007574{
7575 struct http_txn *txn = l7;
7576
Willy Tarreaub6866442008-07-14 23:54:42 +02007577 if (!txn)
7578 return 0;
7579
Willy Tarreau655dce92009-11-08 13:10:58 +01007580 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007581 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007582
Willy Tarreauc11416f2007-06-17 16:58:38 +02007583 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7584 /* ensure the indexes are not affected */
7585 return 0;
7586
Willy Tarreau8797c062007-05-07 00:55:35 +02007587 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007588 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007589
Willy Tarreauf3d25982007-05-08 22:45:09 +02007590 /* we do not need to set READ_ONLY because the data is in a buffer */
7591 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007592 return 1;
7593}
7594
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007595static int
7596acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7597 struct acl_expr *expr, struct acl_test *test)
7598{
7599 struct http_txn *txn = l7;
7600
Willy Tarreaub6866442008-07-14 23:54:42 +02007601 if (!txn)
7602 return 0;
7603
Willy Tarreau655dce92009-11-08 13:10:58 +01007604 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007605 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007606
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007607 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7608 /* ensure the indexes are not affected */
7609 return 0;
7610
7611 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007612 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007613 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7614 test->i = AF_INET;
7615
7616 /*
7617 * If we are parsing url in frontend space, we prepare backend stage
7618 * to not parse again the same url ! optimization lazyness...
7619 */
7620 if (px->options & PR_O_HTTP_PROXY)
7621 l4->flags |= SN_ADDR_SET;
7622
7623 test->flags = ACL_TEST_F_READ_ONLY;
7624 return 1;
7625}
7626
7627static int
7628acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7629 struct acl_expr *expr, struct acl_test *test)
7630{
7631 struct http_txn *txn = l7;
7632
Willy Tarreaub6866442008-07-14 23:54:42 +02007633 if (!txn)
7634 return 0;
7635
Willy Tarreau655dce92009-11-08 13:10:58 +01007636 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007637 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007638
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007639 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7640 /* ensure the indexes are not affected */
7641 return 0;
7642
7643 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007644 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007645 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7646
7647 if (px->options & PR_O_HTTP_PROXY)
7648 l4->flags |= SN_ADDR_SET;
7649
7650 test->flags = ACL_TEST_F_READ_ONLY;
7651 return 1;
7652}
7653
Willy Tarreauc11416f2007-06-17 16:58:38 +02007654/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7655 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7656 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007657static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007658acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007659 struct acl_expr *expr, struct acl_test *test)
7660{
7661 struct http_txn *txn = l7;
7662 struct hdr_idx *idx = &txn->hdr_idx;
7663 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007664
Willy Tarreaub6866442008-07-14 23:54:42 +02007665 if (!txn)
7666 return 0;
7667
Willy Tarreau33a7e692007-06-10 19:45:56 +02007668 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7669 /* search for header from the beginning */
7670 ctx->idx = 0;
7671
Willy Tarreau33a7e692007-06-10 19:45:56 +02007672 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7673 test->flags |= ACL_TEST_F_FETCH_MORE;
7674 test->flags |= ACL_TEST_F_VOL_HDR;
7675 test->len = ctx->vlen;
7676 test->ptr = (char *)ctx->line + ctx->val;
7677 return 1;
7678 }
7679
7680 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7681 test->flags |= ACL_TEST_F_VOL_HDR;
7682 return 0;
7683}
7684
Willy Tarreau33a7e692007-06-10 19:45:56 +02007685static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007686acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7687 struct acl_expr *expr, struct acl_test *test)
7688{
7689 struct http_txn *txn = l7;
7690
Willy Tarreaub6866442008-07-14 23:54:42 +02007691 if (!txn)
7692 return 0;
7693
Willy Tarreau655dce92009-11-08 13:10:58 +01007694 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007695 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007696
Willy Tarreauc11416f2007-06-17 16:58:38 +02007697 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7698 /* ensure the indexes are not affected */
7699 return 0;
7700
7701 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7702}
7703
7704static int
7705acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7706 struct acl_expr *expr, struct acl_test *test)
7707{
7708 struct http_txn *txn = l7;
7709
Willy Tarreaub6866442008-07-14 23:54:42 +02007710 if (!txn)
7711 return 0;
7712
Willy Tarreau655dce92009-11-08 13:10:58 +01007713 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007714 return 0;
7715
7716 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7717}
7718
7719/* 6. Check on HTTP header count. The number of occurrences is returned.
7720 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7721 */
7722static int
7723acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007724 struct acl_expr *expr, struct acl_test *test)
7725{
7726 struct http_txn *txn = l7;
7727 struct hdr_idx *idx = &txn->hdr_idx;
7728 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007729 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007730
Willy Tarreaub6866442008-07-14 23:54:42 +02007731 if (!txn)
7732 return 0;
7733
Willy Tarreau33a7e692007-06-10 19:45:56 +02007734 ctx.idx = 0;
7735 cnt = 0;
7736 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7737 cnt++;
7738
7739 test->i = cnt;
7740 test->flags = ACL_TEST_F_VOL_HDR;
7741 return 1;
7742}
7743
Willy Tarreauc11416f2007-06-17 16:58:38 +02007744static int
7745acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7746 struct acl_expr *expr, struct acl_test *test)
7747{
7748 struct http_txn *txn = l7;
7749
Willy Tarreaub6866442008-07-14 23:54:42 +02007750 if (!txn)
7751 return 0;
7752
Willy Tarreau655dce92009-11-08 13:10:58 +01007753 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007754 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007755
Willy Tarreauc11416f2007-06-17 16:58:38 +02007756 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7757 /* ensure the indexes are not affected */
7758 return 0;
7759
7760 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7761}
7762
7763static int
7764acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7765 struct acl_expr *expr, struct acl_test *test)
7766{
7767 struct http_txn *txn = l7;
7768
Willy Tarreaub6866442008-07-14 23:54:42 +02007769 if (!txn)
7770 return 0;
7771
Willy Tarreau655dce92009-11-08 13:10:58 +01007772 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007773 return 0;
7774
7775 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7776}
7777
Willy Tarreau33a7e692007-06-10 19:45:56 +02007778/* 7. Check on HTTP header's integer value. The integer value is returned.
7779 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007780 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007781 */
7782static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007783acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007784 struct acl_expr *expr, struct acl_test *test)
7785{
7786 struct http_txn *txn = l7;
7787 struct hdr_idx *idx = &txn->hdr_idx;
7788 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007789
Willy Tarreaub6866442008-07-14 23:54:42 +02007790 if (!txn)
7791 return 0;
7792
Willy Tarreau33a7e692007-06-10 19:45:56 +02007793 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7794 /* search for header from the beginning */
7795 ctx->idx = 0;
7796
Willy Tarreau33a7e692007-06-10 19:45:56 +02007797 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7798 test->flags |= ACL_TEST_F_FETCH_MORE;
7799 test->flags |= ACL_TEST_F_VOL_HDR;
7800 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7801 return 1;
7802 }
7803
7804 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7805 test->flags |= ACL_TEST_F_VOL_HDR;
7806 return 0;
7807}
7808
Willy Tarreauc11416f2007-06-17 16:58:38 +02007809static int
7810acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7811 struct acl_expr *expr, struct acl_test *test)
7812{
7813 struct http_txn *txn = l7;
7814
Willy Tarreaub6866442008-07-14 23:54:42 +02007815 if (!txn)
7816 return 0;
7817
Willy Tarreau655dce92009-11-08 13:10:58 +01007818 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007819 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007820
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7822 /* ensure the indexes are not affected */
7823 return 0;
7824
7825 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7826}
7827
7828static int
7829acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7830 struct acl_expr *expr, struct acl_test *test)
7831{
7832 struct http_txn *txn = l7;
7833
Willy Tarreaub6866442008-07-14 23:54:42 +02007834 if (!txn)
7835 return 0;
7836
Willy Tarreau655dce92009-11-08 13:10:58 +01007837 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007838 return 0;
7839
7840 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7841}
7842
Willy Tarreau106f9792009-09-19 07:54:16 +02007843/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7844 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7845 */
7846static int
7847acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7848 struct acl_expr *expr, struct acl_test *test)
7849{
7850 struct http_txn *txn = l7;
7851 struct hdr_idx *idx = &txn->hdr_idx;
7852 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7853
7854 if (!txn)
7855 return 0;
7856
7857 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7858 /* search for header from the beginning */
7859 ctx->idx = 0;
7860
7861 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7862 test->flags |= ACL_TEST_F_FETCH_MORE;
7863 test->flags |= ACL_TEST_F_VOL_HDR;
7864 /* Same optimization as url_ip */
7865 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7866 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7867 test->ptr = (void *)&l4->srv_addr.sin_addr;
7868 test->i = AF_INET;
7869 return 1;
7870 }
7871
7872 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7873 test->flags |= ACL_TEST_F_VOL_HDR;
7874 return 0;
7875}
7876
7877static int
7878acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7879 struct acl_expr *expr, struct acl_test *test)
7880{
7881 struct http_txn *txn = l7;
7882
7883 if (!txn)
7884 return 0;
7885
Willy Tarreau655dce92009-11-08 13:10:58 +01007886 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007887 return 0;
7888
7889 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7890 /* ensure the indexes are not affected */
7891 return 0;
7892
7893 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7894}
7895
7896static int
7897acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7898 struct acl_expr *expr, struct acl_test *test)
7899{
7900 struct http_txn *txn = l7;
7901
7902 if (!txn)
7903 return 0;
7904
Willy Tarreau655dce92009-11-08 13:10:58 +01007905 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007906 return 0;
7907
7908 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7909}
7910
Willy Tarreau737b0c12007-06-10 21:28:46 +02007911/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7912 * the first '/' after the possible hostname, and ends before the possible '?'.
7913 */
7914static int
7915acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7916 struct acl_expr *expr, struct acl_test *test)
7917{
7918 struct http_txn *txn = l7;
7919 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007920
Willy Tarreaub6866442008-07-14 23:54:42 +02007921 if (!txn)
7922 return 0;
7923
Willy Tarreau655dce92009-11-08 13:10:58 +01007924 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007925 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007926
Willy Tarreauc11416f2007-06-17 16:58:38 +02007927 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7928 /* ensure the indexes are not affected */
7929 return 0;
7930
Willy Tarreau962c3f42010-01-10 00:15:35 +01007931 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007932 ptr = http_get_path(txn);
7933 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007934 return 0;
7935
7936 /* OK, we got the '/' ! */
7937 test->ptr = ptr;
7938
7939 while (ptr < end && *ptr != '?')
7940 ptr++;
7941
7942 test->len = ptr - test->ptr;
7943
7944 /* we do not need to set READ_ONLY because the data is in a buffer */
7945 test->flags = ACL_TEST_F_VOL_1ST;
7946 return 1;
7947}
7948
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007949static int
7950acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7951 struct acl_expr *expr, struct acl_test *test)
7952{
7953 struct buffer *req = s->req;
7954 struct http_txn *txn = &s->txn;
7955 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007956
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007957 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7958 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7959 */
7960
7961 if (!s || !req)
7962 return 0;
7963
Willy Tarreau655dce92009-11-08 13:10:58 +01007964 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007965 /* Already decoded as OK */
7966 test->flags |= ACL_TEST_F_SET_RES_PASS;
7967 return 1;
7968 }
7969
7970 /* Try to decode HTTP request */
7971 if (likely(req->lr < req->r))
7972 http_msg_analyzer(req, msg, &txn->hdr_idx);
7973
Willy Tarreau655dce92009-11-08 13:10:58 +01007974 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007975 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7976 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7977 return 1;
7978 }
7979 /* wait for final state */
7980 test->flags |= ACL_TEST_F_MAY_CHANGE;
7981 return 0;
7982 }
7983
7984 /* OK we got a valid HTTP request. We have some minor preparation to
7985 * perform so that further checks can rely on HTTP tests.
7986 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007987 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007988 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7989 s->flags |= SN_REDIRECTABLE;
7990
7991 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7992 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7993 return 1;
7994 }
7995
7996 test->flags |= ACL_TEST_F_SET_RES_PASS;
7997 return 1;
7998}
7999
Willy Tarreau7f18e522010-10-22 20:04:13 +02008000/* return a valid test if the current request is the first one on the connection */
8001static int
8002acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8003 struct acl_expr *expr, struct acl_test *test)
8004{
8005 if (!s)
8006 return 0;
8007
8008 if (s->txn.flags & TX_NOT_FIRST)
8009 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8010 else
8011 test->flags |= ACL_TEST_F_SET_RES_PASS;
8012
8013 return 1;
8014}
8015
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008016static int
8017acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8018 struct acl_expr *expr, struct acl_test *test)
8019{
8020
8021 if (!s)
8022 return 0;
8023
8024 if (!get_http_auth(s))
8025 return 0;
8026
8027 test->ctx.a[0] = expr->arg.ul;
8028 test->ctx.a[1] = s->txn.auth.user;
8029 test->ctx.a[2] = s->txn.auth.pass;
8030
8031 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8032
8033 return 1;
8034}
Willy Tarreau8797c062007-05-07 00:55:35 +02008035
8036/************************************************************************/
8037/* All supported keywords must be declared here. */
8038/************************************************************************/
8039
8040/* Note: must not be declared <const> as its list will be overwritten */
8041static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008042 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8043
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008044 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008045 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8046 { "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 +02008047 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008048
Willy Tarreauc4262962010-05-10 23:42:40 +02008049 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008050 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8051 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8052 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8053 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8054 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8055 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008056 { "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 +02008057 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008058
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008059 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008060 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008061 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8062 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8063 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8064 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8065 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8066 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8067 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8068 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008069 { "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 +02008070
Willy Tarreauc4262962010-05-10 23:42:40 +02008071 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008072 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8073 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8074 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8075 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8076 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8077 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8078 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8079 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008080 { "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 +02008081
Willy Tarreauc4262962010-05-10 23:42:40 +02008082 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008083 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8084 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8085 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8086 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8087 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8088 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008089
Willy Tarreauf3d25982007-05-08 22:45:09 +02008090#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008091 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8092 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8093 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8094 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8095 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8096 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8097 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8098
Willy Tarreau8797c062007-05-07 00:55:35 +02008099 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8100 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8101 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8102 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8103 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8104 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8105 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8106 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008107#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008108
Willy Tarreau7f18e522010-10-22 20:04:13 +02008109 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8110 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8111 { "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 +02008112 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008113}};
8114
Willy Tarreau4a568972010-05-12 08:08:50 +02008115/************************************************************************/
8116/* The code below is dedicated to pattern fetching and matching */
8117/************************************************************************/
8118
8119/* extract the IP address from the last occurrence of specified header. Note
8120 * that we should normally first extract the string then convert it to IP,
8121 * but right now we have all the functions to do this seemlessly, and we will
8122 * be able to change that later without touching the configuration.
8123 */
8124static int
8125pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008126 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008127{
8128 struct http_txn *txn = l7;
8129
Emeric Brun485479d2010-09-23 18:02:19 +02008130 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1));
Willy Tarreau4a568972010-05-12 08:08:50 +02008131 return data->ip.s_addr != 0;
8132}
8133
Emeric Brun485479d2010-09-23 18:02:19 +02008134
8135
Willy Tarreau4a568972010-05-12 08:08:50 +02008136/************************************************************************/
8137/* All supported keywords must be declared here. */
8138/************************************************************************/
8139/* Note: must not be declared <const> as its list will be overwritten */
8140static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008141 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
8142 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008143}};
8144
Willy Tarreau8797c062007-05-07 00:55:35 +02008145
8146__attribute__((constructor))
8147static void __http_protocol_init(void)
8148{
8149 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008150 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008151}
8152
8153
Willy Tarreau58f10d72006-12-04 02:26:12 +01008154/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008155 * Local variables:
8156 * c-indent-level: 8
8157 * c-basic-offset: 8
8158 * End:
8159 */