blob: a2f14c9293865b5cc5a575420178d312d2f206a0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
96const char *HTTP_401_fmt =
97 "HTTP/1.0 401 Unauthorized\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200100 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
102 "\r\n"
103 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
104
Willy Tarreau844a7e72010-01-31 21:46:18 +0100105const char *HTTP_407_fmt =
106 "HTTP/1.0 407 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
109 "Content-Type: text/html\r\n"
110 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200116 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100117 [HTTP_ERR_400] = 400,
118 [HTTP_ERR_403] = 403,
119 [HTTP_ERR_408] = 408,
120 [HTTP_ERR_500] = 500,
121 [HTTP_ERR_502] = 502,
122 [HTTP_ERR_503] = 503,
123 [HTTP_ERR_504] = 504,
124};
125
Willy Tarreau80587432006-12-24 17:47:20 +0100126static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200127 [HTTP_ERR_200] =
128 "HTTP/1.0 200 OK\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "\r\n"
133 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Cyril Bonté19979e12012-04-04 12:57:21 +0200193/* status codes available for the stats admin page (strictly 4 chars length) */
194const char *stat_status_codes[STAT_STATUS_SIZE] = {
195 [STAT_STATUS_DENY] = "DENY",
196 [STAT_STATUS_DONE] = "DONE",
197 [STAT_STATUS_ERRP] = "ERRP",
198 [STAT_STATUS_EXCD] = "EXCD",
199 [STAT_STATUS_NONE] = "NONE",
200 [STAT_STATUS_PART] = "PART",
201 [STAT_STATUS_UNKN] = "UNKN",
202};
203
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205/* We must put the messages here since GCC cannot initialize consts depending
206 * on strlen().
207 */
208struct chunk http_err_chunks[HTTP_ERR_SIZE];
209
Willy Tarreau42250582007-04-01 01:30:43 +0200210#define FD_SETS_ARE_BITFIELDS
211#ifdef FD_SETS_ARE_BITFIELDS
212/*
213 * This map is used with all the FD_* macros to check whether a particular bit
214 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
215 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
216 * byte should be encoded. Be careful to always pass bytes from 0 to 255
217 * exclusively to the macros.
218 */
219fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
221
222#else
223#error "Check if your OS uses bitfields for fd_sets"
224#endif
225
Willy Tarreau80587432006-12-24 17:47:20 +0100226void init_proto_http()
227{
Willy Tarreau42250582007-04-01 01:30:43 +0200228 int i;
229 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100230 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200231
Willy Tarreau80587432006-12-24 17:47:20 +0100232 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
233 if (!http_err_msgs[msg]) {
234 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
235 abort();
236 }
237
238 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
239 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
240 }
Willy Tarreau42250582007-04-01 01:30:43 +0200241
242 /* initialize the log header encoding map : '{|}"#' should be encoded with
243 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
244 * URL encoding only requires '"', '#' to be encoded as well as non-
245 * printable characters above.
246 */
247 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
248 memset(url_encode_map, 0, sizeof(url_encode_map));
249 for (i = 0; i < 32; i++) {
250 FD_SET(i, hdr_encode_map);
251 FD_SET(i, url_encode_map);
252 }
253 for (i = 127; i < 256; i++) {
254 FD_SET(i, hdr_encode_map);
255 FD_SET(i, url_encode_map);
256 }
257
258 tmp = "\"#{|}";
259 while (*tmp) {
260 FD_SET(*tmp, hdr_encode_map);
261 tmp++;
262 }
263
264 tmp = "\"#";
265 while (*tmp) {
266 FD_SET(*tmp, url_encode_map);
267 tmp++;
268 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200269
270 /* memory allocations */
271 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100392 chunk_printf(&trash,
393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
397 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100399 chunk_printf(&trash,
400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
404 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405}
406#else
407#define http_silent_debug(l,s) do { } while (0)
408#endif
409
410/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100411 * Adds a header and its CRLF at the tail of the message's buffer, just before
412 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100417int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418{
419 int bytes, len;
420
421 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200422 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
428
429/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100430 * Adds a header and its CRLF at the tail of the message's buffer, just before
431 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100432 * the buffer is only opened and the space reserved, but nothing is copied.
433 * The header is also automatically added to the index <hdr_idx>, and the end
434 * of headers is automatically adjusted. The number of bytes added is returned
435 * on success, otherwise <0 is returned indicating an error.
436 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100437int http_header_add_tail2(struct http_msg *msg,
438 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439{
440 int bytes;
441
Willy Tarreau9b28e032012-10-12 23:49:43 +0200442 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 if (!bytes)
444 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100445 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
447}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448
449/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100450 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
451 * If so, returns the position of the first non-space character relative to
452 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
453 * to return a pointer to the place after the first space. Returns 0 if the
454 * header name does not match. Checks are case-insensitive.
455 */
456int http_header_match2(const char *hdr, const char *end,
457 const char *name, int len)
458{
459 const char *val;
460
461 if (hdr + len >= end)
462 return 0;
463 if (hdr[len] != ':')
464 return 0;
465 if (strncasecmp(hdr, name, len) != 0)
466 return 0;
467 val = hdr + len + 1;
468 while (val < end && HTTP_IS_SPHT(*val))
469 val++;
470 if ((val >= end) && (len + 2 <= end - hdr))
471 return len + 2; /* we may replace starting from second space */
472 return val - hdr;
473}
474
Willy Tarreau68085d82010-01-18 14:54:04 +0100475/* Find the end of the header value contained between <s> and <e>. See RFC2616,
476 * par 2.2 for more information. Note that it requires a valid header to return
477 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200478 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100479char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480{
481 int quoted, qdpair;
482
483 quoted = qdpair = 0;
484 for (; s < e; s++) {
485 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200486 else if (quoted) {
487 if (*s == '\\') qdpair = 1;
488 else if (*s == '"') quoted = 0;
489 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490 else if (*s == '"') quoted = 1;
491 else if (*s == ',') return s;
492 }
493 return s;
494}
495
496/* Find the first or next occurrence of header <name> in message buffer <sol>
497 * using headers index <idx>, and return it in the <ctx> structure. This
498 * structure holds everything necessary to use the header and find next
499 * occurrence. If its <idx> member is 0, the header is searched from the
500 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100501 * 1 when it finds a value, and 0 when there is no more. It is designed to work
502 * with headers defined as comma-separated lists. As a special case, if ctx->val
503 * is NULL when searching for a new values of a header, the current header is
504 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 */
506int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100507 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 struct hdr_ctx *ctx)
509{
Willy Tarreau68085d82010-01-18 14:54:04 +0100510 char *eol, *sov;
511 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200512
Willy Tarreau68085d82010-01-18 14:54:04 +0100513 cur_idx = ctx->idx;
514 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515 /* We have previously returned a value, let's search
516 * another one on the same line.
517 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200519 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100520 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 eol = sol + idx->v[cur_idx].len;
522
523 if (sov >= eol)
524 /* no more values in this header */
525 goto next_hdr;
526
Willy Tarreau68085d82010-01-18 14:54:04 +0100527 /* values remaining for this header, skip the comma but save it
528 * for later use (eg: for header deletion).
529 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 sov++;
531 while (sov < eol && http_is_lws[(unsigned char)*sov])
532 sov++;
533
534 goto return_hdr;
535 }
536
537 /* first request for this header */
538 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200540 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 while (cur_idx) {
542 eol = sol + idx->v[cur_idx].len;
543
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200544 if (len == 0) {
545 /* No argument was passed, we want any header.
546 * To achieve this, we simply build a fake request. */
547 while (sol + len < eol && sol[len] != ':')
548 len++;
549 name = sol;
550 }
551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 if ((len < eol - sol) &&
553 (sol[len] == ':') &&
554 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 sov = sol + len + 1;
557 while (sov < eol && http_is_lws[(unsigned char)*sov])
558 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 ctx->prev = old_idx;
562 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 ctx->idx = cur_idx;
564 ctx->val = sov - sol;
565
566 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200567 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200568 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 eol--;
570 ctx->tws++;
571 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 ctx->vlen = eol - sov;
573 return 1;
574 }
575 next_hdr:
576 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200578 cur_idx = idx->v[cur_idx].next;
579 }
580 return 0;
581}
582
583int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100584 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200585 struct hdr_ctx *ctx)
586{
587 return http_find_header2(name, strlen(name), sol, idx, ctx);
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Remove one value of a header. This only works on a <ctx> returned by one of
591 * the http_find_header functions. The value is removed, as well as surrounding
592 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100593 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100594 * message <msg>. The new index is returned. If it is zero, it means there is
595 * no more header, so any processing may stop. The ctx is always left in a form
596 * that can be handled by http_find_header2() to find next occurrence.
597 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100598int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100599{
600 int cur_idx = ctx->idx;
601 char *sol = ctx->line;
602 struct hdr_idx_elem *hdr;
603 int delta, skip_comma;
604
605 if (!cur_idx)
606 return 0;
607
608 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200609 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200611 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 http_msg_move_end(msg, delta);
613 idx->used--;
614 hdr->len = 0; /* unused entry */
615 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100616 if (idx->tail == ctx->idx)
617 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 ctx->idx = ctx->prev; /* walk back to the end of previous header */
619 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
620 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 return ctx->idx;
623 }
624
625 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200626 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
627 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 */
629
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200630 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200631 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 NULL, 0);
634 hdr->len += delta;
635 http_msg_move_end(msg, delta);
636 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200637 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100638 return ctx->idx;
639}
640
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641/* This function handles a server error at the stream interface level. The
642 * stream interface is assumed to be already in a closed state. An optional
643 * message is copied into the input buffer, and an HTTP status code stored.
644 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100645 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100647static void http_server_error(struct session *t, struct stream_interface *si,
648 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200650 channel_auto_read(si->ob);
651 channel_abort(si->ob);
652 channel_auto_close(si->ob);
653 channel_erase(si->ob);
654 channel_auto_close(si->ib);
655 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100656 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100657 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200658 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659 }
660 if (!(t->flags & SN_ERR_MASK))
661 t->flags |= err;
662 if (!(t->flags & SN_FINST_MASK))
663 t->flags |= finst;
664}
665
Willy Tarreau80587432006-12-24 17:47:20 +0100666/* This function returns the appropriate error location for the given session
667 * and message.
668 */
669
Willy Tarreau783f2582012-09-04 12:19:04 +0200670struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100671{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200672 if (s->be->errmsg[msgnum].str)
673 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100674 else if (s->fe->errmsg[msgnum].str)
675 return &s->fe->errmsg[msgnum];
676 else
677 return &http_err_chunks[msgnum];
678}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680/*
681 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
682 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
683 */
684static http_meth_t find_http_meth(const char *str, const int len)
685{
686 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100687 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100688
689 m = ((unsigned)*str - 'A');
690
691 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 for (h = http_methods[m]; h->len > 0; h++) {
693 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100694 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 };
698 return HTTP_METH_OTHER;
699 }
700 return HTTP_METH_NONE;
701
702}
703
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704/* Parse the URI from the given transaction (which is assumed to be in request
705 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
706 * It is returned otherwise.
707 */
708static char *
709http_get_path(struct http_txn *txn)
710{
711 char *ptr, *end;
712
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100714 end = ptr + txn->req.sl.rq.u_l;
715
716 if (ptr >= end)
717 return NULL;
718
719 /* RFC2616, par. 5.1.2 :
720 * Request-URI = "*" | absuri | abspath | authority
721 */
722
723 if (*ptr == '*')
724 return NULL;
725
726 if (isalpha((unsigned char)*ptr)) {
727 /* this is a scheme as described by RFC3986, par. 3.1 */
728 ptr++;
729 while (ptr < end &&
730 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
731 ptr++;
732 /* skip '://' */
733 if (ptr == end || *ptr++ != ':')
734 return NULL;
735 if (ptr == end || *ptr++ != '/')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 }
740 /* skip [user[:passwd]@]host[:[port]] */
741
742 while (ptr < end && *ptr != '/')
743 ptr++;
744
745 if (ptr == end)
746 return NULL;
747
748 /* OK, we got the '/' ! */
749 return ptr;
750}
751
Willy Tarreau71241ab2012-12-27 11:30:54 +0100752/* Returns a 302 for a redirectable request that reaches a server working in
753 * in redirect mode. This may only be called just after the stream interface
754 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
755 * follow normal proxy processing. NOTE: this function is designed to support
756 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100758void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200763 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100766 trash.len = strlen(HTTP_302);
767 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100768
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100769 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100770
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100772 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100775 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100777 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
778 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200781 /* 3: add the request URI. Since it was already forwarded, we need
782 * to temporarily rewind the buffer.
783 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200785 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200788 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200789
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 if (!path)
793 return;
794
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100795 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 return;
797
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100798 memcpy(trash.str + trash.len, path, len);
799 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100800
801 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100802 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
803 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100804 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100805 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
806 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100807 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808
809 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200810 si_shutr(si);
811 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 si->err_type = SI_ET_NONE;
813 si->err_loc = NULL;
814 si->state = SI_ST_CLO;
815
816 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100817 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 if (srv)
821 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822}
823
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 * that the server side is closed. Note that err_type is actually a
826 * bitmask, where almost only aborts may be cumulated with other
827 * values. We consider that aborted operations are more important
828 * than timeouts or errors due to the fact that nobody else in the
829 * logs might explain incomplete retries. All others should avoid
830 * being cumulated. It should normally not be possible to have multiple
831 * aborts at once, but just in case, the first one in sequence is reported.
832 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100833void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100835 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836
837 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200839 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100841 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200842 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200845 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200848 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200851 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858}
859
Willy Tarreau42250582007-04-01 01:30:43 +0200860extern const char sess_term_cond[8];
861extern const char sess_fin_state[8];
862extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200863struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100864struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100865struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866
Willy Tarreau117f59e2007-03-04 18:17:17 +0100867/*
868 * Capture headers from message starting at <som> according to header list
869 * <cap_hdr>, and fill the <idx> structure appropriately.
870 */
871void capture_headers(char *som, struct hdr_idx *idx,
872 char **cap, struct cap_hdr *cap_hdr)
873{
874 char *eol, *sol, *col, *sov;
875 int cur_idx;
876 struct cap_hdr *h;
877 int len;
878
879 sol = som + hdr_idx_first_pos(idx);
880 cur_idx = hdr_idx_first_idx(idx);
881
882 while (cur_idx) {
883 eol = sol + idx->v[cur_idx].len;
884
885 col = sol;
886 while (col < eol && *col != ':')
887 col++;
888
889 sov = col + 1;
890 while (sov < eol && http_is_lws[(unsigned char)*sov])
891 sov++;
892
893 for (h = cap_hdr; h; h = h->next) {
894 if ((h->namelen == col - sol) &&
895 (strncasecmp(sol, h->name, h->namelen) == 0)) {
896 if (cap[h->index] == NULL)
897 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200898 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100899
900 if (cap[h->index] == NULL) {
901 Alert("HTTP capture : out of memory.\n");
902 continue;
903 }
904
905 len = eol - sov;
906 if (len > h->len)
907 len = h->len;
908
909 memcpy(cap[h->index], sov, len);
910 cap[h->index][len]=0;
911 }
912 }
913 sol = eol + idx->v[cur_idx].cr + 1;
914 cur_idx = idx->v[cur_idx].next;
915 }
916}
917
918
Willy Tarreau42250582007-04-01 01:30:43 +0200919/* either we find an LF at <ptr> or we jump to <bad>.
920 */
921#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
922
923/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
924 * otherwise to <http_msg_ood> with <state> set to <st>.
925 */
926#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
927 ptr++; \
928 if (likely(ptr < end)) \
929 goto good; \
930 else { \
931 state = (st); \
932 goto http_msg_ood; \
933 } \
934 } while (0)
935
936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100938 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100939 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
940 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
941 * will give undefined results.
942 * Note that it is upon the caller's responsibility to ensure that ptr < end,
943 * and that msg->sol points to the beginning of the response.
944 * If a complete line is found (which implies that at least one CR or LF is
945 * found before <end>, the updated <ptr> is returned, otherwise NULL is
946 * returned indicating an incomplete line (which does not mean that parts have
947 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
948 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
949 * upon next call.
950 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
953 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200954 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100955 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200956const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100957 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100958 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100959{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200960 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100961
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200964 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100965 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
967
968 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100969 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
971 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100972 state = HTTP_MSG_ERROR;
973 break;
974
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200976 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100978 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100979 goto http_msg_rpcode;
980 }
981 if (likely(HTTP_IS_SPHT(*ptr)))
982 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
983 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100984 state = HTTP_MSG_ERROR;
985 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100986
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200988 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 if (likely(!HTTP_IS_LWS(*ptr)))
990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
991
992 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
995 }
996
997 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100998 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 http_msg_rsp_reason:
1000 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001001 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 msg->sl.st.r_l = 0;
1003 goto http_msg_rpline_eol;
1004
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001006 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 goto http_msg_rpreason;
1010 }
1011 if (likely(HTTP_IS_SPHT(*ptr)))
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1013 /* so it's a CR/LF, so there is no reason phrase */
1014 goto http_msg_rsp_reason;
1015
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001017 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001018 if (likely(!HTTP_IS_CRLF(*ptr)))
1019 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001020 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 http_msg_rpline_eol:
1022 /* We have seen the end of line. Note that we do not
1023 * necessarily have the \n yet, but at least we know that we
1024 * have EITHER \r OR \n, otherwise the response would not be
1025 * complete. We can then record the response length and return
1026 * to the caller which will be able to register it.
1027 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001028 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001029 return ptr;
1030
1031#ifdef DEBUG_FULL
1032 default:
1033 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1034 exit(1);
1035#endif
1036 }
1037
1038 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001039 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 if (ret_state)
1041 *ret_state = state;
1042 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001043 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001045}
1046
Willy Tarreau8973c702007-01-21 23:58:29 +01001047/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001048 * This function parses a request line between <ptr> and <end>, starting with
1049 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1050 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1051 * will give undefined results.
1052 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1053 * and that msg->sol points to the beginning of the request.
1054 * If a complete line is found (which implies that at least one CR or LF is
1055 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1056 * returned indicating an incomplete line (which does not mean that parts have
1057 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1058 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1059 * upon next call.
1060 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001061 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1063 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001064 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001066const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001067 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001068 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001070 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001074 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 if (likely(HTTP_IS_TOKEN(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001079 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_CRLF(*ptr))) {
1084 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001087 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001089 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 msg->sl.rq.v_l = 0;
1093 goto http_msg_rqline_eol;
1094 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001095 state = HTTP_MSG_ERROR;
1096 break;
1097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001099 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001101 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 goto http_msg_rquri;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1106 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1107 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001110 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001111 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001115 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001120 /* non-ASCII chars are forbidden unless option
1121 * accept-invalid-http-request is enabled in the frontend.
1122 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001124 if (msg->err_pos < -1)
1125 goto invalid_char;
1126 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001127 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1129 }
1130
1131 if (likely(HTTP_IS_CRLF(*ptr))) {
1132 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1133 goto http_msg_req09_uri_e;
1134 }
1135
1136 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001137 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001138 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001139 state = HTTP_MSG_ERROR;
1140 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001143 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001145 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 goto http_msg_rqver;
1147 }
1148 if (likely(HTTP_IS_SPHT(*ptr)))
1149 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1150 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1151 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001154 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001157
1158 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001159 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 http_msg_rqline_eol:
1161 /* We have seen the end of line. Note that we do not
1162 * necessarily have the \n yet, but at least we know that we
1163 * have EITHER \r OR \n, otherwise the request would not be
1164 * complete. We can then record the request length and return
1165 * to the caller which will be able to register it.
1166 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001167 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001168 return ptr;
1169 }
1170
1171 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001172 state = HTTP_MSG_ERROR;
1173 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175#ifdef DEBUG_FULL
1176 default:
1177 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1178 exit(1);
1179#endif
1180 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (ret_state)
1185 *ret_state = state;
1186 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001187 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001191/*
1192 * Returns the data from Authorization header. Function may be called more
1193 * than once so data is stored in txn->auth_data. When no header is found
1194 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1195 * searching again for something we are unable to find anyway.
1196 */
1197
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001198char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001199
1200int
1201get_http_auth(struct session *s)
1202{
1203
1204 struct http_txn *txn = &s->txn;
1205 struct chunk auth_method;
1206 struct hdr_ctx ctx;
1207 char *h, *p;
1208 int len;
1209
1210#ifdef DEBUG_AUTH
1211 printf("Auth for session %p: %d\n", s, txn->auth.method);
1212#endif
1213
1214 if (txn->auth.method == HTTP_AUTH_WRONG)
1215 return 0;
1216
1217 if (txn->auth.method)
1218 return 1;
1219
1220 txn->auth.method = HTTP_AUTH_WRONG;
1221
1222 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001223
1224 if (txn->flags & TX_USE_PX_CONN) {
1225 h = "Proxy-Authorization";
1226 len = strlen(h);
1227 } else {
1228 h = "Authorization";
1229 len = strlen(h);
1230 }
1231
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001233 return 0;
1234
1235 h = ctx.line + ctx.val;
1236
1237 p = memchr(h, ' ', ctx.vlen);
1238 if (!p || p == h)
1239 return 0;
1240
1241 chunk_initlen(&auth_method, h, 0, p-h);
1242 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1243
1244 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1245
1246 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001247 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248
1249 if (len < 0)
1250 return 0;
1251
1252
1253 get_http_auth_buff[len] = '\0';
1254
1255 p = strchr(get_http_auth_buff, ':');
1256
1257 if (!p)
1258 return 0;
1259
1260 txn->auth.user = get_http_auth_buff;
1261 *p = '\0';
1262 txn->auth.pass = p+1;
1263
1264 txn->auth.method = HTTP_AUTH_BASIC;
1265 return 1;
1266 }
1267
1268 return 0;
1269}
1270
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8973c702007-01-21 23:58:29 +01001272/*
1273 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001274 * depending on the initial msg->msg_state. The caller is responsible for
1275 * ensuring that the message does not wrap. The function can be preempted
1276 * everywhere when data are missing and recalled at the exact same location
1277 * with no information loss. The message may even be realigned between two
1278 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001279 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001280 * fields. Note that msg->sol will be initialized after completing the first
1281 * state, so that none of the msg pointers has to be initialized prior to the
1282 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001284void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001286 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001288 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001290 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001291 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001292 ptr = buf->p + msg->next;
1293 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (unlikely(ptr >= end))
1296 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 /*
1300 * First, states that are specific to the response only.
1301 * We check them first so that request and headers are
1302 * closer to each other (accessed more often).
1303 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 /* we have a start of message, but we have to check
1308 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001309 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001311 if (unlikely(ptr != buf->p)) {
1312 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001313 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001314 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001315 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 }
Willy Tarreau26927362012-05-18 23:22:52 +02001317 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001318 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 hdr_idx_init(idx);
1320 state = HTTP_MSG_RPVER;
1321 goto http_msg_rpver;
1322 }
1323
1324 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1325 goto http_msg_invalid;
1326
1327 if (unlikely(*ptr == '\n'))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1330 /* stop here */
1331
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001333 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 EXPECT_LF_HERE(ptr, http_msg_invalid);
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1336 /* stop here */
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 case HTTP_MSG_RPVER_SP:
1341 case HTTP_MSG_RPCODE:
1342 case HTTP_MSG_RPCODE_SP:
1343 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001344 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001345 state, ptr, end,
1346 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (unlikely(!ptr))
1348 return;
1349
1350 /* we have a full response and we know that we have either a CR
1351 * or an LF at <ptr>.
1352 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1354
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001355 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 if (likely(*ptr == '\r'))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1358 goto http_msg_rpline_end;
1359
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001361 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 /* msg->sol must point to the first of CR or LF. */
1363 EXPECT_LF_HERE(ptr, http_msg_invalid);
1364 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1365 /* stop here */
1366
1367 /*
1368 * Second, states that are specific to the request only
1369 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001373 /* we have a start of message, but we have to check
1374 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001375 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 if (likely(ptr != buf->p)) {
1378 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001380 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 }
Willy Tarreau26927362012-05-18 23:22:52 +02001383 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001384 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 state = HTTP_MSG_RQMETH;
1386 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1390 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (unlikely(*ptr == '\n'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH_SP:
1406 case HTTP_MSG_RQURI:
1407 case HTTP_MSG_RQURI_SP:
1408 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001409 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001410 state, ptr, end,
1411 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (unlikely(!ptr))
1413 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 /* we have a full request and we know that we have either a CR
1416 * or an LF at <ptr>.
1417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001420 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* check for HTTP/0.9 request : no version information available.
1428 * msg->sol must point to the first of CR or LF.
1429 */
1430 if (unlikely(msg->sl.rq.v_l == 0))
1431 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 EXPECT_LF_HERE(ptr, http_msg_invalid);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 /*
1438 * Common states below
1439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001442 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_CRLF(*ptr))) {
1444 goto http_msg_hdr_name;
1445 }
1446
1447 if (likely(*ptr == '\r'))
1448 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1449 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001452 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* assumes msg->sol points to the first char */
1454 if (likely(HTTP_IS_TOKEN(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001457 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001464 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001471 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001500 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001505 /* assumes msg->sol points to the first char, and msg->sov
1506 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
1508 if (likely(!HTTP_IS_CRLF(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001511 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* Note: we could also copy eol into ->eoh so that we have the
1513 * real header end in case it ends with lots of LWS, but is this
1514 * really needed ?
1515 */
1516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1518 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1528 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001529 for (; buf->p + msg->eol < ptr; msg->eol++)
1530 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_hdr_val;
1532 }
1533 http_msg_complete_header:
1534 /*
1535 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001536 * Assumes msg->sol points to the first char, msg->sov points
1537 * to the first character of the value and msg->eol to the
1538 * first CR or LF so we know how the line ends. We insert last
1539 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001541 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 idx, idx->tail) < 0))
1543 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001544
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (likely(!HTTP_IS_CRLF(*ptr))) {
1547 goto http_msg_hdr_name;
1548 }
1549
1550 if (likely(*ptr == '\r'))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1552 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /* Assumes msg->sol points to the first of either CR or LF */
1557 EXPECT_LF_HERE(ptr, http_msg_invalid);
1558 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001560 msg->eoh = msg->sol;
1561 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001562 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001564
1565 case HTTP_MSG_ERROR:
1566 /* this may only happen if we call http_msg_analyser() twice with an error */
1567 break;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569#ifdef DEBUG_FULL
1570 default:
1571 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1572 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
1575 http_msg_ood:
1576 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001578 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 http_msg_invalid:
1582 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
1586}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001587
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001588/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1589 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1590 * nothing is done and 1 is returned.
1591 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593{
1594 int delta;
1595 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau9b28e032012-10-12 23:49:43 +02001601 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001606 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001611 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001614 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001629 * and "keep-alive" values. If we already know that some headers may safely
1630 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001631 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1632 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001633 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1635 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1636 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001637 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001638 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001639void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001640{
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001642 const char *hdr_val = "Connection";
1643 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001644
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001645 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001646 return;
1647
Willy Tarreau88d349d2010-01-25 12:15:43 +01001648 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1649 hdr_val = "Proxy-Connection";
1650 hdr_len = 16;
1651 }
1652
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001654 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001655 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1657 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001658 if (to_del & 2)
1659 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 else
1661 txn->flags |= TX_CON_KAL_SET;
1662 }
1663 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1664 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001665 if (to_del & 1)
1666 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001667 else
1668 txn->flags |= TX_CON_CLO_SET;
1669 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001670 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1671 txn->flags |= TX_HDR_CONN_UPG;
1672 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001673 }
1674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 txn->flags |= TX_HDR_CONN_PRS;
1676 return;
1677}
Willy Tarreau5b154472009-12-21 20:11:07 +01001678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679/* Apply desired changes on the Connection: header. Values may be removed and/or
1680 * added depending on the <wanted> flags, which are exclusively composed of
1681 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1682 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001684void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685{
1686 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687 const char *hdr_val = "Connection";
1688 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689
1690 ctx.idx = 0;
1691
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692
1693 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1694 hdr_val = "Proxy-Connection";
1695 hdr_len = 16;
1696 }
1697
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001699 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1701 if (wanted & TX_CON_KAL_SET)
1702 txn->flags |= TX_CON_KAL_SET;
1703 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001704 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1707 if (wanted & TX_CON_CLO_SET)
1708 txn->flags |= TX_CON_CLO_SET;
1709 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001711 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001712 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713
1714 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1715 return;
1716
1717 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1718 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001719 hdr_val = "Connection: close";
1720 hdr_len = 17;
1721 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1722 hdr_val = "Proxy-Connection: close";
1723 hdr_len = 23;
1724 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 }
1727
1728 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1729 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001730 hdr_val = "Connection: keep-alive";
1731 hdr_len = 22;
1732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection: keep-alive";
1734 hdr_len = 28;
1735 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001736 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001737 }
1738 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739}
1740
Willy Tarreaua458b672012-03-05 11:17:50 +01001741/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001743 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001747static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001749 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001751 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001752 const char *end = buf->data + buf->size;
1753 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001754 unsigned int chunk = 0;
1755
1756 /* The chunk size is in the following form, though we are only
1757 * interested in the size and CRLF :
1758 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1759 */
1760 while (1) {
1761 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001762 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 if (c < 0) /* not a hex digit anymore */
1766 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001768 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001769 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001771 chunk = (chunk << 4) + c;
1772 }
1773
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001775 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001776 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777
1778 while (http_is_spht[(unsigned char)*ptr]) {
1779 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001781 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 }
1784
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 /* Up to there, we know that at least one byte is present at *ptr. Check
1786 * for the end of chunk size.
1787 */
1788 while (1) {
1789 if (likely(HTTP_IS_CRLF(*ptr))) {
1790 /* we now have a CR or an LF at ptr */
1791 if (likely(*ptr == '\r')) {
1792 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001794 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 return 0;
1796 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001797
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001799 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001801 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 /* done */
1803 break;
1804 }
1805 else if (*ptr == ';') {
1806 /* chunk extension, ends at next CRLF */
1807 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001808 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001810 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811
1812 while (!HTTP_IS_CRLF(*ptr)) {
1813 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001815 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816 return 0;
1817 }
1818 /* we have a CRLF now, loop above */
1819 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001822 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 }
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001826 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001829 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001830 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001831 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001833 msg->chunk_len = chunk;
1834 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001838 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001839 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001840}
1841
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001842/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001844 * the trailers is found, it is automatically scheduled to be forwarded,
1845 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1846 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001847 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001848 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001849 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001850 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1851 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001852 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001853 * matches the length of trailers already parsed and not forwarded. It is also
1854 * important to note that this function is designed to be able to parse wrapped
1855 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001857static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001859 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001860
Willy Tarreaua458b672012-03-05 11:17:50 +01001861 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001863 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 const char *ptr = b_ptr(buf, msg->next);
1865 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001866 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 /* scan current line and stop at LF or CRLF */
1869 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001870 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 return 0;
1872
1873 if (*ptr == '\n') {
1874 if (!p1)
1875 p1 = ptr;
1876 p2 = ptr;
1877 break;
1878 }
1879
1880 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001881 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001884 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 p1 = ptr;
1886 }
1887
1888 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001889 if (ptr >= buf->data + buf->size)
1890 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 }
1892
1893 /* after LF; point to beginning of next line */
1894 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 if (p2 >= buf->data + buf->size)
1896 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001897
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001899 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001900 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001901
1902 /* schedule this line for forwarding */
1903 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (msg->sov >= buf->size)
1905 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001907 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001908 /* LF/CRLF at beginning of line => end of trailers at p2.
1909 * Everything was scheduled for forwarding, there's nothing
1910 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001911 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 msg->msg_state = HTTP_MSG_DONE;
1914 return 1;
1915 }
1916 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 }
1919}
1920
Willy Tarreau54d23df2012-10-25 19:04:45 +02001921/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001923 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001924 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1926 * not enough data are available, the function does not change anything and
1927 * returns zero. If a parse error is encountered, the function returns < 0 and
1928 * does not change anything. Note: this function is designed to parse wrapped
1929 * CRLF at the end of the buffer.
1930 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001931static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001933 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001934 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935 int bytes;
1936
1937 /* NB: we'll check data availabilty at the end. It's not a
1938 * problem because whatever we match first will be checked
1939 * against the correct length.
1940 */
1941 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 if (*ptr == '\r') {
1944 bytes++;
1945 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001946 if (ptr >= buf->data + buf->size)
1947 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 }
1949
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001950 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return 0;
1952
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001959 if (ptr >= buf->data + buf->size)
1960 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001961 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1962 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001963 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1965 return 1;
1966}
Willy Tarreau5b154472009-12-21 20:11:07 +01001967
William Lallemand82fe75c2012-10-23 10:25:10 +02001968
1969/*
1970 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001971 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001972int select_compression_request_header(struct session *s, struct buffer *req)
1973{
1974 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001975 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001976 struct hdr_ctx ctx;
1977 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001978 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001979
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001980 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1981 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001982 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1983 */
1984 ctx.idx = 0;
1985 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1986 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001987 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1988 (ctx.vlen < 31 ||
1989 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1990 ctx.line[ctx.val + 30] < '6' ||
1991 (ctx.line[ctx.val + 30] == '6' &&
1992 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1993 s->comp_algo = NULL;
1994 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001995 }
1996
William Lallemand82fe75c2012-10-23 10:25:10 +02001997 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001998 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02001999 ctx.idx = 0;
2000 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002001 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002002 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2003 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002004
2005 /* remove all occurrences of the header when "compression offload" is set */
2006
2007 if ((s->be->comp && s->be->comp->offload) ||
2008 (s->fe->comp && s->fe->comp->offload)) {
2009 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2010 ctx.idx = 0;
2011 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2012 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2013 }
2014 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002015 return 1;
2016 }
2017 }
2018 }
2019 }
2020
2021 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002022 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2023 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002024 if (comp_algo->add_data == identity_add_data) {
2025 s->comp_algo = comp_algo;
2026 return 1;
2027 }
2028 }
2029 }
2030
2031 s->comp_algo = NULL;
2032
2033 return 0;
2034}
2035
2036/*
2037 * Selects a comression algorithm depending of the server response.
2038 */
2039int select_compression_response_header(struct session *s, struct buffer *res)
2040{
2041 struct http_txn *txn = &s->txn;
2042 struct http_msg *msg = &txn->rsp;
2043 struct hdr_ctx ctx;
2044 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002045
2046 /* no common compression algorithm was found in request header */
2047 if (s->comp_algo == NULL)
2048 goto fail;
2049
2050 /* HTTP < 1.1 should not be compressed */
2051 if (!(msg->flags & HTTP_MSGF_VER_11))
2052 goto fail;
2053
William Lallemandd3002612012-11-26 14:34:47 +01002054 /* 200 only */
2055 if (txn->status != 200)
2056 goto fail;
2057
William Lallemand82fe75c2012-10-23 10:25:10 +02002058 /* Content-Length is null */
2059 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2060 goto fail;
2061
2062 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002063 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002064 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2065 goto fail;
2066
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002067 /* no compression when Cache-Control: no-transform is present in the message */
2068 ctx.idx = 0;
2069 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2070 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2071 goto fail;
2072 }
2073
William Lallemand82fe75c2012-10-23 10:25:10 +02002074 comp_type = NULL;
2075
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002076 /* we don't want to compress multipart content-types, nor content-types that are
2077 * not listed in the "compression type" directive if any. If no content-type was
2078 * found but configuration requires one, we don't compress either. Backend has
2079 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002080 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002081 ctx.idx = 0;
2082 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2083 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2084 goto fail;
2085
2086 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2087 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002088 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002089 if (ctx.vlen >= comp_type->name_len &&
2090 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002091 /* this Content-Type should be compressed */
2092 break;
2093 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002094 /* this Content-Type should not be compressed */
2095 if (comp_type == NULL)
2096 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002097 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002098 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002099 else { /* no content-type header */
2100 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2101 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002102 }
2103
William Lallemandd85f9172012-11-09 17:05:39 +01002104 /* limit compression rate */
2105 if (global.comp_rate_lim > 0)
2106 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2107 goto fail;
2108
William Lallemand072a2bf2012-11-20 17:01:01 +01002109 /* limit cpu usage */
2110 if (idle_pct < compress_min_idle)
2111 goto fail;
2112
William Lallemand4c49fae2012-11-07 15:00:23 +01002113 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002114 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002115 goto fail;
2116
William Lallemandec3e3892012-11-12 17:02:18 +01002117 s->flags |= SN_COMP_READY;
2118
William Lallemand82fe75c2012-10-23 10:25:10 +02002119 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002120 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002121 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2122 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2123
2124 /* add Transfer-Encoding header */
2125 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2126 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2127
2128 /*
2129 * Add Content-Encoding header when it's not identity encoding.
2130 * RFC 2616 : Identity encoding: This content-coding is used only in the
2131 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2132 * header.
2133 */
2134 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002135 trash.len = 18;
2136 memcpy(trash.str, "Content-Encoding: ", trash.len);
2137 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2138 trash.len += s->comp_algo->name_len;
2139 trash.str[trash.len] = '\0';
2140 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002141 }
2142
William Lallemand82fe75c2012-10-23 10:25:10 +02002143 return 1;
2144
2145fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002146 if (s->flags & SN_COMP_READY)
William Lallemand1c2d6222012-10-30 15:52:53 +01002147 s->comp_algo->end(&s->comp_ctx);
Willy Tarreaub97b6192012-11-19 14:55:02 +01002148 s->comp_algo = NULL;
2149 s->flags &= ~SN_COMP_READY;
William Lallemand82fe75c2012-10-23 10:25:10 +02002150 return 0;
2151}
2152
2153
Willy Tarreaud787e662009-07-07 10:14:51 +02002154/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2155 * processing can continue on next analysers, or zero if it either needs more
2156 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2157 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2158 * when it has nothing left to do, and may remove any analyser when it wants to
2159 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002160 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002161int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002162{
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 /*
2164 * We will parse the partial (or complete) lines.
2165 * We will check the request syntax, and also join multi-line
2166 * headers. An index of all the lines will be elaborated while
2167 * parsing.
2168 *
2169 * For the parsing, we use a 28 states FSM.
2170 *
2171 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002172 * req->buf->p = beginning of request
2173 * req->buf->p + msg->eoh = end of processed headers / start of current one
2174 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002175 * msg->eol = end of current header or line (LF or CRLF)
2176 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002177 *
2178 * At end of parsing, we may perform a capture of the error (if any), and
2179 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2180 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2181 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002185 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 struct http_txn *txn = &s->txn;
2187 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002188 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002189
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002190 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002191 now_ms, __FUNCTION__,
2192 s,
2193 req,
2194 req->rex, req->wex,
2195 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002196 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002197 req->analysers);
2198
Willy Tarreau52a0c602009-08-16 22:45:38 +02002199 /* we're speaking HTTP here, so let's speak HTTP to the client */
2200 s->srv_error = http_return_srv_error;
2201
Willy Tarreau83e3af02009-12-28 17:39:57 +01002202 /* There's a protected area at the end of the buffer for rewriting
2203 * purposes. We don't want to start to parse the request if the
2204 * protected area is affected, because we may have to move processed
2205 * data later, which is much more complicated.
2206 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002207 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002208 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002209 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002210 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2211 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2212 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002213 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002214 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002215 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002216 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002217 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002218 return 0;
2219 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002220 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2221 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2222 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002223 }
2224
Willy Tarreau065e8332010-01-08 00:30:20 +01002225 /* Note that we have the same problem with the response ; we
2226 * may want to send a redirect, error or anything which requires
2227 * some spare space. So we'll ensure that we have at least
2228 * maxrewrite bytes available in the response buffer before
2229 * processing that one. This will only affect pipelined
2230 * keep-alive requests.
2231 */
2232 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002233 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002234 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2235 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2236 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002237 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002238 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002239 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002240 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002241 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002242 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002243 return 0;
2244 }
2245 }
2246
Willy Tarreau9b28e032012-10-12 23:49:43 +02002247 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002248 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002249 }
2250
Willy Tarreau59234e92008-11-30 23:51:27 +01002251 /* 1: we might have to print this header in debug mode */
2252 if (unlikely((global.mode & MODE_DEBUG) &&
2253 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002254 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002255 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002256
Willy Tarreau9b28e032012-10-12 23:49:43 +02002257 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002258 /* this is a bit complex : in case of error on the request line,
2259 * we know that rq.l is still zero, so we display only the part
2260 * up to the end of the line (truncated by debug_hdr).
2261 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002262 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002264
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 sol += hdr_idx_first_pos(&txn->hdr_idx);
2266 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 while (cur_idx) {
2269 eol = sol + txn->hdr_idx.v[cur_idx].len;
2270 debug_hdr("clihdr", s, sol, eol);
2271 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2272 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002273 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 }
2275
Willy Tarreau58f10d72006-12-04 02:26:12 +01002276
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 /*
2278 * Now we quickly check if we have found a full valid request.
2279 * If not so, we check the FD and buffer states before leaving.
2280 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002281 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002282 * requests are checked first. When waiting for a second request
2283 * on a keep-alive session, if we encounter and error, close, t/o,
2284 * we note the error in the session flags but don't set any state.
2285 * Since the error will be noted there, it will not be counted by
2286 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002287 * Last, we may increase some tracked counters' http request errors on
2288 * the cases that are deliberately the client's fault. For instance,
2289 * a timeout or connection reset is not counted as an error. However
2290 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002292
Willy Tarreau655dce92009-11-08 13:10:58 +01002293 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002294 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002296 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002297 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002298 session_inc_http_req_ctr(s);
2299 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002300 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002301 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 /* 1: Since we are in header mode, if there's no space
2305 * left for headers, we won't be able to free more
2306 * later, so the session will never terminate. We
2307 * must terminate it now.
2308 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002309 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 /* FIXME: check if URI is set and return Status
2311 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002313 session_inc_http_req_ctr(s);
2314 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002315 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002316 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002317 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 goto return_bad_req;
2319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002322 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002323 if (!(s->flags & SN_ERR_MASK))
2324 s->flags |= SN_ERR_CLICL;
2325
Willy Tarreaufcffa692010-01-10 14:21:19 +01002326 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002327 goto failed_keep_alive;
2328
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002330 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002331 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002332 session_inc_http_err_ctr(s);
2333 }
2334
Willy Tarreaudc979f22012-12-04 10:39:01 +01002335 txn->status = 400;
2336 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 msg->msg_state = HTTP_MSG_ERROR;
2338 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002339
Willy Tarreauda7ff642010-06-23 11:44:09 +02002340 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002341 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002342 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002343 if (s->listener->counters)
2344 s->listener->counters->failed_req++;
2345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 if (!(s->flags & SN_FINST_MASK))
2347 s->flags |= SN_FINST_R;
2348 return 0;
2349 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002352 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002353 if (!(s->flags & SN_ERR_MASK))
2354 s->flags |= SN_ERR_CLITO;
2355
Willy Tarreaufcffa692010-01-10 14:21:19 +01002356 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002357 goto failed_keep_alive;
2358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002360 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002361 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002362 session_inc_http_err_ctr(s);
2363 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002365 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 msg->msg_state = HTTP_MSG_ERROR;
2367 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002368
Willy Tarreauda7ff642010-06-23 11:44:09 +02002369 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002370 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002371 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002372 if (s->listener->counters)
2373 s->listener->counters->failed_req++;
2374
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 if (!(s->flags & SN_FINST_MASK))
2376 s->flags |= SN_FINST_R;
2377 return 0;
2378 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002379
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002381 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002382 if (!(s->flags & SN_ERR_MASK))
2383 s->flags |= SN_ERR_CLICL;
2384
Willy Tarreaufcffa692010-01-10 14:21:19 +01002385 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002386 goto failed_keep_alive;
2387
Willy Tarreau4076a152009-04-02 15:18:36 +02002388 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002389 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002391 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 msg->msg_state = HTTP_MSG_ERROR;
2393 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002394
Willy Tarreauda7ff642010-06-23 11:44:09 +02002395 session_inc_http_err_ctr(s);
2396 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002397 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002398 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002399 if (s->listener->counters)
2400 s->listener->counters->failed_req++;
2401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 if (!(s->flags & SN_FINST_MASK))
2403 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002404 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002405 }
2406
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002407 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002408 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2409 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002410#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002411 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002412 /* We need more data, we have to re-enable quick-ack in case we
2413 * previously disabled it, otherwise we might cause the client
2414 * to delay next data.
2415 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002416 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002417 }
2418#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002419
Willy Tarreaufcffa692010-01-10 14:21:19 +01002420 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2421 /* If the client starts to talk, let's fall back to
2422 * request timeout processing.
2423 */
2424 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002425 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002426 }
2427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002429 if (!tick_isset(req->analyse_exp)) {
2430 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2431 (txn->flags & TX_WAIT_NEXT_RQ) &&
2432 tick_isset(s->be->timeout.httpka))
2433 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2434 else
2435 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2436 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002437
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 /* we're not ready yet */
2439 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002440
2441 failed_keep_alive:
2442 /* Here we process low-level errors for keep-alive requests. In
2443 * short, if the request is not the first one and it experiences
2444 * a timeout, read error or shutdown, we just silently close so
2445 * that the client can try again.
2446 */
2447 txn->status = 0;
2448 msg->msg_state = HTTP_MSG_RQBEFORE;
2449 req->analysers = 0;
2450 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002451 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002452 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002453 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002455
Willy Tarreaud787e662009-07-07 10:14:51 +02002456 /* OK now we have a complete HTTP request with indexed headers. Let's
2457 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002458 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002459 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002460 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002461 * byte after the last LF. msg->sov points to the first byte of data.
2462 * msg->eol cannot be trusted because it may have been left uninitialized
2463 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002464 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002465
Willy Tarreauda7ff642010-06-23 11:44:09 +02002466 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002467 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2468
Willy Tarreaub16a5742010-01-10 14:46:16 +01002469 if (txn->flags & TX_WAIT_NEXT_RQ) {
2470 /* kill the pending keep-alive timeout */
2471 txn->flags &= ~TX_WAIT_NEXT_RQ;
2472 req->analyse_exp = TICK_ETERNITY;
2473 }
2474
2475
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 /* Maybe we found in invalid header name while we were configured not
2477 * to block on that, so we have to capture it now.
2478 */
2479 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002480 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002481
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 /*
2483 * 1: identify the method
2484 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002485 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002486
2487 /* we can make use of server redirect on GET and HEAD */
2488 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2489 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /*
2492 * 2: check if the URI matches the monitor_uri.
2493 * We have to do this for every request which gets in, because
2494 * the monitor-uri is defined by the frontend.
2495 */
2496 if (unlikely((s->fe->monitor_uri_len != 0) &&
2497 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002498 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 s->fe->monitor_uri,
2500 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002501 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002503 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002507 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002510 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002511 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 ret = acl_pass(ret);
2514 if (cond->pol == ACL_COND_UNLESS)
2515 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 if (ret) {
2518 /* we fail this request, let's return 503 service unavail */
2519 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002520 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002522 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002523 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002524
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 /* nothing to fail, let's reply normaly */
2526 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002527 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 goto return_prx_cond;
2529 }
2530
2531 /*
2532 * 3: Maybe we have to copy the original REQURI for the logs ?
2533 * Note: we cannot log anymore if the request has been
2534 * classified as invalid.
2535 */
2536 if (unlikely(s->logs.logwait & LW_REQ)) {
2537 /* we have a complete HTTP request that we must log */
2538 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2539 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002540
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 if (urilen >= REQURI_LEN)
2542 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002543 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002545
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002546 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 s->do_log(s);
2548 } else {
2549 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002550 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 }
Willy Tarreau06619262006-12-17 08:37:22 +01002552
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002553 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2554 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002555
Willy Tarreau59234e92008-11-30 23:51:27 +01002556 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002557 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002558 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002559
Willy Tarreau5b154472009-12-21 20:11:07 +01002560 /* ... and check if the request is HTTP/1.1 or above */
2561 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002562 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2563 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2564 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002565 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002566
2567 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002568 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002569
Willy Tarreau88d349d2010-01-25 12:15:43 +01002570 /* if the frontend has "option http-use-proxy-header", we'll check if
2571 * we have what looks like a proxied connection instead of a connection,
2572 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2573 * Note that this is *not* RFC-compliant, however browsers and proxies
2574 * happen to do that despite being non-standard :-(
2575 * We consider that a request not beginning with either '/' or '*' is
2576 * a proxied connection, which covers both "scheme://location" and
2577 * CONNECT ip:port.
2578 */
2579 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002581 txn->flags |= TX_USE_PX_CONN;
2582
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002583 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002584 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002587 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002588 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002590
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002591 /* 6: determine the transfer-length.
2592 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2593 * the presence of a message-body in a REQUEST and its transfer length
2594 * must be determined that way (in order of precedence) :
2595 * 1. The presence of a message-body in a request is signaled by the
2596 * inclusion of a Content-Length or Transfer-Encoding header field
2597 * in the request's header fields. When a request message contains
2598 * both a message-body of non-zero length and a method that does
2599 * not define any semantics for that request message-body, then an
2600 * origin server SHOULD either ignore the message-body or respond
2601 * with an appropriate error message (e.g., 413). A proxy or
2602 * gateway, when presented the same request, SHOULD either forward
2603 * the request inbound with the message- body or ignore the
2604 * message-body when determining a response.
2605 *
2606 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2607 * and the "chunked" transfer-coding (Section 6.2) is used, the
2608 * transfer-length is defined by the use of this transfer-coding.
2609 * If a Transfer-Encoding header field is present and the "chunked"
2610 * transfer-coding is not present, the transfer-length is defined
2611 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002612 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002613 * 3. If a Content-Length header field is present, its decimal value in
2614 * OCTETs represents both the entity-length and the transfer-length.
2615 * If a message is received with both a Transfer-Encoding header
2616 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002617 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002618 * 4. By the server closing the connection. (Closing the connection
2619 * cannot be used to indicate the end of a request body, since that
2620 * would leave no possibility for the server to send back a response.)
2621 *
2622 * Whenever a transfer-coding is applied to a message-body, the set of
2623 * transfer-codings MUST include "chunked", unless the message indicates
2624 * it is terminated by closing the connection. When the "chunked"
2625 * transfer-coding is used, it MUST be the last transfer-coding applied
2626 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002627 */
2628
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002629 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002630 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002631 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002632 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002633 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002634 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002635 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2636 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002637 /* bad transfer-encoding (chunked followed by something else) */
2638 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002639 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002640 break;
2641 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002642 }
2643
Willy Tarreau32b47f42009-10-18 20:55:02 +02002644 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002645 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002646 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002647 signed long long cl;
2648
Willy Tarreauad14f752011-09-02 20:33:27 +02002649 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002650 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002651 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002652 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002653
Willy Tarreauad14f752011-09-02 20:33:27 +02002654 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002655 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002656 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002657 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658
Willy Tarreauad14f752011-09-02 20:33:27 +02002659 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002660 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002661 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002662 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002663
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002664 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002666 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002667 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002668
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002669 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002670 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002671 }
2672
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673 /* bodyless requests have a known length */
2674 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002675 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002676
Willy Tarreaud787e662009-07-07 10:14:51 +02002677 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002678 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002679 req->analyse_exp = TICK_ETERNITY;
2680 return 1;
2681
2682 return_bad_req:
2683 /* We centralize bad requests processing here */
2684 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2685 /* we detected a parsing error. We want to archive this request
2686 * in the dedicated proxy area for later troubleshooting.
2687 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002688 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002689 }
2690
2691 txn->req.msg_state = HTTP_MSG_ERROR;
2692 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002693 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002694
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002695 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002696 if (s->listener->counters)
2697 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002698
2699 return_prx_cond:
2700 if (!(s->flags & SN_ERR_MASK))
2701 s->flags |= SN_ERR_PRXCOND;
2702 if (!(s->flags & SN_FINST_MASK))
2703 s->flags |= SN_FINST_R;
2704
2705 req->analysers = 0;
2706 req->analyse_exp = TICK_ETERNITY;
2707 return 0;
2708}
2709
Cyril Bonté70be45d2010-10-12 00:14:35 +02002710/* We reached the stats page through a POST request.
2711 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002712 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002713 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002714int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002715{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002716 struct proxy *px = NULL;
2717 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002718
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002719 char key[LINESIZE];
2720 int action = ST_ADM_ACTION_NONE;
2721 int reprocess = 0;
2722
2723 int total_servers = 0;
2724 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002725
2726 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002727 char *st_cur_param = NULL;
2728 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002729
Willy Tarreau9b28e032012-10-12 23:49:43 +02002730 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002731 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732
2733 cur_param = next_param = end_params;
2734
Willy Tarreau9b28e032012-10-12 23:49:43 +02002735 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002736 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002737 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002738 return 1;
2739 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002740 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002741 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002742 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002743 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002744 }
2745
2746 *end_params = '\0';
2747
Willy Tarreau295a8372011-03-10 11:25:07 +01002748 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002749
2750 /*
2751 * Parse the parameters in reverse order to only store the last value.
2752 * From the html form, the backend and the action are at the end.
2753 */
2754 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002755 char *value;
2756 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002757
2758 cur_param--;
2759 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002760 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002761 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002762 poffset = (cur_param != first_param ? 1 : 0);
2763 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2764 if ((plen > 0) && (plen <= sizeof(key))) {
2765 strncpy(key, cur_param + poffset, plen);
2766 key[plen - 1] = '\0';
2767 } else {
2768 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2769 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002770 }
2771
2772 /* Parse the value */
2773 value = key;
2774 while (*value != '\0' && *value != '=') {
2775 value++;
2776 }
2777 if (*value == '=') {
2778 /* Ok, a value is found, we can mark the end of the key */
2779 *value++ = '\0';
2780 }
2781
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002782 if (!url_decode(key) || !url_decode(value))
2783 break;
2784
Cyril Bonté70be45d2010-10-12 00:14:35 +02002785 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002786 if (!px && (strcmp(key, "b") == 0)) {
2787 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2788 /* the backend name is unknown or ambiguous (duplicate names) */
2789 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2790 goto out;
2791 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002792 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002793 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002794 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002795 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002796 }
2797 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002798 action = ST_ADM_ACTION_ENABLE;
2799 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002800 else if (strcmp(value, "stop") == 0) {
2801 action = ST_ADM_ACTION_STOP;
2802 }
2803 else if (strcmp(value, "start") == 0) {
2804 action = ST_ADM_ACTION_START;
2805 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002806 else if (strcmp(value, "shutdown") == 0) {
2807 action = ST_ADM_ACTION_SHUTDOWN;
2808 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002809 else {
2810 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2811 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002812 }
2813 }
2814 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002815 if (!(px && action)) {
2816 /*
2817 * Indicates that we'll need to reprocess the parameters
2818 * as soon as backend and action are known
2819 */
2820 if (!reprocess) {
2821 st_cur_param = cur_param;
2822 st_next_param = next_param;
2823 }
2824 reprocess = 1;
2825 }
2826 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002827 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002828 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002829 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002830 /* Not already in maintenance, we can change the server state */
2831 sv->state |= SRV_MAINTAIN;
2832 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002833 altered_servers++;
2834 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002835 }
2836 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002837 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002838 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002839 /* Already in maintenance, we can change the server state */
2840 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002841 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002842 altered_servers++;
2843 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002844 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002845 break;
2846 case ST_ADM_ACTION_STOP:
2847 case ST_ADM_ACTION_START:
2848 if (action == ST_ADM_ACTION_START)
2849 sv->uweight = sv->iweight;
2850 else
2851 sv->uweight = 0;
2852
2853 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2854 /* we must take care of not pushing the server to full throttle during slow starts */
2855 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2856 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2857 else
2858 sv->eweight = BE_WEIGHT_SCALE;
2859 sv->eweight *= sv->uweight;
2860 } else {
2861 sv->eweight = sv->uweight;
2862 }
2863
2864 /* static LB algorithms are a bit harder to update */
2865 if (px->lbprm.update_server_eweight)
2866 px->lbprm.update_server_eweight(sv);
2867 else if (sv->eweight) {
2868 if (px->lbprm.set_server_status_up)
2869 px->lbprm.set_server_status_up(sv);
2870 }
2871 else {
2872 if (px->lbprm.set_server_status_down)
2873 px->lbprm.set_server_status_down(sv);
2874 }
2875 altered_servers++;
2876 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002878 case ST_ADM_ACTION_SHUTDOWN:
2879 if (px->state != PR_STSTOPPED) {
2880 struct session *sess, *sess_bck;
2881
2882 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2883 if (sess->srv_conn == sv)
2884 session_shutdown(sess, SN_ERR_KILLED);
2885
2886 altered_servers++;
2887 total_servers++;
2888 }
2889 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002890 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002891 } else {
2892 /* the server name is unknown or ambiguous (duplicate names) */
2893 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002894 }
2895 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002896 if (reprocess && px && action) {
2897 /* Now, we know the backend and the action chosen by the user.
2898 * We can safely restart from the first server parameter
2899 * to reprocess them
2900 */
2901 cur_param = st_cur_param;
2902 next_param = st_next_param;
2903 reprocess = 0;
2904 goto reprocess_servers;
2905 }
2906
Cyril Bonté70be45d2010-10-12 00:14:35 +02002907 next_param = cur_param;
2908 }
2909 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002910
2911 if (total_servers == 0) {
2912 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2913 }
2914 else if (altered_servers == 0) {
2915 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2916 }
2917 else if (altered_servers == total_servers) {
2918 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2919 }
2920 else {
2921 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2922 }
2923 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002924 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002925}
2926
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002927/* This function checks whether we need to enable a POST analyser to parse a
2928 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002929 * if it needs to come back again, otherwise non-zero if it finishes. In the
2930 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002931 */
2932int http_handle_stats(struct session *s, struct channel *req)
2933{
2934 struct stats_admin_rule *stats_admin_rule;
2935 struct stream_interface *si = s->rep->prod;
2936 struct http_txn *txn = &s->txn;
2937 struct http_msg *msg = &txn->req;
2938 struct uri_auth *uri = s->be->uri_auth;
2939
2940 /* now check whether we have some admin rules for this request */
2941 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2942 int ret = 1;
2943
2944 if (stats_admin_rule->cond) {
2945 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2946 ret = acl_pass(ret);
2947 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2948 ret = !ret;
2949 }
2950
2951 if (ret) {
2952 /* no rule, or the rule matches */
2953 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2954 break;
2955 }
2956 }
2957
2958 /* Was the status page requested with a POST ? */
2959 if (unlikely(txn->meth == HTTP_METH_POST)) {
2960 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2961 if (msg->msg_state < HTTP_MSG_100_SENT) {
2962 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2963 * send an HTTP/1.1 100 Continue intermediate response.
2964 */
2965 if (msg->flags & HTTP_MSGF_VER_11) {
2966 struct hdr_ctx ctx;
2967 ctx.idx = 0;
2968 /* Expect is allowed in 1.1, look for it */
2969 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2970 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2971 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2972 }
2973 }
2974 msg->msg_state = HTTP_MSG_100_SENT;
2975 s->logs.tv_request = now; /* update the request timer to reflect full request */
2976 }
2977 if (!http_process_req_stat_post(si, txn, req))
2978 return 0; /* we need more data */
2979 }
2980 else
2981 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
2982
2983 /* We don't want to land on the posted stats page because a refresh will
2984 * repost the data. We don't want this to happen on accident so we redirect
2985 * the browse to the stats page with a GET.
2986 */
2987 chunk_printf(&trash,
2988 "HTTP/1.0 303 See Other\r\n"
2989 "Cache-Control: no-cache\r\n"
2990 "Content-Type: text/plain\r\n"
2991 "Connection: close\r\n"
2992 "Location: %s;st=%s\r\n"
2993 "\r\n",
2994 uri->uri_prefix,
2995 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
2996 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
2997 stat_status_codes[si->applet.ctx.stats.st_code]) ?
2998 stat_status_codes[si->applet.ctx.stats.st_code] :
2999 stat_status_codes[STAT_STATUS_UNKN]);
3000
3001 s->txn.status = 303;
3002 s->logs.tv_request = now;
3003 stream_int_retnclose(req->prod, &trash);
3004 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3005
3006 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3007 s->fe->fe_counters.intercepted_req++;
3008
3009 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3010 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3011 if (!(s->flags & SN_FINST_MASK))
3012 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003013 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003014 return 1;
3015 }
3016
3017 /* OK, let's go on now */
3018
3019 chunk_printf(&trash,
3020 "HTTP/1.0 200 OK\r\n"
3021 "Cache-Control: no-cache\r\n"
3022 "Connection: close\r\n"
3023 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003024 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003025
3026 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3027 chunk_appendf(&trash, "Refresh: %d\r\n",
3028 uri->refresh);
3029
3030 chunk_appendf(&trash, "\r\n");
3031
3032 s->txn.status = 200;
3033 s->logs.tv_request = now;
3034
3035 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3036 s->fe->fe_counters.intercepted_req++;
3037
3038 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3039 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3040 if (!(s->flags & SN_FINST_MASK))
3041 s->flags |= SN_FINST_R;
3042
3043 if (s->txn.meth == HTTP_METH_HEAD) {
3044 /* that's all we return in case of HEAD request, so let's immediately close. */
3045 stream_int_retnclose(req->prod, &trash);
3046 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003047 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003048 return 1;
3049 }
3050
3051 /* OK, push the response and hand over to the stats I/O handler */
3052 bi_putchk(s->rep, &trash);
3053
3054 s->task->nice = -32; /* small boost for HTTP statistics */
3055 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3056 s->target = s->rep->prod->conn->target; // for logging only
3057 s->rep->prod->conn->xprt_ctx = s;
3058 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3059 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003060 return 1;
3061}
3062
Willy Tarreau20b0de52012-12-24 15:45:22 +01003063/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003064 * transaction <txn>. Returns the first rule that prevents further processing
3065 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3066 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3067 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003068 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003069static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003070http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003071{
Willy Tarreauff011f22011-01-06 17:51:27 +01003072 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003073 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003074
Willy Tarreauff011f22011-01-06 17:51:27 +01003075 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003076 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003077 continue;
3078
Willy Tarreau96257ec2012-12-27 10:46:37 +01003079 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003081 int ret;
3082
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003083 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003084 ret = acl_pass(ret);
3085
Willy Tarreauff011f22011-01-06 17:51:27 +01003086 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003087 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088
3089 if (!ret) /* condition not matched */
3090 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003091 }
3092
Willy Tarreau20b0de52012-12-24 15:45:22 +01003093
Willy Tarreau96257ec2012-12-27 10:46:37 +01003094 switch (rule->action) {
3095 case HTTP_REQ_ACT_ALLOW:
3096 return NULL; /* "allow" rules are OK */
3097
3098 case HTTP_REQ_ACT_DENY:
3099 txn->flags |= TX_CLDENY;
3100 return rule;
3101
Willy Tarreauccbcc372012-12-27 12:37:57 +01003102 case HTTP_REQ_ACT_TARPIT:
3103 txn->flags |= TX_CLTARPIT;
3104 return rule;
3105
Willy Tarreau96257ec2012-12-27 10:46:37 +01003106 case HTTP_REQ_ACT_AUTH:
3107 return rule;
3108
Willy Tarreau81499eb2012-12-27 12:19:02 +01003109 case HTTP_REQ_ACT_REDIR:
3110 return rule;
3111
Willy Tarreau96257ec2012-12-27 10:46:37 +01003112 case HTTP_REQ_ACT_SET_HDR:
3113 ctx.idx = 0;
3114 /* remove all occurrences of the header */
3115 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3116 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3117 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003118 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003119 /* now fall through to header addition */
3120
3121 case HTTP_REQ_ACT_ADD_HDR:
3122 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3123 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3124 trash.len = rule->arg.hdr_add.name_len;
3125 trash.str[trash.len++] = ':';
3126 trash.str[trash.len++] = ' ';
3127 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3128 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3129 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003130 }
3131 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003132
3133 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003134 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003135}
3136
Willy Tarreau71241ab2012-12-27 11:30:54 +01003137
3138/* Perform an HTTP redirect based on the information in <rule>. The function
3139 * returns non-zero on success, or zero in case of a, irrecoverable error such
3140 * as too large a request to build a valid response.
3141 */
3142static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3143{
3144 struct http_msg *msg = &txn->req;
3145 const char *msg_fmt;
3146
3147 /* build redirect message */
3148 switch(rule->code) {
3149 case 303:
3150 msg_fmt = HTTP_303;
3151 break;
3152 case 301:
3153 msg_fmt = HTTP_301;
3154 break;
3155 case 302:
3156 default:
3157 msg_fmt = HTTP_302;
3158 break;
3159 }
3160
3161 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3162 return 0;
3163
3164 switch(rule->type) {
3165 case REDIRECT_TYPE_SCHEME: {
3166 const char *path;
3167 const char *host;
3168 struct hdr_ctx ctx;
3169 int pathlen;
3170 int hostlen;
3171
3172 host = "";
3173 hostlen = 0;
3174 ctx.idx = 0;
3175 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3176 host = ctx.line + ctx.val;
3177 hostlen = ctx.vlen;
3178 }
3179
3180 path = http_get_path(txn);
3181 /* build message using path */
3182 if (path) {
3183 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3184 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3185 int qs = 0;
3186 while (qs < pathlen) {
3187 if (path[qs] == '?') {
3188 pathlen = qs;
3189 break;
3190 }
3191 qs++;
3192 }
3193 }
3194 } else {
3195 path = "/";
3196 pathlen = 1;
3197 }
3198
3199 /* check if we can add scheme + "://" + host + path */
3200 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3201 return 0;
3202
3203 /* add scheme */
3204 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3205 trash.len += rule->rdr_len;
3206
3207 /* add "://" */
3208 memcpy(trash.str + trash.len, "://", 3);
3209 trash.len += 3;
3210
3211 /* add host */
3212 memcpy(trash.str + trash.len, host, hostlen);
3213 trash.len += hostlen;
3214
3215 /* add path */
3216 memcpy(trash.str + trash.len, path, pathlen);
3217 trash.len += pathlen;
3218
3219 /* append a slash at the end of the location is needed and missing */
3220 if (trash.len && trash.str[trash.len - 1] != '/' &&
3221 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3222 if (trash.len > trash.size - 5)
3223 return 0;
3224 trash.str[trash.len] = '/';
3225 trash.len++;
3226 }
3227
3228 break;
3229 }
3230 case REDIRECT_TYPE_PREFIX: {
3231 const char *path;
3232 int pathlen;
3233
3234 path = http_get_path(txn);
3235 /* build message using path */
3236 if (path) {
3237 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3238 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3239 int qs = 0;
3240 while (qs < pathlen) {
3241 if (path[qs] == '?') {
3242 pathlen = qs;
3243 break;
3244 }
3245 qs++;
3246 }
3247 }
3248 } else {
3249 path = "/";
3250 pathlen = 1;
3251 }
3252
3253 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3254 return 0;
3255
3256 /* add prefix. Note that if prefix == "/", we don't want to
3257 * add anything, otherwise it makes it hard for the user to
3258 * configure a self-redirection.
3259 */
3260 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3261 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3262 trash.len += rule->rdr_len;
3263 }
3264
3265 /* add path */
3266 memcpy(trash.str + trash.len, path, pathlen);
3267 trash.len += pathlen;
3268
3269 /* append a slash at the end of the location is needed and missing */
3270 if (trash.len && trash.str[trash.len - 1] != '/' &&
3271 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3272 if (trash.len > trash.size - 5)
3273 return 0;
3274 trash.str[trash.len] = '/';
3275 trash.len++;
3276 }
3277
3278 break;
3279 }
3280 case REDIRECT_TYPE_LOCATION:
3281 default:
3282 if (trash.len + rule->rdr_len > trash.size - 4)
3283 return 0;
3284
3285 /* add location */
3286 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3287 trash.len += rule->rdr_len;
3288 break;
3289 }
3290
3291 if (rule->cookie_len) {
3292 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3293 trash.len += 14;
3294 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3295 trash.len += rule->cookie_len;
3296 memcpy(trash.str + trash.len, "\r\n", 2);
3297 trash.len += 2;
3298 }
3299
3300 /* add end of headers and the keep-alive/close status.
3301 * We may choose to set keep-alive if the Location begins
3302 * with a slash, because the client will come back to the
3303 * same server.
3304 */
3305 txn->status = rule->code;
3306 /* let's log the request time */
3307 s->logs.tv_request = now;
3308
3309 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3310 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3311 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3312 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3313 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3314 /* keep-alive possible */
3315 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3316 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3317 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3318 trash.len += 30;
3319 } else {
3320 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3321 trash.len += 24;
3322 }
3323 }
3324 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3325 trash.len += 4;
3326 bo_inject(txn->rsp.chn, trash.str, trash.len);
3327 /* "eat" the request */
3328 bi_fast_delete(txn->req.chn->buf, msg->sov);
3329 msg->sov = 0;
3330 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3331 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3332 txn->req.msg_state = HTTP_MSG_CLOSED;
3333 txn->rsp.msg_state = HTTP_MSG_DONE;
3334 } else {
3335 /* keep-alive not possible */
3336 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3337 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3338 trash.len += 29;
3339 } else {
3340 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3341 trash.len += 23;
3342 }
3343 stream_int_retnclose(txn->req.chn->prod, &trash);
3344 txn->req.chn->analysers = 0;
3345 }
3346
3347 if (!(s->flags & SN_ERR_MASK))
3348 s->flags |= SN_ERR_PRXCOND;
3349 if (!(s->flags & SN_FINST_MASK))
3350 s->flags |= SN_FINST_R;
3351
3352 return 1;
3353}
3354
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003355/* This stream analyser runs all HTTP request processing which is common to
3356 * frontends and backends, which means blocking ACLs, filters, connection-close,
3357 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003358 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003359 * either needs more data or wants to immediately abort the request (eg: deny,
3360 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003361 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003362int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003363{
Willy Tarreaud787e662009-07-07 10:14:51 +02003364 struct http_txn *txn = &s->txn;
3365 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003366 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003367 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003368 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003369 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003370 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003371
Willy Tarreau655dce92009-11-08 13:10:58 +01003372 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003373 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003374 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003375 return 0;
3376 }
3377
Willy Tarreau3a816292009-07-07 10:55:49 +02003378 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003379 req->analyse_exp = TICK_ETERNITY;
3380
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003381 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003382 now_ms, __FUNCTION__,
3383 s,
3384 req,
3385 req->rex, req->wex,
3386 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003387 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003388 req->analysers);
3389
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003390 /* first check whether we have some ACLs set to block this request */
3391 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003392 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003393
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003394 ret = acl_pass(ret);
3395 if (cond->pol == ACL_COND_UNLESS)
3396 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003397
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 if (ret) {
3399 txn->status = 403;
3400 /* let's log the request time */
3401 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003402 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003403 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003404 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003405 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003406 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003407
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003408 /* just in case we have some per-backend tracking */
3409 session_inc_be_http_req_ctr(s);
3410
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003411 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003412 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003413
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003414 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003415 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003416 do_stats = stats_check_uri(s->rep->prod, txn, px);
3417 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003418 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003419 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003420 else
3421 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003422
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003423 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003424 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3425 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003426 txn->status = 403;
3427 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003428 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003429 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003430 s->fe->fe_counters.denied_req++;
3431 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3432 s->be->be_counters.denied_req++;
3433 if (s->listener->counters)
3434 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003435 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003436 }
3437 /* When a connection is tarpitted, we use the tarpit timeout,
3438 * which may be the same as the connect timeout if unspecified.
3439 * If unset, then set it to zero because we really want it to
3440 * eventually expire. We build the tarpit as an analyser.
3441 */
3442 if (txn->flags & TX_CLTARPIT) {
3443 channel_erase(s->req);
3444 /* wipe the request out so that we can drop the connection early
3445 * if the client closes first.
3446 */
3447 channel_dont_connect(req);
3448 req->analysers = 0; /* remove switching rules etc... */
3449 req->analysers |= AN_REQ_HTTP_TARPIT;
3450 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3451 if (!req->analyse_exp)
3452 req->analyse_exp = tick_add(now_ms, 0);
3453 session_inc_http_err_ctr(s);
3454 s->fe->fe_counters.denied_req++;
3455 if (s->fe != s->be)
3456 s->be->be_counters.denied_req++;
3457 if (s->listener->counters)
3458 s->listener->counters->denied_req++;
3459 return 1;
3460 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003461 }
3462
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463 /* try headers filters */
3464 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003465 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003466 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003467
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 /* has the request been denied ? */
3469 if (txn->flags & TX_CLDENY) {
3470 /* no need to go further */
3471 txn->status = 403;
3472 /* let's log the request time */
3473 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003474 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003475 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003476 goto return_prx_cond;
3477 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003478
3479 /* When a connection is tarpitted, we use the tarpit timeout,
3480 * which may be the same as the connect timeout if unspecified.
3481 * If unset, then set it to zero because we really want it to
3482 * eventually expire. We build the tarpit as an analyser.
3483 */
3484 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003485 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003486 /* wipe the request out so that we can drop the connection early
3487 * if the client closes first.
3488 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003489 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003490 req->analysers = 0; /* remove switching rules etc... */
3491 req->analysers |= AN_REQ_HTTP_TARPIT;
3492 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3493 if (!req->analyse_exp)
3494 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003495 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003496 return 1;
3497 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 }
Willy Tarreau06619262006-12-17 08:37:22 +01003499
Willy Tarreau5b154472009-12-21 20:11:07 +01003500 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3501 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003502 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3503 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003504 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3505 * avoid to redo the same work if FE and BE have the same settings (common).
3506 * The method consists in checking if options changed between the two calls
3507 * (implying that either one is non-null, or one of them is non-null and we
3508 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003509 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003510
Willy Tarreaudc008c52010-02-01 16:20:08 +01003511 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3512 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3513 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3514 (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 +01003515 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003516
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003517 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3518 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003519 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003520 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3521 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003522 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003523 tmp = TX_CON_WANT_CLO;
3524
Willy Tarreau5b154472009-12-21 20:11:07 +01003525 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003527
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003528 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3529 /* parse the Connection header and possibly clean it */
3530 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003531 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003532 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3533 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003534 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003535 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003536 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003537 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003538 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003539
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003540 /* check if client or config asks for explicit close in KAL/SCL */
3541 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3542 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3543 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003544 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003545 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003546 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003547 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003548 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3549 }
Willy Tarreau78599912009-10-17 20:12:21 +02003550
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003551 /* we can be blocked here because the request needs to be authenticated,
3552 * either to pass or to access stats.
3553 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003554 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003555 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003556
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003557 if (!realm)
3558 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3559
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003560 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003561 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003562 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003563 /* on 401 we still count one error, because normal browsing
3564 * won't significantly increase the counter but brute force
3565 * attempts will.
3566 */
3567 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003568 goto return_prx_cond;
3569 }
3570
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003571 /* add request headers from the rule sets in the same order */
3572 list_for_each_entry(wl, &px->req_add, list) {
3573 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003574 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003575 ret = acl_pass(ret);
3576 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3577 ret = !ret;
3578 if (!ret)
3579 continue;
3580 }
3581
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003582 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003583 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003584 }
3585
3586 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3587 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3588 goto return_bad_req;
3589 req->analyse_exp = TICK_ETERNITY;
3590 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003591 }
3592
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003593 if (unlikely(do_stats)) {
3594 /* process the stats request now */
3595 if (!http_handle_stats(s, req)) {
3596 /* we need more data, let's come back here later */
3597 req->analysers |= an_bit;
3598 channel_dont_connect(req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003599 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003600 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003602
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603 /* check whether we have some ACLs set to redirect this request */
3604 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003605 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003606 int ret;
3607
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003608 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003609 ret = acl_pass(ret);
3610 if (rule->cond->pol == ACL_COND_UNLESS)
3611 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003612 if (!ret)
3613 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003614 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003615 if (!http_apply_redirect_rule(rule, s, txn))
3616 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003617
Willy Tarreau71241ab2012-12-27 11:30:54 +01003618 req->analyse_exp = TICK_ETERNITY;
3619 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003620 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003621
Willy Tarreau2be39392010-01-03 17:24:51 +01003622 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3623 * If this happens, then the data will not come immediately, so we must
3624 * send all what we have without waiting. Note that due to the small gain
3625 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003626 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003627 * itself once used.
3628 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003629 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003630
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003631 /* that's OK for us now, let's move on to next analysers */
3632 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003633
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003634 return_bad_req:
3635 /* We centralize bad requests processing here */
3636 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3637 /* we detected a parsing error. We want to archive this request
3638 * in the dedicated proxy area for later troubleshooting.
3639 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003640 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003641 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003642
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003643 txn->req.msg_state = HTTP_MSG_ERROR;
3644 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003645 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003646
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003647 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003648 if (s->listener->counters)
3649 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003650
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003651 return_prx_cond:
3652 if (!(s->flags & SN_ERR_MASK))
3653 s->flags |= SN_ERR_PRXCOND;
3654 if (!(s->flags & SN_FINST_MASK))
3655 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003656
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003657 req->analysers = 0;
3658 req->analyse_exp = TICK_ETERNITY;
3659 return 0;
3660}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003661
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003662/* This function performs all the processing enabled for the current request.
3663 * It returns 1 if the processing can continue on next analysers, or zero if it
3664 * needs more data, encounters an error, or wants to immediately abort the
3665 * request. It relies on buffers flags, and updates s->req->analysers.
3666 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003667int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003668{
3669 struct http_txn *txn = &s->txn;
3670 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003671
Willy Tarreau655dce92009-11-08 13:10:58 +01003672 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003673 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003674 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003675 return 0;
3676 }
3677
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003678 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003679 now_ms, __FUNCTION__,
3680 s,
3681 req,
3682 req->rex, req->wex,
3683 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003684 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003685 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003686
William Lallemand82fe75c2012-10-23 10:25:10 +02003687 if (s->fe->comp || s->be->comp)
3688 select_compression_request_header(s, req->buf);
3689
Willy Tarreau59234e92008-11-30 23:51:27 +01003690 /*
3691 * Right now, we know that we have processed the entire headers
3692 * and that unwanted requests have been filtered out. We can do
3693 * whatever we want with the remaining request. Also, now we
3694 * may have separate values for ->fe, ->be.
3695 */
Willy Tarreau06619262006-12-17 08:37:22 +01003696
Willy Tarreau59234e92008-11-30 23:51:27 +01003697 /*
3698 * If HTTP PROXY is set we simply get remote server address
3699 * parsing incoming request.
3700 */
3701 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003702 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704
Willy Tarreau59234e92008-11-30 23:51:27 +01003705 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003706 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003707 * Note that doing so might move headers in the request, but
3708 * the fields will stay coherent and the URI will not move.
3709 * This should only be performed in the backend.
3710 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003711 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003712 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3713 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003714
Willy Tarreau59234e92008-11-30 23:51:27 +01003715 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003716 * 8: the appsession cookie was looked up very early in 1.2,
3717 * so let's do the same now.
3718 */
3719
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003720 /* It needs to look into the URI unless persistence must be ignored */
3721 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003722 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003723 }
3724
William Lallemanda73203e2012-03-12 12:48:57 +01003725 /* add unique-id if "header-unique-id" is specified */
3726
3727 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3728 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3729
3730 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003731 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3732 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003733 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003734 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003735 goto return_bad_req;
3736 }
3737
Cyril Bontéb21570a2009-11-29 20:04:48 +01003738 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 * 9: add X-Forwarded-For if either the frontend or the backend
3740 * asks for it.
3741 */
3742 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003743 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003744 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003745 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3746 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003747 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003748 /* The header is set to be added only if none is present
3749 * and we found it, so don't do anything.
3750 */
3751 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003752 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003753 /* Add an X-Forwarded-For header unless the source IP is
3754 * in the 'except' network range.
3755 */
3756 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003757 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003758 != s->fe->except_net.s_addr) &&
3759 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003760 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003762 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003764 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003765
3766 /* Note: we rely on the backend to get the header name to be used for
3767 * x-forwarded-for, because the header is really meant for the backends.
3768 * However, if the backend did not specify any option, we have to rely
3769 * on the frontend's header name.
3770 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 if (s->be->fwdfor_hdr_len) {
3772 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003773 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003774 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003775 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003776 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003777 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003778 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003779
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003780 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003781 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003782 }
3783 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003784 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003785 /* FIXME: for the sake of completeness, we should also support
3786 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003787 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003788 int len;
3789 char pn[INET6_ADDRSTRLEN];
3790 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003791 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003792 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003793
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 /* Note: we rely on the backend to get the header name to be used for
3795 * x-forwarded-for, because the header is really meant for the backends.
3796 * However, if the backend did not specify any option, we have to rely
3797 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003798 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 if (s->be->fwdfor_hdr_len) {
3800 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003801 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 } else {
3803 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003804 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003805 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003806 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003807
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003808 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003809 goto return_bad_req;
3810 }
3811 }
3812
3813 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003814 * 10: add X-Original-To if either the frontend or the backend
3815 * asks for it.
3816 */
3817 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3818
3819 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003820 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003821 /* Add an X-Original-To header unless the destination IP is
3822 * in the 'except' network range.
3823 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003824 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003825
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003826 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003827 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003828 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003829 != s->fe->except_to.s_addr) &&
3830 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003831 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003832 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003833 int len;
3834 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003835 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003836
3837 /* Note: we rely on the backend to get the header name to be used for
3838 * x-original-to, because the header is really meant for the backends.
3839 * However, if the backend did not specify any option, we have to rely
3840 * on the frontend's header name.
3841 */
3842 if (s->be->orgto_hdr_len) {
3843 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003844 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003845 } else {
3846 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003847 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003848 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003849 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003850
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003851 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003852 goto return_bad_req;
3853 }
3854 }
3855 }
3856
Willy Tarreau50fc7772012-11-11 22:19:57 +01003857 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3858 * If an "Upgrade" token is found, the header is left untouched in order not to have
3859 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3860 * "Upgrade" is present in the Connection header.
3861 */
3862 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3863 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3864 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003865 unsigned int want_flags = 0;
3866
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003867 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003868 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3869 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3870 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003871 want_flags |= TX_CON_CLO_SET;
3872 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003873 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3874 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3875 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003876 want_flags |= TX_CON_KAL_SET;
3877 }
3878
3879 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003880 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003881 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003883
Willy Tarreau522d6c02009-12-06 18:49:18 +01003884 /* If we have no server assigned yet and we're balancing on url_param
3885 * with a POST request, we may be interested in checking the body for
3886 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003887 */
3888 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3889 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003890 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003891 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003892 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003893 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003894 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003895
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003896 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003897 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003898#ifdef TCP_QUICKACK
3899 /* We expect some data from the client. Unless we know for sure
3900 * we already have a full request, we have to re-enable quick-ack
3901 * in case we previously disabled it, otherwise we might cause
3902 * the client to delay further data.
3903 */
3904 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003905 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003906 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003907 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003908#endif
3909 }
Willy Tarreau03945942009-12-22 16:50:27 +01003910
Willy Tarreau59234e92008-11-30 23:51:27 +01003911 /*************************************************************
3912 * OK, that's finished for the headers. We have done what we *
3913 * could. Let's switch to the DATA state. *
3914 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003915 req->analyse_exp = TICK_ETERNITY;
3916 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003917
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003918 /* if the server closes the connection, we want to immediately react
3919 * and close the socket to save packets and syscalls.
3920 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003921 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3922 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003923
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003925 /* OK let's go on with the BODY now */
3926 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003927
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003929 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003930 /* we detected a parsing error. We want to archive this request
3931 * in the dedicated proxy area for later troubleshooting.
3932 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003933 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003934 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003935
Willy Tarreau59234e92008-11-30 23:51:27 +01003936 txn->req.msg_state = HTTP_MSG_ERROR;
3937 txn->status = 400;
3938 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003939 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003940
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003941 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003942 if (s->listener->counters)
3943 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003944
Willy Tarreau59234e92008-11-30 23:51:27 +01003945 if (!(s->flags & SN_ERR_MASK))
3946 s->flags |= SN_ERR_PRXCOND;
3947 if (!(s->flags & SN_FINST_MASK))
3948 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003949 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003950}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003951
Willy Tarreau60b85b02008-11-30 23:28:40 +01003952/* This function is an analyser which processes the HTTP tarpit. It always
3953 * returns zero, at the beginning because it prevents any other processing
3954 * from occurring, and at the end because it terminates the request.
3955 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003956int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003957{
3958 struct http_txn *txn = &s->txn;
3959
3960 /* This connection is being tarpitted. The CLIENT side has
3961 * already set the connect expiration date to the right
3962 * timeout. We just have to check that the client is still
3963 * there and that the timeout has not expired.
3964 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003965 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003966 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003967 !tick_is_expired(req->analyse_exp, now_ms))
3968 return 0;
3969
3970 /* We will set the queue timer to the time spent, just for
3971 * logging purposes. We fake a 500 server error, so that the
3972 * attacker will not suspect his connection has been tarpitted.
3973 * It will not cause trouble to the logs because we can exclude
3974 * the tarpitted connections by filtering on the 'PT' status flags.
3975 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003976 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3977
3978 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003979 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003980 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003981
3982 req->analysers = 0;
3983 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003984
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003985 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003986 if (s->listener->counters)
3987 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003988
Willy Tarreau60b85b02008-11-30 23:28:40 +01003989 if (!(s->flags & SN_ERR_MASK))
3990 s->flags |= SN_ERR_PRXCOND;
3991 if (!(s->flags & SN_FINST_MASK))
3992 s->flags |= SN_FINST_T;
3993 return 0;
3994}
3995
Willy Tarreaud34af782008-11-30 23:36:37 +01003996/* This function is an analyser which processes the HTTP request body. It looks
3997 * for parameters to be used for the load balancing algorithm (url_param). It
3998 * must only be called after the standard HTTP request processing has occurred,
3999 * because it expects the request to be parsed. It returns zero if it needs to
4000 * read more data, or 1 once it has completed its analysis.
4001 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004002int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004003{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004004 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004005 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004006 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004007
4008 /* We have to parse the HTTP request body to find any required data.
4009 * "balance url_param check_post" should have been the only way to get
4010 * into this. We were brought here after HTTP header analysis, so all
4011 * related structures are ready.
4012 */
4013
Willy Tarreau522d6c02009-12-06 18:49:18 +01004014 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4015 goto missing_data;
4016
4017 if (msg->msg_state < HTTP_MSG_100_SENT) {
4018 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4019 * send an HTTP/1.1 100 Continue intermediate response.
4020 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004021 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004022 struct hdr_ctx ctx;
4023 ctx.idx = 0;
4024 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004025 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004026 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004027 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004028 }
4029 }
4030 msg->msg_state = HTTP_MSG_100_SENT;
4031 }
4032
4033 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004034 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004035 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004036 * is still null. We must save the body in msg->next because it
4037 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004038 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004039 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004040
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004041 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004042 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4043 else
4044 msg->msg_state = HTTP_MSG_DATA;
4045 }
4046
4047 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004048 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004049 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004050 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004051 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004052 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004053
Willy Tarreau115acb92009-12-26 13:56:06 +01004054 if (!ret)
4055 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004056 else if (ret < 0) {
4057 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004058 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004059 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004060 }
4061
Willy Tarreaud98cf932009-12-27 22:54:55 +01004062 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004063 * We have the first data byte is in msg->sov. We're waiting for at
4064 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004065 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004066
Willy Tarreau124d9912011-03-01 20:30:48 +01004067 if (msg->body_len < limit)
4068 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004069
Willy Tarreau9b28e032012-10-12 23:49:43 +02004070 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 goto http_end;
4072
4073 missing_data:
4074 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004075 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004076 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004077 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004078 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004079
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004080 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004081 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004082 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004083
4084 if (!(s->flags & SN_ERR_MASK))
4085 s->flags |= SN_ERR_CLITO;
4086 if (!(s->flags & SN_FINST_MASK))
4087 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004088 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004089 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004090
4091 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004092 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004093 /* Not enough data. We'll re-use the http-request
4094 * timeout here. Ideally, we should set the timeout
4095 * relative to the accept() date. We just set the
4096 * request timeout once at the beginning of the
4097 * request.
4098 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004099 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004100 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004101 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004102 return 0;
4103 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004104
4105 http_end:
4106 /* The situation will not evolve, so let's give up on the analysis. */
4107 s->logs.tv_request = now; /* update the request timer to reflect full request */
4108 req->analysers &= ~an_bit;
4109 req->analyse_exp = TICK_ETERNITY;
4110 return 1;
4111
4112 return_bad_req: /* let's centralize all bad requests */
4113 txn->req.msg_state = HTTP_MSG_ERROR;
4114 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004115 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116
Willy Tarreau79ebac62010-06-07 13:47:49 +02004117 if (!(s->flags & SN_ERR_MASK))
4118 s->flags |= SN_ERR_PRXCOND;
4119 if (!(s->flags & SN_FINST_MASK))
4120 s->flags |= SN_FINST_R;
4121
Willy Tarreau522d6c02009-12-06 18:49:18 +01004122 return_err_msg:
4123 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004124 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004125 if (s->listener->counters)
4126 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004128}
4129
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004130/* send a server's name with an outgoing request over an established connection.
4131 * Note: this function is designed to be called once the request has been scheduled
4132 * for being forwarded. This is the reason why it rewinds the buffer before
4133 * proceeding.
4134 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004135int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004136
4137 struct hdr_ctx ctx;
4138
Mark Lamourinec2247f02012-01-04 13:02:01 -05004139 char *hdr_name = be->server_id_hdr_name;
4140 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004141 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004142 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004143 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004144
William Lallemandd9e90662012-01-30 17:27:17 +01004145 ctx.idx = 0;
4146
Willy Tarreau9b28e032012-10-12 23:49:43 +02004147 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004148 if (old_o) {
4149 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004150 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004151 }
4152
Willy Tarreau9b28e032012-10-12 23:49:43 +02004153 old_i = chn->buf->i;
4154 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004155 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004156 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004157 }
4158
4159 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004160 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004161 memcpy(hdr_val, hdr_name, hdr_name_len);
4162 hdr_val += hdr_name_len;
4163 *hdr_val++ = ':';
4164 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004165 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4166 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004167
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004168 if (old_o) {
4169 /* If this was a forwarded request, we must readjust the amount of
4170 * data to be forwarded in order to take into account the size
4171 * variations.
4172 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004173 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004174 }
4175
Mark Lamourinec2247f02012-01-04 13:02:01 -05004176 return 0;
4177}
4178
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179/* Terminate current transaction and prepare a new one. This is very tricky
4180 * right now but it works.
4181 */
4182void http_end_txn_clean_session(struct session *s)
4183{
4184 /* FIXME: We need a more portable way of releasing a backend's and a
4185 * server's connections. We need a safer way to reinitialize buffer
4186 * flags. We also need a more accurate method for computing per-request
4187 * data.
4188 */
4189 http_silent_debug(__LINE__, s);
4190
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004191 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004192 si_shutr(s->req->cons);
4193 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194
4195 http_silent_debug(__LINE__, s);
4196
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004197 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004198 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004199 if (unlikely(s->srv_conn))
4200 sess_change_server(s, NULL);
4201 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004202
4203 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4204 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004205 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206
4207 if (s->txn.status) {
4208 int n;
4209
4210 n = s->txn.status / 100;
4211 if (n < 1 || n > 5)
4212 n = 0;
4213
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004214 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004215 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004216 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004217 s->fe->fe_counters.p.http.comp_rsp++;
4218 }
Willy Tarreau24657792010-02-26 10:30:28 +01004219 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004220 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004221 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004222 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004223 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004224 s->be->be_counters.p.http.comp_rsp++;
4225 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 }
4227
4228 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004229 s->logs.bytes_in -= s->req->buf->i;
4230 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231
4232 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004233 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234 !(s->flags & SN_MONITOR) &&
4235 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4236 s->do_log(s);
4237 }
4238
4239 s->logs.accept_date = date; /* user-visible date for logging */
4240 s->logs.tv_accept = now; /* corrected date for internal use */
4241 tv_zero(&s->logs.tv_request);
4242 s->logs.t_queue = -1;
4243 s->logs.t_connect = -1;
4244 s->logs.t_data = -1;
4245 s->logs.t_close = 0;
4246 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4247 s->logs.srv_queue_size = 0; /* we will get this number soon */
4248
Willy Tarreau9b28e032012-10-12 23:49:43 +02004249 s->logs.bytes_in = s->req->total = s->req->buf->i;
4250 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251
4252 if (s->pend_pos)
4253 pendconn_free(s->pend_pos);
4254
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004255 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 if (s->flags & SN_CURR_SESS) {
4257 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004258 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004260 if (may_dequeue_tasks(objt_server(s->target), s->be))
4261 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 }
4263
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004264 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265
4266 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004267 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4268 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004269 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004271 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 s->req->cons->err_loc = NULL;
4273 s->req->cons->exp = TICK_ETERNITY;
4274 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004275 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4276 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004277 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 +01004278 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004279
4280 if (s->flags & SN_COMP_READY)
4281 s->comp_algo->end(&s->comp_ctx);
4282 s->comp_algo = NULL;
4283 s->flags &= ~SN_COMP_READY;
4284
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285 s->txn.meth = 0;
4286 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004287 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004288 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 s->req->cons->flags |= SI_FL_INDEP_STR;
4290
Willy Tarreau96e31212011-05-30 18:10:30 +02004291 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004292 s->req->flags |= CF_NEVER_WAIT;
4293 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004294 }
4295
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 /* if the request buffer is not empty, it means we're
4297 * about to process another request, so send pending
4298 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004299 * Just don't do this if the buffer is close to be full,
4300 * because the request will wait for it to flush a little
4301 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004303 if (s->req->buf->i) {
4304 if (s->rep->buf->o &&
4305 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4306 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004307 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004308 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004309
4310 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004311 channel_auto_read(s->req);
4312 channel_auto_close(s->req);
4313 channel_auto_read(s->rep);
4314 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315
Willy Tarreau342b11c2010-11-24 16:22:09 +01004316 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 s->rep->analysers = 0;
4318
4319 http_silent_debug(__LINE__, s);
4320}
4321
4322
4323/* This function updates the request state machine according to the response
4324 * state machine and buffer flags. It returns 1 if it changes anything (flag
4325 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4326 * it is only used to find when a request/response couple is complete. Both
4327 * this function and its equivalent should loop until both return zero. It
4328 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4329 */
4330int http_sync_req_state(struct session *s)
4331{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004332 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004334 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 unsigned int old_state = txn->req.msg_state;
4336
4337 http_silent_debug(__LINE__, s);
4338 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4339 return 0;
4340
4341 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004342 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004343 * We can shut the read side unless we want to abort_on_close,
4344 * or we have a POST request. The issue with POST requests is
4345 * that some browsers still send a CRLF after the request, and
4346 * this CRLF must be read so that it does not remain in the kernel
4347 * buffers, otherwise a close could cause an RST on some systems
4348 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004349 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004350 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004351 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352
Willy Tarreau40f151a2012-12-20 12:10:09 +01004353 /* if the server closes the connection, we want to immediately react
4354 * and close the socket to save packets and syscalls.
4355 */
4356 chn->cons->flags |= SI_FL_NOHALF;
4357
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4359 goto wait_other_side;
4360
4361 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4362 /* The server has not finished to respond, so we
4363 * don't want to move in order not to upset it.
4364 */
4365 goto wait_other_side;
4366 }
4367
4368 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4369 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004370 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004372 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 goto wait_other_side;
4374 }
4375
4376 /* When we get here, it means that both the request and the
4377 * response have finished receiving. Depending on the connection
4378 * mode, we'll have to wait for the last bytes to leave in either
4379 * direction, and sometimes for a close to be effective.
4380 */
4381
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004382 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4383 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004384 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4385 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004386 }
4387 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4388 /* Option forceclose is set, or either side wants to close,
4389 * let's enforce it now that we're not expecting any new
4390 * data to come. The caller knows the session is complete
4391 * once both states are CLOSED.
4392 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004393 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4394 channel_shutr_now(chn);
4395 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004397 }
4398 else {
4399 /* The last possible modes are keep-alive and tunnel. Since tunnel
4400 * mode does not set the body analyser, we can't reach this place
4401 * in tunnel mode, so we're left with keep-alive only.
4402 * This mode is currently not implemented, we switch to tunnel mode.
4403 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004404 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004405 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004406 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 }
4408
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004409 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004411 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004412
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004413 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414 txn->req.msg_state = HTTP_MSG_CLOSING;
4415 goto http_msg_closing;
4416 }
4417 else {
4418 txn->req.msg_state = HTTP_MSG_CLOSED;
4419 goto http_msg_closed;
4420 }
4421 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004422 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004423 }
4424
4425 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4426 http_msg_closing:
4427 /* nothing else to forward, just waiting for the output buffer
4428 * to be empty and for the shutw_now to take effect.
4429 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004430 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431 txn->req.msg_state = HTTP_MSG_CLOSED;
4432 goto http_msg_closed;
4433 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004434 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 txn->req.msg_state = HTTP_MSG_ERROR;
4436 goto wait_other_side;
4437 }
4438 }
4439
4440 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4441 http_msg_closed:
4442 goto wait_other_side;
4443 }
4444
4445 wait_other_side:
4446 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004447 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448}
4449
4450
4451/* This function updates the response state machine according to the request
4452 * state machine and buffer flags. It returns 1 if it changes anything (flag
4453 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4454 * it is only used to find when a request/response couple is complete. Both
4455 * this function and its equivalent should loop until both return zero. It
4456 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4457 */
4458int http_sync_res_state(struct session *s)
4459{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004460 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004461 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004462 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 unsigned int old_state = txn->rsp.msg_state;
4464
4465 http_silent_debug(__LINE__, s);
4466 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4467 return 0;
4468
4469 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4470 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004471 * still monitor the server connection for a possible close
4472 * while the request is being uploaded, so we don't disable
4473 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004475 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004476
4477 if (txn->req.msg_state == HTTP_MSG_ERROR)
4478 goto wait_other_side;
4479
4480 if (txn->req.msg_state < HTTP_MSG_DONE) {
4481 /* The client seems to still be sending data, probably
4482 * because we got an error response during an upload.
4483 * We have the choice of either breaking the connection
4484 * or letting it pass through. Let's do the later.
4485 */
4486 goto wait_other_side;
4487 }
4488
4489 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4490 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004491 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004493 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 goto wait_other_side;
4495 }
4496
4497 /* When we get here, it means that both the request and the
4498 * response have finished receiving. Depending on the connection
4499 * mode, we'll have to wait for the last bytes to leave in either
4500 * direction, and sometimes for a close to be effective.
4501 */
4502
4503 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4504 /* Server-close mode : shut read and wait for the request
4505 * side to close its output buffer. The caller will detect
4506 * when we're in DONE and the other is in CLOSED and will
4507 * catch that for the final cleanup.
4508 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004509 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4510 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004512 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4513 /* Option forceclose is set, or either side wants to close,
4514 * let's enforce it now that we're not expecting any new
4515 * data to come. The caller knows the session is complete
4516 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004518 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4519 channel_shutr_now(chn);
4520 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004521 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 }
4523 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004524 /* The last possible modes are keep-alive and tunnel. Since tunnel
4525 * mode does not set the body analyser, we can't reach this place
4526 * in tunnel mode, so we're left with keep-alive only.
4527 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004528 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004529 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004531 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 }
4533
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004534 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004536 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4538 goto http_msg_closing;
4539 }
4540 else {
4541 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4542 goto http_msg_closed;
4543 }
4544 }
4545 goto wait_other_side;
4546 }
4547
4548 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4549 http_msg_closing:
4550 /* nothing else to forward, just waiting for the output buffer
4551 * to be empty and for the shutw_now to take effect.
4552 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004553 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4555 goto http_msg_closed;
4556 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004557 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004558 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004559 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004560 if (objt_server(s->target))
4561 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 goto wait_other_side;
4563 }
4564 }
4565
4566 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4567 http_msg_closed:
4568 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004569 bi_erase(chn);
4570 channel_auto_close(chn);
4571 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 goto wait_other_side;
4573 }
4574
4575 wait_other_side:
4576 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004577 /* We force the response to leave immediately if we're waiting for the
4578 * other side, since there is no pending shutdown to push it out.
4579 */
4580 if (!channel_is_empty(chn))
4581 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583}
4584
4585
4586/* Resync the request and response state machines. Return 1 if either state
4587 * changes.
4588 */
4589int http_resync_states(struct session *s)
4590{
4591 struct http_txn *txn = &s->txn;
4592 int old_req_state = txn->req.msg_state;
4593 int old_res_state = txn->rsp.msg_state;
4594
4595 http_silent_debug(__LINE__, s);
4596 http_sync_req_state(s);
4597 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004598 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599 if (!http_sync_res_state(s))
4600 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004601 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 if (!http_sync_req_state(s))
4603 break;
4604 }
4605 http_silent_debug(__LINE__, s);
4606 /* OK, both state machines agree on a compatible state.
4607 * There are a few cases we're interested in :
4608 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4609 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4610 * directions, so let's simply disable both analysers.
4611 * - HTTP_MSG_CLOSED on the response only means we must abort the
4612 * request.
4613 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4614 * with server-close mode means we've completed one request and we
4615 * must re-initialize the server connection.
4616 */
4617
4618 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4619 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4620 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4621 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4622 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004623 channel_auto_close(s->req);
4624 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004625 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004626 channel_auto_close(s->rep);
4627 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004628 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004629 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4630 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004631 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004632 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004633 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004634 channel_auto_close(s->rep);
4635 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004636 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004637 channel_abort(s->req);
4638 channel_auto_close(s->req);
4639 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004640 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 }
4642 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4643 txn->rsp.msg_state == HTTP_MSG_DONE &&
4644 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4645 /* server-close: terminate this server connection and
4646 * reinitialize a fresh-new transaction.
4647 */
4648 http_end_txn_clean_session(s);
4649 }
4650
4651 http_silent_debug(__LINE__, s);
4652 return txn->req.msg_state != old_req_state ||
4653 txn->rsp.msg_state != old_res_state;
4654}
4655
Willy Tarreaud98cf932009-12-27 22:54:55 +01004656/* This function is an analyser which forwards request body (including chunk
4657 * sizes if any). It is called as soon as we must forward, even if we forward
4658 * zero byte. The only situation where it must not be called is when we're in
4659 * tunnel mode and we want to forward till the close. It's used both to forward
4660 * remaining data and to resync after end of body. It expects the msg_state to
4661 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4662 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004663 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004664 * bytes of pending data + the headers if not already done (between sol and sov).
4665 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004666 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004667int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004668{
4669 struct http_txn *txn = &s->txn;
4670 struct http_msg *msg = &s->txn.req;
4671
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004672 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4673 return 0;
4674
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004675 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004676 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004677 /* Output closed while we were sending data. We must abort and
4678 * wake the other side up.
4679 */
4680 msg->msg_state = HTTP_MSG_ERROR;
4681 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004682 return 1;
4683 }
4684
Willy Tarreau4fe41902010-06-07 22:27:41 +02004685 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004686 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687
4688 /* Note that we don't have to send 100-continue back because we don't
4689 * need the data to complete our job, and it's up to the server to
4690 * decide whether to return 100, 417 or anything else in return of
4691 * an "Expect: 100-continue" header.
4692 */
4693
4694 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004695 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004696 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004697 * is still null. We must save the body in msg->next because it
4698 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004699 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004700 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004701
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004702 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004703 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004704 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004705 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004706 }
4707
Willy Tarreaud98cf932009-12-27 22:54:55 +01004708 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004709 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004710
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004712 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004713 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004714 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004715 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004716 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004717 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004718 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004719 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004720
Willy Tarreaucaabe412010-01-03 23:08:28 +01004721 if (msg->msg_state == HTTP_MSG_DATA) {
4722 /* must still forward */
4723 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004724 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004725
4726 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004727 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004728 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004730 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004731 }
4732 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004733 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004734 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004735 * TRAILERS state.
4736 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004737 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004738
Willy Tarreau54d23df2012-10-25 19:04:45 +02004739 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004741 else if (ret < 0) {
4742 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004743 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004744 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004745 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004746 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004747 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004748 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004749 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004750 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004751 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004752
4753 if (ret == 0)
4754 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004755 else if (ret < 0) {
4756 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004757 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004758 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004759 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004760 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004761 /* we're in MSG_CHUNK_SIZE now */
4762 }
4763 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004764 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765
4766 if (ret == 0)
4767 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004768 else if (ret < 0) {
4769 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004770 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004771 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004772 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004773 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 /* we're in HTTP_MSG_DONE now */
4775 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004776 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004777 int old_state = msg->msg_state;
4778
Willy Tarreau610ecce2010-01-04 21:15:02 +01004779 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004780 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004781 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4782 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004783 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004784 if (http_resync_states(s)) {
4785 /* some state changes occurred, maybe the analyser
4786 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004787 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004788 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004789 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004790 /* request errors are most likely due to
4791 * the server aborting the transfer.
4792 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004793 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004794 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004795 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004796 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004797 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004798 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004799 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004800 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004801
4802 /* If "option abortonclose" is set on the backend, we
4803 * want to monitor the client's connection and forward
4804 * any shutdown notification to the server, which will
4805 * decide whether to close or to go on processing the
4806 * request.
4807 */
4808 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004809 channel_auto_read(req);
4810 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004811 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004812 else if (s->txn.meth == HTTP_METH_POST) {
4813 /* POST requests may require to read extra CRLF
4814 * sent by broken browsers and which could cause
4815 * an RST to be sent upon close on some systems
4816 * (eg: Linux).
4817 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004818 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004819 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004820
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822 }
4823 }
4824
Willy Tarreaud98cf932009-12-27 22:54:55 +01004825 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004826 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004827 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004828 if (!(s->flags & SN_ERR_MASK))
4829 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004830 if (!(s->flags & SN_FINST_MASK)) {
4831 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4832 s->flags |= SN_FINST_H;
4833 else
4834 s->flags |= SN_FINST_D;
4835 }
4836
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004837 s->fe->fe_counters.cli_aborts++;
4838 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004839 if (objt_server(s->target))
4840 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004841
4842 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004843 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004844
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004845 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004846 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004847 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004848
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004849 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004850 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004851 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004852 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004853 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004854
Willy Tarreau5c620922011-05-11 19:56:11 +02004855 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004856 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004857 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004858 * modes are already handled by the stream sock layer. We must not do
4859 * this in content-length mode because it could present the MSG_MORE
4860 * flag with the last block of forwarded data, which would cause an
4861 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004862 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004863 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004864 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004865
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004867 return 0;
4868
Willy Tarreaud98cf932009-12-27 22:54:55 +01004869 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004870 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004871 if (s->listener->counters)
4872 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004873 return_bad_req_stats_ok:
4874 txn->req.msg_state = HTTP_MSG_ERROR;
4875 if (txn->status) {
4876 /* Note: we don't send any error if some data were already sent */
4877 stream_int_retnclose(req->prod, NULL);
4878 } else {
4879 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004880 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004881 }
4882 req->analysers = 0;
4883 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004884
4885 if (!(s->flags & SN_ERR_MASK))
4886 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004887 if (!(s->flags & SN_FINST_MASK)) {
4888 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4889 s->flags |= SN_FINST_H;
4890 else
4891 s->flags |= SN_FINST_D;
4892 }
4893 return 0;
4894
4895 aborted_xfer:
4896 txn->req.msg_state = HTTP_MSG_ERROR;
4897 if (txn->status) {
4898 /* Note: we don't send any error if some data were already sent */
4899 stream_int_retnclose(req->prod, NULL);
4900 } else {
4901 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004902 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004903 }
4904 req->analysers = 0;
4905 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4906
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004907 s->fe->fe_counters.srv_aborts++;
4908 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004909 if (objt_server(s->target))
4910 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004911
4912 if (!(s->flags & SN_ERR_MASK))
4913 s->flags |= SN_ERR_SRVCL;
4914 if (!(s->flags & SN_FINST_MASK)) {
4915 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4916 s->flags |= SN_FINST_H;
4917 else
4918 s->flags |= SN_FINST_D;
4919 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004920 return 0;
4921}
4922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004923/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4924 * processing can continue on next analysers, or zero if it either needs more
4925 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4926 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4927 * when it has nothing left to do, and may remove any analyser when it wants to
4928 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004929 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004930int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004931{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004932 struct http_txn *txn = &s->txn;
4933 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004934 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004935 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004936 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004937 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004938
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004939 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004940 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004941 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004942 rep,
4943 rep->rex, rep->wex,
4944 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004945 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004946 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004947
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948 /*
4949 * Now parse the partial (or complete) lines.
4950 * We will check the response syntax, and also join multi-line
4951 * headers. An index of all the lines will be elaborated while
4952 * parsing.
4953 *
4954 * For the parsing, we use a 28 states FSM.
4955 *
4956 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004957 * rep->buf->p = beginning of response
4958 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4959 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004960 * msg->eol = end of current header or line (LF or CRLF)
4961 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004962 */
4963
Willy Tarreau83e3af02009-12-28 17:39:57 +01004964 /* There's a protected area at the end of the buffer for rewriting
4965 * purposes. We don't want to start to parse the request if the
4966 * protected area is affected, because we may have to move processed
4967 * data later, which is much more complicated.
4968 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004969 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004970 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004971 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4972 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4973 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004974 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004975 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004976 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004977 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004978 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004979 return 0;
4980 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004981 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4982 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004983 }
4984
Willy Tarreau9b28e032012-10-12 23:49:43 +02004985 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004986 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004987 }
4988
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004989 /* 1: we might have to print this header in debug mode */
4990 if (unlikely((global.mode & MODE_DEBUG) &&
4991 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004992 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004994
Willy Tarreau9b28e032012-10-12 23:49:43 +02004995 sol = rep->buf->p;
4996 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004997 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 sol += hdr_idx_first_pos(&txn->hdr_idx);
5000 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005002 while (cur_idx) {
5003 eol = sol + txn->hdr_idx.v[cur_idx].len;
5004 debug_hdr("srvhdr", s, sol, eol);
5005 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5006 cur_idx = txn->hdr_idx.v[cur_idx].next;
5007 }
5008 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005009
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005010 /*
5011 * Now we quickly check if we have found a full valid response.
5012 * If not so, we check the FD and buffer states before leaving.
5013 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005014 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 * responses are checked first.
5016 *
5017 * Depending on whether the client is still there or not, we
5018 * may send an error response back or not. Note that normally
5019 * we should only check for HTTP status there, and check I/O
5020 * errors somewhere else.
5021 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005022
Willy Tarreau655dce92009-11-08 13:10:58 +01005023 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005024 /* Invalid response */
5025 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5026 /* we detected a parsing error. We want to archive this response
5027 * in the dedicated proxy area for later troubleshooting.
5028 */
5029 hdr_response_bad:
5030 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005031 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005032
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005033 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005034 if (objt_server(s->target)) {
5035 objt_server(s->target)->counters.failed_resp++;
5036 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005037 }
Willy Tarreau64648412010-03-05 10:41:54 +01005038 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005039 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005040 rep->analysers = 0;
5041 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005042 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005043 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005044 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005045
5046 if (!(s->flags & SN_ERR_MASK))
5047 s->flags |= SN_ERR_PRXCOND;
5048 if (!(s->flags & SN_FINST_MASK))
5049 s->flags |= SN_FINST_H;
5050
5051 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005052 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005053
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005055 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005056 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005057 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058 goto hdr_response_bad;
5059 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005060
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005062 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005064 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005065
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005066 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005067 if (objt_server(s->target)) {
5068 objt_server(s->target)->counters.failed_resp++;
5069 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005070 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005071
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005072 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 rep->analysers = 0;
5074 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005075 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005076 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005077 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 if (!(s->flags & SN_ERR_MASK))
5080 s->flags |= SN_ERR_SRVCL;
5081 if (!(s->flags & SN_FINST_MASK))
5082 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005083 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005085
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005086 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005087 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005088 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005089 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005090
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005091 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005092 if (objt_server(s->target)) {
5093 objt_server(s->target)->counters.failed_resp++;
5094 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005095 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005096
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005097 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 rep->analysers = 0;
5099 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005100 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005101 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005102 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005104 if (!(s->flags & SN_ERR_MASK))
5105 s->flags |= SN_ERR_SRVTO;
5106 if (!(s->flags & SN_FINST_MASK))
5107 s->flags |= SN_FINST_H;
5108 return 0;
5109 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005110
Willy Tarreauf003d372012-11-26 13:35:37 +01005111 /* client abort with an abortonclose */
5112 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5113 s->fe->fe_counters.cli_aborts++;
5114 s->be->be_counters.cli_aborts++;
5115 if (objt_server(s->target))
5116 objt_server(s->target)->counters.cli_aborts++;
5117
5118 rep->analysers = 0;
5119 channel_auto_close(rep);
5120
5121 txn->status = 400;
5122 bi_erase(rep);
5123 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5124
5125 if (!(s->flags & SN_ERR_MASK))
5126 s->flags |= SN_ERR_CLICL;
5127 if (!(s->flags & SN_FINST_MASK))
5128 s->flags |= SN_FINST_H;
5129
5130 /* process_session() will take care of the error */
5131 return 0;
5132 }
5133
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005134 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005135 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005136 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005137 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005138
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005139 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005140 if (objt_server(s->target)) {
5141 objt_server(s->target)->counters.failed_resp++;
5142 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005143 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005144
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005145 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 rep->analysers = 0;
5147 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005148 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005149 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005150 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005151
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005152 if (!(s->flags & SN_ERR_MASK))
5153 s->flags |= SN_ERR_SRVCL;
5154 if (!(s->flags & SN_FINST_MASK))
5155 s->flags |= SN_FINST_H;
5156 return 0;
5157 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005158
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005160 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005162 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005163
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005164 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005165 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005166 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005167
5168 if (!(s->flags & SN_ERR_MASK))
5169 s->flags |= SN_ERR_CLICL;
5170 if (!(s->flags & SN_FINST_MASK))
5171 s->flags |= SN_FINST_H;
5172
5173 /* process_session() will take care of the error */
5174 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005175 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005176
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005177 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 return 0;
5179 }
5180
5181 /* More interesting part now : we know that we have a complete
5182 * response which at least looks like HTTP. We have an indicator
5183 * of each header's length, so we can parse them quickly.
5184 */
5185
5186 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005187 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 /*
5190 * 1: get the status code
5191 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005192 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 if (n < 1 || n > 5)
5194 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005195 /* when the client triggers a 4xx from the server, it's most often due
5196 * to a missing object or permission. These events should be tracked
5197 * because if they happen often, it may indicate a brute force or a
5198 * vulnerability scan.
5199 */
5200 if (n == 4)
5201 session_inc_http_err_ctr(s);
5202
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005203 if (objt_server(s->target))
5204 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005205
Willy Tarreau5b154472009-12-21 20:11:07 +01005206 /* check if the response is HTTP/1.1 or above */
5207 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005208 ((rep->buf->p[5] > '1') ||
5209 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005210 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005211
5212 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005213 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005214
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005215 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005216 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005217
Willy Tarreau9b28e032012-10-12 23:49:43 +02005218 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219
Willy Tarreau39650402010-03-15 19:44:39 +01005220 /* Adjust server's health based on status code. Note: status codes 501
5221 * and 505 are triggered on demand by client request, so we must not
5222 * count them as server failures.
5223 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005224 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005225 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005226 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005227 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005228 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005229 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005230
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231 /*
5232 * 2: check for cacheability.
5233 */
5234
5235 switch (txn->status) {
5236 case 200:
5237 case 203:
5238 case 206:
5239 case 300:
5240 case 301:
5241 case 410:
5242 /* RFC2616 @13.4:
5243 * "A response received with a status code of
5244 * 200, 203, 206, 300, 301 or 410 MAY be stored
5245 * by a cache (...) unless a cache-control
5246 * directive prohibits caching."
5247 *
5248 * RFC2616 @9.5: POST method :
5249 * "Responses to this method are not cacheable,
5250 * unless the response includes appropriate
5251 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005252 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005254 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5256 break;
5257 default:
5258 break;
5259 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005260
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005261 /*
5262 * 3: we may need to capture headers
5263 */
5264 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005265 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005266 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 txn->rsp.cap, s->fe->rsp_cap);
5268
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005269 /* 4: determine the transfer-length.
5270 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5271 * the presence of a message-body in a RESPONSE and its transfer length
5272 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005273 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005274 * All responses to the HEAD request method MUST NOT include a
5275 * message-body, even though the presence of entity-header fields
5276 * might lead one to believe they do. All 1xx (informational), 204
5277 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5278 * message-body. All other responses do include a message-body,
5279 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005280 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005281 * 1. Any response which "MUST NOT" include a message-body (such as the
5282 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5283 * always terminated by the first empty line after the header fields,
5284 * regardless of the entity-header fields present in the message.
5285 *
5286 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5287 * the "chunked" transfer-coding (Section 6.2) is used, the
5288 * transfer-length is defined by the use of this transfer-coding.
5289 * If a Transfer-Encoding header field is present and the "chunked"
5290 * transfer-coding is not present, the transfer-length is defined by
5291 * the sender closing the connection.
5292 *
5293 * 3. If a Content-Length header field is present, its decimal value in
5294 * OCTETs represents both the entity-length and the transfer-length.
5295 * If a message is received with both a Transfer-Encoding header
5296 * field and a Content-Length header field, the latter MUST be ignored.
5297 *
5298 * 4. If the message uses the media type "multipart/byteranges", and
5299 * the transfer-length is not otherwise specified, then this self-
5300 * delimiting media type defines the transfer-length. This media
5301 * type MUST NOT be used unless the sender knows that the recipient
5302 * can parse it; the presence in a request of a Range header with
5303 * multiple byte-range specifiers from a 1.1 client implies that the
5304 * client can parse multipart/byteranges responses.
5305 *
5306 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005307 */
5308
5309 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005310 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005311 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005312 * FIXME: should we parse anyway and return an error on chunked encoding ?
5313 */
5314 if (txn->meth == HTTP_METH_HEAD ||
5315 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005316 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005317 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005318 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005319 goto skip_content_length;
5320 }
5321
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005322 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005323 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005324 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005325 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005326 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005327 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5328 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005329 /* bad transfer-encoding (chunked followed by something else) */
5330 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005331 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005332 break;
5333 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005334 }
5335
5336 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5337 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005338 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005339 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005340 signed long long cl;
5341
Willy Tarreauad14f752011-09-02 20:33:27 +02005342 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005343 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005344 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005345 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005346
Willy Tarreauad14f752011-09-02 20:33:27 +02005347 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005348 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005349 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005350 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005351
Willy Tarreauad14f752011-09-02 20:33:27 +02005352 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005353 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005354 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005355 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005356
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005357 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005358 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005359 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005360 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005361
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005362 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005363 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005364 }
5365
William Lallemand82fe75c2012-10-23 10:25:10 +02005366 if (s->fe->comp || s->be->comp)
5367 select_compression_response_header(s, rep->buf);
5368
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005369 /* FIXME: we should also implement the multipart/byterange method.
5370 * For now on, we resort to close mode in this case (unknown length).
5371 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005372skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005373
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374 /* end of job, return OK */
5375 rep->analysers &= ~an_bit;
5376 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005377 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005378 return 1;
5379}
5380
5381/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005382 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5383 * and updates t->rep->analysers. It might make sense to explode it into several
5384 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005385 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005386int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387{
5388 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 struct http_msg *msg = &txn->rsp;
5390 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005391 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005392
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005393 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394 now_ms, __FUNCTION__,
5395 t,
5396 rep,
5397 rep->rex, rep->wex,
5398 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005399 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005400 rep->analysers);
5401
Willy Tarreau655dce92009-11-08 13:10:58 +01005402 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403 return 0;
5404
5405 rep->analysers &= ~an_bit;
5406 rep->analyse_exp = TICK_ETERNITY;
5407
Willy Tarreau5b154472009-12-21 20:11:07 +01005408 /* Now we have to check if we need to modify the Connection header.
5409 * This is more difficult on the response than it is on the request,
5410 * because we can have two different HTTP versions and we don't know
5411 * how the client will interprete a response. For instance, let's say
5412 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5413 * HTTP/1.1 response without any header. Maybe it will bound itself to
5414 * HTTP/1.0 because it only knows about it, and will consider the lack
5415 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5416 * the lack of header as a keep-alive. Thus we will use two flags
5417 * indicating how a request MAY be understood by the client. In case
5418 * of multiple possibilities, we'll fix the header to be explicit. If
5419 * ambiguous cases such as both close and keepalive are seen, then we
5420 * will fall back to explicit close. Note that we won't take risks with
5421 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005422 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005423 */
5424
Willy Tarreaudc008c52010-02-01 16:20:08 +01005425 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5426 txn->status == 101)) {
5427 /* Either we've established an explicit tunnel, or we're
5428 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005429 * to understand the next protocols. We have to switch to tunnel
5430 * mode, so that we transfer the request and responses then let
5431 * this protocol pass unmodified. When we later implement specific
5432 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005433 * header which contains information about that protocol for
5434 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005435 */
5436 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5437 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005438 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5439 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5440 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005441 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005442
Willy Tarreau60466522010-01-18 19:08:45 +01005443 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005444 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005445 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5446 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005447
Willy Tarreau60466522010-01-18 19:08:45 +01005448 /* now adjust header transformations depending on current state */
5449 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5450 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5451 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005452 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005453 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005454 }
Willy Tarreau60466522010-01-18 19:08:45 +01005455 else { /* SCL / KAL */
5456 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005457 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005458 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005459 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005460
Willy Tarreau60466522010-01-18 19:08:45 +01005461 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005462 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005463
Willy Tarreau60466522010-01-18 19:08:45 +01005464 /* Some keep-alive responses are converted to Server-close if
5465 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005466 */
Willy Tarreau60466522010-01-18 19:08:45 +01005467 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5468 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005469 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005470 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005471 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005472 }
5473
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005474 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005475 /*
5476 * 3: we will have to evaluate the filters.
5477 * As opposed to version 1.2, now they will be evaluated in the
5478 * filters order and not in the header order. This means that
5479 * each filter has to be validated among all headers.
5480 *
5481 * Filters are tried with ->be first, then with ->fe if it is
5482 * different from ->be.
5483 */
5484
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005485 cur_proxy = t->be;
5486 while (1) {
5487 struct proxy *rule_set = cur_proxy;
5488
5489 /* try headers filters */
5490 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005491 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005492 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005493 if (objt_server(t->target)) {
5494 objt_server(t->target)->counters.failed_resp++;
5495 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005496 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005497 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005498 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005499 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005500 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005501 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005502 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005503 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005504 if (!(t->flags & SN_ERR_MASK))
5505 t->flags |= SN_ERR_PRXCOND;
5506 if (!(t->flags & SN_FINST_MASK))
5507 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005508 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005509 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005510 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005511
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005512 /* has the response been denied ? */
5513 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005514 if (objt_server(t->target))
5515 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005516
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005517 t->be->be_counters.denied_resp++;
5518 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005519 if (t->listener->counters)
5520 t->listener->counters->denied_resp++;
5521
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005522 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005523 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005524
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005525 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005526 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005527 if (txn->status < 200)
5528 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005529 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005530 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005531 ret = acl_pass(ret);
5532 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5533 ret = !ret;
5534 if (!ret)
5535 continue;
5536 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005537 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005538 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005539 }
5540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 /* check whether we're already working on the frontend */
5542 if (cur_proxy == t->fe)
5543 break;
5544 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005545 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005546
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005547 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005548 * We may be facing a 100-continue response, in which case this
5549 * is not the right response, and we're waiting for the next one.
5550 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005551 * next one.
5552 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005553 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005554 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005555 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005556 msg->msg_state = HTTP_MSG_RPBEFORE;
5557 txn->status = 0;
5558 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5559 return 1;
5560 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005561 else if (unlikely(txn->status < 200))
5562 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005563
5564 /* we don't have any 1xx status code now */
5565
5566 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005567 * 4: check for server cookie.
5568 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005569 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5570 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005571 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005572
Willy Tarreaubaaee002006-06-26 02:48:02 +02005573
Willy Tarreaua15645d2007-03-18 16:22:39 +01005574 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005575 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005576 */
Willy Tarreau67402132012-05-31 20:40:20 +02005577 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005578 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005579
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005580 /*
5581 * 6: add server cookie in the response if needed
5582 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005583 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005584 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005585 (!(t->flags & SN_DIRECT) ||
5586 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5587 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5588 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5589 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005590 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005591 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005592 /* the server is known, it's not the one the client requested, or the
5593 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005594 * insert a set-cookie here, except if we want to insert only on POST
5595 * requests and this one isn't. Note that servers which don't have cookies
5596 * (eg: some backup servers) will return a full cookie removal request.
5597 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005598 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005600 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5601 t->be->cookie_name);
5602 }
5603 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005604 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005605
5606 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5607 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005608 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5609 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5610 trash.len += 5;
5611
Willy Tarreauef4f3912010-10-07 21:00:29 +02005612 if (t->be->cookie_maxlife) {
5613 /* emit first_date, which is either the original one or
5614 * the current date.
5615 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005616 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005617 s30tob64(txn->cookie_first_date ?
5618 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005619 (date.tv_sec+3) >> 2, trash.str + trash.len);
5620 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005621 }
5622 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005623 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005624 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005625
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005626 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005627 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005628
Willy Tarreau4992dd22012-05-31 21:02:17 +02005629 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005630 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005631
5632 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005633 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005634
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005635 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005636 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005637
Willy Tarreauf1348312010-10-07 15:54:11 +02005638 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005639 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005640 /* the server did not change, only the date was updated */
5641 txn->flags |= TX_SCK_UPDATED;
5642 else
5643 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005644
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005645 /* Here, we will tell an eventual cache on the client side that we don't
5646 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5647 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5648 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5649 */
Willy Tarreau67402132012-05-31 20:40:20 +02005650 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005651
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005652 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5653
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005654 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005655 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005656 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005657 }
5658 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005659
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005660 /*
5661 * 7: check if result will be cacheable with a cookie.
5662 * We'll block the response if security checks have caught
5663 * nasty things such as a cacheable cookie.
5664 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005665 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5666 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005667 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005668
5669 /* we're in presence of a cacheable response containing
5670 * a set-cookie header. We'll block it as requested by
5671 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005672 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005673 if (objt_server(t->target))
5674 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005675
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005676 t->be->be_counters.denied_resp++;
5677 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005678 if (t->listener->counters)
5679 t->listener->counters->denied_resp++;
5680
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005681 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005682 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005683 send_log(t->be, LOG_ALERT,
5684 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005685 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005686 goto return_srv_prx_502;
5687 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005688
5689 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005690 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005691 * If an "Upgrade" token is found, the header is left untouched in order
5692 * not to have to deal with some client bugs : some of them fail an upgrade
5693 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005695 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5696 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5697 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005698 unsigned int want_flags = 0;
5699
5700 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5701 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5702 /* we want a keep-alive response here. Keep-alive header
5703 * required if either side is not 1.1.
5704 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005705 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005706 want_flags |= TX_CON_KAL_SET;
5707 }
5708 else {
5709 /* we want a close response here. Close header required if
5710 * the server is 1.1, regardless of the client.
5711 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005712 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005713 want_flags |= TX_CON_CLO_SET;
5714 }
5715
5716 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005717 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005718 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005719
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005720 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005721 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005722 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005723 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005724
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005725 /*************************************************************
5726 * OK, that's finished for the headers. We have done what we *
5727 * could. Let's switch to the DATA state. *
5728 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005732 /* if the user wants to log as soon as possible, without counting
5733 * bytes from the server, then this is the right moment. We have
5734 * to temporarily assign bytes_out to log what we currently have.
5735 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005736 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5738 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005739 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740 t->logs.bytes_out = 0;
5741 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005742
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005743 /* Note: we must not try to cheat by jumping directly to DATA,
5744 * otherwise we would not let the client side wake up.
5745 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005746
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005747 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005749 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005750}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005751
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752/* This function is an analyser which forwards response body (including chunk
5753 * sizes if any). It is called as soon as we must forward, even if we forward
5754 * zero byte. The only situation where it must not be called is when we're in
5755 * tunnel mode and we want to forward till the close. It's used both to forward
5756 * remaining data and to resync after end of body. It expects the msg_state to
5757 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5758 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005759 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005760 * bytes of pending data + the headers if not already done (between sol and sov).
5761 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005762 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005763int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005764{
5765 struct http_txn *txn = &s->txn;
5766 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005767 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005768 static struct buffer *tmpbuf = NULL;
5769 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005770 int consumed_data = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005771
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005772 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5773 return 0;
5774
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005775 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005776 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005777 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005778 /* Output closed while we were sending data. We must abort and
5779 * wake the other side up.
5780 */
5781 msg->msg_state = HTTP_MSG_ERROR;
5782 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005783 return 1;
5784 }
5785
Willy Tarreau4fe41902010-06-07 22:27:41 +02005786 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005787 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005788
William Lallemand82fe75c2012-10-23 10:25:10 +02005789 /* this is the first time we need the compression buffer */
5790 if (s->comp_algo != NULL && tmpbuf == NULL) {
5791 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5792 goto aborted_xfer; /* no memory */
5793 }
5794
Willy Tarreaud98cf932009-12-27 22:54:55 +01005795 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005796 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005797 * rep->buf.p still points to the beginning of the message and msg->sol
5798 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005799 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005800 channel_forward(res, msg->sov);
5801 msg->next = 0;
5802 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005803
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005804 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005805 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005806 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005807 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005808 }
5809
William Lallemand82fe75c2012-10-23 10:25:10 +02005810 if (s->comp_algo != NULL) {
5811 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5812 if (ret < 0)
5813 goto missing_data; /* not enough spaces in buffers */
5814 compressing = 1;
5815 }
5816
Willy Tarreaud98cf932009-12-27 22:54:55 +01005817 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005818 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005819 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005820 if (s->comp_algo == NULL) {
5821 bytes = msg->sov - msg->sol;
5822 if (msg->chunk_len || bytes) {
5823 msg->sol = msg->sov;
5824 msg->next -= bytes; /* will be forwarded */
5825 msg->chunk_len += bytes;
5826 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5827 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005828 }
5829
Willy Tarreaucaabe412010-01-03 23:08:28 +01005830 if (msg->msg_state == HTTP_MSG_DATA) {
William Lallemandbf3ae612012-11-19 12:35:37 +01005831 int ret;
5832
Willy Tarreaucaabe412010-01-03 23:08:28 +01005833 /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005834 if (compressing) {
5835 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5836 if (ret < 0)
5837 goto aborted_xfer;
5838 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005839
5840 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005841 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005842
5843 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005844 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005845 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005846 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005847 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005848 if (compressing && consumed_data) {
5849 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5850 compressing = 0;
5851 }
5852 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005853 }
5854 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005855 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005856 * set ->sov and ->next to point to the body and switch to DATA or
5857 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005858 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005859 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005860
Willy Tarreau54d23df2012-10-25 19:04:45 +02005861 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005862 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005863 else if (ret < 0) {
5864 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005865 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005866 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005867 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005868 if (compressing) {
5869 if (likely(msg->chunk_len > 0)) {
5870 /* skipping data if we are in compression mode */
5871 b_adv(res->buf, msg->next);
5872 msg->next = 0;
5873 msg->sov = 0;
5874 msg->sol = 0;
5875 } else {
5876 if (consumed_data) {
5877 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5878 compressing = 0;
5879 }
5880 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005881 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005882 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005883 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005884 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005885 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005886 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005887
Willy Tarreau54d23df2012-10-25 19:04:45 +02005888 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005889 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005890 else if (ret < 0) {
5891 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005892 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005893 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005894 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005895 /* skipping data in buffer for compression */
5896 if (compressing) {
5897 b_adv(res->buf, msg->next);
5898 msg->next = 0;
5899 msg->sov = 0;
5900 msg->sol = 0;
5901 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005902 /* we're in MSG_CHUNK_SIZE now */
5903 }
5904 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005905 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005906
Willy Tarreaud98cf932009-12-27 22:54:55 +01005907 if (ret == 0)
5908 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005909 else if (ret < 0) {
5910 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005911 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005912 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005913 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005914 if (s->comp_algo != NULL) {
5915 /* forwarding trailers */
5916 channel_forward(res, msg->next);
5917 msg->next = 0;
5918 }
5919 /* we're in HTTP_MSG_DONE now */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005920 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005921 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005922 int old_state = msg->msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005923 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005924 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005925 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005927 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005928 if (http_resync_states(s)) {
5929 http_silent_debug(__LINE__, s);
5930 /* some state changes occurred, maybe the analyser
5931 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005932 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005933 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005934 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005935 /* response errors are most likely due to
5936 * the client aborting the transfer.
5937 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005938 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005939 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005940 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005941 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005942 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005943 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005944 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005945 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005946 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005947 }
5948 }
5949
Willy Tarreaud98cf932009-12-27 22:54:55 +01005950 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005951 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005952 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5953 compressing = 0;
5954 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005955
5956 if (res->flags & CF_SHUTW)
5957 goto aborted_xfer;
5958
5959 /* stop waiting for data if the input is closed before the end. If the
5960 * client side was already closed, it means that the client has aborted,
5961 * so we don't want to count this as a server abort. Otherwise it's a
5962 * server abort.
5963 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005964 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005965 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5966 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005967 if (!(s->flags & SN_ERR_MASK))
5968 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005969 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005970 if (objt_server(s->target))
5971 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005972 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005973 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005974
Willy Tarreau40dba092010-03-04 18:14:51 +01005975 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005976 if (!s->req->analysers)
5977 goto return_bad_res;
5978
Willy Tarreauea953162012-05-18 23:41:28 +02005979 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005980 if (s->comp_algo == NULL) {
5981 bytes = msg->sov - msg->sol;
5982 if (msg->chunk_len || bytes) {
5983 msg->sol = msg->sov;
5984 msg->next -= bytes; /* will be forwarded */
5985 msg->chunk_len += bytes;
5986 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5987 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005988 }
5989
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005990 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005991 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005992 * Similarly, with keep-alive on the client side, we don't want to forward a
5993 * close.
5994 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005995 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005996 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5997 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005998 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005999
Willy Tarreau5c620922011-05-11 19:56:11 +02006000 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006001 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006002 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006003 * modes are already handled by the stream sock layer. We must not do
6004 * this in content-length mode because it could present the MSG_MORE
6005 * flag with the last block of forwarded data, which would cause an
6006 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006007 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006008 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006009 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006010
Willy Tarreaud98cf932009-12-27 22:54:55 +01006011 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006012 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006013 return 0;
6014
Willy Tarreau40dba092010-03-04 18:14:51 +01006015 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006016 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006017 if (objt_server(s->target))
6018 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006019
6020 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006021 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006022 /* don't send any error message as we're in the body */
6023 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006024 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006025 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006026 if (objt_server(s->target))
6027 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006028
6029 if (!(s->flags & SN_ERR_MASK))
6030 s->flags |= SN_ERR_PRXCOND;
6031 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006032 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006033 return 0;
6034
6035 aborted_xfer:
6036 txn->rsp.msg_state = HTTP_MSG_ERROR;
6037 /* don't send any error message as we're in the body */
6038 stream_int_retnclose(res->cons, NULL);
6039 res->analysers = 0;
6040 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6041
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006042 s->fe->fe_counters.cli_aborts++;
6043 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006044 if (objt_server(s->target))
6045 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006046
6047 if (!(s->flags & SN_ERR_MASK))
6048 s->flags |= SN_ERR_CLICL;
6049 if (!(s->flags & SN_FINST_MASK))
6050 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006051 return 0;
6052}
6053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054/* Iterate the same filter through all request headers.
6055 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 * Since it can manage the switch to another backend, it updates the per-proxy
6057 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006059int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006061 char term;
6062 char *cur_ptr, *cur_end, *cur_next;
6063 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006064 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006065 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006066 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006068 last_hdr = 0;
6069
Willy Tarreau9b28e032012-10-12 23:49:43 +02006070 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 old_idx = 0;
6072
6073 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006074 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006076 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 (exp->action == ACT_ALLOW ||
6078 exp->action == ACT_DENY ||
6079 exp->action == ACT_TARPIT))
6080 return 0;
6081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006082 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 if (!cur_idx)
6084 break;
6085
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006086 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006087 cur_ptr = cur_next;
6088 cur_end = cur_ptr + cur_hdr->len;
6089 cur_next = cur_end + cur_hdr->cr + 1;
6090
6091 /* Now we have one header between cur_ptr and cur_end,
6092 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093 */
6094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 /* The annoying part is that pattern matching needs
6096 * that we modify the contents to null-terminate all
6097 * strings before testing them.
6098 */
6099
6100 term = *cur_end;
6101 *cur_end = '\0';
6102
6103 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6104 switch (exp->action) {
6105 case ACT_SETBE:
6106 /* It is not possible to jump a second time.
6107 * FIXME: should we return an HTTP/500 here so that
6108 * the admin knows there's a problem ?
6109 */
6110 if (t->be != t->fe)
6111 break;
6112
6113 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006114 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006115 last_hdr = 1;
6116 break;
6117
6118 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006119 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006120 last_hdr = 1;
6121 break;
6122
6123 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006124 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006125 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006126
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006127 t->fe->fe_counters.denied_req++;
6128 if (t->fe != t->be)
6129 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006130 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006131 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 break;
6134
6135 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006136 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006137 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006138
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006139 t->fe->fe_counters.denied_req++;
6140 if (t->fe != t->be)
6141 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006142 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006143 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006145 break;
6146
6147 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006148 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6149 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006150 /* FIXME: if the user adds a newline in the replacement, the
6151 * index will not be recalculated for now, and the new line
6152 * will not be counted as a new header.
6153 */
6154
6155 cur_end += delta;
6156 cur_next += delta;
6157 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006158 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159 break;
6160
6161 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006162 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 cur_next += delta;
6164
Willy Tarreaufa355d42009-11-29 18:12:29 +01006165 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006166 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6167 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006168 cur_hdr->len = 0;
6169 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006170 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006171 break;
6172
6173 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006174 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175 if (cur_end)
6176 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006178 /* keep the link from this header to next one in case of later
6179 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006180 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006181 old_idx = cur_idx;
6182 }
6183 return 0;
6184}
6185
6186
6187/* Apply the filter to the request line.
6188 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6189 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006190 * Since it can manage the switch to another backend, it updates the per-proxy
6191 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006192 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006193int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006194{
6195 char term;
6196 char *cur_ptr, *cur_end;
6197 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006198 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006199 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006200
Willy Tarreau3d300592007-03-18 18:34:41 +01006201 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006203 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006204 (exp->action == ACT_ALLOW ||
6205 exp->action == ACT_DENY ||
6206 exp->action == ACT_TARPIT))
6207 return 0;
6208 else if (exp->action == ACT_REMOVE)
6209 return 0;
6210
6211 done = 0;
6212
Willy Tarreau9b28e032012-10-12 23:49:43 +02006213 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006214 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006215
6216 /* Now we have the request line between cur_ptr and cur_end */
6217
6218 /* The annoying part is that pattern matching needs
6219 * that we modify the contents to null-terminate all
6220 * strings before testing them.
6221 */
6222
6223 term = *cur_end;
6224 *cur_end = '\0';
6225
6226 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6227 switch (exp->action) {
6228 case ACT_SETBE:
6229 /* It is not possible to jump a second time.
6230 * FIXME: should we return an HTTP/500 here so that
6231 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006232 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006233 if (t->be != t->fe)
6234 break;
6235
6236 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006237 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006238 done = 1;
6239 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006241 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006242 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006243 done = 1;
6244 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006246 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006247 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006248
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006249 t->fe->fe_counters.denied_req++;
6250 if (t->fe != t->be)
6251 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006252 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006253 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 done = 1;
6256 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006259 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006260
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006261 t->fe->fe_counters.denied_req++;
6262 if (t->fe != t->be)
6263 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006264 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006265 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006267 done = 1;
6268 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006270 case ACT_REPLACE:
6271 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006272 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6273 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006274 /* FIXME: if the user adds a newline in the replacement, the
6275 * index will not be recalculated for now, and the new line
6276 * will not be counted as a new header.
6277 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006278
Willy Tarreaufa355d42009-11-29 18:12:29 +01006279 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006280 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006281 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 HTTP_MSG_RQMETH,
6283 cur_ptr, cur_end + 1,
6284 NULL, NULL);
6285 if (unlikely(!cur_end))
6286 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006287
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 /* we have a full request and we know that we have either a CR
6289 * or an LF at <ptr>.
6290 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006291 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6292 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006293 /* there is no point trying this regex on headers */
6294 return 1;
6295 }
6296 }
6297 *cur_end = term; /* restore the string terminator */
6298 return done;
6299}
Willy Tarreau97de6242006-12-27 17:18:38 +01006300
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006303/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006304 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006306 * unparsable request. Since it can manage the switch to another backend, it
6307 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006308 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006309int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006311 struct http_txn *txn = &s->txn;
6312 struct hdr_exp *exp;
6313
6314 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006315 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 /*
6318 * The interleaving of transformations and verdicts
6319 * makes it difficult to decide to continue or stop
6320 * the evaluation.
6321 */
6322
Willy Tarreau6c123b12010-01-28 20:22:06 +01006323 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6324 break;
6325
Willy Tarreau3d300592007-03-18 18:34:41 +01006326 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006327 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006328 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006329 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006330
6331 /* if this filter had a condition, evaluate it now and skip to
6332 * next filter if the condition does not match.
6333 */
6334 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006335 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006336 ret = acl_pass(ret);
6337 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6338 ret = !ret;
6339
6340 if (!ret)
6341 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 }
6343
6344 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006345 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006346 if (unlikely(ret < 0))
6347 return -1;
6348
6349 if (likely(ret == 0)) {
6350 /* The filter did not match the request, it can be
6351 * iterated through all headers.
6352 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006353 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006356 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006357}
6358
6359
Willy Tarreaua15645d2007-03-18 16:22:39 +01006360
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006362 * Try to retrieve the server associated to the appsession.
6363 * If the server is found, it's assigned to the session.
6364 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006365void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006366 struct http_txn *txn = &t->txn;
6367 appsess *asession = NULL;
6368 char *sessid_temp = NULL;
6369
Cyril Bontéb21570a2009-11-29 20:04:48 +01006370 if (len > t->be->appsession_len) {
6371 len = t->be->appsession_len;
6372 }
6373
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006374 if (t->be->options2 & PR_O2_AS_REQL) {
6375 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006376 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006377 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006378 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006379 }
6380
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006381 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006382 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6383 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6384 return;
6385 }
6386
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006387 memcpy(txn->sessid, buf, len);
6388 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006389 }
6390
6391 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6392 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6393 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6394 return;
6395 }
6396
Cyril Bontéb21570a2009-11-29 20:04:48 +01006397 memcpy(sessid_temp, buf, len);
6398 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006399
6400 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6401 /* free previously allocated memory */
6402 pool_free2(apools.sessid, sessid_temp);
6403
6404 if (asession != NULL) {
6405 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6406 if (!(t->be->options2 & PR_O2_AS_REQL))
6407 asession->request_count++;
6408
6409 if (asession->serverid != NULL) {
6410 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006411
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006412 while (srv) {
6413 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006414 if ((srv->state & SRV_RUNNING) ||
6415 (t->be->options & PR_O_PERSIST) ||
6416 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006417 /* we found the server and it's usable */
6418 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006419 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006420 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006421 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006422
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006423 break;
6424 } else {
6425 txn->flags &= ~TX_CK_MASK;
6426 txn->flags |= TX_CK_DOWN;
6427 }
6428 }
6429 srv = srv->next;
6430 }
6431 }
6432 }
6433}
6434
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435/* Find the end of a cookie value contained between <s> and <e>. It works the
6436 * same way as with headers above except that the semi-colon also ends a token.
6437 * See RFC2965 for more information. Note that it requires a valid header to
6438 * return a valid result.
6439 */
6440char *find_cookie_value_end(char *s, const char *e)
6441{
6442 int quoted, qdpair;
6443
6444 quoted = qdpair = 0;
6445 for (; s < e; s++) {
6446 if (qdpair) qdpair = 0;
6447 else if (quoted) {
6448 if (*s == '\\') qdpair = 1;
6449 else if (*s == '"') quoted = 0;
6450 }
6451 else if (*s == '"') quoted = 1;
6452 else if (*s == ',' || *s == ';') return s;
6453 }
6454 return s;
6455}
6456
6457/* Delete a value in a header between delimiters <from> and <next> in buffer
6458 * <buf>. The number of characters displaced is returned, and the pointer to
6459 * the first delimiter is updated if required. The function tries as much as
6460 * possible to respect the following principles :
6461 * - replace <from> delimiter by the <next> one unless <from> points to a
6462 * colon, in which case <next> is simply removed
6463 * - set exactly one space character after the new first delimiter, unless
6464 * there are not enough characters in the block being moved to do so.
6465 * - remove unneeded spaces before the previous delimiter and after the new
6466 * one.
6467 *
6468 * It is the caller's responsibility to ensure that :
6469 * - <from> points to a valid delimiter or the colon ;
6470 * - <next> points to a valid delimiter or the final CR/LF ;
6471 * - there are non-space chars before <from> ;
6472 * - there is a CR/LF at or after <next>.
6473 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006474int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475{
6476 char *prev = *from;
6477
6478 if (*prev == ':') {
6479 /* We're removing the first value, preserve the colon and add a
6480 * space if possible.
6481 */
6482 if (!http_is_crlf[(unsigned char)*next])
6483 next++;
6484 prev++;
6485 if (prev < next)
6486 *prev++ = ' ';
6487
6488 while (http_is_spht[(unsigned char)*next])
6489 next++;
6490 } else {
6491 /* Remove useless spaces before the old delimiter. */
6492 while (http_is_spht[(unsigned char)*(prev-1)])
6493 prev--;
6494 *from = prev;
6495
6496 /* copy the delimiter and if possible a space if we're
6497 * not at the end of the line.
6498 */
6499 if (!http_is_crlf[(unsigned char)*next]) {
6500 *prev++ = *next++;
6501 if (prev + 1 < next)
6502 *prev++ = ' ';
6503 while (http_is_spht[(unsigned char)*next])
6504 next++;
6505 }
6506 }
6507 return buffer_replace2(buf, prev, next, NULL, 0);
6508}
6509
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006510/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006511 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006512 * desirable to call it only when needed. This code is quite complex because
6513 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6514 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006515 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006516void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006517{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006518 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006519 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006521 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6522 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006525 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006526 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006527
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006528 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006529 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006530 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006531
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006532 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006533 hdr_beg = hdr_next;
6534 hdr_end = hdr_beg + cur_hdr->len;
6535 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 /* We have one full header between hdr_beg and hdr_end, and the
6538 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539 * "Cookie:" headers.
6540 */
6541
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006542 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006543 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544 old_idx = cur_idx;
6545 continue;
6546 }
6547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 del_from = NULL; /* nothing to be deleted */
6549 preserve_hdr = 0; /* assume we may kill the whole header */
6550
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551 /* Now look for cookies. Conforming to RFC2109, we have to support
6552 * attributes whose name begin with a '$', and associate them with
6553 * the right cookie, if we want to delete this cookie.
6554 * So there are 3 cases for each cookie read :
6555 * 1) it's a special attribute, beginning with a '$' : ignore it.
6556 * 2) it's a server id cookie that we *MAY* want to delete : save
6557 * some pointers on it (last semi-colon, beginning of cookie...)
6558 * 3) it's an application cookie : we *MAY* have to delete a previous
6559 * "special" cookie.
6560 * At the end of loop, if a "special" cookie remains, we may have to
6561 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 * *MUST* delete it.
6563 *
6564 * Note: RFC2965 is unclear about the processing of spaces around
6565 * the equal sign in the ATTR=VALUE form. A careful inspection of
6566 * the RFC explicitly allows spaces before it, and not within the
6567 * tokens (attrs or values). An inspection of RFC2109 allows that
6568 * too but section 10.1.3 lets one think that spaces may be allowed
6569 * after the equal sign too, resulting in some (rare) buggy
6570 * implementations trying to do that. So let's do what servers do.
6571 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6572 * allowed quoted strings in values, with any possible character
6573 * after a backslash, including control chars and delimitors, which
6574 * causes parsing to become ambiguous. Browsers also allow spaces
6575 * within values even without quotes.
6576 *
6577 * We have to keep multiple pointers in order to support cookie
6578 * removal at the beginning, middle or end of header without
6579 * corrupting the header. All of these headers are valid :
6580 *
6581 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6582 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6583 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6584 * | | | | | | | | |
6585 * | | | | | | | | hdr_end <--+
6586 * | | | | | | | +--> next
6587 * | | | | | | +----> val_end
6588 * | | | | | +-----------> val_beg
6589 * | | | | +--------------> equal
6590 * | | | +----------------> att_end
6591 * | | +---------------------> att_beg
6592 * | +--------------------------> prev
6593 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006594 */
6595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6597 /* Iterate through all cookies on this line */
6598
6599 /* find att_beg */
6600 att_beg = prev + 1;
6601 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6602 att_beg++;
6603
6604 /* find att_end : this is the first character after the last non
6605 * space before the equal. It may be equal to hdr_end.
6606 */
6607 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006609 while (equal < hdr_end) {
6610 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 if (http_is_spht[(unsigned char)*equal++])
6613 continue;
6614 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615 }
6616
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6618 * is between <att_beg> and <equal>, both may be identical.
6619 */
6620
6621 /* look for end of cookie if there is an equal sign */
6622 if (equal < hdr_end && *equal == '=') {
6623 /* look for the beginning of the value */
6624 val_beg = equal + 1;
6625 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6626 val_beg++;
6627
6628 /* find the end of the value, respecting quotes */
6629 next = find_cookie_value_end(val_beg, hdr_end);
6630
6631 /* make val_end point to the first white space or delimitor after the value */
6632 val_end = next;
6633 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6634 val_end--;
6635 } else {
6636 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006637 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 /* We have nothing to do with attributes beginning with '$'. However,
6640 * they will automatically be removed if a header before them is removed,
6641 * since they're supposed to be linked together.
6642 */
6643 if (*att_beg == '$')
6644 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 /* Ignore cookies with no equal sign */
6647 if (equal == next) {
6648 /* This is not our cookie, so we must preserve it. But if we already
6649 * scheduled another cookie for removal, we cannot remove the
6650 * complete header, but we can remove the previous block itself.
6651 */
6652 preserve_hdr = 1;
6653 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006654 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006655 val_end += delta;
6656 next += delta;
6657 hdr_end += delta;
6658 hdr_next += delta;
6659 cur_hdr->len += delta;
6660 http_msg_move_end(&txn->req, delta);
6661 prev = del_from;
6662 del_from = NULL;
6663 }
6664 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006666
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006667 /* if there are spaces around the equal sign, we need to
6668 * strip them otherwise we'll get trouble for cookie captures,
6669 * or even for rewrites. Since this happens extremely rarely,
6670 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006671 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6673 int stripped_before = 0;
6674 int stripped_after = 0;
6675
6676 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006677 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 equal += stripped_before;
6679 val_beg += stripped_before;
6680 }
6681
6682 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006683 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006684 val_beg += stripped_after;
6685 stripped_before += stripped_after;
6686 }
6687
6688 val_end += stripped_before;
6689 next += stripped_before;
6690 hdr_end += stripped_before;
6691 hdr_next += stripped_before;
6692 cur_hdr->len += stripped_before;
6693 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006694 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697 /* First, let's see if we want to capture this cookie. We check
6698 * that we don't already have a client side cookie, because we
6699 * can only capture one. Also as an optimisation, we ignore
6700 * cookies shorter than the declared name.
6701 */
6702 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6703 (val_end - att_beg >= t->fe->capture_namelen) &&
6704 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6705 int log_len = val_end - att_beg;
6706
6707 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6708 Alert("HTTP logging : out of memory.\n");
6709 } else {
6710 if (log_len > t->fe->capture_len)
6711 log_len = t->fe->capture_len;
6712 memcpy(txn->cli_cookie, att_beg, log_len);
6713 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716
Willy Tarreaubca99692010-10-06 19:25:55 +02006717 /* Persistence cookies in passive, rewrite or insert mode have the
6718 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006720 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006722 * For cookies in prefix mode, the form is :
6723 *
6724 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006725 */
6726 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6727 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6728 struct server *srv = t->be->srv;
6729 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006731 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6732 * have the server ID between val_beg and delim, and the original cookie between
6733 * delim+1 and val_end. Otherwise, delim==val_end :
6734 *
6735 * Cookie: NAME=SRV; # in all but prefix modes
6736 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6737 * | || || | |+-> next
6738 * | || || | +--> val_end
6739 * | || || +---------> delim
6740 * | || |+------------> val_beg
6741 * | || +-------------> att_end = equal
6742 * | |+-----------------> att_beg
6743 * | +------------------> prev
6744 * +-------------------------> hdr_beg
6745 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746
Willy Tarreau67402132012-05-31 20:40:20 +02006747 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 for (delim = val_beg; delim < val_end; delim++)
6749 if (*delim == COOKIE_DELIM)
6750 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006751 } else {
6752 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006753 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006754 /* Now check if the cookie contains a date field, which would
6755 * appear after a vertical bar ('|') just after the server name
6756 * and before the delimiter.
6757 */
6758 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6759 if (vbar1) {
6760 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006761 * right is the last seen date. It is a base64 encoded
6762 * 30-bit value representing the UNIX date since the
6763 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006764 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006765 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006766 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006767 if (val_end - vbar1 >= 5) {
6768 val = b64tos30(vbar1);
6769 if (val > 0)
6770 txn->cookie_last_date = val << 2;
6771 }
6772 /* look for a second vertical bar */
6773 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6774 if (vbar1 && (val_end - vbar1 > 5)) {
6775 val = b64tos30(vbar1 + 1);
6776 if (val > 0)
6777 txn->cookie_first_date = val << 2;
6778 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006779 }
6780 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006781
Willy Tarreauf64d1412010-10-07 20:06:11 +02006782 /* if the cookie has an expiration date and the proxy wants to check
6783 * it, then we do that now. We first check if the cookie is too old,
6784 * then only if it has expired. We detect strict overflow because the
6785 * time resolution here is not great (4 seconds). Cookies with dates
6786 * in the future are ignored if their offset is beyond one day. This
6787 * allows an admin to fix timezone issues without expiring everyone
6788 * and at the same time avoids keeping unwanted side effects for too
6789 * long.
6790 */
6791 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006792 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6793 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006794 txn->flags &= ~TX_CK_MASK;
6795 txn->flags |= TX_CK_OLD;
6796 delim = val_beg; // let's pretend we have not found the cookie
6797 txn->cookie_first_date = 0;
6798 txn->cookie_last_date = 0;
6799 }
6800 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006801 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6802 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006803 txn->flags &= ~TX_CK_MASK;
6804 txn->flags |= TX_CK_EXPIRED;
6805 delim = val_beg; // let's pretend we have not found the cookie
6806 txn->cookie_first_date = 0;
6807 txn->cookie_last_date = 0;
6808 }
6809
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006810 /* Here, we'll look for the first running server which supports the cookie.
6811 * This allows to share a same cookie between several servers, for example
6812 * to dedicate backup servers to specific servers only.
6813 * However, to prevent clients from sticking to cookie-less backup server
6814 * when they have incidentely learned an empty cookie, we simply ignore
6815 * empty cookies and mark them as invalid.
6816 * The same behaviour is applied when persistence must be ignored.
6817 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006818 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 while (srv) {
6822 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6823 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6824 if ((srv->state & SRV_RUNNING) ||
6825 (t->be->options & PR_O_PERSIST) ||
6826 (t->flags & SN_FORCE_PRST)) {
6827 /* we found the server and we can use it */
6828 txn->flags &= ~TX_CK_MASK;
6829 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6830 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006831 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 break;
6833 } else {
6834 /* we found a server, but it's down,
6835 * mark it as such and go on in case
6836 * another one is available.
6837 */
6838 txn->flags &= ~TX_CK_MASK;
6839 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006841 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006842 srv = srv->next;
6843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006844
Willy Tarreauf64d1412010-10-07 20:06:11 +02006845 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006846 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006848 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6849 txn->flags |= TX_CK_UNUSED;
6850 else
6851 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006852 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006853
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854 /* depending on the cookie mode, we may have to either :
6855 * - delete the complete cookie if we're in insert+indirect mode, so that
6856 * the server never sees it ;
6857 * - remove the server id from the cookie value, and tag the cookie as an
6858 * application cookie so that it does not get accidentely removed later,
6859 * if we're in cookie prefix mode
6860 */
Willy Tarreau67402132012-05-31 20:40:20 +02006861 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863
Willy Tarreau9b28e032012-10-12 23:49:43 +02006864 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 val_end += delta;
6866 next += delta;
6867 hdr_end += delta;
6868 hdr_next += delta;
6869 cur_hdr->len += delta;
6870 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 del_from = NULL;
6873 preserve_hdr = 1; /* we want to keep this cookie */
6874 }
6875 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006876 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 del_from = prev;
6878 }
6879 } else {
6880 /* This is not our cookie, so we must preserve it. But if we already
6881 * scheduled another cookie for removal, we cannot remove the
6882 * complete header, but we can remove the previous block itself.
6883 */
6884 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006885
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006887 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006888 if (att_beg >= del_from)
6889 att_beg += delta;
6890 if (att_end >= del_from)
6891 att_end += delta;
6892 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 val_end += delta;
6894 next += delta;
6895 hdr_end += delta;
6896 hdr_next += delta;
6897 cur_hdr->len += delta;
6898 http_msg_move_end(&txn->req, delta);
6899 prev = del_from;
6900 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006901 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006903
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 /* Look for the appsession cookie unless persistence must be ignored */
6905 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6906 int cmp_len, value_len;
6907 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006908
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 if (t->be->options2 & PR_O2_AS_PFX) {
6910 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6911 value_begin = att_beg + t->be->appsession_name_len;
6912 value_len = val_end - att_beg - t->be->appsession_name_len;
6913 } else {
6914 cmp_len = att_end - att_beg;
6915 value_begin = val_beg;
6916 value_len = val_end - val_beg;
6917 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 /* let's see if the cookie is our appcookie */
6920 if (cmp_len == t->be->appsession_name_len &&
6921 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6922 manage_client_side_appsession(t, value_begin, value_len);
6923 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006924 }
6925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* continue with next cookie on this header line */
6927 att_beg = next;
6928 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 /* There are no more cookies on this line.
6931 * We may still have one (or several) marked for deletion at the
6932 * end of the line. We must do this now in two ways :
6933 * - if some cookies must be preserved, we only delete from the
6934 * mark to the end of line ;
6935 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006936 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006938 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006940 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006942 cur_hdr->len += delta;
6943 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006944 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945
6946 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006947 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6948 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006949 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006950 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006951 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006953 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006954 }
6955
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006957 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006959}
6960
6961
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962/* Iterate the same filter through all response headers contained in <rtr>.
6963 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6964 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006965int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966{
6967 char term;
6968 char *cur_ptr, *cur_end, *cur_next;
6969 int cur_idx, old_idx, last_hdr;
6970 struct http_txn *txn = &t->txn;
6971 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006972 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973
6974 last_hdr = 0;
6975
Willy Tarreau9b28e032012-10-12 23:49:43 +02006976 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977 old_idx = 0;
6978
6979 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006980 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006982 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983 (exp->action == ACT_ALLOW ||
6984 exp->action == ACT_DENY))
6985 return 0;
6986
6987 cur_idx = txn->hdr_idx.v[old_idx].next;
6988 if (!cur_idx)
6989 break;
6990
6991 cur_hdr = &txn->hdr_idx.v[cur_idx];
6992 cur_ptr = cur_next;
6993 cur_end = cur_ptr + cur_hdr->len;
6994 cur_next = cur_end + cur_hdr->cr + 1;
6995
6996 /* Now we have one header between cur_ptr and cur_end,
6997 * and the next header starts at cur_next.
6998 */
6999
7000 /* The annoying part is that pattern matching needs
7001 * that we modify the contents to null-terminate all
7002 * strings before testing them.
7003 */
7004
7005 term = *cur_end;
7006 *cur_end = '\0';
7007
7008 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7009 switch (exp->action) {
7010 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007011 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012 last_hdr = 1;
7013 break;
7014
7015 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007016 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017 last_hdr = 1;
7018 break;
7019
7020 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007021 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7022 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 /* FIXME: if the user adds a newline in the replacement, the
7024 * index will not be recalculated for now, and the new line
7025 * will not be counted as a new header.
7026 */
7027
7028 cur_end += delta;
7029 cur_next += delta;
7030 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007031 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032 break;
7033
7034 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007035 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 cur_next += delta;
7037
Willy Tarreaufa355d42009-11-29 18:12:29 +01007038 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007039 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7040 txn->hdr_idx.used--;
7041 cur_hdr->len = 0;
7042 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007043 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 break;
7045
7046 }
7047 }
7048 if (cur_end)
7049 *cur_end = term; /* restore the string terminator */
7050
7051 /* keep the link from this header to next one in case of later
7052 * removal of next header.
7053 */
7054 old_idx = cur_idx;
7055 }
7056 return 0;
7057}
7058
7059
7060/* Apply the filter to the status line in the response buffer <rtr>.
7061 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7062 * or -1 if a replacement resulted in an invalid status line.
7063 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007064int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065{
7066 char term;
7067 char *cur_ptr, *cur_end;
7068 int done;
7069 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007070 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071
7072
Willy Tarreau3d300592007-03-18 18:34:41 +01007073 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007074 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007075 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 (exp->action == ACT_ALLOW ||
7077 exp->action == ACT_DENY))
7078 return 0;
7079 else if (exp->action == ACT_REMOVE)
7080 return 0;
7081
7082 done = 0;
7083
Willy Tarreau9b28e032012-10-12 23:49:43 +02007084 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007085 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086
7087 /* Now we have the status line between cur_ptr and cur_end */
7088
7089 /* The annoying part is that pattern matching needs
7090 * that we modify the contents to null-terminate all
7091 * strings before testing them.
7092 */
7093
7094 term = *cur_end;
7095 *cur_end = '\0';
7096
7097 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7098 switch (exp->action) {
7099 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007100 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 done = 1;
7102 break;
7103
7104 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007105 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 done = 1;
7107 break;
7108
7109 case ACT_REPLACE:
7110 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007111 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7112 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113 /* FIXME: if the user adds a newline in the replacement, the
7114 * index will not be recalculated for now, and the new line
7115 * will not be counted as a new header.
7116 */
7117
Willy Tarreaufa355d42009-11-29 18:12:29 +01007118 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007120 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007121 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122 cur_ptr, cur_end + 1,
7123 NULL, NULL);
7124 if (unlikely(!cur_end))
7125 return -1;
7126
7127 /* we have a full respnse and we know that we have either a CR
7128 * or an LF at <ptr>.
7129 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007130 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007131 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 /* there is no point trying this regex on headers */
7133 return 1;
7134 }
7135 }
7136 *cur_end = term; /* restore the string terminator */
7137 return done;
7138}
7139
7140
7141
7142/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007143 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7145 * unparsable response.
7146 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007147int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007149 struct http_txn *txn = &s->txn;
7150 struct hdr_exp *exp;
7151
7152 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 int ret;
7154
7155 /*
7156 * The interleaving of transformations and verdicts
7157 * makes it difficult to decide to continue or stop
7158 * the evaluation.
7159 */
7160
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007161 if (txn->flags & TX_SVDENY)
7162 break;
7163
Willy Tarreau3d300592007-03-18 18:34:41 +01007164 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7166 exp->action == ACT_PASS)) {
7167 exp = exp->next;
7168 continue;
7169 }
7170
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007171 /* if this filter had a condition, evaluate it now and skip to
7172 * next filter if the condition does not match.
7173 */
7174 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007175 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007176 ret = acl_pass(ret);
7177 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7178 ret = !ret;
7179 if (!ret)
7180 continue;
7181 }
7182
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007184 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 if (unlikely(ret < 0))
7186 return -1;
7187
7188 if (likely(ret == 0)) {
7189 /* The filter did not match the response, it can be
7190 * iterated through all headers.
7191 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007192 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 }
7195 return 0;
7196}
7197
7198
Willy Tarreaua15645d2007-03-18 16:22:39 +01007199/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007200 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007201 * desirable to call it only when needed. This function is also used when we
7202 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007204void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205{
7206 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007207 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007208 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007210 char *hdr_beg, *hdr_end, *hdr_next;
7211 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213 /* Iterate through the headers.
7214 * we start with the start line.
7215 */
7216 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007217 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218
7219 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7220 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007221 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222
7223 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007224 hdr_beg = hdr_next;
7225 hdr_end = hdr_beg + cur_hdr->len;
7226 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227
Willy Tarreau24581ba2010-08-31 22:39:35 +02007228 /* We have one full header between hdr_beg and hdr_end, and the
7229 * next header starts at hdr_next. We're only interested in
7230 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 */
7232
Willy Tarreau24581ba2010-08-31 22:39:35 +02007233 is_cookie2 = 0;
7234 prev = hdr_beg + 10;
7235 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007236 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007237 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7238 if (!val) {
7239 old_idx = cur_idx;
7240 continue;
7241 }
7242 is_cookie2 = 1;
7243 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 }
7245
Willy Tarreau24581ba2010-08-31 22:39:35 +02007246 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7247 * <prev> points to the colon.
7248 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007249 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250
Willy Tarreau24581ba2010-08-31 22:39:35 +02007251 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7252 * check-cache is enabled) and we are not interested in checking
7253 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007254 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007255 if (t->be->cookie_name == NULL &&
7256 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007257 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258 return;
7259
Willy Tarreau24581ba2010-08-31 22:39:35 +02007260 /* OK so now we know we have to process this response cookie.
7261 * The format of the Set-Cookie header is slightly different
7262 * from the format of the Cookie header in that it does not
7263 * support the comma as a cookie delimiter (thus the header
7264 * cannot be folded) because the Expires attribute described in
7265 * the original Netscape's spec may contain an unquoted date
7266 * with a comma inside. We have to live with this because
7267 * many browsers don't support Max-Age and some browsers don't
7268 * support quoted strings. However the Set-Cookie2 header is
7269 * clean.
7270 *
7271 * We have to keep multiple pointers in order to support cookie
7272 * removal at the beginning, middle or end of header without
7273 * corrupting the header (in case of set-cookie2). A special
7274 * pointer, <scav> points to the beginning of the set-cookie-av
7275 * fields after the first semi-colon. The <next> pointer points
7276 * either to the end of line (set-cookie) or next unquoted comma
7277 * (set-cookie2). All of these headers are valid :
7278 *
7279 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7280 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7281 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7282 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7283 * | | | | | | | | | |
7284 * | | | | | | | | +-> next hdr_end <--+
7285 * | | | | | | | +------------> scav
7286 * | | | | | | +--------------> val_end
7287 * | | | | | +--------------------> val_beg
7288 * | | | | +----------------------> equal
7289 * | | | +------------------------> att_end
7290 * | | +----------------------------> att_beg
7291 * | +------------------------------> prev
7292 * +-----------------------------------------> hdr_beg
7293 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294
Willy Tarreau24581ba2010-08-31 22:39:35 +02007295 for (; prev < hdr_end; prev = next) {
7296 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297
Willy Tarreau24581ba2010-08-31 22:39:35 +02007298 /* find att_beg */
7299 att_beg = prev + 1;
7300 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7301 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302
Willy Tarreau24581ba2010-08-31 22:39:35 +02007303 /* find att_end : this is the first character after the last non
7304 * space before the equal. It may be equal to hdr_end.
7305 */
7306 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307
Willy Tarreau24581ba2010-08-31 22:39:35 +02007308 while (equal < hdr_end) {
7309 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7310 break;
7311 if (http_is_spht[(unsigned char)*equal++])
7312 continue;
7313 att_end = equal;
7314 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315
Willy Tarreau24581ba2010-08-31 22:39:35 +02007316 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7317 * is between <att_beg> and <equal>, both may be identical.
7318 */
7319
7320 /* look for end of cookie if there is an equal sign */
7321 if (equal < hdr_end && *equal == '=') {
7322 /* look for the beginning of the value */
7323 val_beg = equal + 1;
7324 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7325 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 /* find the end of the value, respecting quotes */
7328 next = find_cookie_value_end(val_beg, hdr_end);
7329
7330 /* make val_end point to the first white space or delimitor after the value */
7331 val_end = next;
7332 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7333 val_end--;
7334 } else {
7335 /* <equal> points to next comma, semi-colon or EOL */
7336 val_beg = val_end = next = equal;
7337 }
7338
7339 if (next < hdr_end) {
7340 /* Set-Cookie2 supports multiple cookies, and <next> points to
7341 * a colon or semi-colon before the end. So skip all attr-value
7342 * pairs and look for the next comma. For Set-Cookie, since
7343 * commas are permitted in values, skip to the end.
7344 */
7345 if (is_cookie2)
7346 next = find_hdr_value_end(next, hdr_end);
7347 else
7348 next = hdr_end;
7349 }
7350
7351 /* Now everything is as on the diagram above */
7352
7353 /* Ignore cookies with no equal sign */
7354 if (equal == val_end)
7355 continue;
7356
7357 /* If there are spaces around the equal sign, we need to
7358 * strip them otherwise we'll get trouble for cookie captures,
7359 * or even for rewrites. Since this happens extremely rarely,
7360 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007362 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7363 int stripped_before = 0;
7364 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365
Willy Tarreau24581ba2010-08-31 22:39:35 +02007366 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007367 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 equal += stripped_before;
7369 val_beg += stripped_before;
7370 }
7371
7372 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007373 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007374 val_beg += stripped_after;
7375 stripped_before += stripped_after;
7376 }
7377
7378 val_end += stripped_before;
7379 next += stripped_before;
7380 hdr_end += stripped_before;
7381 hdr_next += stripped_before;
7382 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007383 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007384 }
7385
7386 /* First, let's see if we want to capture this cookie. We check
7387 * that we don't already have a server side cookie, because we
7388 * can only capture one. Also as an optimisation, we ignore
7389 * cookies shorter than the declared name.
7390 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007391 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007392 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007393 (val_end - att_beg >= t->fe->capture_namelen) &&
7394 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7395 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007396 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 Alert("HTTP logging : out of memory.\n");
7398 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007399 else {
7400 if (log_len > t->fe->capture_len)
7401 log_len = t->fe->capture_len;
7402 memcpy(txn->srv_cookie, att_beg, log_len);
7403 txn->srv_cookie[log_len] = 0;
7404 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 }
7406
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007407 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007409 if (!(t->flags & SN_IGNORE_PRST) &&
7410 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7411 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007412 /* assume passive cookie by default */
7413 txn->flags &= ~TX_SCK_MASK;
7414 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415
7416 /* If the cookie is in insert mode on a known server, we'll delete
7417 * this occurrence because we'll insert another one later.
7418 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007419 * a direct access.
7420 */
Willy Tarreau67402132012-05-31 20:40:20 +02007421 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007422 /* The "preserve" flag was set, we don't want to touch the
7423 * server's cookie.
7424 */
7425 }
Willy Tarreau67402132012-05-31 20:40:20 +02007426 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7427 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007428 /* this cookie must be deleted */
7429 if (*prev == ':' && next == hdr_end) {
7430 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007431 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007432 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7433 txn->hdr_idx.used--;
7434 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007435 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 hdr_next += delta;
7437 http_msg_move_end(&txn->rsp, delta);
7438 /* note: while both invalid now, <next> and <hdr_end>
7439 * are still equal, so the for() will stop as expected.
7440 */
7441 } else {
7442 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007443 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 next = prev;
7445 hdr_end += delta;
7446 hdr_next += delta;
7447 cur_hdr->len += delta;
7448 http_msg_move_end(&txn->rsp, delta);
7449 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007450 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007451 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453 }
Willy Tarreau67402132012-05-31 20:40:20 +02007454 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 * with this server since we know it.
7457 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007458 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 next += delta;
7460 hdr_end += delta;
7461 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007463 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007464
Willy Tarreauf1348312010-10-07 15:54:11 +02007465 txn->flags &= ~TX_SCK_MASK;
7466 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007468 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007469 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007471 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007472 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007473 next += delta;
7474 hdr_end += delta;
7475 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007476 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007477 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007478
Willy Tarreau827aee92011-03-10 16:55:02 +01007479 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007480 txn->flags &= ~TX_SCK_MASK;
7481 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482 }
7483 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007484 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7485 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007486 int cmp_len, value_len;
7487 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488
Cyril Bontéb21570a2009-11-29 20:04:48 +01007489 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007490 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7491 value_begin = att_beg + t->be->appsession_name_len;
7492 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007493 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007494 cmp_len = att_end - att_beg;
7495 value_begin = val_beg;
7496 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007497 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007498
Cyril Bonté17530c32010-04-06 21:11:10 +02007499 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007500 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7501 /* free a possibly previously allocated memory */
7502 pool_free2(apools.sessid, txn->sessid);
7503
Cyril Bontéb21570a2009-11-29 20:04:48 +01007504 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007505 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007506 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7507 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7508 return;
7509 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007510 memcpy(txn->sessid, value_begin, value_len);
7511 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 }
7514 /* that's done for this cookie, check the next one on the same
7515 * line when next != hdr_end (only if is_cookie2).
7516 */
7517 }
7518 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007521
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007522 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007523 appsess *asession = NULL;
7524 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007525 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007526 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007527 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007528 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7529 Alert("Not enough Memory process_srv():asession:calloc().\n");
7530 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7531 return;
7532 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007533 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7534
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007535 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7536 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7537 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007538 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007539 return;
7540 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007541 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007542 asession->sessid[t->be->appsession_len] = 0;
7543
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007544 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007545 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007546 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007547 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007548 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007549 return;
7550 }
7551 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007552 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007553
7554 asession->request_count = 0;
7555 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7556 }
7557
7558 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7559 asession->request_count++;
7560 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007561}
7562
7563
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564/*
7565 * Check if response is cacheable or not. Updates t->flags.
7566 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007567void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568{
7569 struct http_txn *txn = &t->txn;
7570 char *p1, *p2;
7571
7572 char *cur_ptr, *cur_end, *cur_next;
7573 int cur_idx;
7574
Willy Tarreau5df51872007-11-25 16:20:08 +01007575 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576 return;
7577
7578 /* Iterate through the headers.
7579 * we start with the start line.
7580 */
7581 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007582 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007583
7584 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7585 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007586 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587
7588 cur_hdr = &txn->hdr_idx.v[cur_idx];
7589 cur_ptr = cur_next;
7590 cur_end = cur_ptr + cur_hdr->len;
7591 cur_next = cur_end + cur_hdr->cr + 1;
7592
7593 /* We have one full header between cur_ptr and cur_end, and the
7594 * next header starts at cur_next. We're only interested in
7595 * "Cookie:" headers.
7596 */
7597
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007598 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7599 if (val) {
7600 if ((cur_end - (cur_ptr + val) >= 8) &&
7601 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7602 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7603 return;
7604 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605 }
7606
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007607 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7608 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609 continue;
7610
7611 /* OK, right now we know we have a cache-control header at cur_ptr */
7612
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007613 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007614
7615 if (p1 >= cur_end) /* no more info */
7616 continue;
7617
7618 /* p1 is at the beginning of the value */
7619 p2 = p1;
7620
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007621 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007622 p2++;
7623
7624 /* we have a complete value between p1 and p2 */
7625 if (p2 < cur_end && *p2 == '=') {
7626 /* we have something of the form no-cache="set-cookie" */
7627 if ((cur_end - p1 >= 21) &&
7628 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7629 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007630 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 continue;
7632 }
7633
7634 /* OK, so we know that either p2 points to the end of string or to a comma */
7635 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7636 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7637 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7638 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007639 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 return;
7641 }
7642
7643 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007644 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645 continue;
7646 }
7647 }
7648}
7649
7650
Willy Tarreau58f10d72006-12-04 02:26:12 +01007651/*
7652 * Try to retrieve a known appsession in the URI, then the associated server.
7653 * If the server is found, it's assigned to the session.
7654 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007655void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007657 char *end_params, *first_param, *cur_param, *next_param;
7658 char separator;
7659 int value_len;
7660
7661 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007663 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007664 (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 +01007665 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007667
Cyril Bontéb21570a2009-11-29 20:04:48 +01007668 first_param = NULL;
7669 switch (mode) {
7670 case PR_O2_AS_M_PP:
7671 first_param = memchr(begin, ';', len);
7672 break;
7673 case PR_O2_AS_M_QS:
7674 first_param = memchr(begin, '?', len);
7675 break;
7676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677
Cyril Bontéb21570a2009-11-29 20:04:48 +01007678 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007680 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007681
Cyril Bontéb21570a2009-11-29 20:04:48 +01007682 switch (mode) {
7683 case PR_O2_AS_M_PP:
7684 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7685 end_params = (char *) begin + len;
7686 }
7687 separator = ';';
7688 break;
7689 case PR_O2_AS_M_QS:
7690 end_params = (char *) begin + len;
7691 separator = '&';
7692 break;
7693 default:
7694 /* unknown mode, shouldn't happen */
7695 return;
7696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697
Cyril Bontéb21570a2009-11-29 20:04:48 +01007698 cur_param = next_param = end_params;
7699 while (cur_param > first_param) {
7700 cur_param--;
7701 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7702 /* let's see if this is the appsession parameter */
7703 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7704 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7705 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7706 /* Cool... it's the right one */
7707 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7708 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7709 if (value_len > 0) {
7710 manage_client_side_appsession(t, cur_param, value_len);
7711 }
7712 break;
7713 }
7714 next_param = cur_param;
7715 }
7716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007718 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007719 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007720#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721}
7722
Willy Tarreaub2513902006-12-17 14:52:38 +01007723/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007724 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007725 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007726 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007727 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007728 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007729 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007730 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007731 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007732int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007733{
7734 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007735 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007737 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007738
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007739 if (!uri_auth)
7740 return 0;
7741
Cyril Bonté70be45d2010-10-12 00:14:35 +02007742 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007743 return 0;
7744
Willy Tarreau295a8372011-03-10 11:25:07 +01007745 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007746 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007747 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007749 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007750 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007751 return 0;
7752
Willy Tarreau3a215be2012-03-09 21:39:51 +01007753 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007754 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007755 return 0;
7756
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007757 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007758 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007759 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007760 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007761 break;
7762 }
7763 h++;
7764 }
7765
7766 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007767 h = uri + uri_auth->uri_len;
7768 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007769 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007770 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007771 break;
7772 }
7773 h++;
7774 }
7775 }
7776
Willy Tarreau3a215be2012-03-09 21:39:51 +01007777 h = uri + uri_auth->uri_len;
7778 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007779 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007780 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007781 break;
7782 }
7783 h++;
7784 }
7785
Willy Tarreau3a215be2012-03-09 21:39:51 +01007786 h = uri + uri_auth->uri_len;
7787 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007788 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007789 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007790 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007791 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007792 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7793 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7794 si->applet.ctx.stats.st_code = i;
7795 break;
7796 }
7797 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007798 break;
7799 }
7800 h++;
7801 }
Willy Tarreaub2513902006-12-17 14:52:38 +01007802 return 1;
7803}
7804
Willy Tarreau4076a152009-04-02 15:18:36 +02007805/*
7806 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007807 * By default it tries to report the error position as msg->err_pos. However if
7808 * this one is not set, it will then report msg->next, which is the last known
7809 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007810 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007811 */
7812void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007813 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007814 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007815{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007816 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007817 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007818
Willy Tarreau9b28e032012-10-12 23:49:43 +02007819 es->len = MIN(chn->buf->i, sizeof(es->buf));
7820 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007821 len1 = MIN(len1, es->len);
7822 len2 = es->len - len1; /* remaining data if buffer wraps */
7823
Willy Tarreau9b28e032012-10-12 23:49:43 +02007824 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007825 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007826 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007827
Willy Tarreau4076a152009-04-02 15:18:36 +02007828 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007829 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007830 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007831 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007832
Willy Tarreau4076a152009-04-02 15:18:36 +02007833 es->when = date; // user-visible date
7834 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007835 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007836 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007837 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007838 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007839 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007840 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007841 es->s_flags = s->flags;
7842 es->t_flags = s->txn.flags;
7843 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 es->b_out = chn->buf->o;
7845 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007846 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007847 es->m_clen = msg->chunk_len;
7848 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007849}
Willy Tarreaub2513902006-12-17 14:52:38 +01007850
Willy Tarreau294c4732011-12-16 21:35:50 +01007851/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7852 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7853 * performed over the whole headers. Otherwise it must contain a valid header
7854 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7855 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7856 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7857 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7858 * -1.
7859 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007860 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007861unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007862 struct hdr_idx *idx, int occ,
7863 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007864{
Willy Tarreau294c4732011-12-16 21:35:50 +01007865 struct hdr_ctx local_ctx;
7866 char *ptr_hist[MAX_HDR_HISTORY];
7867 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007868 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007869 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007870
Willy Tarreau294c4732011-12-16 21:35:50 +01007871 if (!ctx) {
7872 local_ctx.idx = 0;
7873 ctx = &local_ctx;
7874 }
7875
Willy Tarreaubce70882009-09-07 11:51:47 +02007876 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007877 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007878 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007879 occ--;
7880 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007881 *vptr = ctx->line + ctx->val;
7882 *vlen = ctx->vlen;
7883 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007884 }
7885 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007886 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007887 }
7888
7889 /* negative occurrence, we scan all the list then walk back */
7890 if (-occ > MAX_HDR_HISTORY)
7891 return 0;
7892
Willy Tarreau294c4732011-12-16 21:35:50 +01007893 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007894 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007895 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7896 len_hist[hist_ptr] = ctx->vlen;
7897 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007898 hist_ptr = 0;
7899 found++;
7900 }
7901 if (-occ > found)
7902 return 0;
7903 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7904 * find occurrence -occ, so we have to check [hist_ptr+occ].
7905 */
7906 hist_ptr += occ;
7907 if (hist_ptr >= MAX_HDR_HISTORY)
7908 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007909 *vptr = ptr_hist[hist_ptr];
7910 *vlen = len_hist[hist_ptr];
7911 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007912}
7913
Willy Tarreaubaaee002006-06-26 02:48:02 +02007914/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007915 * Print a debug line with a header. Always stop at the first CR or LF char,
7916 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7917 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918 */
7919void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7920{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007921 int max;
7922 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007923 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7924 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007925
7926 for (max = 0; start + max < end; max++)
7927 if (start[max] == '\r' || start[max] == '\n')
7928 break;
7929
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007930 UBOUND(max, trash.size - trash.len - 3);
7931 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7932 trash.str[trash.len++] = '\n';
7933 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007934}
7935
Willy Tarreau0937bc42009-12-22 15:03:09 +01007936/*
7937 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7938 * the required fields are properly allocated and that we only need to (re)init
7939 * them. This should be used before processing any new request.
7940 */
7941void http_init_txn(struct session *s)
7942{
7943 struct http_txn *txn = &s->txn;
7944 struct proxy *fe = s->fe;
7945
7946 txn->flags = 0;
7947 txn->status = -1;
7948
William Lallemand5f232402012-04-05 18:02:55 +02007949 global.req_count++;
7950
Willy Tarreauf64d1412010-10-07 20:06:11 +02007951 txn->cookie_first_date = 0;
7952 txn->cookie_last_date = 0;
7953
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007954 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007955 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007956 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007957 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007958 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007959 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007960 txn->req.chunk_len = 0LL;
7961 txn->req.body_len = 0LL;
7962 txn->rsp.chunk_len = 0LL;
7963 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007964 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7965 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007966 txn->req.chn = s->req;
7967 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007968
7969 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007970
7971 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7972 if (fe->options2 & PR_O2_REQBUG_OK)
7973 txn->req.err_pos = -1; /* let buggy requests pass */
7974
Willy Tarreau46023632010-01-07 22:51:47 +01007975 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007976 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7977
Willy Tarreau46023632010-01-07 22:51:47 +01007978 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007979 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7980
7981 if (txn->hdr_idx.v)
7982 hdr_idx_init(&txn->hdr_idx);
7983}
7984
7985/* to be used at the end of a transaction */
7986void http_end_txn(struct session *s)
7987{
7988 struct http_txn *txn = &s->txn;
7989
7990 /* these ones will have been dynamically allocated */
7991 pool_free2(pool2_requri, txn->uri);
7992 pool_free2(pool2_capture, txn->cli_cookie);
7993 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007994 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007995 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007996
William Lallemanda73203e2012-03-12 12:48:57 +01007997 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007998 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007999 txn->uri = NULL;
8000 txn->srv_cookie = NULL;
8001 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008002
8003 if (txn->req.cap) {
8004 struct cap_hdr *h;
8005 for (h = s->fe->req_cap; h; h = h->next)
8006 pool_free2(h->pool, txn->req.cap[h->index]);
8007 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8008 }
8009
8010 if (txn->rsp.cap) {
8011 struct cap_hdr *h;
8012 for (h = s->fe->rsp_cap; h; h = h->next)
8013 pool_free2(h->pool, txn->rsp.cap[h->index]);
8014 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8015 }
8016
Willy Tarreau0937bc42009-12-22 15:03:09 +01008017}
8018
8019/* to be used at the end of a transaction to prepare a new one */
8020void http_reset_txn(struct session *s)
8021{
8022 http_end_txn(s);
8023 http_init_txn(s);
8024
8025 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008026 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008027 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008028 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008029 /* re-init store persistence */
8030 s->store_count = 0;
8031
Willy Tarreau0937bc42009-12-22 15:03:09 +01008032 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008033
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008034 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035
Willy Tarreau739cfba2010-01-25 23:11:14 +01008036 /* We must trim any excess data from the response buffer, because we
8037 * may have blocked an invalid response from a server that we don't
8038 * want to accidentely forward once we disable the analysers, nor do
8039 * we want those data to come along with next response. A typical
8040 * example of such data would be from a buggy server responding to
8041 * a HEAD with some data, or sending more than the advertised
8042 * content-length.
8043 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008044 if (unlikely(s->rep->buf->i))
8045 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008046
Willy Tarreau0937bc42009-12-22 15:03:09 +01008047 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008048 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008049
Willy Tarreaud04e8582010-05-31 12:31:35 +02008050 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008051 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008052
8053 s->req->rex = TICK_ETERNITY;
8054 s->req->wex = TICK_ETERNITY;
8055 s->req->analyse_exp = TICK_ETERNITY;
8056 s->rep->rex = TICK_ETERNITY;
8057 s->rep->wex = TICK_ETERNITY;
8058 s->rep->analyse_exp = TICK_ETERNITY;
8059}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008060
Willy Tarreauff011f22011-01-06 17:51:27 +01008061void free_http_req_rules(struct list *r) {
8062 struct http_req_rule *tr, *pr;
8063
8064 list_for_each_entry_safe(pr, tr, r, list) {
8065 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008066 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008067 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008068
8069 free(pr);
8070 }
8071}
8072
8073struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8074{
8075 struct http_req_rule *rule;
8076 int cur_arg;
8077
8078 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8079 if (!rule) {
8080 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008081 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008082 }
8083
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008084 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008085 rule->action = HTTP_REQ_ACT_ALLOW;
8086 cur_arg = 1;
8087 } else if (!strcmp(args[0], "deny")) {
8088 rule->action = HTTP_REQ_ACT_DENY;
8089 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008090 } else if (!strcmp(args[0], "tarpit")) {
8091 rule->action = HTTP_REQ_ACT_TARPIT;
8092 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008093 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008094 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008095 cur_arg = 1;
8096
8097 while(*args[cur_arg]) {
8098 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008099 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008100 cur_arg+=2;
8101 continue;
8102 } else
8103 break;
8104 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008105 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8106 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8107 cur_arg = 1;
8108
8109 if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) {
8110 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8111 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008112 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008113 }
8114
8115 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8116 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8117 LIST_INIT(&rule->arg.hdr_add.fmt);
8118 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, PR_MODE_HTTP);
8119 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008120 } else if (strcmp(args[0], "redirect") == 0) {
8121 struct redirect_rule *redir;
8122 char *errmsg;
8123
8124 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8125 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8126 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8127 goto out_err;
8128 }
8129
8130 /* this redirect rule might already contain a parsed condition which
8131 * we'll pass to the http-request rule.
8132 */
8133 rule->action = HTTP_REQ_ACT_REDIR;
8134 rule->arg.redir = redir;
8135 rule->cond = redir->cond;
8136 redir->cond = NULL;
8137 cur_arg = 2;
8138 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008139 } else {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008140 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008141 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008142 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008143 }
8144
8145 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8146 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008147 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008148
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008149 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8150 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8151 file, linenum, args[0], errmsg);
8152 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008153 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008154 }
8155 rule->cond = cond;
8156 }
8157 else if (*args[cur_arg]) {
8158 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8159 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8160 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008161 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008162 }
8163
8164 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008165 out_err:
8166 free(rule);
8167 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008168}
8169
Willy Tarreau4baae242012-12-27 12:00:31 +01008170/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8171 * with <err> filled with the error message.
8172 */
8173struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8174 const char **args, char **errmsg)
8175{
8176 struct redirect_rule *rule;
8177 int cur_arg;
8178 int type = REDIRECT_TYPE_NONE;
8179 int code = 302;
8180 const char *destination = NULL;
8181 const char *cookie = NULL;
8182 int cookie_set = 0;
8183 unsigned int flags = REDIRECT_FLAG_NONE;
8184 struct acl_cond *cond = NULL;
8185
8186 cur_arg = 0;
8187 while (*(args[cur_arg])) {
8188 if (strcmp(args[cur_arg], "location") == 0) {
8189 if (!*args[cur_arg + 1])
8190 goto missing_arg;
8191
8192 type = REDIRECT_TYPE_LOCATION;
8193 cur_arg++;
8194 destination = args[cur_arg];
8195 }
8196 else if (strcmp(args[cur_arg], "prefix") == 0) {
8197 if (!*args[cur_arg + 1])
8198 goto missing_arg;
8199
8200 type = REDIRECT_TYPE_PREFIX;
8201 cur_arg++;
8202 destination = args[cur_arg];
8203 }
8204 else if (strcmp(args[cur_arg], "scheme") == 0) {
8205 if (!*args[cur_arg + 1])
8206 goto missing_arg;
8207
8208 type = REDIRECT_TYPE_SCHEME;
8209 cur_arg++;
8210 destination = args[cur_arg];
8211 }
8212 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8213 if (!*args[cur_arg + 1])
8214 goto missing_arg;
8215
8216 cur_arg++;
8217 cookie = args[cur_arg];
8218 cookie_set = 1;
8219 }
8220 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8221 if (!*args[cur_arg + 1])
8222 goto missing_arg;
8223
8224 cur_arg++;
8225 cookie = args[cur_arg];
8226 cookie_set = 0;
8227 }
8228 else if (strcmp(args[cur_arg], "code") == 0) {
8229 if (!*args[cur_arg + 1])
8230 goto missing_arg;
8231
8232 cur_arg++;
8233 code = atol(args[cur_arg]);
8234 if (code < 301 || code > 303) {
8235 memprintf(errmsg,
8236 "'%s': unsupported HTTP code '%s' (must be a number between 301 and 303)",
8237 args[cur_arg - 1], args[cur_arg]);
8238 return NULL;
8239 }
8240 }
8241 else if (!strcmp(args[cur_arg],"drop-query")) {
8242 flags |= REDIRECT_FLAG_DROP_QS;
8243 }
8244 else if (!strcmp(args[cur_arg],"append-slash")) {
8245 flags |= REDIRECT_FLAG_APPEND_SLASH;
8246 }
8247 else if (strcmp(args[cur_arg], "if") == 0 ||
8248 strcmp(args[cur_arg], "unless") == 0) {
8249 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8250 if (!cond) {
8251 memprintf(errmsg, "error in condition: %s", *errmsg);
8252 return NULL;
8253 }
8254 break;
8255 }
8256 else {
8257 memprintf(errmsg,
8258 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8259 args[cur_arg]);
8260 return NULL;
8261 }
8262 cur_arg++;
8263 }
8264
8265 if (type == REDIRECT_TYPE_NONE) {
8266 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8267 return NULL;
8268 }
8269
8270 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8271 rule->cond = cond;
8272 rule->rdr_str = strdup(destination);
8273 rule->rdr_len = strlen(destination);
8274 if (cookie) {
8275 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8276 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8277 */
8278 rule->cookie_len = strlen(cookie);
8279 if (cookie_set) {
8280 rule->cookie_str = malloc(rule->cookie_len + 10);
8281 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8282 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8283 rule->cookie_len += 9;
8284 } else {
8285 rule->cookie_str = malloc(rule->cookie_len + 21);
8286 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8287 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8288 rule->cookie_len += 20;
8289 }
8290 }
8291 rule->type = type;
8292 rule->code = code;
8293 rule->flags = flags;
8294 LIST_INIT(&rule->list);
8295 return rule;
8296
8297 missing_arg:
8298 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8299 return NULL;
8300}
8301
Willy Tarreau8797c062007-05-07 00:55:35 +02008302/************************************************************************/
8303/* The code below is dedicated to ACL parsing and matching */
8304/************************************************************************/
8305
8306
Willy Tarreau14174bc2012-04-16 14:34:04 +02008307/* This function ensures that the prerequisites for an L7 fetch are ready,
8308 * which means that a request or response is ready. If some data is missing,
8309 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008310 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8311 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008312 *
8313 * The function returns :
8314 * 0 if some data is missing or if the requested data cannot be fetched
8315 * -1 if it is certain that we'll never have any HTTP message there
8316 * 1 if an HTTP message is ready
8317 */
8318static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008319acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008320 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008321{
8322 struct http_txn *txn = l7;
8323 struct http_msg *msg = &txn->req;
8324
8325 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8326 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8327 */
8328
8329 if (unlikely(!s || !txn))
8330 return 0;
8331
8332 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008333 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008334
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008335 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008336 if (unlikely(!s->req))
8337 return 0;
8338
8339 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008340 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008342 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008343 return -1;
8344 }
8345
8346 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008347 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008348 http_msg_analyzer(msg, &txn->hdr_idx);
8349
8350 /* Still no valid request ? */
8351 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008352 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008353 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008354 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008355 return -1;
8356 }
8357 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008358 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008359 return 0;
8360 }
8361
8362 /* OK we just got a valid HTTP request. We have some minor
8363 * preparation to perform so that further checks can rely
8364 * on HTTP tests.
8365 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008366 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008367 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8368 s->flags |= SN_REDIRECTABLE;
8369
8370 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008371 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008372 return -1;
8373 }
8374 }
8375
Willy Tarreau24e32d82012-04-23 23:55:44 +02008376 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008377 return 0; /* data might have moved and indexes changed */
8378
8379 /* otherwise everything's ready for the request */
8380 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008381 else {
8382 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008383 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8384 return 0;
8385 }
8386
8387 /* everything's OK */
8388 return 1;
8389}
Willy Tarreau8797c062007-05-07 00:55:35 +02008390
Willy Tarreauc0239e02012-04-16 14:42:55 +02008391#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008392 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02008393
Willy Tarreau24e32d82012-04-23 23:55:44 +02008394#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008395 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02008396
Willy Tarreau8797c062007-05-07 00:55:35 +02008397
8398/* 1. Check on METHOD
8399 * We use the pre-parsed method if it is known, and store its number as an
8400 * integer. If it is unknown, we use the pointer and the length.
8401 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008402static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008403{
8404 int len, meth;
8405
Willy Tarreauae8b7962007-06-09 23:10:04 +02008406 len = strlen(*text);
8407 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008408
8409 pattern->val.i = meth;
8410 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008411 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008412 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008413 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008414 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008415 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008416 pattern->len = len;
8417 }
8418 return 1;
8419}
8420
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008421/* This function fetches the method of current HTTP request and stores
8422 * it in the global pattern struct as a chunk. There are two possibilities :
8423 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8424 * in <len> and <ptr> is NULL ;
8425 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8426 * <len> to its length.
8427 * This is intended to be used with acl_match_meth() only.
8428 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008429static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008430acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008431 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008432{
8433 int meth;
8434 struct http_txn *txn = l7;
8435
Willy Tarreau24e32d82012-04-23 23:55:44 +02008436 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008437
Willy Tarreau8797c062007-05-07 00:55:35 +02008438 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008439 smp->type = SMP_T_UINT;
8440 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008441 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008442 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8443 /* ensure the indexes are not affected */
8444 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008445 smp->type = SMP_T_CSTR;
8446 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008447 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008448 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008449 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008450 return 1;
8451}
8452
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008453/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008454static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008455{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008456 int icase;
8457
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008458
Willy Tarreauf853c462012-04-23 18:53:56 +02008459 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008460 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008461 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008462 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008463 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008464 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008465
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008466 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8467 if (pattern->val.i != HTTP_METH_OTHER)
8468 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008469
8470 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008471 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008472 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008473
8474 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008475 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8476 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008477 return ACL_PAT_FAIL;
8478 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008479}
8480
8481/* 2. Check on Request/Status Version
8482 * We simply compare strings here.
8483 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008484static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008485{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008486 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008487 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008488 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008489 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008490 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008491 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008492 return 1;
8493}
8494
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008495static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008496acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008497 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008498{
8499 struct http_txn *txn = l7;
8500 char *ptr;
8501 int len;
8502
Willy Tarreauc0239e02012-04-16 14:42:55 +02008503 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008504
Willy Tarreau8797c062007-05-07 00:55:35 +02008505 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008506 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008507
8508 while ((len-- > 0) && (*ptr++ != '/'));
8509 if (len <= 0)
8510 return 0;
8511
Willy Tarreauf853c462012-04-23 18:53:56 +02008512 smp->type = SMP_T_CSTR;
8513 smp->data.str.str = ptr;
8514 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008515
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008516 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008517 return 1;
8518}
8519
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008520static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008521acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008522 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008523{
8524 struct http_txn *txn = l7;
8525 char *ptr;
8526 int len;
8527
Willy Tarreauc0239e02012-04-16 14:42:55 +02008528 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008529
Willy Tarreauf26b2522012-12-14 08:33:14 +01008530 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8531 return 0;
8532
Willy Tarreau8797c062007-05-07 00:55:35 +02008533 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008534 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008535
8536 while ((len-- > 0) && (*ptr++ != '/'));
8537 if (len <= 0)
8538 return 0;
8539
Willy Tarreauf853c462012-04-23 18:53:56 +02008540 smp->type = SMP_T_CSTR;
8541 smp->data.str.str = ptr;
8542 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008543
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008544 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008545 return 1;
8546}
8547
8548/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008549static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008550acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008551 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008552{
8553 struct http_txn *txn = l7;
8554 char *ptr;
8555 int len;
8556
Willy Tarreauc0239e02012-04-16 14:42:55 +02008557 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008558
Willy Tarreauf26b2522012-12-14 08:33:14 +01008559 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8560 return 0;
8561
Willy Tarreau8797c062007-05-07 00:55:35 +02008562 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008563 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008564
Willy Tarreauf853c462012-04-23 18:53:56 +02008565 smp->type = SMP_T_UINT;
8566 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008567 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008568 return 1;
8569}
8570
8571/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008572static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008573smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008574 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008575{
8576 struct http_txn *txn = l7;
8577
Willy Tarreauc0239e02012-04-16 14:42:55 +02008578 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008579
Willy Tarreauf853c462012-04-23 18:53:56 +02008580 smp->type = SMP_T_CSTR;
8581 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008582 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008583 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008584 return 1;
8585}
8586
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008587static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008588smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008589 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008590{
8591 struct http_txn *txn = l7;
8592
Willy Tarreauc0239e02012-04-16 14:42:55 +02008593 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008594
8595 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008596 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8597 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008598 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008599 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008600 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008601
8602 /*
8603 * If we are parsing url in frontend space, we prepare backend stage
8604 * to not parse again the same url ! optimization lazyness...
8605 */
8606 if (px->options & PR_O_HTTP_PROXY)
8607 l4->flags |= SN_ADDR_SET;
8608
Willy Tarreau37406352012-04-23 16:16:37 +02008609 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008610 return 1;
8611}
8612
8613static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008614smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008615 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008616{
8617 struct http_txn *txn = l7;
8618
Willy Tarreauc0239e02012-04-16 14:42:55 +02008619 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008620
8621 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008622 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02008623 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008624 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008625
8626 if (px->options & PR_O_HTTP_PROXY)
8627 l4->flags |= SN_ADDR_SET;
8628
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008629 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008630 return 1;
8631}
8632
Willy Tarreau185b5c42012-04-26 15:11:51 +02008633/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8634 * Accepts an optional argument of type string containing the header field name,
8635 * and an optional argument of type signed or unsigned integer to request an
8636 * explicit occurrence of the header. Note that in the event of a missing name,
8637 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008638 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008639static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008640smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008641 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008642{
8643 struct http_txn *txn = l7;
8644 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008645 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008646 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008647 int occ = 0;
8648 const char *name_str = NULL;
8649 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008650
Willy Tarreau185b5c42012-04-26 15:11:51 +02008651 if (args) {
8652 if (args[0].type != ARGT_STR)
8653 return 0;
8654 name_str = args[0].data.str.str;
8655 name_len = args[0].data.str.len;
8656
8657 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8658 occ = args[1].data.uint;
8659 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008660
Willy Tarreaue333ec92012-04-16 16:26:40 +02008661 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008662
Willy Tarreau185b5c42012-04-26 15:11:51 +02008663 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008664 /* search for header from the beginning */
8665 ctx->idx = 0;
8666
Willy Tarreau185b5c42012-04-26 15:11:51 +02008667 if (!occ && !(opt & SMP_OPT_ITERATE))
8668 /* no explicit occurrence and single fetch => last header by default */
8669 occ = -1;
8670
8671 if (!occ)
8672 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008673 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008674
Willy Tarreau185b5c42012-04-26 15:11:51 +02008675 smp->type = SMP_T_CSTR;
8676 smp->flags |= SMP_F_VOL_HDR;
8677 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008678 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008679
Willy Tarreau37406352012-04-23 16:16:37 +02008680 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008681 return 0;
8682}
8683
Willy Tarreauc11416f2007-06-17 16:58:38 +02008684/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008685 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008686 */
8687static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008688smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008689 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008690{
8691 struct http_txn *txn = l7;
8692 struct hdr_idx *idx = &txn->hdr_idx;
8693 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008694 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008695 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008696
Willy Tarreau24e32d82012-04-23 23:55:44 +02008697 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008698 return 0;
8699
Willy Tarreaue333ec92012-04-16 16:26:40 +02008700 CHECK_HTTP_MESSAGE_FIRST();
8701
Willy Tarreau33a7e692007-06-10 19:45:56 +02008702 ctx.idx = 0;
8703 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008704 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008705 cnt++;
8706
Willy Tarreauf853c462012-04-23 18:53:56 +02008707 smp->type = SMP_T_UINT;
8708 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008709 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008710 return 1;
8711}
8712
Willy Tarreau185b5c42012-04-26 15:11:51 +02008713/* Fetch an HTTP header's integer value. The integer value is returned. It
8714 * takes a mandatory argument of type string and an optional one of type int
8715 * to designate a specific occurrence. It returns an unsigned integer, which
8716 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008717 */
8718static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008719smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008720 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008721{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008722 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008723
Willy Tarreauf853c462012-04-23 18:53:56 +02008724 if (ret > 0) {
8725 smp->type = SMP_T_UINT;
8726 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8727 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008728
Willy Tarreaud53e2422012-04-16 17:21:11 +02008729 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008730}
8731
Cyril Bonté69fa9922012-10-25 00:01:06 +02008732/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8733 * and an optional one of type int to designate a specific occurrence.
8734 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008735 */
8736static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008737smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008738 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008739{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008740 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008741
Willy Tarreau185b5c42012-04-26 15:11:51 +02008742 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008743 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8744 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008745 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008746 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008747 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008748 if (smp->data.str.len < temp->size - 1) {
8749 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8750 temp->str[smp->data.str.len] = '\0';
8751 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8752 smp->type = SMP_T_IPV6;
8753 break;
8754 }
8755 }
8756 }
8757
Willy Tarreaud53e2422012-04-16 17:21:11 +02008758 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008759 if (!(smp->flags & SMP_F_NOT_LAST))
8760 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008761 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008762 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008763}
8764
Willy Tarreau737b0c12007-06-10 21:28:46 +02008765/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8766 * the first '/' after the possible hostname, and ends before the possible '?'.
8767 */
8768static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008769smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008770 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008771{
8772 struct http_txn *txn = l7;
8773 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008774
Willy Tarreauc0239e02012-04-16 14:42:55 +02008775 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008776
Willy Tarreau9b28e032012-10-12 23:49:43 +02008777 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008778 ptr = http_get_path(txn);
8779 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008780 return 0;
8781
8782 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008783 smp->type = SMP_T_CSTR;
8784 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008785
8786 while (ptr < end && *ptr != '?')
8787 ptr++;
8788
Willy Tarreauf853c462012-04-23 18:53:56 +02008789 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008790 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008791 return 1;
8792}
8793
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008794/* This produces a concatenation of the first occurrence of the Host header
8795 * followed by the path component if it begins with a slash ('/'). This means
8796 * that '*' will not be added, resulting in exactly the first Host entry.
8797 * If no Host header is found, then the path is returned as-is. The returned
8798 * value is stored in the trash so it does not need to be marked constant.
8799 */
8800static int
8801smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8802 const struct arg *args, struct sample *smp)
8803{
8804 struct http_txn *txn = l7;
8805 char *ptr, *end, *beg;
8806 struct hdr_ctx ctx;
8807
8808 CHECK_HTTP_MESSAGE_FIRST();
8809
8810 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008811 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008812 !ctx.vlen)
8813 return smp_fetch_path(px, l4, l7, opt, args, smp);
8814
8815 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008816 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008817 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008818 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008819 smp->data.str.len = ctx.vlen;
8820
8821 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008822 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008823 beg = http_get_path(txn);
8824 if (!beg)
8825 beg = end;
8826
8827 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8828
8829 if (beg < ptr && *beg == '/') {
8830 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8831 smp->data.str.len += ptr - beg;
8832 }
8833
8834 smp->flags = SMP_F_VOL_1ST;
8835 return 1;
8836}
8837
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008838/* This produces a 32-bit hash of the concatenation of the first occurrence of
8839 * the Host header followed by the path component if it begins with a slash ('/').
8840 * This means that '*' will not be added, resulting in exactly the first Host
8841 * entry. If no Host header is found, then the path is used. The resulting value
8842 * is hashed using the url hash followed by a full avalanche hash and provides a
8843 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8844 * high-traffic sites without having to store whole paths.
8845 */
8846static int
8847smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8848 const struct arg *args, struct sample *smp)
8849{
8850 struct http_txn *txn = l7;
8851 struct hdr_ctx ctx;
8852 unsigned int hash = 0;
8853 char *ptr, *beg, *end;
8854 int len;
8855
8856 CHECK_HTTP_MESSAGE_FIRST();
8857
8858 ctx.idx = 0;
8859 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8860 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8861 ptr = ctx.line + ctx.val;
8862 len = ctx.vlen;
8863 while (len--)
8864 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8865 }
8866
8867 /* now retrieve the path */
8868 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8869 beg = http_get_path(txn);
8870 if (!beg)
8871 beg = end;
8872
8873 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8874
8875 if (beg < ptr && *beg == '/') {
8876 while (beg < ptr)
8877 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8878 }
8879 hash = full_hash(hash);
8880
8881 smp->type = SMP_T_UINT;
8882 smp->data.uint = hash;
8883 smp->flags = SMP_F_VOL_1ST;
8884 return 1;
8885}
8886
Willy Tarreau4a550602012-12-09 14:53:32 +01008887/* This concatenates the source address with the 32-bit hash of the Host and
8888 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8889 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8890 * on the source address length. The URL hash is stored before the address so
8891 * that in environments where IPv6 is insignificant, truncating the output to
8892 * 8 bytes would still work.
8893 */
8894static int
8895smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8896 const struct arg *args, struct sample *smp)
8897{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008898 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008899
8900 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8901 return 0;
8902
Willy Tarreau47ca5452012-12-23 20:22:19 +01008903 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008904 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8905 temp->len += sizeof(smp->data.uint);
8906
8907 switch (l4->si[0].conn->addr.from.ss_family) {
8908 case AF_INET:
8909 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
8910 temp->len += 4;
8911 break;
8912 case AF_INET6:
8913 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
8914 temp->len += 16;
8915 break;
8916 default:
8917 return 0;
8918 }
8919
8920 smp->data.str = *temp;
8921 smp->type = SMP_T_BIN;
8922 return 1;
8923}
8924
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008925static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008926acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008927 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008928{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008929 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8930 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8931 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008932
Willy Tarreau24e32d82012-04-23 23:55:44 +02008933 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008934
Willy Tarreauf853c462012-04-23 18:53:56 +02008935 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008936 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008937 return 1;
8938}
8939
Willy Tarreau7f18e522010-10-22 20:04:13 +02008940/* return a valid test if the current request is the first one on the connection */
8941static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008942acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008943 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008944{
8945 if (!s)
8946 return 0;
8947
Willy Tarreauf853c462012-04-23 18:53:56 +02008948 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008949 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008950 return 1;
8951}
8952
Willy Tarreau34db1082012-04-19 17:16:54 +02008953/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008954static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008955acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008956 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008957{
8958
Willy Tarreau24e32d82012-04-23 23:55:44 +02008959 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008960 return 0;
8961
Willy Tarreauc0239e02012-04-16 14:42:55 +02008962 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008963
Willy Tarreauc0239e02012-04-16 14:42:55 +02008964 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008965 return 0;
8966
Willy Tarreauf853c462012-04-23 18:53:56 +02008967 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008968 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008969 return 1;
8970}
Willy Tarreau8797c062007-05-07 00:55:35 +02008971
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008972/* Accepts exactly 1 argument of type userlist */
8973static int
8974acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8975 const struct arg *args, struct sample *smp)
8976{
8977
8978 if (!args || args->type != ARGT_USR)
8979 return 0;
8980
8981 CHECK_HTTP_MESSAGE_FIRST();
8982
8983 if (!get_http_auth(l4))
8984 return 0;
8985
8986 /* acl_match_auth() will need several information at once */
8987 smp->ctx.a[0] = args->data.usr; /* user list */
8988 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8989 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8990
8991 /* if the user does not belong to the userlist or has a wrong password,
8992 * report that it unconditionally does not match. Otherwise we return
8993 * a non-zero integer which will be ignored anyway since all the params
8994 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8995 */
8996 smp->type = SMP_T_BOOL;
8997 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8998 if (smp->data.uint)
8999 smp->type = SMP_T_UINT;
9000
9001 return 1;
9002}
9003
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009004/* Try to find the next occurrence of a cookie name in a cookie header value.
9005 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9006 * the cookie value is returned into *value and *value_l, and the function
9007 * returns a pointer to the next pointer to search from if the value was found.
9008 * Otherwise if the cookie was not found, NULL is returned and neither value
9009 * nor value_l are touched. The input <hdr> string should first point to the
9010 * header's value, and the <hdr_end> pointer must point to the first character
9011 * not part of the value. <list> must be non-zero if value may represent a list
9012 * of values (cookie headers). This makes it faster to abort parsing when no
9013 * list is expected.
9014 */
9015static char *
9016extract_cookie_value(char *hdr, const char *hdr_end,
9017 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009018 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009019{
9020 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9021 char *next;
9022
9023 /* we search at least a cookie name followed by an equal, and more
9024 * generally something like this :
9025 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9026 */
9027 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9028 /* Iterate through all cookies on this line */
9029
9030 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9031 att_beg++;
9032
9033 /* find att_end : this is the first character after the last non
9034 * space before the equal. It may be equal to hdr_end.
9035 */
9036 equal = att_end = att_beg;
9037
9038 while (equal < hdr_end) {
9039 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9040 break;
9041 if (http_is_spht[(unsigned char)*equal++])
9042 continue;
9043 att_end = equal;
9044 }
9045
9046 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9047 * is between <att_beg> and <equal>, both may be identical.
9048 */
9049
9050 /* look for end of cookie if there is an equal sign */
9051 if (equal < hdr_end && *equal == '=') {
9052 /* look for the beginning of the value */
9053 val_beg = equal + 1;
9054 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9055 val_beg++;
9056
9057 /* find the end of the value, respecting quotes */
9058 next = find_cookie_value_end(val_beg, hdr_end);
9059
9060 /* make val_end point to the first white space or delimitor after the value */
9061 val_end = next;
9062 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9063 val_end--;
9064 } else {
9065 val_beg = val_end = next = equal;
9066 }
9067
9068 /* We have nothing to do with attributes beginning with '$'. However,
9069 * they will automatically be removed if a header before them is removed,
9070 * since they're supposed to be linked together.
9071 */
9072 if (*att_beg == '$')
9073 continue;
9074
9075 /* Ignore cookies with no equal sign */
9076 if (equal == next)
9077 continue;
9078
9079 /* Now we have the cookie name between att_beg and att_end, and
9080 * its value between val_beg and val_end.
9081 */
9082
9083 if (att_end - att_beg == cookie_name_l &&
9084 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9085 /* let's return this value and indicate where to go on from */
9086 *value = val_beg;
9087 *value_l = val_end - val_beg;
9088 return next + 1;
9089 }
9090
9091 /* Set-Cookie headers only have the name in the first attr=value part */
9092 if (!list)
9093 break;
9094 }
9095
9096 return NULL;
9097}
9098
Willy Tarreaue333ec92012-04-16 16:26:40 +02009099/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009100 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
9101 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009102 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009103 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009104 * Accepts exactly 1 argument of type string. If the input options indicate
9105 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009106 */
9107static int
Willy Tarreau51539362012-05-08 12:46:28 +02009108smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9109 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009110{
9111 struct http_txn *txn = l7;
9112 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02009113 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009114 const struct http_msg *msg;
9115 const char *hdr_name;
9116 int hdr_name_len;
9117 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009118 int occ = 0;
9119 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009120
Willy Tarreau24e32d82012-04-23 23:55:44 +02009121 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009122 return 0;
9123
Willy Tarreaue333ec92012-04-16 16:26:40 +02009124 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009125
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009126 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009127 msg = &txn->req;
9128 hdr_name = "Cookie";
9129 hdr_name_len = 6;
9130 } else {
9131 msg = &txn->rsp;
9132 hdr_name = "Set-Cookie";
9133 hdr_name_len = 10;
9134 }
9135
Willy Tarreau28376d62012-04-26 21:26:10 +02009136 if (!occ && !(opt & SMP_OPT_ITERATE))
9137 /* no explicit occurrence and single fetch => last cookie by default */
9138 occ = -1;
9139
9140 /* OK so basically here, either we want only one value and it's the
9141 * last one, or we want to iterate over all of them and we fetch the
9142 * next one.
9143 */
9144
Willy Tarreau9b28e032012-10-12 23:49:43 +02009145 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009146 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009147 /* search for the header from the beginning, we must first initialize
9148 * the search parameters.
9149 */
Willy Tarreau37406352012-04-23 16:16:37 +02009150 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009151 ctx->idx = 0;
9152 }
9153
Willy Tarreau28376d62012-04-26 21:26:10 +02009154 smp->flags |= SMP_F_VOL_HDR;
9155
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009156 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009157 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9158 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009159 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9160 goto out;
9161
Willy Tarreau24e32d82012-04-23 23:55:44 +02009162 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009163 continue;
9164
Willy Tarreau37406352012-04-23 16:16:37 +02009165 smp->ctx.a[0] = ctx->line + ctx->val;
9166 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009167 }
9168
Willy Tarreauf853c462012-04-23 18:53:56 +02009169 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009170 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009171 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009172 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009173 &smp->data.str.str,
9174 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009175 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009176 found = 1;
9177 if (occ >= 0) {
9178 /* one value was returned into smp->data.str.{str,len} */
9179 smp->flags |= SMP_F_NOT_LAST;
9180 return 1;
9181 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009182 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009183 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009184 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009185 /* all cookie headers and values were scanned. If we're looking for the
9186 * last occurrence, we may return it now.
9187 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009188 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009189 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009190 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009191}
9192
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009193/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009194 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009195 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009196 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009197 */
9198static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009199acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009200 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009201{
9202 struct http_txn *txn = l7;
9203 struct hdr_idx *idx = &txn->hdr_idx;
9204 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009205 const struct http_msg *msg;
9206 const char *hdr_name;
9207 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009208 int cnt;
9209 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009210 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009211
Willy Tarreau24e32d82012-04-23 23:55:44 +02009212 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009213 return 0;
9214
Willy Tarreaue333ec92012-04-16 16:26:40 +02009215 CHECK_HTTP_MESSAGE_FIRST();
9216
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009217 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009218 msg = &txn->req;
9219 hdr_name = "Cookie";
9220 hdr_name_len = 6;
9221 } else {
9222 msg = &txn->rsp;
9223 hdr_name = "Set-Cookie";
9224 hdr_name_len = 10;
9225 }
9226
Willy Tarreau9b28e032012-10-12 23:49:43 +02009227 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009228 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009229 ctx.idx = 0;
9230 cnt = 0;
9231
9232 while (1) {
9233 /* Note: val_beg == NULL every time we need to fetch a new header */
9234 if (!val_beg) {
9235 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9236 break;
9237
Willy Tarreau24e32d82012-04-23 23:55:44 +02009238 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009239 continue;
9240
9241 val_beg = ctx.line + ctx.val;
9242 val_end = val_beg + ctx.vlen;
9243 }
9244
Willy Tarreauf853c462012-04-23 18:53:56 +02009245 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009246 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009247 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009248 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009249 &smp->data.str.str,
9250 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009251 cnt++;
9252 }
9253 }
9254
Willy Tarreauf853c462012-04-23 18:53:56 +02009255 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009256 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009257 return 1;
9258}
9259
Willy Tarreau51539362012-05-08 12:46:28 +02009260/* Fetch an cookie's integer value. The integer value is returned. It
9261 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9262 */
9263static int
9264smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9265 const struct arg *args, struct sample *smp)
9266{
9267 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9268
9269 if (ret > 0) {
9270 smp->type = SMP_T_UINT;
9271 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9272 }
9273
9274 return ret;
9275}
9276
Willy Tarreau8797c062007-05-07 00:55:35 +02009277/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009278/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009279/************************************************************************/
9280
David Cournapeau16023ee2010-12-23 20:55:41 +09009281/*
9282 * Given a path string and its length, find the position of beginning of the
9283 * query string. Returns NULL if no query string is found in the path.
9284 *
9285 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9286 *
9287 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9288 */
bedis4c75cca2012-10-05 08:38:24 +02009289static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009290{
9291 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009292
bedis4c75cca2012-10-05 08:38:24 +02009293 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009294 return p ? p + 1 : NULL;
9295}
9296
bedis4c75cca2012-10-05 08:38:24 +02009297static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009298{
bedis4c75cca2012-10-05 08:38:24 +02009299 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009300}
9301
9302/*
9303 * Given a url parameter, find the starting position of the first occurence,
9304 * or NULL if the parameter is not found.
9305 *
9306 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9307 * the function will return query_string+8.
9308 */
9309static char*
9310find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009311 char* url_param_name, size_t url_param_name_l,
9312 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009313{
9314 char *pos, *last;
9315
9316 pos = query_string;
9317 last = query_string + query_string_l - url_param_name_l - 1;
9318
9319 while (pos <= last) {
9320 if (pos[url_param_name_l] == '=') {
9321 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9322 return pos;
9323 pos += url_param_name_l + 1;
9324 }
bedis4c75cca2012-10-05 08:38:24 +02009325 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009326 pos++;
9327 pos++;
9328 }
9329 return NULL;
9330}
9331
9332/*
9333 * Given a url parameter name, returns its value and size into *value and
9334 * *value_l respectively, and returns non-zero. If the parameter is not found,
9335 * zero is returned and value/value_l are not touched.
9336 */
9337static int
9338find_url_param_value(char* path, size_t path_l,
9339 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009340 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009341{
9342 char *query_string, *qs_end;
9343 char *arg_start;
9344 char *value_start, *value_end;
9345
bedis4c75cca2012-10-05 08:38:24 +02009346 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009347 if (!query_string)
9348 return 0;
9349
9350 qs_end = path + path_l;
9351 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009352 url_param_name, url_param_name_l,
9353 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009354 if (!arg_start)
9355 return 0;
9356
9357 value_start = arg_start + url_param_name_l + 1;
9358 value_end = value_start;
9359
bedis4c75cca2012-10-05 08:38:24 +02009360 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009361 value_end++;
9362
9363 *value = value_start;
9364 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009365 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009366}
9367
9368static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009369smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9370 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009371{
bedis4c75cca2012-10-05 08:38:24 +02009372 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009373 struct http_txn *txn = l7;
9374 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009375
bedis4c75cca2012-10-05 08:38:24 +02009376 if (!args || args[0].type != ARGT_STR ||
9377 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009378 return 0;
9379
9380 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009381
bedis4c75cca2012-10-05 08:38:24 +02009382 if (args[1].type)
9383 delim = *args[1].data.str.str;
9384
Willy Tarreau9b28e032012-10-12 23:49:43 +02009385 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009386 args->data.str.str, args->data.str.len,
9387 &smp->data.str.str, &smp->data.str.len,
9388 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009389 return 0;
9390
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009391 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009392 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009393 return 1;
9394}
9395
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009396/* Return the signed integer value for the specified url parameter (see url_param
9397 * above).
9398 */
9399static int
9400smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9401 const struct arg *args, struct sample *smp)
9402{
9403 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9404
9405 if (ret > 0) {
9406 smp->type = SMP_T_UINT;
9407 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9408 }
9409
9410 return ret;
9411}
9412
Willy Tarreau185b5c42012-04-26 15:11:51 +02009413/* This function is used to validate the arguments passed to any "hdr" fetch
9414 * keyword. These keywords support an optional positive or negative occurrence
9415 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9416 * is assumed that the types are already the correct ones. Returns 0 on error,
9417 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9418 * error message in case of error, that the caller is responsible for freeing.
9419 * The initial location must either be freeable or NULL.
9420 */
9421static int val_hdr(struct arg *arg, char **err_msg)
9422{
9423 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009424 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009425 return 0;
9426 }
9427 return 1;
9428}
9429
Willy Tarreau4a568972010-05-12 08:08:50 +02009430/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009431/* All supported ACL keywords must be declared here. */
9432/************************************************************************/
9433
9434/* Note: must not be declared <const> as its list will be overwritten.
9435 * Please take care of keeping this list alphabetically sorted.
9436 */
9437static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009438 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9439 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9440 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9441 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9442 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9443 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9444 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9445 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
9446
Willy Tarreau51539362012-05-08 12:46:28 +02009447 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9448 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009449 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009450 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9451 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9452 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9453 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9454 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9455 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9456 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009457
Willy Tarreau185b5c42012-04-26 15:11:51 +02009458 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9459 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9460 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9461 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9462 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9463 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9464 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9465 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9466 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9467 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9468 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009469
9470 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009471 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009472 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9473
9474 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9475
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009476 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9477 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9478 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9479 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9480 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9481 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9482 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9483 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009484
9485 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9486 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9487 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
9488
Willy Tarreau51539362012-05-08 12:46:28 +02009489 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9490 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009491 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009492 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9493 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9494 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9495 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9496 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9497 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9498 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009499
Willy Tarreau185b5c42012-04-26 15:11:51 +02009500 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9501 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9502 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9503 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9504 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9505 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9506 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9507 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9508 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9509 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9510 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009511
9512 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9513
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009514 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9515 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9516 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9517 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9518 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9519 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9520 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9521 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9522 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9523 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009524
9525 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9526 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9527 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9528 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9529 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9530 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9531 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9532 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9533 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009534 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009535
9536 { NULL, NULL, NULL, NULL },
9537}};
9538
9539/************************************************************************/
9540/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009541/************************************************************************/
9542/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009543static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009544 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9545 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009546 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau4a550602012-12-09 14:53:32 +01009547 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009548 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9549 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9550 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9551 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9552 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9553 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9554 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02009555 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009556}};
9557
Willy Tarreau8797c062007-05-07 00:55:35 +02009558
9559__attribute__((constructor))
9560static void __http_protocol_init(void)
9561{
9562 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009563 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009564}
9565
9566
Willy Tarreau58f10d72006-12-04 02:26:12 +01009567/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009568 * Local variables:
9569 * c-indent-level: 8
9570 * c-basic-offset: 8
9571 * End:
9572 */