blob: d1bf51ead094fdb713449283a77ea19e8f8f2293 [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 Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040094
95/* same as 302 except that the browser MUST retry with the same method */
96const char *HTTP_307 =
97 "HTTP/1.1 307 Temporary Redirect\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Content-length: 0\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
102/* same as 301 except that the browser MUST retry with the same method */
103const char *HTTP_308 =
104 "HTTP/1.1 308 Permanent Redirect\r\n"
105 "Content-length: 0\r\n"
106 "Location: "; /* not terminated since it will be concatenated with the URL */
107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
109const char *HTTP_401_fmt =
110 "HTTP/1.0 401 Unauthorized\r\n"
111 "Cache-Control: no-cache\r\n"
112 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200113 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
115 "\r\n"
116 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
117
Willy Tarreau844a7e72010-01-31 21:46:18 +0100118const char *HTTP_407_fmt =
119 "HTTP/1.0 407 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau0f772532006-12-23 20:51:41 +0100127
128const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200129 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 [HTTP_ERR_400] = 400,
131 [HTTP_ERR_403] = 403,
132 [HTTP_ERR_408] = 408,
133 [HTTP_ERR_500] = 500,
134 [HTTP_ERR_502] = 502,
135 [HTTP_ERR_503] = 503,
136 [HTTP_ERR_504] = 504,
137};
138
Willy Tarreau80587432006-12-24 17:47:20 +0100139static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] =
141 "HTTP/1.0 200 OK\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
147
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100149 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
155
156 [HTTP_ERR_403] =
157 "HTTP/1.0 403 Forbidden\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
163
164 [HTTP_ERR_408] =
165 "HTTP/1.0 408 Request Time-out\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
171
172 [HTTP_ERR_500] =
173 "HTTP/1.0 500 Server Error\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
179
180 [HTTP_ERR_502] =
181 "HTTP/1.0 502 Bad Gateway\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
187
188 [HTTP_ERR_503] =
189 "HTTP/1.0 503 Service Unavailable\r\n"
190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
195
196 [HTTP_ERR_504] =
197 "HTTP/1.0 504 Gateway Time-out\r\n"
198 "Cache-Control: no-cache\r\n"
199 "Connection: close\r\n"
200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
203
204};
205
Cyril Bonté19979e12012-04-04 12:57:21 +0200206/* status codes available for the stats admin page (strictly 4 chars length) */
207const char *stat_status_codes[STAT_STATUS_SIZE] = {
208 [STAT_STATUS_DENY] = "DENY",
209 [STAT_STATUS_DONE] = "DONE",
210 [STAT_STATUS_ERRP] = "ERRP",
211 [STAT_STATUS_EXCD] = "EXCD",
212 [STAT_STATUS_NONE] = "NONE",
213 [STAT_STATUS_PART] = "PART",
214 [STAT_STATUS_UNKN] = "UNKN",
215};
216
217
Willy Tarreau80587432006-12-24 17:47:20 +0100218/* We must put the messages here since GCC cannot initialize consts depending
219 * on strlen().
220 */
221struct chunk http_err_chunks[HTTP_ERR_SIZE];
222
Willy Tarreaua890d072013-04-02 12:01:06 +0200223/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
224static struct hdr_ctx static_hdr_ctx;
225
Willy Tarreau42250582007-04-01 01:30:43 +0200226#define FD_SETS_ARE_BITFIELDS
227#ifdef FD_SETS_ARE_BITFIELDS
228/*
229 * This map is used with all the FD_* macros to check whether a particular bit
230 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
231 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
232 * byte should be encoded. Be careful to always pass bytes from 0 to 255
233 * exclusively to the macros.
234 */
235fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
236fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237
238#else
239#error "Check if your OS uses bitfields for fd_sets"
240#endif
241
Willy Tarreau80587432006-12-24 17:47:20 +0100242void init_proto_http()
243{
Willy Tarreau42250582007-04-01 01:30:43 +0200244 int i;
245 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100246 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200247
Willy Tarreau80587432006-12-24 17:47:20 +0100248 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
249 if (!http_err_msgs[msg]) {
250 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
251 abort();
252 }
253
254 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
255 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
256 }
Willy Tarreau42250582007-04-01 01:30:43 +0200257
258 /* initialize the log header encoding map : '{|}"#' should be encoded with
259 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
260 * URL encoding only requires '"', '#' to be encoded as well as non-
261 * printable characters above.
262 */
263 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
264 memset(url_encode_map, 0, sizeof(url_encode_map));
265 for (i = 0; i < 32; i++) {
266 FD_SET(i, hdr_encode_map);
267 FD_SET(i, url_encode_map);
268 }
269 for (i = 127; i < 256; i++) {
270 FD_SET(i, hdr_encode_map);
271 FD_SET(i, url_encode_map);
272 }
273
274 tmp = "\"#{|}";
275 while (*tmp) {
276 FD_SET(*tmp, hdr_encode_map);
277 tmp++;
278 }
279
280 tmp = "\"#";
281 while (*tmp) {
282 FD_SET(*tmp, url_encode_map);
283 tmp++;
284 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200285
286 /* memory allocations */
287 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100288 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100289}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
Willy Tarreau53b6c742006-12-17 13:37:46 +0100291/*
292 * We have 26 list of methods (1 per first letter), each of which can have
293 * up to 3 entries (2 valid, 1 null).
294 */
295struct http_method_desc {
296 http_meth_t meth;
297 int len;
298 const char text[8];
299};
300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100302 ['C' - 'A'] = {
303 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
304 },
305 ['D' - 'A'] = {
306 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
307 },
308 ['G' - 'A'] = {
309 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
310 },
311 ['H' - 'A'] = {
312 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
313 },
314 ['P' - 'A'] = {
315 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
316 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
317 },
318 ['T' - 'A'] = {
319 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
320 },
321 /* rest is empty like this :
322 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
323 */
324};
325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100326/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100328 * RFC2616 for those chars.
329 */
330
331const char http_is_spht[256] = {
332 [' '] = 1, ['\t'] = 1,
333};
334
335const char http_is_crlf[256] = {
336 ['\r'] = 1, ['\n'] = 1,
337};
338
339const char http_is_lws[256] = {
340 [' '] = 1, ['\t'] = 1,
341 ['\r'] = 1, ['\n'] = 1,
342};
343
344const char http_is_sep[256] = {
345 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
346 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
347 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
348 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
349 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
350};
351
352const char http_is_ctl[256] = {
353 [0 ... 31] = 1,
354 [127] = 1,
355};
356
357/*
358 * A token is any ASCII char that is neither a separator nor a CTL char.
359 * Do not overwrite values in assignment since gcc-2.95 will not handle
360 * them correctly. Instead, define every non-CTL char's status.
361 */
362const char http_is_token[256] = {
363 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
364 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
365 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
366 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
367 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
368 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
369 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
370 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
371 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
372 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
373 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
374 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
375 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
376 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
377 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
378 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
379 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
380 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
381 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
382 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
383 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
384 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
385 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
386 ['|'] = 1, ['}'] = 0, ['~'] = 1,
387};
388
389
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100390/*
391 * An http ver_token is any ASCII which can be found in an HTTP version,
392 * which includes 'H', 'T', 'P', '/', '.' and any digit.
393 */
394const char http_is_ver_token[256] = {
395 ['.'] = 1, ['/'] = 1,
396 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
397 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
398 ['H'] = 1, ['P'] = 1, ['T'] = 1,
399};
400
401
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100403 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
404 */
405#if defined(DEBUG_FSM)
406static void http_silent_debug(int line, struct session *s)
407{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100408 chunk_printf(&trash,
409 "[%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",
410 line,
411 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
412 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);
413 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100414
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100415 chunk_printf(&trash,
416 " %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",
417 line,
418 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
419 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);
420 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100421}
422#else
423#define http_silent_debug(l,s) do { } while (0)
424#endif
425
426/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100427 * Adds a header and its CRLF at the tail of the message's buffer, just before
428 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100429 * The header is also automatically added to the index <hdr_idx>, and the end
430 * of headers is automatically adjusted. The number of bytes added is returned
431 * on success, otherwise <0 is returned indicating an error.
432 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100433int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434{
435 int bytes, len;
436
437 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200438 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439 if (!bytes)
440 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100441 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100442 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
443}
444
445/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100446 * Adds a header and its CRLF at the tail of the message's buffer, just before
447 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 * the buffer is only opened and the space reserved, but nothing is copied.
449 * The header is also automatically added to the index <hdr_idx>, and the end
450 * of headers is automatically adjusted. The number of bytes added is returned
451 * on success, otherwise <0 is returned indicating an error.
452 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100453int http_header_add_tail2(struct http_msg *msg,
454 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100455{
456 int bytes;
457
Willy Tarreau9b28e032012-10-12 23:49:43 +0200458 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 if (!bytes)
460 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100461 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100462 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
465/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100466 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
467 * If so, returns the position of the first non-space character relative to
468 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
469 * to return a pointer to the place after the first space. Returns 0 if the
470 * header name does not match. Checks are case-insensitive.
471 */
472int http_header_match2(const char *hdr, const char *end,
473 const char *name, int len)
474{
475 const char *val;
476
477 if (hdr + len >= end)
478 return 0;
479 if (hdr[len] != ':')
480 return 0;
481 if (strncasecmp(hdr, name, len) != 0)
482 return 0;
483 val = hdr + len + 1;
484 while (val < end && HTTP_IS_SPHT(*val))
485 val++;
486 if ((val >= end) && (len + 2 <= end - hdr))
487 return len + 2; /* we may replace starting from second space */
488 return val - hdr;
489}
490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491/* Find the end of the header value contained between <s> and <e>. See RFC2616,
492 * par 2.2 for more information. Note that it requires a valid header to return
493 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100495char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496{
497 int quoted, qdpair;
498
499 quoted = qdpair = 0;
500 for (; s < e; s++) {
501 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200502 else if (quoted) {
503 if (*s == '\\') qdpair = 1;
504 else if (*s == '"') quoted = 0;
505 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 else if (*s == '"') quoted = 1;
507 else if (*s == ',') return s;
508 }
509 return s;
510}
511
512/* Find the first or next occurrence of header <name> in message buffer <sol>
513 * using headers index <idx>, and return it in the <ctx> structure. This
514 * structure holds everything necessary to use the header and find next
515 * occurrence. If its <idx> member is 0, the header is searched from the
516 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 * 1 when it finds a value, and 0 when there is no more. It is designed to work
518 * with headers defined as comma-separated lists. As a special case, if ctx->val
519 * is NULL when searching for a new values of a header, the current header is
520 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 */
522int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100523 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 struct hdr_ctx *ctx)
525{
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 char *eol, *sov;
527 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 cur_idx = ctx->idx;
530 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 /* We have previously returned a value, let's search
532 * another one on the same line.
533 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200535 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100536 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200537 eol = sol + idx->v[cur_idx].len;
538
539 if (sov >= eol)
540 /* no more values in this header */
541 goto next_hdr;
542
Willy Tarreau68085d82010-01-18 14:54:04 +0100543 /* values remaining for this header, skip the comma but save it
544 * for later use (eg: for header deletion).
545 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200546 sov++;
547 while (sov < eol && http_is_lws[(unsigned char)*sov])
548 sov++;
549
550 goto return_hdr;
551 }
552
553 /* first request for this header */
554 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 while (cur_idx) {
558 eol = sol + idx->v[cur_idx].len;
559
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200560 if (len == 0) {
561 /* No argument was passed, we want any header.
562 * To achieve this, we simply build a fake request. */
563 while (sol + len < eol && sol[len] != ':')
564 len++;
565 name = sol;
566 }
567
Willy Tarreau33a7e692007-06-10 19:45:56 +0200568 if ((len < eol - sol) &&
569 (sol[len] == ':') &&
570 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 sov = sol + len + 1;
573 while (sov < eol && http_is_lws[(unsigned char)*sov])
574 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100575
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 ctx->prev = old_idx;
578 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 ctx->idx = cur_idx;
580 ctx->val = sov - sol;
581
582 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200583 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200584 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200585 eol--;
586 ctx->tws++;
587 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200588 ctx->vlen = eol - sov;
589 return 1;
590 }
591 next_hdr:
592 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 cur_idx = idx->v[cur_idx].next;
595 }
596 return 0;
597}
598
599int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100600 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 struct hdr_ctx *ctx)
602{
603 return http_find_header2(name, strlen(name), sol, idx, ctx);
604}
605
Willy Tarreau68085d82010-01-18 14:54:04 +0100606/* Remove one value of a header. This only works on a <ctx> returned by one of
607 * the http_find_header functions. The value is removed, as well as surrounding
608 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100609 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 * message <msg>. The new index is returned. If it is zero, it means there is
611 * no more header, so any processing may stop. The ctx is always left in a form
612 * that can be handled by http_find_header2() to find next occurrence.
613 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100614int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100615{
616 int cur_idx = ctx->idx;
617 char *sol = ctx->line;
618 struct hdr_idx_elem *hdr;
619 int delta, skip_comma;
620
621 if (!cur_idx)
622 return 0;
623
624 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200625 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200627 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 http_msg_move_end(msg, delta);
629 idx->used--;
630 hdr->len = 0; /* unused entry */
631 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100632 if (idx->tail == ctx->idx)
633 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 ctx->idx = ctx->prev; /* walk back to the end of previous header */
635 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
636 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
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
641 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200642 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
643 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100644 */
645
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200646 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200647 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200648 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100649 NULL, 0);
650 hdr->len += delta;
651 http_msg_move_end(msg, delta);
652 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 return ctx->idx;
655}
656
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100657/* This function handles a server error at the stream interface level. The
658 * stream interface is assumed to be already in a closed state. An optional
659 * message is copied into the input buffer, and an HTTP status code stored.
660 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100661 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100663static void http_server_error(struct session *t, struct stream_interface *si,
664 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200666 channel_auto_read(si->ob);
667 channel_abort(si->ob);
668 channel_auto_close(si->ob);
669 channel_erase(si->ob);
670 channel_auto_close(si->ib);
671 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100672 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100673 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200674 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676 if (!(t->flags & SN_ERR_MASK))
677 t->flags |= err;
678 if (!(t->flags & SN_FINST_MASK))
679 t->flags |= finst;
680}
681
Willy Tarreau80587432006-12-24 17:47:20 +0100682/* This function returns the appropriate error location for the given session
683 * and message.
684 */
685
Willy Tarreau783f2582012-09-04 12:19:04 +0200686struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100687{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200688 if (s->be->errmsg[msgnum].str)
689 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100690 else if (s->fe->errmsg[msgnum].str)
691 return &s->fe->errmsg[msgnum];
692 else
693 return &http_err_chunks[msgnum];
694}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696/*
697 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
698 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
699 */
700static http_meth_t find_http_meth(const char *str, const int len)
701{
702 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100703 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100704
705 m = ((unsigned)*str - 'A');
706
707 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100708 for (h = http_methods[m]; h->len > 0; h++) {
709 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100710 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100711 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100712 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100713 };
714 return HTTP_METH_OTHER;
715 }
716 return HTTP_METH_NONE;
717
718}
719
Willy Tarreau21d2af32008-02-14 20:25:24 +0100720/* Parse the URI from the given transaction (which is assumed to be in request
721 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
722 * It is returned otherwise.
723 */
724static char *
725http_get_path(struct http_txn *txn)
726{
727 char *ptr, *end;
728
Willy Tarreau9b28e032012-10-12 23:49:43 +0200729 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100730 end = ptr + txn->req.sl.rq.u_l;
731
732 if (ptr >= end)
733 return NULL;
734
735 /* RFC2616, par. 5.1.2 :
736 * Request-URI = "*" | absuri | abspath | authority
737 */
738
739 if (*ptr == '*')
740 return NULL;
741
742 if (isalpha((unsigned char)*ptr)) {
743 /* this is a scheme as described by RFC3986, par. 3.1 */
744 ptr++;
745 while (ptr < end &&
746 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
747 ptr++;
748 /* skip '://' */
749 if (ptr == end || *ptr++ != ':')
750 return NULL;
751 if (ptr == end || *ptr++ != '/')
752 return NULL;
753 if (ptr == end || *ptr++ != '/')
754 return NULL;
755 }
756 /* skip [user[:passwd]@]host[:[port]] */
757
758 while (ptr < end && *ptr != '/')
759 ptr++;
760
761 if (ptr == end)
762 return NULL;
763
764 /* OK, we got the '/' ! */
765 return ptr;
766}
767
Willy Tarreau71241ab2012-12-27 11:30:54 +0100768/* Returns a 302 for a redirectable request that reaches a server working in
769 * in redirect mode. This may only be called just after the stream interface
770 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
771 * follow normal proxy processing. NOTE: this function is designed to support
772 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100774void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775{
776 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200779 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
781 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100782 trash.len = strlen(HTTP_302);
783 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100788 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 return;
790
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100791 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100792 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100793 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
794 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100795 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200797 /* 3: add the request URI. Since it was already forwarded, we need
798 * to temporarily rewind the buffer.
799 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200801 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200802
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200804 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 +0200805
Willy Tarreau9b28e032012-10-12 23:49:43 +0200806 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200807
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 if (!path)
809 return;
810
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100811 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 return;
813
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100814 memcpy(trash.str + trash.len, path, len);
815 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100816
817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
819 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100820 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100821 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
822 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100823 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824
825 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200826 si_shutr(si);
827 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 si->err_type = SI_ET_NONE;
829 si->err_loc = NULL;
830 si->state = SI_ST_CLO;
831
832 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100833 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834
835 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100836 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837}
838
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 * that the server side is closed. Note that err_type is actually a
841 * bitmask, where almost only aborts may be cumulated with other
842 * values. We consider that aborted operations are more important
843 * than timeouts or errors due to the fact that nobody else in the
844 * logs might explain incomplete retries. All others should avoid
845 * being cumulated. It should normally not be possible to have multiple
846 * aborts at once, but just in case, the first one in sequence is reported.
847 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100848void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100850 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851
852 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
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 if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100859 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200860 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100861 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100862 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200863 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100864 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100865 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200866 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100867 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100868 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200869 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100871 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200872 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873}
874
Willy Tarreau42250582007-04-01 01:30:43 +0200875extern const char sess_term_cond[8];
876extern const char sess_fin_state[8];
877extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200878struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100879struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100880struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881
Willy Tarreau117f59e2007-03-04 18:17:17 +0100882/*
883 * Capture headers from message starting at <som> according to header list
884 * <cap_hdr>, and fill the <idx> structure appropriately.
885 */
886void capture_headers(char *som, struct hdr_idx *idx,
887 char **cap, struct cap_hdr *cap_hdr)
888{
889 char *eol, *sol, *col, *sov;
890 int cur_idx;
891 struct cap_hdr *h;
892 int len;
893
894 sol = som + hdr_idx_first_pos(idx);
895 cur_idx = hdr_idx_first_idx(idx);
896
897 while (cur_idx) {
898 eol = sol + idx->v[cur_idx].len;
899
900 col = sol;
901 while (col < eol && *col != ':')
902 col++;
903
904 sov = col + 1;
905 while (sov < eol && http_is_lws[(unsigned char)*sov])
906 sov++;
907
908 for (h = cap_hdr; h; h = h->next) {
909 if ((h->namelen == col - sol) &&
910 (strncasecmp(sol, h->name, h->namelen) == 0)) {
911 if (cap[h->index] == NULL)
912 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200913 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100914
915 if (cap[h->index] == NULL) {
916 Alert("HTTP capture : out of memory.\n");
917 continue;
918 }
919
920 len = eol - sov;
921 if (len > h->len)
922 len = h->len;
923
924 memcpy(cap[h->index], sov, len);
925 cap[h->index][len]=0;
926 }
927 }
928 sol = eol + idx->v[cur_idx].cr + 1;
929 cur_idx = idx->v[cur_idx].next;
930 }
931}
932
933
Willy Tarreau42250582007-04-01 01:30:43 +0200934/* either we find an LF at <ptr> or we jump to <bad>.
935 */
936#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
937
938/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
939 * otherwise to <http_msg_ood> with <state> set to <st>.
940 */
941#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
942 ptr++; \
943 if (likely(ptr < end)) \
944 goto good; \
945 else { \
946 state = (st); \
947 goto http_msg_ood; \
948 } \
949 } while (0)
950
951
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100953 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
955 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
956 * will give undefined results.
957 * Note that it is upon the caller's responsibility to ensure that ptr < end,
958 * and that msg->sol points to the beginning of the response.
959 * If a complete line is found (which implies that at least one CR or LF is
960 * found before <end>, the updated <ptr> is returned, otherwise NULL is
961 * returned indicating an incomplete line (which does not mean that parts have
962 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
963 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
964 * upon next call.
965 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200966 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
968 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200969 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200971const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100972 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100973 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100974{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200975 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100976
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200979 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100980 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100981 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
982
983 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100984 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
986 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100987 state = HTTP_MSG_ERROR;
988 break;
989
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200991 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 goto http_msg_rpcode;
995 }
996 if (likely(HTTP_IS_SPHT(*ptr)))
997 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
998 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100999 state = HTTP_MSG_ERROR;
1000 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001001
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001003 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 if (likely(!HTTP_IS_LWS(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1006
1007 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1010 }
1011
1012 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 http_msg_rsp_reason:
1015 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 msg->sl.st.r_l = 0;
1018 goto http_msg_rpline_eol;
1019
Willy Tarreau8973c702007-01-21 23:58:29 +01001020 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001021 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001023 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 goto http_msg_rpreason;
1025 }
1026 if (likely(HTTP_IS_SPHT(*ptr)))
1027 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1028 /* so it's a CR/LF, so there is no reason phrase */
1029 goto http_msg_rsp_reason;
1030
Willy Tarreau8973c702007-01-21 23:58:29 +01001031 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001032 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 if (likely(!HTTP_IS_CRLF(*ptr)))
1034 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001035 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 http_msg_rpline_eol:
1037 /* We have seen the end of line. Note that we do not
1038 * necessarily have the \n yet, but at least we know that we
1039 * have EITHER \r OR \n, otherwise the response would not be
1040 * complete. We can then record the response length and return
1041 * to the caller which will be able to register it.
1042 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001043 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return ptr;
1045
1046#ifdef DEBUG_FULL
1047 default:
1048 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1049 exit(1);
1050#endif
1051 }
1052
1053 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001054 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 if (ret_state)
1056 *ret_state = state;
1057 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001058 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001060}
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * This function parses a request line between <ptr> and <end>, starting with
1064 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1065 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1066 * will give undefined results.
1067 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1068 * and that msg->sol points to the beginning of the request.
1069 * If a complete line is found (which implies that at least one CR or LF is
1070 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1071 * returned indicating an incomplete line (which does not mean that parts have
1072 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1073 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1074 * upon next call.
1075 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001076 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1078 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001079 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001081const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001082 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001083 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001085 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001089 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 if (likely(HTTP_IS_TOKEN(*ptr)))
1091 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 if (likely(HTTP_IS_CRLF(*ptr))) {
1099 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001100 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 msg->sl.rq.v_l = 0;
1108 goto http_msg_rqline_eol;
1109 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001110 state = HTTP_MSG_ERROR;
1111 break;
1112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001116 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 goto http_msg_rquri;
1118 }
1119 if (likely(HTTP_IS_SPHT(*ptr)))
1120 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1121 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1122 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001125 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001126 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001130 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001133
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001135 /* non-ASCII chars are forbidden unless option
1136 * accept-invalid-http-request is enabled in the frontend.
1137 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001138 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001139 if (msg->err_pos < -1)
1140 goto invalid_char;
1141 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001142 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001143 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1144 }
1145
1146 if (likely(HTTP_IS_CRLF(*ptr))) {
1147 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1148 goto http_msg_req09_uri_e;
1149 }
1150
1151 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001152 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001153 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001154 state = HTTP_MSG_ERROR;
1155 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 goto http_msg_rqver;
1162 }
1163 if (likely(HTTP_IS_SPHT(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1165 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1166 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001170 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172
1173 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001174 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001175 http_msg_rqline_eol:
1176 /* We have seen the end of line. Note that we do not
1177 * necessarily have the \n yet, but at least we know that we
1178 * have EITHER \r OR \n, otherwise the request would not be
1179 * complete. We can then record the request length and return
1180 * to the caller which will be able to register it.
1181 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001182 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001183 return ptr;
1184 }
1185
1186 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001187 state = HTTP_MSG_ERROR;
1188 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190#ifdef DEBUG_FULL
1191 default:
1192 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1193 exit(1);
1194#endif
1195 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001198 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 if (ret_state)
1200 *ret_state = state;
1201 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001202 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001203 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001205
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001206/*
1207 * Returns the data from Authorization header. Function may be called more
1208 * than once so data is stored in txn->auth_data. When no header is found
1209 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1210 * searching again for something we are unable to find anyway.
1211 */
1212
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001213char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001214
1215int
1216get_http_auth(struct session *s)
1217{
1218
1219 struct http_txn *txn = &s->txn;
1220 struct chunk auth_method;
1221 struct hdr_ctx ctx;
1222 char *h, *p;
1223 int len;
1224
1225#ifdef DEBUG_AUTH
1226 printf("Auth for session %p: %d\n", s, txn->auth.method);
1227#endif
1228
1229 if (txn->auth.method == HTTP_AUTH_WRONG)
1230 return 0;
1231
1232 if (txn->auth.method)
1233 return 1;
1234
1235 txn->auth.method = HTTP_AUTH_WRONG;
1236
1237 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001238
1239 if (txn->flags & TX_USE_PX_CONN) {
1240 h = "Proxy-Authorization";
1241 len = strlen(h);
1242 } else {
1243 h = "Authorization";
1244 len = strlen(h);
1245 }
1246
Willy Tarreau9b28e032012-10-12 23:49:43 +02001247 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 return 0;
1249
1250 h = ctx.line + ctx.val;
1251
1252 p = memchr(h, ' ', ctx.vlen);
1253 if (!p || p == h)
1254 return 0;
1255
1256 chunk_initlen(&auth_method, h, 0, p-h);
1257 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1258
1259 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1260
1261 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001262 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001263
1264 if (len < 0)
1265 return 0;
1266
1267
1268 get_http_auth_buff[len] = '\0';
1269
1270 p = strchr(get_http_auth_buff, ':');
1271
1272 if (!p)
1273 return 0;
1274
1275 txn->auth.user = get_http_auth_buff;
1276 *p = '\0';
1277 txn->auth.pass = p+1;
1278
1279 txn->auth.method = HTTP_AUTH_BASIC;
1280 return 1;
1281 }
1282
1283 return 0;
1284}
1285
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8973c702007-01-21 23:58:29 +01001287/*
1288 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001289 * depending on the initial msg->msg_state. The caller is responsible for
1290 * ensuring that the message does not wrap. The function can be preempted
1291 * everywhere when data are missing and recalled at the exact same location
1292 * with no information loss. The message may even be realigned between two
1293 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001294 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001295 * fields. Note that msg->sol will be initialized after completing the first
1296 * state, so that none of the msg pointers has to be initialized prior to the
1297 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001299void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001303 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001304
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001305 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001306 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001307 ptr = buf->p + msg->next;
1308 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 if (unlikely(ptr >= end))
1311 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001314 /*
1315 * First, states that are specific to the response only.
1316 * We check them first so that request and headers are
1317 * closer to each other (accessed more often).
1318 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001320 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001322 /* we have a start of message, but we have to check
1323 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001324 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001325 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001326 if (unlikely(ptr != buf->p)) {
1327 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001328 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001329 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001330 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 }
Willy Tarreau26927362012-05-18 23:22:52 +02001332 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001333 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 hdr_idx_init(idx);
1335 state = HTTP_MSG_RPVER;
1336 goto http_msg_rpver;
1337 }
1338
1339 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1340 goto http_msg_invalid;
1341
1342 if (unlikely(*ptr == '\n'))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1345 /* stop here */
1346
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001348 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 EXPECT_LF_HERE(ptr, http_msg_invalid);
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1351 /* stop here */
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 case HTTP_MSG_RPVER_SP:
1356 case HTTP_MSG_RPCODE:
1357 case HTTP_MSG_RPCODE_SP:
1358 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001359 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001360 state, ptr, end,
1361 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 if (unlikely(!ptr))
1363 return;
1364
1365 /* we have a full response and we know that we have either a CR
1366 * or an LF at <ptr>.
1367 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1369
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001370 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 if (likely(*ptr == '\r'))
1372 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1373 goto http_msg_rpline_end;
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 /* msg->sol must point to the first of CR or LF. */
1378 EXPECT_LF_HERE(ptr, http_msg_invalid);
1379 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1380 /* stop here */
1381
1382 /*
1383 * Second, states that are specific to the request only
1384 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001386 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001388 /* we have a start of message, but we have to check
1389 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001390 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001392 if (likely(ptr != buf->p)) {
1393 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001395 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001396 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 }
Willy Tarreau26927362012-05-18 23:22:52 +02001398 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001399 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 state = HTTP_MSG_RQMETH;
1401 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1405 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (unlikely(*ptr == '\n'))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EXPECT_LF_HERE(ptr, http_msg_invalid);
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001419 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 case HTTP_MSG_RQMETH_SP:
1421 case HTTP_MSG_RQURI:
1422 case HTTP_MSG_RQURI_SP:
1423 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001424 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001425 state, ptr, end,
1426 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (unlikely(!ptr))
1428 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 /* we have a full request and we know that we have either a CR
1431 * or an LF at <ptr>.
1432 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001435 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 if (likely(*ptr == '\r'))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 /* check for HTTP/0.9 request : no version information available.
1443 * msg->sol must point to the first of CR or LF.
1444 */
1445 if (unlikely(msg->sl.rq.v_l == 0))
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EXPECT_LF_HERE(ptr, http_msg_invalid);
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /*
1453 * Common states below
1454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001457 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (likely(!HTTP_IS_CRLF(*ptr))) {
1459 goto http_msg_hdr_name;
1460 }
1461
1462 if (likely(*ptr == '\r'))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1464 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 /* assumes msg->sol points to the first char */
1469 if (likely(HTTP_IS_TOKEN(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001472 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001475 if (likely(msg->err_pos < -1) || *ptr == '\n')
1476 goto http_msg_invalid;
1477
1478 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001480
1481 /* and we still accept this non-token character */
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001486 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 if (likely(HTTP_IS_SPHT(*ptr)))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001491 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(!HTTP_IS_CRLF(*ptr))) {
1494 goto http_msg_hdr_val;
1495 }
1496
1497 if (likely(*ptr == '\r'))
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1499 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001502 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 EXPECT_LF_HERE(ptr, http_msg_invalid);
1504 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(HTTP_IS_SPHT(*ptr))) {
1509 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001510 for (; buf->p + msg->sov < ptr; msg->sov++)
1511 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_hdr_l1_sp;
1513 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001514 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001515 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 goto http_msg_complete_header;
1517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001519 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001520 /* assumes msg->sol points to the first char, and msg->sov
1521 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 */
1523 if (likely(!HTTP_IS_CRLF(*ptr)))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 /* Note: we could also copy eol into ->eoh so that we have the
1528 * real header end in case it ends with lots of LWS, but is this
1529 * really needed ?
1530 */
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1533 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1543 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 for (; buf->p + msg->eol < ptr; msg->eol++)
1545 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 goto http_msg_hdr_val;
1547 }
1548 http_msg_complete_header:
1549 /*
1550 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001551 * Assumes msg->sol points to the first char, msg->sov points
1552 * to the first character of the value and msg->eol to the
1553 * first CR or LF so we know how the line ends. We insert last
1554 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001556 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 idx, idx->tail) < 0))
1558 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(!HTTP_IS_CRLF(*ptr))) {
1562 goto http_msg_hdr_name;
1563 }
1564
1565 if (likely(*ptr == '\r'))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1567 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 /* Assumes msg->sol points to the first of either CR or LF */
1572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001574 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001575 msg->eoh = msg->sol;
1576 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001579
1580 case HTTP_MSG_ERROR:
1581 /* this may only happen if we call http_msg_analyser() twice with an error */
1582 break;
1583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584#ifdef DEBUG_FULL
1585 default:
1586 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1587 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001588#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 }
1590 http_msg_ood:
1591 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001592 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001593 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 http_msg_invalid:
1597 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001598 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001599 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 return;
1601}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001602
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1604 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1605 * nothing is done and 1 is returned.
1606 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001607static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608{
1609 int delta;
1610 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612
1613 if (msg->sl.rq.v_l != 0)
1614 return 1;
1615
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 delta = 0;
1618
1619 if (msg->sl.rq.u_l == 0) {
1620 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001621 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001623 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001624 }
1625 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001626 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001627 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001628 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001629 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001630 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001631 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001632 NULL, NULL);
1633 if (unlikely(!cur_end))
1634 return 0;
1635
1636 /* we have a full HTTP/1.0 request now and we know that
1637 * we have either a CR or an LF at <ptr>.
1638 */
1639 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1640 return 1;
1641}
1642
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001644 * and "keep-alive" values. If we already know that some headers may safely
1645 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1647 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001648 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1650 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1651 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001652 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001655{
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001657 const char *hdr_val = "Connection";
1658 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001659
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001661 return;
1662
Willy Tarreau88d349d2010-01-25 12:15:43 +01001663 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1664 hdr_val = "Proxy-Connection";
1665 hdr_len = 16;
1666 }
1667
Willy Tarreau5b154472009-12-21 20:11:07 +01001668 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001669 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001670 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1672 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001673 if (to_del & 2)
1674 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 else
1676 txn->flags |= TX_CON_KAL_SET;
1677 }
1678 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1679 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001680 if (to_del & 1)
1681 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682 else
1683 txn->flags |= TX_CON_CLO_SET;
1684 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001685 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1686 txn->flags |= TX_HDR_CONN_UPG;
1687 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001688 }
1689
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690 txn->flags |= TX_HDR_CONN_PRS;
1691 return;
1692}
Willy Tarreau5b154472009-12-21 20:11:07 +01001693
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694/* Apply desired changes on the Connection: header. Values may be removed and/or
1695 * added depending on the <wanted> flags, which are exclusively composed of
1696 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1697 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1698 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001699void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700{
1701 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001702 const char *hdr_val = "Connection";
1703 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704
1705 ctx.idx = 0;
1706
Willy Tarreau88d349d2010-01-25 12:15:43 +01001707
1708 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1709 hdr_val = "Proxy-Connection";
1710 hdr_len = 16;
1711 }
1712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001714 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1716 if (wanted & TX_CON_KAL_SET)
1717 txn->flags |= TX_CON_KAL_SET;
1718 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001720 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1722 if (wanted & TX_CON_CLO_SET)
1723 txn->flags |= TX_CON_CLO_SET;
1724 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001727 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001728
1729 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1730 return;
1731
1732 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1733 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001734 hdr_val = "Connection: close";
1735 hdr_len = 17;
1736 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1737 hdr_val = "Proxy-Connection: close";
1738 hdr_len = 23;
1739 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001740 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 }
1742
1743 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1744 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001745 hdr_val = "Connection: keep-alive";
1746 hdr_len = 22;
1747 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1748 hdr_val = "Proxy-Connection: keep-alive";
1749 hdr_len = 28;
1750 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001751 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 }
1753 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001754}
1755
Willy Tarreaua458b672012-03-05 11:17:50 +01001756/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001758 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001760 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001761 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001762static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001764 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001765 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001766 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 const char *end = buf->data + buf->size;
1768 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 unsigned int chunk = 0;
1770
1771 /* The chunk size is in the following form, though we are only
1772 * interested in the size and CRLF :
1773 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1774 */
1775 while (1) {
1776 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001777 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001778 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 if (c < 0) /* not a hex digit anymore */
1781 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001782 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001783 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001784 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001785 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001786 chunk = (chunk << 4) + c;
1787 }
1788
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001790 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001791 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792
1793 while (http_is_spht[(unsigned char)*ptr]) {
1794 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001796 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001797 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001798 }
1799
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 /* Up to there, we know that at least one byte is present at *ptr. Check
1801 * for the end of chunk size.
1802 */
1803 while (1) {
1804 if (likely(HTTP_IS_CRLF(*ptr))) {
1805 /* we now have a CR or an LF at ptr */
1806 if (likely(*ptr == '\r')) {
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 Tarreaud98cf932009-12-27 22:54:55 +01001810 return 0;
1811 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001812
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001814 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001816 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 /* done */
1818 break;
1819 }
1820 else if (*ptr == ';') {
1821 /* chunk extension, ends at next CRLF */
1822 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001823 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001824 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826
1827 while (!HTTP_IS_CRLF(*ptr)) {
1828 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001830 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 return 0;
1832 }
1833 /* we have a CRLF now, loop above */
1834 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 }
1839
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001843 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001844 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001848 msg->chunk_len = chunk;
1849 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001851 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001852 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855}
1856
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001857/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001858 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 * the trailers is found, it is automatically scheduled to be forwarded,
1860 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1861 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001863 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001864 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1866 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001867 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001868 * matches the length of trailers already parsed and not forwarded. It is also
1869 * important to note that this function is designed to be able to parse wrapped
1870 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001872static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001875
Willy Tarreaua458b672012-03-05 11:17:50 +01001876 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001878 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 const char *ptr = b_ptr(buf, msg->next);
1880 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001881 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882
1883 /* scan current line and stop at LF or CRLF */
1884 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001885 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 return 0;
1887
1888 if (*ptr == '\n') {
1889 if (!p1)
1890 p1 = ptr;
1891 p2 = ptr;
1892 break;
1893 }
1894
1895 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001896 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001899 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 p1 = ptr;
1901 }
1902
1903 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (ptr >= buf->data + buf->size)
1905 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 }
1907
1908 /* after LF; point to beginning of next line */
1909 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 if (p2 >= buf->data + buf->size)
1911 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001914 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001916
1917 /* schedule this line for forwarding */
1918 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 if (msg->sov >= buf->size)
1920 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001923 /* LF/CRLF at beginning of line => end of trailers at p2.
1924 * Everything was scheduled for forwarding, there's nothing
1925 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001926 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 msg->msg_state = HTTP_MSG_DONE;
1929 return 1;
1930 }
1931 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 }
1934}
1935
Willy Tarreau54d23df2012-10-25 19:04:45 +02001936/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001938 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1941 * not enough data are available, the function does not change anything and
1942 * returns zero. If a parse error is encountered, the function returns < 0 and
1943 * does not change anything. Note: this function is designed to parse wrapped
1944 * CRLF at the end of the buffer.
1945 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001946static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001948 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001949 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 int bytes;
1951
1952 /* NB: we'll check data availabilty at the end. It's not a
1953 * problem because whatever we match first will be checked
1954 * against the correct length.
1955 */
1956 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 if (*ptr == '\r') {
1959 bytes++;
1960 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 if (ptr >= buf->data + buf->size)
1962 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 }
1964
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 return 0;
1967
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972
1973 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02001974 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001976 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1977 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001978 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1980 return 1;
1981}
Willy Tarreau5b154472009-12-21 20:11:07 +01001982
William Lallemand82fe75c2012-10-23 10:25:10 +02001983
1984/*
1985 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001986 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001987int select_compression_request_header(struct session *s, struct buffer *req)
1988{
1989 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001990 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001991 struct hdr_ctx ctx;
1992 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001993 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001994
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001995 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1996 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001997 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1998 */
1999 ctx.idx = 0;
2000 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2001 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002002 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2003 (ctx.vlen < 31 ||
2004 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2005 ctx.line[ctx.val + 30] < '6' ||
2006 (ctx.line[ctx.val + 30] == '6' &&
2007 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2008 s->comp_algo = NULL;
2009 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002010 }
2011
William Lallemand82fe75c2012-10-23 10:25:10 +02002012 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002013 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 +02002014 ctx.idx = 0;
2015 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002016 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002017 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2018 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002019
2020 /* remove all occurrences of the header when "compression offload" is set */
2021
2022 if ((s->be->comp && s->be->comp->offload) ||
2023 (s->fe->comp && s->fe->comp->offload)) {
2024 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2025 ctx.idx = 0;
2026 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2027 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2028 }
2029 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002030 return 1;
2031 }
2032 }
2033 }
2034 }
2035
2036 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002037 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2038 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002039 if (comp_algo->add_data == identity_add_data) {
2040 s->comp_algo = comp_algo;
2041 return 1;
2042 }
2043 }
2044 }
2045
2046 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002047 return 0;
2048}
2049
2050/*
2051 * Selects a comression algorithm depending of the server response.
2052 */
2053int select_compression_response_header(struct session *s, struct buffer *res)
2054{
2055 struct http_txn *txn = &s->txn;
2056 struct http_msg *msg = &txn->rsp;
2057 struct hdr_ctx ctx;
2058 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002059
2060 /* no common compression algorithm was found in request header */
2061 if (s->comp_algo == NULL)
2062 goto fail;
2063
2064 /* HTTP < 1.1 should not be compressed */
2065 if (!(msg->flags & HTTP_MSGF_VER_11))
2066 goto fail;
2067
William Lallemandd3002612012-11-26 14:34:47 +01002068 /* 200 only */
2069 if (txn->status != 200)
2070 goto fail;
2071
William Lallemand82fe75c2012-10-23 10:25:10 +02002072 /* Content-Length is null */
2073 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2074 goto fail;
2075
Willy Tarreau667c2a32013-04-09 08:13:58 +02002076 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2077 if (msg->flags & HTTP_MSGF_TE_CHNK)
2078 goto fail;
2079
William Lallemand82fe75c2012-10-23 10:25:10 +02002080 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002081 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002082 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2083 goto fail;
2084
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002085 /* no compression when Cache-Control: no-transform is present in the message */
2086 ctx.idx = 0;
2087 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2088 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2089 goto fail;
2090 }
2091
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 comp_type = NULL;
2093
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002094 /* we don't want to compress multipart content-types, nor content-types that are
2095 * not listed in the "compression type" directive if any. If no content-type was
2096 * found but configuration requires one, we don't compress either. Backend has
2097 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002098 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002099 ctx.idx = 0;
2100 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2101 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2102 goto fail;
2103
2104 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2105 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002106 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002107 if (ctx.vlen >= comp_type->name_len &&
2108 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002109 /* this Content-Type should be compressed */
2110 break;
2111 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002112 /* this Content-Type should not be compressed */
2113 if (comp_type == NULL)
2114 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002115 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002116 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002117 else { /* no content-type header */
2118 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2119 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002120 }
2121
William Lallemandd85f9172012-11-09 17:05:39 +01002122 /* limit compression rate */
2123 if (global.comp_rate_lim > 0)
2124 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2125 goto fail;
2126
William Lallemand072a2bf2012-11-20 17:01:01 +01002127 /* limit cpu usage */
2128 if (idle_pct < compress_min_idle)
2129 goto fail;
2130
William Lallemand4c49fae2012-11-07 15:00:23 +01002131 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002132 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002133 goto fail;
2134
William Lallemandec3e3892012-11-12 17:02:18 +01002135 s->flags |= SN_COMP_READY;
2136
William Lallemand82fe75c2012-10-23 10:25:10 +02002137 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002138 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002139 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2140 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2141
2142 /* add Transfer-Encoding header */
2143 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2144 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2145
2146 /*
2147 * Add Content-Encoding header when it's not identity encoding.
2148 * RFC 2616 : Identity encoding: This content-coding is used only in the
2149 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2150 * header.
2151 */
2152 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002153 trash.len = 18;
2154 memcpy(trash.str, "Content-Encoding: ", trash.len);
2155 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2156 trash.len += s->comp_algo->name_len;
2157 trash.str[trash.len] = '\0';
2158 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002159 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002160 return 1;
2161
2162fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002163 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002164 return 0;
2165}
2166
2167
Willy Tarreaud787e662009-07-07 10:14:51 +02002168/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2169 * processing can continue on next analysers, or zero if it either needs more
2170 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2171 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2172 * when it has nothing left to do, and may remove any analyser when it wants to
2173 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002174 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002175int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002176{
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 /*
2178 * We will parse the partial (or complete) lines.
2179 * We will check the request syntax, and also join multi-line
2180 * headers. An index of all the lines will be elaborated while
2181 * parsing.
2182 *
2183 * For the parsing, we use a 28 states FSM.
2184 *
2185 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002186 * req->buf->p = beginning of request
2187 * req->buf->p + msg->eoh = end of processed headers / start of current one
2188 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002189 * msg->eol = end of current header or line (LF or CRLF)
2190 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002191 *
2192 * At end of parsing, we may perform a capture of the error (if any), and
2193 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2194 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2195 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002199 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 struct http_txn *txn = &s->txn;
2201 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002202 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002203
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002204 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 +01002205 now_ms, __FUNCTION__,
2206 s,
2207 req,
2208 req->rex, req->wex,
2209 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002210 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002211 req->analysers);
2212
Willy Tarreau52a0c602009-08-16 22:45:38 +02002213 /* we're speaking HTTP here, so let's speak HTTP to the client */
2214 s->srv_error = http_return_srv_error;
2215
Willy Tarreau83e3af02009-12-28 17:39:57 +01002216 /* There's a protected area at the end of the buffer for rewriting
2217 * purposes. We don't want to start to parse the request if the
2218 * protected area is affected, because we may have to move processed
2219 * data later, which is much more complicated.
2220 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002221 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002222 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002223 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002224 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2225 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2226 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002227 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002228 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002229 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002230 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002231 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002232 return 0;
2233 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002234 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2235 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2236 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002237 }
2238
Willy Tarreau065e8332010-01-08 00:30:20 +01002239 /* Note that we have the same problem with the response ; we
2240 * may want to send a redirect, error or anything which requires
2241 * some spare space. So we'll ensure that we have at least
2242 * maxrewrite bytes available in the response buffer before
2243 * processing that one. This will only affect pipelined
2244 * keep-alive requests.
2245 */
2246 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002247 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002248 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2249 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2250 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002251 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002252 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002253 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002254 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002255 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002256 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002257 return 0;
2258 }
2259 }
2260
Willy Tarreau9b28e032012-10-12 23:49:43 +02002261 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002262 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002263 }
2264
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 /* 1: we might have to print this header in debug mode */
2266 if (unlikely((global.mode & MODE_DEBUG) &&
2267 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002268 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002269 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002270
Willy Tarreau9b28e032012-10-12 23:49:43 +02002271 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002272 /* this is a bit complex : in case of error on the request line,
2273 * we know that rq.l is still zero, so we display only the part
2274 * up to the end of the line (truncated by debug_hdr).
2275 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002276 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002278
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 sol += hdr_idx_first_pos(&txn->hdr_idx);
2280 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002281
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 while (cur_idx) {
2283 eol = sol + txn->hdr_idx.v[cur_idx].len;
2284 debug_hdr("clihdr", s, sol, eol);
2285 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2286 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002287 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 }
2289
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /*
2292 * Now we quickly check if we have found a full valid request.
2293 * If not so, we check the FD and buffer states before leaving.
2294 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002295 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002296 * requests are checked first. When waiting for a second request
2297 * on a keep-alive session, if we encounter and error, close, t/o,
2298 * we note the error in the session flags but don't set any state.
2299 * Since the error will be noted there, it will not be counted by
2300 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002301 * Last, we may increase some tracked counters' http request errors on
2302 * the cases that are deliberately the client's fault. For instance,
2303 * a timeout or connection reset is not counted as an error. However
2304 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002306
Willy Tarreau655dce92009-11-08 13:10:58 +01002307 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002308 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002311 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002312 session_inc_http_req_ctr(s);
2313 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002314 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 /* 1: Since we are in header mode, if there's no space
2319 * left for headers, we won't be able to free more
2320 * later, so the session will never terminate. We
2321 * must terminate it now.
2322 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002323 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 /* FIXME: check if URI is set and return Status
2325 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002326 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002327 session_inc_http_req_ctr(s);
2328 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002329 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002330 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002331 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 goto return_bad_req;
2333 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002334
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002336 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002337 if (!(s->flags & SN_ERR_MASK))
2338 s->flags |= SN_ERR_CLICL;
2339
Willy Tarreaufcffa692010-01-10 14:21:19 +01002340 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002341 goto failed_keep_alive;
2342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002344 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002345 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002346 session_inc_http_err_ctr(s);
2347 }
2348
Willy Tarreaudc979f22012-12-04 10:39:01 +01002349 txn->status = 400;
2350 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 msg->msg_state = HTTP_MSG_ERROR;
2352 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002353
Willy Tarreauda7ff642010-06-23 11:44:09 +02002354 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002355 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002356 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002357 if (s->listener->counters)
2358 s->listener->counters->failed_req++;
2359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 if (!(s->flags & SN_FINST_MASK))
2361 s->flags |= SN_FINST_R;
2362 return 0;
2363 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002366 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002367 if (!(s->flags & SN_ERR_MASK))
2368 s->flags |= SN_ERR_CLITO;
2369
Willy Tarreaufcffa692010-01-10 14:21:19 +01002370 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002371 goto failed_keep_alive;
2372
Willy Tarreau59234e92008-11-30 23:51:27 +01002373 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002374 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002375 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002376 session_inc_http_err_ctr(s);
2377 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002379 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 msg->msg_state = HTTP_MSG_ERROR;
2381 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002382
Willy Tarreauda7ff642010-06-23 11:44:09 +02002383 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002384 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002385 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002386 if (s->listener->counters)
2387 s->listener->counters->failed_req++;
2388
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 if (!(s->flags & SN_FINST_MASK))
2390 s->flags |= SN_FINST_R;
2391 return 0;
2392 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002393
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002395 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002396 if (!(s->flags & SN_ERR_MASK))
2397 s->flags |= SN_ERR_CLICL;
2398
Willy Tarreaufcffa692010-01-10 14:21:19 +01002399 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002400 goto failed_keep_alive;
2401
Willy Tarreau4076a152009-04-02 15:18:36 +02002402 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002403 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002405 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 msg->msg_state = HTTP_MSG_ERROR;
2407 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002408
Willy Tarreauda7ff642010-06-23 11:44:09 +02002409 session_inc_http_err_ctr(s);
2410 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002411 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002412 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002413 if (s->listener->counters)
2414 s->listener->counters->failed_req++;
2415
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 if (!(s->flags & SN_FINST_MASK))
2417 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002418 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002419 }
2420
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002421 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002422 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2423 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002424#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002425 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002426 /* We need more data, we have to re-enable quick-ack in case we
2427 * previously disabled it, otherwise we might cause the client
2428 * to delay next data.
2429 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002430 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002431 }
2432#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002433
Willy Tarreaufcffa692010-01-10 14:21:19 +01002434 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2435 /* If the client starts to talk, let's fall back to
2436 * request timeout processing.
2437 */
2438 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002439 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002440 }
2441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002443 if (!tick_isset(req->analyse_exp)) {
2444 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2445 (txn->flags & TX_WAIT_NEXT_RQ) &&
2446 tick_isset(s->be->timeout.httpka))
2447 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2448 else
2449 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* we're not ready yet */
2453 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002454
2455 failed_keep_alive:
2456 /* Here we process low-level errors for keep-alive requests. In
2457 * short, if the request is not the first one and it experiences
2458 * a timeout, read error or shutdown, we just silently close so
2459 * that the client can try again.
2460 */
2461 txn->status = 0;
2462 msg->msg_state = HTTP_MSG_RQBEFORE;
2463 req->analysers = 0;
2464 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002465 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002466 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002467 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 /* OK now we have a complete HTTP request with indexed headers. Let's
2471 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002472 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002473 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002474 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002475 * byte after the last LF. msg->sov points to the first byte of data.
2476 * msg->eol cannot be trusted because it may have been left uninitialized
2477 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002478 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002479
Willy Tarreauda7ff642010-06-23 11:44:09 +02002480 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002481 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2482
Willy Tarreaub16a5742010-01-10 14:46:16 +01002483 if (txn->flags & TX_WAIT_NEXT_RQ) {
2484 /* kill the pending keep-alive timeout */
2485 txn->flags &= ~TX_WAIT_NEXT_RQ;
2486 req->analyse_exp = TICK_ETERNITY;
2487 }
2488
2489
Willy Tarreaud787e662009-07-07 10:14:51 +02002490 /* Maybe we found in invalid header name while we were configured not
2491 * to block on that, so we have to capture it now.
2492 */
2493 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002494 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 /*
2497 * 1: identify the method
2498 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002499 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002500
2501 /* we can make use of server redirect on GET and HEAD */
2502 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2503 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 /*
2506 * 2: check if the URI matches the monitor_uri.
2507 * We have to do this for every request which gets in, because
2508 * the monitor-uri is defined by the frontend.
2509 */
2510 if (unlikely((s->fe->monitor_uri_len != 0) &&
2511 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002512 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 s->fe->monitor_uri,
2514 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002515 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002517 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002521 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002525 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 ret = acl_pass(ret);
2528 if (cond->pol == ACL_COND_UNLESS)
2529 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002530
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 if (ret) {
2532 /* we fail this request, let's return 503 service unavail */
2533 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002534 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002536 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002537 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002538
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 /* nothing to fail, let's reply normaly */
2540 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002541 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 goto return_prx_cond;
2543 }
2544
2545 /*
2546 * 3: Maybe we have to copy the original REQURI for the logs ?
2547 * Note: we cannot log anymore if the request has been
2548 * classified as invalid.
2549 */
2550 if (unlikely(s->logs.logwait & LW_REQ)) {
2551 /* we have a complete HTTP request that we must log */
2552 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2553 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002554
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 if (urilen >= REQURI_LEN)
2556 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002559
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002560 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 s->do_log(s);
2562 } else {
2563 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002564 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002565 }
Willy Tarreau06619262006-12-17 08:37:22 +01002566
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002567 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2568 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002569
Willy Tarreau59234e92008-11-30 23:51:27 +01002570 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002571 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002572 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002573
Willy Tarreau5b154472009-12-21 20:11:07 +01002574 /* ... and check if the request is HTTP/1.1 or above */
2575 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002576 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2577 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2578 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002579 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002580
2581 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002582 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 +01002583
Willy Tarreau88d349d2010-01-25 12:15:43 +01002584 /* if the frontend has "option http-use-proxy-header", we'll check if
2585 * we have what looks like a proxied connection instead of a connection,
2586 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2587 * Note that this is *not* RFC-compliant, however browsers and proxies
2588 * happen to do that despite being non-standard :-(
2589 * We consider that a request not beginning with either '/' or '*' is
2590 * a proxied connection, which covers both "scheme://location" and
2591 * CONNECT ip:port.
2592 */
2593 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002594 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002595 txn->flags |= TX_USE_PX_CONN;
2596
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002597 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002598 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002601 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002602 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002604
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002605 /* 6: determine the transfer-length.
2606 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2607 * the presence of a message-body in a REQUEST and its transfer length
2608 * must be determined that way (in order of precedence) :
2609 * 1. The presence of a message-body in a request is signaled by the
2610 * inclusion of a Content-Length or Transfer-Encoding header field
2611 * in the request's header fields. When a request message contains
2612 * both a message-body of non-zero length and a method that does
2613 * not define any semantics for that request message-body, then an
2614 * origin server SHOULD either ignore the message-body or respond
2615 * with an appropriate error message (e.g., 413). A proxy or
2616 * gateway, when presented the same request, SHOULD either forward
2617 * the request inbound with the message- body or ignore the
2618 * message-body when determining a response.
2619 *
2620 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2621 * and the "chunked" transfer-coding (Section 6.2) is used, the
2622 * transfer-length is defined by the use of this transfer-coding.
2623 * If a Transfer-Encoding header field is present and the "chunked"
2624 * transfer-coding is not present, the transfer-length is defined
2625 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002626 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002627 * 3. If a Content-Length header field is present, its decimal value in
2628 * OCTETs represents both the entity-length and the transfer-length.
2629 * If a message is received with both a Transfer-Encoding header
2630 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002631 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002632 * 4. By the server closing the connection. (Closing the connection
2633 * cannot be used to indicate the end of a request body, since that
2634 * would leave no possibility for the server to send back a response.)
2635 *
2636 * Whenever a transfer-coding is applied to a message-body, the set of
2637 * transfer-codings MUST include "chunked", unless the message indicates
2638 * it is terminated by closing the connection. When the "chunked"
2639 * transfer-coding is used, it MUST be the last transfer-coding applied
2640 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 */
2642
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002643 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002644 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002646 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002647 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002648 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002649 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2650 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002651 /* bad transfer-encoding (chunked followed by something else) */
2652 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002653 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002654 break;
2655 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002656 }
2657
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002659 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002660 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002661 signed long long cl;
2662
Willy Tarreauad14f752011-09-02 20:33:27 +02002663 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002664 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002665 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002666 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002667
Willy Tarreauad14f752011-09-02 20:33:27 +02002668 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002669 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002670 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002671 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002672
Willy Tarreauad14f752011-09-02 20:33:27 +02002673 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002674 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002675 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002676 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002677
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002678 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002679 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002680 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002681 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002682
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002683 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002684 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002685 }
2686
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002687 /* bodyless requests have a known length */
2688 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002689 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002690
Willy Tarreaud787e662009-07-07 10:14:51 +02002691 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002692 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002693 req->analyse_exp = TICK_ETERNITY;
2694 return 1;
2695
2696 return_bad_req:
2697 /* We centralize bad requests processing here */
2698 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2699 /* we detected a parsing error. We want to archive this request
2700 * in the dedicated proxy area for later troubleshooting.
2701 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002702 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002703 }
2704
2705 txn->req.msg_state = HTTP_MSG_ERROR;
2706 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002707 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002708
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002709 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002710 if (s->listener->counters)
2711 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002712
2713 return_prx_cond:
2714 if (!(s->flags & SN_ERR_MASK))
2715 s->flags |= SN_ERR_PRXCOND;
2716 if (!(s->flags & SN_FINST_MASK))
2717 s->flags |= SN_FINST_R;
2718
2719 req->analysers = 0;
2720 req->analyse_exp = TICK_ETERNITY;
2721 return 0;
2722}
2723
Cyril Bonté70be45d2010-10-12 00:14:35 +02002724/* We reached the stats page through a POST request.
2725 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002726 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002727 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002728int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002729{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002730 struct proxy *px = NULL;
2731 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002733 char key[LINESIZE];
2734 int action = ST_ADM_ACTION_NONE;
2735 int reprocess = 0;
2736
2737 int total_servers = 0;
2738 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
2740 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002741 char *st_cur_param = NULL;
2742 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002743
Willy Tarreau9b28e032012-10-12 23:49:43 +02002744 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002745 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002746
2747 cur_param = next_param = end_params;
2748
Willy Tarreau9b28e032012-10-12 23:49:43 +02002749 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002750 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002751 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002752 return 1;
2753 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002754 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002755 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002756 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002757 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002758 }
2759
2760 *end_params = '\0';
2761
Willy Tarreau295a8372011-03-10 11:25:07 +01002762 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002763
2764 /*
2765 * Parse the parameters in reverse order to only store the last value.
2766 * From the html form, the backend and the action are at the end.
2767 */
2768 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002769 char *value;
2770 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002771
2772 cur_param--;
2773 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002774 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002775 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002776 poffset = (cur_param != first_param ? 1 : 0);
2777 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2778 if ((plen > 0) && (plen <= sizeof(key))) {
2779 strncpy(key, cur_param + poffset, plen);
2780 key[plen - 1] = '\0';
2781 } else {
2782 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2783 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002784 }
2785
2786 /* Parse the value */
2787 value = key;
2788 while (*value != '\0' && *value != '=') {
2789 value++;
2790 }
2791 if (*value == '=') {
2792 /* Ok, a value is found, we can mark the end of the key */
2793 *value++ = '\0';
2794 }
2795
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002796 if (!url_decode(key) || !url_decode(value))
2797 break;
2798
Cyril Bonté70be45d2010-10-12 00:14:35 +02002799 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002800 if (!px && (strcmp(key, "b") == 0)) {
2801 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2802 /* the backend name is unknown or ambiguous (duplicate names) */
2803 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2804 goto out;
2805 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002806 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002807 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002808 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002809 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002810 }
2811 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002812 action = ST_ADM_ACTION_ENABLE;
2813 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002814 else if (strcmp(value, "stop") == 0) {
2815 action = ST_ADM_ACTION_STOP;
2816 }
2817 else if (strcmp(value, "start") == 0) {
2818 action = ST_ADM_ACTION_START;
2819 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002820 else if (strcmp(value, "shutdown") == 0) {
2821 action = ST_ADM_ACTION_SHUTDOWN;
2822 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002823 else {
2824 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2825 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002826 }
2827 }
2828 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002829 if (!(px && action)) {
2830 /*
2831 * Indicates that we'll need to reprocess the parameters
2832 * as soon as backend and action are known
2833 */
2834 if (!reprocess) {
2835 st_cur_param = cur_param;
2836 st_next_param = next_param;
2837 }
2838 reprocess = 1;
2839 }
2840 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002841 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002842 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002843 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002844 /* Not already in maintenance, we can change the server state */
2845 sv->state |= SRV_MAINTAIN;
2846 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002847 altered_servers++;
2848 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002849 }
2850 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002851 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002852 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002853 /* Already in maintenance, we can change the server state */
2854 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002855 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002856 altered_servers++;
2857 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002858 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002859 break;
2860 case ST_ADM_ACTION_STOP:
2861 case ST_ADM_ACTION_START:
2862 if (action == ST_ADM_ACTION_START)
2863 sv->uweight = sv->iweight;
2864 else
2865 sv->uweight = 0;
2866
2867 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2868 /* we must take care of not pushing the server to full throttle during slow starts */
2869 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2870 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2871 else
2872 sv->eweight = BE_WEIGHT_SCALE;
2873 sv->eweight *= sv->uweight;
2874 } else {
2875 sv->eweight = sv->uweight;
2876 }
2877
2878 /* static LB algorithms are a bit harder to update */
2879 if (px->lbprm.update_server_eweight)
2880 px->lbprm.update_server_eweight(sv);
2881 else if (sv->eweight) {
2882 if (px->lbprm.set_server_status_up)
2883 px->lbprm.set_server_status_up(sv);
2884 }
2885 else {
2886 if (px->lbprm.set_server_status_down)
2887 px->lbprm.set_server_status_down(sv);
2888 }
2889 altered_servers++;
2890 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002891 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002892 case ST_ADM_ACTION_SHUTDOWN:
2893 if (px->state != PR_STSTOPPED) {
2894 struct session *sess, *sess_bck;
2895
2896 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2897 if (sess->srv_conn == sv)
2898 session_shutdown(sess, SN_ERR_KILLED);
2899
2900 altered_servers++;
2901 total_servers++;
2902 }
2903 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002904 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002905 } else {
2906 /* the server name is unknown or ambiguous (duplicate names) */
2907 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002908 }
2909 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002910 if (reprocess && px && action) {
2911 /* Now, we know the backend and the action chosen by the user.
2912 * We can safely restart from the first server parameter
2913 * to reprocess them
2914 */
2915 cur_param = st_cur_param;
2916 next_param = st_next_param;
2917 reprocess = 0;
2918 goto reprocess_servers;
2919 }
2920
Cyril Bonté70be45d2010-10-12 00:14:35 +02002921 next_param = cur_param;
2922 }
2923 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002924
2925 if (total_servers == 0) {
2926 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2927 }
2928 else if (altered_servers == 0) {
2929 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2930 }
2931 else if (altered_servers == total_servers) {
2932 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2933 }
2934 else {
2935 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2936 }
2937 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002938 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002939}
2940
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002941/* This function checks whether we need to enable a POST analyser to parse a
2942 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002943 * if it needs to come back again, otherwise non-zero if it finishes. In the
2944 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002945 */
2946int http_handle_stats(struct session *s, struct channel *req)
2947{
2948 struct stats_admin_rule *stats_admin_rule;
2949 struct stream_interface *si = s->rep->prod;
2950 struct http_txn *txn = &s->txn;
2951 struct http_msg *msg = &txn->req;
2952 struct uri_auth *uri = s->be->uri_auth;
2953
2954 /* now check whether we have some admin rules for this request */
2955 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2956 int ret = 1;
2957
2958 if (stats_admin_rule->cond) {
2959 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2960 ret = acl_pass(ret);
2961 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2962 ret = !ret;
2963 }
2964
2965 if (ret) {
2966 /* no rule, or the rule matches */
2967 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2968 break;
2969 }
2970 }
2971
2972 /* Was the status page requested with a POST ? */
2973 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002974 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2975
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002976 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2977 if (msg->msg_state < HTTP_MSG_100_SENT) {
2978 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2979 * send an HTTP/1.1 100 Continue intermediate response.
2980 */
2981 if (msg->flags & HTTP_MSGF_VER_11) {
2982 struct hdr_ctx ctx;
2983 ctx.idx = 0;
2984 /* Expect is allowed in 1.1, look for it */
2985 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2986 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2987 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2988 }
2989 }
2990 msg->msg_state = HTTP_MSG_100_SENT;
2991 s->logs.tv_request = now; /* update the request timer to reflect full request */
2992 }
2993 if (!http_process_req_stat_post(si, txn, req))
2994 return 0; /* we need more data */
2995 }
2996 else
2997 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002998 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2999 scope_txt[0] = 0;
3000 if (si->applet.ctx.stats.scope_len) {
3001 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3002 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3003 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3004 }
3005
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003006
3007 /* We don't want to land on the posted stats page because a refresh will
3008 * repost the data. We don't want this to happen on accident so we redirect
3009 * the browse to the stats page with a GET.
3010 */
3011 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003012 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003013 "Cache-Control: no-cache\r\n"
3014 "Content-Type: text/plain\r\n"
3015 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003016 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003017 "\r\n",
3018 uri->uri_prefix,
3019 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3020 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3021 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3022 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003023 stat_status_codes[STAT_STATUS_UNKN],
3024 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3025 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3026 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003027
3028 s->txn.status = 303;
3029 s->logs.tv_request = now;
3030 stream_int_retnclose(req->prod, &trash);
3031 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3032
3033 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3034 s->fe->fe_counters.intercepted_req++;
3035
3036 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3037 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3038 if (!(s->flags & SN_FINST_MASK))
3039 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003040 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003041 return 1;
3042 }
3043
3044 /* OK, let's go on now */
3045
3046 chunk_printf(&trash,
3047 "HTTP/1.0 200 OK\r\n"
3048 "Cache-Control: no-cache\r\n"
3049 "Connection: close\r\n"
3050 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003051 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003052
3053 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3054 chunk_appendf(&trash, "Refresh: %d\r\n",
3055 uri->refresh);
3056
3057 chunk_appendf(&trash, "\r\n");
3058
3059 s->txn.status = 200;
3060 s->logs.tv_request = now;
3061
3062 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3063 s->fe->fe_counters.intercepted_req++;
3064
3065 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3066 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3067 if (!(s->flags & SN_FINST_MASK))
3068 s->flags |= SN_FINST_R;
3069
3070 if (s->txn.meth == HTTP_METH_HEAD) {
3071 /* that's all we return in case of HEAD request, so let's immediately close. */
3072 stream_int_retnclose(req->prod, &trash);
3073 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003074 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003075 return 1;
3076 }
3077
3078 /* OK, push the response and hand over to the stats I/O handler */
3079 bi_putchk(s->rep, &trash);
3080
3081 s->task->nice = -32; /* small boost for HTTP statistics */
3082 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3083 s->target = s->rep->prod->conn->target; // for logging only
3084 s->rep->prod->conn->xprt_ctx = s;
3085 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3086 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 return 1;
3088}
3089
Willy Tarreau20b0de52012-12-24 15:45:22 +01003090/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003091 * transaction <txn>. Returns the first rule that prevents further processing
3092 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3093 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3094 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003095 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003096static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003097http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003098{
Willy Tarreauff011f22011-01-06 17:51:27 +01003099 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003100 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003101
Willy Tarreauff011f22011-01-06 17:51:27 +01003102 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003103 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003104 continue;
3105
Willy Tarreau96257ec2012-12-27 10:46:37 +01003106 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003107 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003108 int ret;
3109
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003110 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003111 ret = acl_pass(ret);
3112
Willy Tarreauff011f22011-01-06 17:51:27 +01003113 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003114 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003115
3116 if (!ret) /* condition not matched */
3117 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003118 }
3119
Willy Tarreau20b0de52012-12-24 15:45:22 +01003120
Willy Tarreau96257ec2012-12-27 10:46:37 +01003121 switch (rule->action) {
3122 case HTTP_REQ_ACT_ALLOW:
3123 return NULL; /* "allow" rules are OK */
3124
3125 case HTTP_REQ_ACT_DENY:
3126 txn->flags |= TX_CLDENY;
3127 return rule;
3128
Willy Tarreauccbcc372012-12-27 12:37:57 +01003129 case HTTP_REQ_ACT_TARPIT:
3130 txn->flags |= TX_CLTARPIT;
3131 return rule;
3132
Willy Tarreau96257ec2012-12-27 10:46:37 +01003133 case HTTP_REQ_ACT_AUTH:
3134 return rule;
3135
Willy Tarreau81499eb2012-12-27 12:19:02 +01003136 case HTTP_REQ_ACT_REDIR:
3137 return rule;
3138
Willy Tarreau96257ec2012-12-27 10:46:37 +01003139 case HTTP_REQ_ACT_SET_HDR:
3140 ctx.idx = 0;
3141 /* remove all occurrences of the header */
3142 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3143 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3144 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003145 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003146 /* now fall through to header addition */
3147
3148 case HTTP_REQ_ACT_ADD_HDR:
3149 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3150 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3151 trash.len = rule->arg.hdr_add.name_len;
3152 trash.str[trash.len++] = ':';
3153 trash.str[trash.len++] = ' ';
3154 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3155 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3156 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003157 }
3158 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003159
3160 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003161 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003162}
3163
Willy Tarreau71241ab2012-12-27 11:30:54 +01003164
3165/* Perform an HTTP redirect based on the information in <rule>. The function
3166 * returns non-zero on success, or zero in case of a, irrecoverable error such
3167 * as too large a request to build a valid response.
3168 */
3169static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3170{
3171 struct http_msg *msg = &txn->req;
3172 const char *msg_fmt;
3173
3174 /* build redirect message */
3175 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003176 case 308:
3177 msg_fmt = HTTP_308;
3178 break;
3179 case 307:
3180 msg_fmt = HTTP_307;
3181 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003182 case 303:
3183 msg_fmt = HTTP_303;
3184 break;
3185 case 301:
3186 msg_fmt = HTTP_301;
3187 break;
3188 case 302:
3189 default:
3190 msg_fmt = HTTP_302;
3191 break;
3192 }
3193
3194 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3195 return 0;
3196
3197 switch(rule->type) {
3198 case REDIRECT_TYPE_SCHEME: {
3199 const char *path;
3200 const char *host;
3201 struct hdr_ctx ctx;
3202 int pathlen;
3203 int hostlen;
3204
3205 host = "";
3206 hostlen = 0;
3207 ctx.idx = 0;
3208 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3209 host = ctx.line + ctx.val;
3210 hostlen = ctx.vlen;
3211 }
3212
3213 path = http_get_path(txn);
3214 /* build message using path */
3215 if (path) {
3216 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3217 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3218 int qs = 0;
3219 while (qs < pathlen) {
3220 if (path[qs] == '?') {
3221 pathlen = qs;
3222 break;
3223 }
3224 qs++;
3225 }
3226 }
3227 } else {
3228 path = "/";
3229 pathlen = 1;
3230 }
3231
3232 /* check if we can add scheme + "://" + host + path */
3233 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3234 return 0;
3235
3236 /* add scheme */
3237 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3238 trash.len += rule->rdr_len;
3239
3240 /* add "://" */
3241 memcpy(trash.str + trash.len, "://", 3);
3242 trash.len += 3;
3243
3244 /* add host */
3245 memcpy(trash.str + trash.len, host, hostlen);
3246 trash.len += hostlen;
3247
3248 /* add path */
3249 memcpy(trash.str + trash.len, path, pathlen);
3250 trash.len += pathlen;
3251
3252 /* append a slash at the end of the location is needed and missing */
3253 if (trash.len && trash.str[trash.len - 1] != '/' &&
3254 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3255 if (trash.len > trash.size - 5)
3256 return 0;
3257 trash.str[trash.len] = '/';
3258 trash.len++;
3259 }
3260
3261 break;
3262 }
3263 case REDIRECT_TYPE_PREFIX: {
3264 const char *path;
3265 int pathlen;
3266
3267 path = http_get_path(txn);
3268 /* build message using path */
3269 if (path) {
3270 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3271 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3272 int qs = 0;
3273 while (qs < pathlen) {
3274 if (path[qs] == '?') {
3275 pathlen = qs;
3276 break;
3277 }
3278 qs++;
3279 }
3280 }
3281 } else {
3282 path = "/";
3283 pathlen = 1;
3284 }
3285
3286 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3287 return 0;
3288
3289 /* add prefix. Note that if prefix == "/", we don't want to
3290 * add anything, otherwise it makes it hard for the user to
3291 * configure a self-redirection.
3292 */
3293 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3294 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3295 trash.len += rule->rdr_len;
3296 }
3297
3298 /* add path */
3299 memcpy(trash.str + trash.len, path, pathlen);
3300 trash.len += pathlen;
3301
3302 /* append a slash at the end of the location is needed and missing */
3303 if (trash.len && trash.str[trash.len - 1] != '/' &&
3304 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3305 if (trash.len > trash.size - 5)
3306 return 0;
3307 trash.str[trash.len] = '/';
3308 trash.len++;
3309 }
3310
3311 break;
3312 }
3313 case REDIRECT_TYPE_LOCATION:
3314 default:
3315 if (trash.len + rule->rdr_len > trash.size - 4)
3316 return 0;
3317
3318 /* add location */
3319 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3320 trash.len += rule->rdr_len;
3321 break;
3322 }
3323
3324 if (rule->cookie_len) {
3325 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3326 trash.len += 14;
3327 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3328 trash.len += rule->cookie_len;
3329 memcpy(trash.str + trash.len, "\r\n", 2);
3330 trash.len += 2;
3331 }
3332
3333 /* add end of headers and the keep-alive/close status.
3334 * We may choose to set keep-alive if the Location begins
3335 * with a slash, because the client will come back to the
3336 * same server.
3337 */
3338 txn->status = rule->code;
3339 /* let's log the request time */
3340 s->logs.tv_request = now;
3341
3342 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3343 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3344 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3345 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3346 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3347 /* keep-alive possible */
3348 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3349 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3350 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3351 trash.len += 30;
3352 } else {
3353 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3354 trash.len += 24;
3355 }
3356 }
3357 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3358 trash.len += 4;
3359 bo_inject(txn->rsp.chn, trash.str, trash.len);
3360 /* "eat" the request */
3361 bi_fast_delete(txn->req.chn->buf, msg->sov);
3362 msg->sov = 0;
3363 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3364 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3365 txn->req.msg_state = HTTP_MSG_CLOSED;
3366 txn->rsp.msg_state = HTTP_MSG_DONE;
3367 } else {
3368 /* keep-alive not possible */
3369 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3370 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3371 trash.len += 29;
3372 } else {
3373 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3374 trash.len += 23;
3375 }
3376 stream_int_retnclose(txn->req.chn->prod, &trash);
3377 txn->req.chn->analysers = 0;
3378 }
3379
3380 if (!(s->flags & SN_ERR_MASK))
3381 s->flags |= SN_ERR_PRXCOND;
3382 if (!(s->flags & SN_FINST_MASK))
3383 s->flags |= SN_FINST_R;
3384
3385 return 1;
3386}
3387
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003388/* This stream analyser runs all HTTP request processing which is common to
3389 * frontends and backends, which means blocking ACLs, filters, connection-close,
3390 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003391 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003392 * either needs more data or wants to immediately abort the request (eg: deny,
3393 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003394 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003395int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003396{
Willy Tarreaud787e662009-07-07 10:14:51 +02003397 struct http_txn *txn = &s->txn;
3398 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003399 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003400 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003401 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003402 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003403 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003404
Willy Tarreau655dce92009-11-08 13:10:58 +01003405 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003406 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003407 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003408 return 0;
3409 }
3410
Willy Tarreau3a816292009-07-07 10:55:49 +02003411 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003412 req->analyse_exp = TICK_ETERNITY;
3413
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003414 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 +02003415 now_ms, __FUNCTION__,
3416 s,
3417 req,
3418 req->rex, req->wex,
3419 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003420 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003421 req->analysers);
3422
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003423 /* first check whether we have some ACLs set to block this request */
3424 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003425 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003426
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003427 ret = acl_pass(ret);
3428 if (cond->pol == ACL_COND_UNLESS)
3429 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003430
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003431 if (ret) {
3432 txn->status = 403;
3433 /* let's log the request time */
3434 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003435 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003436 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003437 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003439 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003440
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003441 /* just in case we have some per-backend tracking */
3442 session_inc_be_http_req_ctr(s);
3443
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003444 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003445 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003446
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003447 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003448 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003449 do_stats = stats_check_uri(s->rep->prod, txn, px);
3450 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003451 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 +01003452 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003453 else
3454 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003455
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003456 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003457 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3458 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003459 txn->status = 403;
3460 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003461 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003462 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003463 s->fe->fe_counters.denied_req++;
3464 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3465 s->be->be_counters.denied_req++;
3466 if (s->listener->counters)
3467 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003468 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003469 }
3470 /* When a connection is tarpitted, we use the tarpit timeout,
3471 * which may be the same as the connect timeout if unspecified.
3472 * If unset, then set it to zero because we really want it to
3473 * eventually expire. We build the tarpit as an analyser.
3474 */
3475 if (txn->flags & TX_CLTARPIT) {
3476 channel_erase(s->req);
3477 /* wipe the request out so that we can drop the connection early
3478 * if the client closes first.
3479 */
3480 channel_dont_connect(req);
3481 req->analysers = 0; /* remove switching rules etc... */
3482 req->analysers |= AN_REQ_HTTP_TARPIT;
3483 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3484 if (!req->analyse_exp)
3485 req->analyse_exp = tick_add(now_ms, 0);
3486 session_inc_http_err_ctr(s);
3487 s->fe->fe_counters.denied_req++;
3488 if (s->fe != s->be)
3489 s->be->be_counters.denied_req++;
3490 if (s->listener->counters)
3491 s->listener->counters->denied_req++;
3492 return 1;
3493 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003494 }
3495
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003496 /* try headers filters */
3497 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003498 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003499 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003500
Willy Tarreau59234e92008-11-30 23:51:27 +01003501 /* has the request been denied ? */
3502 if (txn->flags & TX_CLDENY) {
3503 /* no need to go further */
3504 txn->status = 403;
3505 /* let's log the request time */
3506 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003507 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003508 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003509 goto return_prx_cond;
3510 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003511
3512 /* When a connection is tarpitted, we use the tarpit timeout,
3513 * which may be the same as the connect timeout if unspecified.
3514 * If unset, then set it to zero because we really want it to
3515 * eventually expire. We build the tarpit as an analyser.
3516 */
3517 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003518 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003519 /* wipe the request out so that we can drop the connection early
3520 * if the client closes first.
3521 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003522 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003523 req->analysers = 0; /* remove switching rules etc... */
3524 req->analysers |= AN_REQ_HTTP_TARPIT;
3525 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3526 if (!req->analyse_exp)
3527 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003528 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003529 return 1;
3530 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003531 }
Willy Tarreau06619262006-12-17 08:37:22 +01003532
Willy Tarreau5b154472009-12-21 20:11:07 +01003533 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3534 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003535 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3536 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003537 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3538 * avoid to redo the same work if FE and BE have the same settings (common).
3539 * The method consists in checking if options changed between the two calls
3540 * (implying that either one is non-null, or one of them is non-null and we
3541 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003542 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003543
Willy Tarreaudc008c52010-02-01 16:20:08 +01003544 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3545 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3546 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3547 (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 +01003548 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003549
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003550 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3551 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003552 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003553 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3554 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003555 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003556 tmp = TX_CON_WANT_CLO;
3557
Willy Tarreau5b154472009-12-21 20:11:07 +01003558 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3559 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003560
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003561 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3562 /* parse the Connection header and possibly clean it */
3563 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003564 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003565 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3566 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003567 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003568 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003569 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003570 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003571 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003572
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003573 /* check if client or config asks for explicit close in KAL/SCL */
3574 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3575 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3576 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003577 (!(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 +01003578 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003579 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003580 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003581 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3582 }
Willy Tarreau78599912009-10-17 20:12:21 +02003583
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003584 /* we can be blocked here because the request needs to be authenticated,
3585 * either to pass or to access stats.
3586 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003587 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003588 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003589
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003590 if (!realm)
3591 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3592
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003593 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003594 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003595 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003596 /* on 401 we still count one error, because normal browsing
3597 * won't significantly increase the counter but brute force
3598 * attempts will.
3599 */
3600 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003601 goto return_prx_cond;
3602 }
3603
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003604 /* add request headers from the rule sets in the same order */
3605 list_for_each_entry(wl, &px->req_add, list) {
3606 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003607 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003608 ret = acl_pass(ret);
3609 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3610 ret = !ret;
3611 if (!ret)
3612 continue;
3613 }
3614
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003615 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003616 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003617 }
3618
3619 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3620 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3621 goto return_bad_req;
3622 req->analyse_exp = TICK_ETERNITY;
3623 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003624 }
3625
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003626 if (unlikely(do_stats)) {
3627 /* process the stats request now */
3628 if (!http_handle_stats(s, req)) {
3629 /* we need more data, let's come back here later */
3630 req->analysers |= an_bit;
3631 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003632 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003633 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003634 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003636
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003637 /* check whether we have some ACLs set to redirect this request */
3638 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003639 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003640 int ret;
3641
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003642 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003643 ret = acl_pass(ret);
3644 if (rule->cond->pol == ACL_COND_UNLESS)
3645 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003646 if (!ret)
3647 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003648 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003649 if (!http_apply_redirect_rule(rule, s, txn))
3650 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003651
Willy Tarreau71241ab2012-12-27 11:30:54 +01003652 req->analyse_exp = TICK_ETERNITY;
3653 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003654 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003655
Willy Tarreau2be39392010-01-03 17:24:51 +01003656 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3657 * If this happens, then the data will not come immediately, so we must
3658 * send all what we have without waiting. Note that due to the small gain
3659 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003660 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003661 * itself once used.
3662 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003663 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003664
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 /* that's OK for us now, let's move on to next analysers */
3666 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003667
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003668 return_bad_req:
3669 /* We centralize bad requests processing here */
3670 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3671 /* we detected a parsing error. We want to archive this request
3672 * in the dedicated proxy area for later troubleshooting.
3673 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003674 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003675 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003676
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003677 txn->req.msg_state = HTTP_MSG_ERROR;
3678 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003679 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003680
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003681 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003682 if (s->listener->counters)
3683 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003684
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003685 return_prx_cond:
3686 if (!(s->flags & SN_ERR_MASK))
3687 s->flags |= SN_ERR_PRXCOND;
3688 if (!(s->flags & SN_FINST_MASK))
3689 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003690
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003691 req->analysers = 0;
3692 req->analyse_exp = TICK_ETERNITY;
3693 return 0;
3694}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003696/* This function performs all the processing enabled for the current request.
3697 * It returns 1 if the processing can continue on next analysers, or zero if it
3698 * needs more data, encounters an error, or wants to immediately abort the
3699 * request. It relies on buffers flags, and updates s->req->analysers.
3700 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003701int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003702{
3703 struct http_txn *txn = &s->txn;
3704 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003705
Willy Tarreau655dce92009-11-08 13:10:58 +01003706 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003707 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003708 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003709 return 0;
3710 }
3711
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003712 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 +02003713 now_ms, __FUNCTION__,
3714 s,
3715 req,
3716 req->rex, req->wex,
3717 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003718 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003719 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003720
William Lallemand82fe75c2012-10-23 10:25:10 +02003721 if (s->fe->comp || s->be->comp)
3722 select_compression_request_header(s, req->buf);
3723
Willy Tarreau59234e92008-11-30 23:51:27 +01003724 /*
3725 * Right now, we know that we have processed the entire headers
3726 * and that unwanted requests have been filtered out. We can do
3727 * whatever we want with the remaining request. Also, now we
3728 * may have separate values for ->fe, ->be.
3729 */
Willy Tarreau06619262006-12-17 08:37:22 +01003730
Willy Tarreau59234e92008-11-30 23:51:27 +01003731 /*
3732 * If HTTP PROXY is set we simply get remote server address
3733 * parsing incoming request.
3734 */
3735 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003736 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 +01003737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003738
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003740 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 * Note that doing so might move headers in the request, but
3742 * the fields will stay coherent and the URI will not move.
3743 * This should only be performed in the backend.
3744 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003745 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003746 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3747 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003748
Willy Tarreau59234e92008-11-30 23:51:27 +01003749 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003750 * 8: the appsession cookie was looked up very early in 1.2,
3751 * so let's do the same now.
3752 */
3753
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003754 /* It needs to look into the URI unless persistence must be ignored */
3755 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003756 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 +01003757 }
3758
William Lallemanda73203e2012-03-12 12:48:57 +01003759 /* add unique-id if "header-unique-id" is specified */
3760
3761 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3762 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3763
3764 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003765 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3766 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003767 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003768 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003769 goto return_bad_req;
3770 }
3771
Cyril Bontéb21570a2009-11-29 20:04:48 +01003772 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003773 * 9: add X-Forwarded-For if either the frontend or the backend
3774 * asks for it.
3775 */
3776 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003777 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003778 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003779 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3780 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003781 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003782 /* The header is set to be added only if none is present
3783 * and we found it, so don't do anything.
3784 */
3785 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003786 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 /* Add an X-Forwarded-For header unless the source IP is
3788 * in the 'except' network range.
3789 */
3790 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003791 (((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 +01003792 != s->fe->except_net.s_addr) &&
3793 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003794 (((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 +01003795 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003796 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003797 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003798 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003799
3800 /* Note: we rely on the backend to get the header name to be used for
3801 * x-forwarded-for, because the header is really meant for the backends.
3802 * However, if the backend did not specify any option, we have to rely
3803 * on the frontend's header name.
3804 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 if (s->be->fwdfor_hdr_len) {
3806 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003807 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003808 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003809 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003810 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003811 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003812 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003814 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003815 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003816 }
3817 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003818 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003819 /* FIXME: for the sake of completeness, we should also support
3820 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003821 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 int len;
3823 char pn[INET6_ADDRSTRLEN];
3824 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003825 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003826 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003827
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 /* Note: we rely on the backend to get the header name to be used for
3829 * x-forwarded-for, because the header is really meant for the backends.
3830 * However, if the backend did not specify any option, we have to rely
3831 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003832 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003833 if (s->be->fwdfor_hdr_len) {
3834 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003835 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 } else {
3837 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003838 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003839 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003840 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003841
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003842 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003843 goto return_bad_req;
3844 }
3845 }
3846
3847 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003848 * 10: add X-Original-To if either the frontend or the backend
3849 * asks for it.
3850 */
3851 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3852
3853 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003854 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003855 /* Add an X-Original-To header unless the destination IP is
3856 * in the 'except' network range.
3857 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003858 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003859
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003860 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003861 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003862 (((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 +02003863 != s->fe->except_to.s_addr) &&
3864 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003865 (((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 +02003866 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003867 int len;
3868 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003869 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003870
3871 /* Note: we rely on the backend to get the header name to be used for
3872 * x-original-to, because the header is really meant for the backends.
3873 * However, if the backend did not specify any option, we have to rely
3874 * on the frontend's header name.
3875 */
3876 if (s->be->orgto_hdr_len) {
3877 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003878 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003879 } else {
3880 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003881 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003882 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003883 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003885 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003886 goto return_bad_req;
3887 }
3888 }
3889 }
3890
Willy Tarreau50fc7772012-11-11 22:19:57 +01003891 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3892 * If an "Upgrade" token is found, the header is left untouched in order not to have
3893 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3894 * "Upgrade" is present in the Connection header.
3895 */
3896 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3897 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3898 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003899 unsigned int want_flags = 0;
3900
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003901 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003902 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3903 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3904 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003905 want_flags |= TX_CON_CLO_SET;
3906 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003907 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3908 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3909 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003910 want_flags |= TX_CON_KAL_SET;
3911 }
3912
3913 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003914 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003917
Willy Tarreau522d6c02009-12-06 18:49:18 +01003918 /* If we have no server assigned yet and we're balancing on url_param
3919 * with a POST request, we may be interested in checking the body for
3920 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003921 */
3922 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3923 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003924 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003925 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003926 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003927 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003929
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003930 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003931 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003932#ifdef TCP_QUICKACK
3933 /* We expect some data from the client. Unless we know for sure
3934 * we already have a full request, we have to re-enable quick-ack
3935 * in case we previously disabled it, otherwise we might cause
3936 * the client to delay further data.
3937 */
3938 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003939 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003940 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003941 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003942#endif
3943 }
Willy Tarreau03945942009-12-22 16:50:27 +01003944
Willy Tarreau59234e92008-11-30 23:51:27 +01003945 /*************************************************************
3946 * OK, that's finished for the headers. We have done what we *
3947 * could. Let's switch to the DATA state. *
3948 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003949 req->analyse_exp = TICK_ETERNITY;
3950 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003951
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003952 /* if the server closes the connection, we want to immediately react
3953 * and close the socket to save packets and syscalls.
3954 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003955 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3956 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003957
Willy Tarreau59234e92008-11-30 23:51:27 +01003958 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003959 /* OK let's go on with the BODY now */
3960 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003961
Willy Tarreau59234e92008-11-30 23:51:27 +01003962 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003963 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003964 /* we detected a parsing error. We want to archive this request
3965 * in the dedicated proxy area for later troubleshooting.
3966 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003967 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003968 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003969
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 txn->req.msg_state = HTTP_MSG_ERROR;
3971 txn->status = 400;
3972 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003973 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003974
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003975 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003976 if (s->listener->counters)
3977 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003978
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 if (!(s->flags & SN_ERR_MASK))
3980 s->flags |= SN_ERR_PRXCOND;
3981 if (!(s->flags & SN_FINST_MASK))
3982 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003983 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003984}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003985
Willy Tarreau60b85b02008-11-30 23:28:40 +01003986/* This function is an analyser which processes the HTTP tarpit. It always
3987 * returns zero, at the beginning because it prevents any other processing
3988 * from occurring, and at the end because it terminates the request.
3989 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003990int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003991{
3992 struct http_txn *txn = &s->txn;
3993
3994 /* This connection is being tarpitted. The CLIENT side has
3995 * already set the connect expiration date to the right
3996 * timeout. We just have to check that the client is still
3997 * there and that the timeout has not expired.
3998 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003999 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004000 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004001 !tick_is_expired(req->analyse_exp, now_ms))
4002 return 0;
4003
4004 /* We will set the queue timer to the time spent, just for
4005 * logging purposes. We fake a 500 server error, so that the
4006 * attacker will not suspect his connection has been tarpitted.
4007 * It will not cause trouble to the logs because we can exclude
4008 * the tarpitted connections by filtering on the 'PT' status flags.
4009 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004010 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4011
4012 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004013 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004014 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004015
4016 req->analysers = 0;
4017 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004018
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004019 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004020 if (s->listener->counters)
4021 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004022
Willy Tarreau60b85b02008-11-30 23:28:40 +01004023 if (!(s->flags & SN_ERR_MASK))
4024 s->flags |= SN_ERR_PRXCOND;
4025 if (!(s->flags & SN_FINST_MASK))
4026 s->flags |= SN_FINST_T;
4027 return 0;
4028}
4029
Willy Tarreaud34af782008-11-30 23:36:37 +01004030/* This function is an analyser which processes the HTTP request body. It looks
4031 * for parameters to be used for the load balancing algorithm (url_param). It
4032 * must only be called after the standard HTTP request processing has occurred,
4033 * because it expects the request to be parsed. It returns zero if it needs to
4034 * read more data, or 1 once it has completed its analysis.
4035 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004036int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004037{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004038 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004039 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004040 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004041
4042 /* We have to parse the HTTP request body to find any required data.
4043 * "balance url_param check_post" should have been the only way to get
4044 * into this. We were brought here after HTTP header analysis, so all
4045 * related structures are ready.
4046 */
4047
Willy Tarreau522d6c02009-12-06 18:49:18 +01004048 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4049 goto missing_data;
4050
4051 if (msg->msg_state < HTTP_MSG_100_SENT) {
4052 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4053 * send an HTTP/1.1 100 Continue intermediate response.
4054 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004055 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004056 struct hdr_ctx ctx;
4057 ctx.idx = 0;
4058 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004059 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004060 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004061 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062 }
4063 }
4064 msg->msg_state = HTTP_MSG_100_SENT;
4065 }
4066
4067 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004068 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004069 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004070 * is still null. We must save the body in msg->next because it
4071 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004072 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004073 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004074
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004075 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004076 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4077 else
4078 msg->msg_state = HTTP_MSG_DATA;
4079 }
4080
4081 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004082 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004083 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004084 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004085 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004086 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004087
Willy Tarreau115acb92009-12-26 13:56:06 +01004088 if (!ret)
4089 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004090 else if (ret < 0) {
4091 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004092 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004093 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004094 }
4095
Willy Tarreaud98cf932009-12-27 22:54:55 +01004096 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004097 * We have the first data byte is in msg->sov. We're waiting for at
4098 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004099 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004100
Willy Tarreau124d9912011-03-01 20:30:48 +01004101 if (msg->body_len < limit)
4102 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103
Willy Tarreau9b28e032012-10-12 23:49:43 +02004104 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004105 goto http_end;
4106
4107 missing_data:
4108 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004109 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004110 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004111 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004112 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004113
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004114 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004116 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004117
4118 if (!(s->flags & SN_ERR_MASK))
4119 s->flags |= SN_ERR_CLITO;
4120 if (!(s->flags & SN_FINST_MASK))
4121 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004122 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004124
4125 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004126 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004127 /* Not enough data. We'll re-use the http-request
4128 * timeout here. Ideally, we should set the timeout
4129 * relative to the accept() date. We just set the
4130 * request timeout once at the beginning of the
4131 * request.
4132 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004133 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004134 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004135 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004136 return 0;
4137 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138
4139 http_end:
4140 /* The situation will not evolve, so let's give up on the analysis. */
4141 s->logs.tv_request = now; /* update the request timer to reflect full request */
4142 req->analysers &= ~an_bit;
4143 req->analyse_exp = TICK_ETERNITY;
4144 return 1;
4145
4146 return_bad_req: /* let's centralize all bad requests */
4147 txn->req.msg_state = HTTP_MSG_ERROR;
4148 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004149 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004150
Willy Tarreau79ebac62010-06-07 13:47:49 +02004151 if (!(s->flags & SN_ERR_MASK))
4152 s->flags |= SN_ERR_PRXCOND;
4153 if (!(s->flags & SN_FINST_MASK))
4154 s->flags |= SN_FINST_R;
4155
Willy Tarreau522d6c02009-12-06 18:49:18 +01004156 return_err_msg:
4157 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004158 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004159 if (s->listener->counters)
4160 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004161 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004162}
4163
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004164/* send a server's name with an outgoing request over an established connection.
4165 * Note: this function is designed to be called once the request has been scheduled
4166 * for being forwarded. This is the reason why it rewinds the buffer before
4167 * proceeding.
4168 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004169int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170
4171 struct hdr_ctx ctx;
4172
Mark Lamourinec2247f02012-01-04 13:02:01 -05004173 char *hdr_name = be->server_id_hdr_name;
4174 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004175 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004176 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004177 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004178
William Lallemandd9e90662012-01-30 17:27:17 +01004179 ctx.idx = 0;
4180
Willy Tarreau9b28e032012-10-12 23:49:43 +02004181 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004182 if (old_o) {
4183 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004184 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004185 }
4186
Willy Tarreau9b28e032012-10-12 23:49:43 +02004187 old_i = chn->buf->i;
4188 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 -05004189 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004190 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004191 }
4192
4193 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004194 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004195 memcpy(hdr_val, hdr_name, hdr_name_len);
4196 hdr_val += hdr_name_len;
4197 *hdr_val++ = ':';
4198 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004199 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4200 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004201
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004202 if (old_o) {
4203 /* If this was a forwarded request, we must readjust the amount of
4204 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004205 * variations. Note that if the request was already scheduled for
4206 * forwarding, it had its req->sol pointing to the body, which
4207 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004208 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004209 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004210 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004211 }
4212
Mark Lamourinec2247f02012-01-04 13:02:01 -05004213 return 0;
4214}
4215
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216/* Terminate current transaction and prepare a new one. This is very tricky
4217 * right now but it works.
4218 */
4219void http_end_txn_clean_session(struct session *s)
4220{
4221 /* FIXME: We need a more portable way of releasing a backend's and a
4222 * server's connections. We need a safer way to reinitialize buffer
4223 * flags. We also need a more accurate method for computing per-request
4224 * data.
4225 */
4226 http_silent_debug(__LINE__, s);
4227
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004228 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004229 si_shutr(s->req->cons);
4230 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231
4232 http_silent_debug(__LINE__, s);
4233
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004234 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004235 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004236 if (unlikely(s->srv_conn))
4237 sess_change_server(s, NULL);
4238 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239
4240 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4241 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004242 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243
4244 if (s->txn.status) {
4245 int n;
4246
4247 n = s->txn.status / 100;
4248 if (n < 1 || n > 5)
4249 n = 0;
4250
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004251 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004252 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004253 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004254 s->fe->fe_counters.p.http.comp_rsp++;
4255 }
Willy Tarreau24657792010-02-26 10:30:28 +01004256 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004257 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004258 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004259 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004260 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004261 s->be->be_counters.p.http.comp_rsp++;
4262 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 }
4264
4265 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004266 s->logs.bytes_in -= s->req->buf->i;
4267 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268
4269 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004270 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 !(s->flags & SN_MONITOR) &&
4272 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4273 s->do_log(s);
4274 }
4275
4276 s->logs.accept_date = date; /* user-visible date for logging */
4277 s->logs.tv_accept = now; /* corrected date for internal use */
4278 tv_zero(&s->logs.tv_request);
4279 s->logs.t_queue = -1;
4280 s->logs.t_connect = -1;
4281 s->logs.t_data = -1;
4282 s->logs.t_close = 0;
4283 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4284 s->logs.srv_queue_size = 0; /* we will get this number soon */
4285
Willy Tarreau9b28e032012-10-12 23:49:43 +02004286 s->logs.bytes_in = s->req->total = s->req->buf->i;
4287 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288
4289 if (s->pend_pos)
4290 pendconn_free(s->pend_pos);
4291
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004292 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 if (s->flags & SN_CURR_SESS) {
4294 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004295 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004297 if (may_dequeue_tasks(objt_server(s->target), s->be))
4298 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 }
4300
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004301 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302
4303 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004304 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4305 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004306 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004308 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 s->req->cons->err_loc = NULL;
4310 s->req->cons->exp = TICK_ETERNITY;
4311 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004312 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4313 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 +02004314 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 +01004315 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004316
4317 if (s->flags & SN_COMP_READY)
4318 s->comp_algo->end(&s->comp_ctx);
4319 s->comp_algo = NULL;
4320 s->flags &= ~SN_COMP_READY;
4321
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 s->txn.meth = 0;
4323 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004324 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004325 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326 s->req->cons->flags |= SI_FL_INDEP_STR;
4327
Willy Tarreau96e31212011-05-30 18:10:30 +02004328 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004329 s->req->flags |= CF_NEVER_WAIT;
4330 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004331 }
4332
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 /* if the request buffer is not empty, it means we're
4334 * about to process another request, so send pending
4335 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004336 * Just don't do this if the buffer is close to be full,
4337 * because the request will wait for it to flush a little
4338 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004340 if (s->req->buf->i) {
4341 if (s->rep->buf->o &&
4342 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4343 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004344 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004345 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004346
4347 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004348 channel_auto_read(s->req);
4349 channel_auto_close(s->req);
4350 channel_auto_read(s->rep);
4351 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352
Willy Tarreau342b11c2010-11-24 16:22:09 +01004353 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354 s->rep->analysers = 0;
4355
4356 http_silent_debug(__LINE__, s);
4357}
4358
4359
4360/* This function updates the request state machine according to the response
4361 * state machine and buffer flags. It returns 1 if it changes anything (flag
4362 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4363 * it is only used to find when a request/response couple is complete. Both
4364 * this function and its equivalent should loop until both return zero. It
4365 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4366 */
4367int http_sync_req_state(struct session *s)
4368{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004369 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004370 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004371 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372 unsigned int old_state = txn->req.msg_state;
4373
4374 http_silent_debug(__LINE__, s);
4375 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4376 return 0;
4377
4378 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004379 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004380 * We can shut the read side unless we want to abort_on_close,
4381 * or we have a POST request. The issue with POST requests is
4382 * that some browsers still send a CRLF after the request, and
4383 * this CRLF must be read so that it does not remain in the kernel
4384 * buffers, otherwise a close could cause an RST on some systems
4385 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004386 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004387 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004388 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389
Willy Tarreau40f151a2012-12-20 12:10:09 +01004390 /* if the server closes the connection, we want to immediately react
4391 * and close the socket to save packets and syscalls.
4392 */
4393 chn->cons->flags |= SI_FL_NOHALF;
4394
Willy Tarreau610ecce2010-01-04 21:15:02 +01004395 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4396 goto wait_other_side;
4397
4398 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4399 /* The server has not finished to respond, so we
4400 * don't want to move in order not to upset it.
4401 */
4402 goto wait_other_side;
4403 }
4404
4405 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4406 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004407 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004409 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 goto wait_other_side;
4411 }
4412
4413 /* When we get here, it means that both the request and the
4414 * response have finished receiving. Depending on the connection
4415 * mode, we'll have to wait for the last bytes to leave in either
4416 * direction, and sometimes for a close to be effective.
4417 */
4418
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004419 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4420 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004421 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4422 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004423 }
4424 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4425 /* Option forceclose is set, or either side wants to close,
4426 * let's enforce it now that we're not expecting any new
4427 * data to come. The caller knows the session is complete
4428 * once both states are CLOSED.
4429 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004430 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4431 channel_shutr_now(chn);
4432 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004433 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004434 }
4435 else {
4436 /* The last possible modes are keep-alive and tunnel. Since tunnel
4437 * mode does not set the body analyser, we can't reach this place
4438 * in tunnel mode, so we're left with keep-alive only.
4439 * This mode is currently not implemented, we switch to tunnel mode.
4440 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004442 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004443 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 }
4445
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004446 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004448 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004449
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004450 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 txn->req.msg_state = HTTP_MSG_CLOSING;
4452 goto http_msg_closing;
4453 }
4454 else {
4455 txn->req.msg_state = HTTP_MSG_CLOSED;
4456 goto http_msg_closed;
4457 }
4458 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004459 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004460 }
4461
4462 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4463 http_msg_closing:
4464 /* nothing else to forward, just waiting for the output buffer
4465 * to be empty and for the shutw_now to take effect.
4466 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004467 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 txn->req.msg_state = HTTP_MSG_CLOSED;
4469 goto http_msg_closed;
4470 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004471 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 txn->req.msg_state = HTTP_MSG_ERROR;
4473 goto wait_other_side;
4474 }
4475 }
4476
4477 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4478 http_msg_closed:
4479 goto wait_other_side;
4480 }
4481
4482 wait_other_side:
4483 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004484 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485}
4486
4487
4488/* This function updates the response state machine according to the request
4489 * state machine and buffer flags. It returns 1 if it changes anything (flag
4490 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4491 * it is only used to find when a request/response couple is complete. Both
4492 * this function and its equivalent should loop until both return zero. It
4493 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4494 */
4495int http_sync_res_state(struct session *s)
4496{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004497 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004499 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 unsigned int old_state = txn->rsp.msg_state;
4501
4502 http_silent_debug(__LINE__, s);
4503 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4504 return 0;
4505
4506 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4507 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004508 * still monitor the server connection for a possible close
4509 * while the request is being uploaded, so we don't disable
4510 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004512 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513
4514 if (txn->req.msg_state == HTTP_MSG_ERROR)
4515 goto wait_other_side;
4516
4517 if (txn->req.msg_state < HTTP_MSG_DONE) {
4518 /* The client seems to still be sending data, probably
4519 * because we got an error response during an upload.
4520 * We have the choice of either breaking the connection
4521 * or letting it pass through. Let's do the later.
4522 */
4523 goto wait_other_side;
4524 }
4525
4526 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4527 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004528 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004529 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004530 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 goto wait_other_side;
4532 }
4533
4534 /* When we get here, it means that both the request and the
4535 * response have finished receiving. Depending on the connection
4536 * mode, we'll have to wait for the last bytes to leave in either
4537 * direction, and sometimes for a close to be effective.
4538 */
4539
4540 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4541 /* Server-close mode : shut read and wait for the request
4542 * side to close its output buffer. The caller will detect
4543 * when we're in DONE and the other is in CLOSED and will
4544 * catch that for the final cleanup.
4545 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4547 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004549 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4550 /* Option forceclose is set, or either side wants to close,
4551 * let's enforce it now that we're not expecting any new
4552 * data to come. The caller knows the session is complete
4553 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004555 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4556 channel_shutr_now(chn);
4557 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004558 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 }
4560 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004561 /* The last possible modes are keep-alive and tunnel. Since tunnel
4562 * mode does not set the body analyser, we can't reach this place
4563 * in tunnel mode, so we're left with keep-alive only.
4564 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004568 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 }
4570
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004571 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004573 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4575 goto http_msg_closing;
4576 }
4577 else {
4578 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4579 goto http_msg_closed;
4580 }
4581 }
4582 goto wait_other_side;
4583 }
4584
4585 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4586 http_msg_closing:
4587 /* nothing else to forward, just waiting for the output buffer
4588 * to be empty and for the shutw_now to take effect.
4589 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004590 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4592 goto http_msg_closed;
4593 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004594 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004596 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004597 if (objt_server(s->target))
4598 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599 goto wait_other_side;
4600 }
4601 }
4602
4603 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4604 http_msg_closed:
4605 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004606 bi_erase(chn);
4607 channel_auto_close(chn);
4608 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004609 goto wait_other_side;
4610 }
4611
4612 wait_other_side:
4613 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004614 /* We force the response to leave immediately if we're waiting for the
4615 * other side, since there is no pending shutdown to push it out.
4616 */
4617 if (!channel_is_empty(chn))
4618 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620}
4621
4622
4623/* Resync the request and response state machines. Return 1 if either state
4624 * changes.
4625 */
4626int http_resync_states(struct session *s)
4627{
4628 struct http_txn *txn = &s->txn;
4629 int old_req_state = txn->req.msg_state;
4630 int old_res_state = txn->rsp.msg_state;
4631
4632 http_silent_debug(__LINE__, s);
4633 http_sync_req_state(s);
4634 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004635 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 if (!http_sync_res_state(s))
4637 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004638 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 if (!http_sync_req_state(s))
4640 break;
4641 }
4642 http_silent_debug(__LINE__, s);
4643 /* OK, both state machines agree on a compatible state.
4644 * There are a few cases we're interested in :
4645 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4646 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4647 * directions, so let's simply disable both analysers.
4648 * - HTTP_MSG_CLOSED on the response only means we must abort the
4649 * request.
4650 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4651 * with server-close mode means we've completed one request and we
4652 * must re-initialize the server connection.
4653 */
4654
4655 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4656 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4657 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4658 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4659 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004660 channel_auto_close(s->req);
4661 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004663 channel_auto_close(s->rep);
4664 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004666 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4667 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004668 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004669 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004670 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004671 channel_auto_close(s->rep);
4672 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004673 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004674 channel_abort(s->req);
4675 channel_auto_close(s->req);
4676 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004677 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 }
4679 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4680 txn->rsp.msg_state == HTTP_MSG_DONE &&
4681 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4682 /* server-close: terminate this server connection and
4683 * reinitialize a fresh-new transaction.
4684 */
4685 http_end_txn_clean_session(s);
4686 }
4687
4688 http_silent_debug(__LINE__, s);
4689 return txn->req.msg_state != old_req_state ||
4690 txn->rsp.msg_state != old_res_state;
4691}
4692
Willy Tarreaud98cf932009-12-27 22:54:55 +01004693/* This function is an analyser which forwards request body (including chunk
4694 * sizes if any). It is called as soon as we must forward, even if we forward
4695 * zero byte. The only situation where it must not be called is when we're in
4696 * tunnel mode and we want to forward till the close. It's used both to forward
4697 * remaining data and to resync after end of body. It expects the msg_state to
4698 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4699 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004700 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004701 * bytes of pending data + the headers if not already done (between sol and sov).
4702 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004703 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004704int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004705{
4706 struct http_txn *txn = &s->txn;
4707 struct http_msg *msg = &s->txn.req;
4708
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004709 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4710 return 0;
4711
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004712 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004713 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004714 /* Output closed while we were sending data. We must abort and
4715 * wake the other side up.
4716 */
4717 msg->msg_state = HTTP_MSG_ERROR;
4718 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004719 return 1;
4720 }
4721
Willy Tarreau4fe41902010-06-07 22:27:41 +02004722 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004723 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004724
4725 /* Note that we don't have to send 100-continue back because we don't
4726 * need the data to complete our job, and it's up to the server to
4727 * decide whether to return 100, 417 or anything else in return of
4728 * an "Expect: 100-continue" header.
4729 */
4730
4731 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004732 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004733 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004734 * is still null. We must save the body in msg->next because it
4735 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004736 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004737 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004738
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004739 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004741 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004742 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004743 }
4744
Willy Tarreaud98cf932009-12-27 22:54:55 +01004745 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004746 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004747
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004749 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004750 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004751 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004752 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004753 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004754 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004755 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004756 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004757
Willy Tarreaucaabe412010-01-03 23:08:28 +01004758 if (msg->msg_state == HTTP_MSG_DATA) {
4759 /* must still forward */
4760 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004761 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004762
4763 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004764 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004765 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004766 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004767 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004768 }
4769 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004770 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004771 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004772 * TRAILERS state.
4773 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004774 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775
Willy Tarreau54d23df2012-10-25 19:04:45 +02004776 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004778 else if (ret < 0) {
4779 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004780 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004781 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004783 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004785 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004786 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004787 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004788 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004789
4790 if (ret == 0)
4791 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004792 else if (ret < 0) {
4793 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004794 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004795 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004796 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004797 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004798 /* we're in MSG_CHUNK_SIZE now */
4799 }
4800 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004801 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004802
4803 if (ret == 0)
4804 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004805 else if (ret < 0) {
4806 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004807 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004808 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004809 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004810 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811 /* we're in HTTP_MSG_DONE now */
4812 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004814 int old_state = msg->msg_state;
4815
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004817 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004818 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4819 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004820 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 if (http_resync_states(s)) {
4822 /* some state changes occurred, maybe the analyser
4823 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004824 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004825 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004826 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004827 /* request errors are most likely due to
4828 * the server aborting the transfer.
4829 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004830 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004831 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004832 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004833 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004835 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004837 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004838
4839 /* If "option abortonclose" is set on the backend, we
4840 * want to monitor the client's connection and forward
4841 * any shutdown notification to the server, which will
4842 * decide whether to close or to go on processing the
4843 * request.
4844 */
4845 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004846 channel_auto_read(req);
4847 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004848 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004849 else if (s->txn.meth == HTTP_METH_POST) {
4850 /* POST requests may require to read extra CRLF
4851 * sent by broken browsers and which could cause
4852 * an RST to be sent upon close on some systems
4853 * (eg: Linux).
4854 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004855 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004856 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004857
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 }
4860 }
4861
Willy Tarreaud98cf932009-12-27 22:54:55 +01004862 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004863 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004864 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004865 if (!(s->flags & SN_ERR_MASK))
4866 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004867 if (!(s->flags & SN_FINST_MASK)) {
4868 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4869 s->flags |= SN_FINST_H;
4870 else
4871 s->flags |= SN_FINST_D;
4872 }
4873
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004874 s->fe->fe_counters.cli_aborts++;
4875 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004876 if (objt_server(s->target))
4877 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004878
4879 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004880 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004881
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004882 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004883 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004884 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004885
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004886 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004887 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004888 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004889 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004890 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004891
Willy Tarreau5c620922011-05-11 19:56:11 +02004892 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004893 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004894 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004895 * modes are already handled by the stream sock layer. We must not do
4896 * this in content-length mode because it could present the MSG_MORE
4897 * flag with the last block of forwarded data, which would cause an
4898 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004899 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004900 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004901 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004904 return 0;
4905
Willy Tarreaud98cf932009-12-27 22:54:55 +01004906 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004907 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004908 if (s->listener->counters)
4909 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004910 return_bad_req_stats_ok:
4911 txn->req.msg_state = HTTP_MSG_ERROR;
4912 if (txn->status) {
4913 /* Note: we don't send any error if some data were already sent */
4914 stream_int_retnclose(req->prod, NULL);
4915 } else {
4916 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004917 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004918 }
4919 req->analysers = 0;
4920 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004921
4922 if (!(s->flags & SN_ERR_MASK))
4923 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004924 if (!(s->flags & SN_FINST_MASK)) {
4925 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4926 s->flags |= SN_FINST_H;
4927 else
4928 s->flags |= SN_FINST_D;
4929 }
4930 return 0;
4931
4932 aborted_xfer:
4933 txn->req.msg_state = HTTP_MSG_ERROR;
4934 if (txn->status) {
4935 /* Note: we don't send any error if some data were already sent */
4936 stream_int_retnclose(req->prod, NULL);
4937 } else {
4938 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004939 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004940 }
4941 req->analysers = 0;
4942 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4943
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004944 s->fe->fe_counters.srv_aborts++;
4945 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004946 if (objt_server(s->target))
4947 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004948
4949 if (!(s->flags & SN_ERR_MASK))
4950 s->flags |= SN_ERR_SRVCL;
4951 if (!(s->flags & SN_FINST_MASK)) {
4952 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4953 s->flags |= SN_FINST_H;
4954 else
4955 s->flags |= SN_FINST_D;
4956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004957 return 0;
4958}
4959
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004960/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4961 * processing can continue on next analysers, or zero if it either needs more
4962 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4963 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4964 * when it has nothing left to do, and may remove any analyser when it wants to
4965 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004966 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004967int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004968{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004969 struct http_txn *txn = &s->txn;
4970 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004971 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004972 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004973 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004974 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004975
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004976 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 +02004977 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004978 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004979 rep,
4980 rep->rex, rep->wex,
4981 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004982 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004983 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004985 /*
4986 * Now parse the partial (or complete) lines.
4987 * We will check the response syntax, and also join multi-line
4988 * headers. An index of all the lines will be elaborated while
4989 * parsing.
4990 *
4991 * For the parsing, we use a 28 states FSM.
4992 *
4993 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004994 * rep->buf->p = beginning of response
4995 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4996 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004997 * msg->eol = end of current header or line (LF or CRLF)
4998 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 */
5000
Willy Tarreau83e3af02009-12-28 17:39:57 +01005001 /* There's a protected area at the end of the buffer for rewriting
5002 * purposes. We don't want to start to parse the request if the
5003 * protected area is affected, because we may have to move processed
5004 * data later, which is much more complicated.
5005 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005006 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02005007 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005008 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5009 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
5010 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01005011 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005012 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01005013 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005014 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005015 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01005016 return 0;
5017 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02005018 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
5019 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005020 }
5021
Willy Tarreau9b28e032012-10-12 23:49:43 +02005022 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005023 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005024 }
5025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005026 /* 1: we might have to print this header in debug mode */
5027 if (unlikely((global.mode & MODE_DEBUG) &&
5028 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005029 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005030 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031
Willy Tarreau9b28e032012-10-12 23:49:43 +02005032 sol = rep->buf->p;
5033 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005034 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005036 sol += hdr_idx_first_pos(&txn->hdr_idx);
5037 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039 while (cur_idx) {
5040 eol = sol + txn->hdr_idx.v[cur_idx].len;
5041 debug_hdr("srvhdr", s, sol, eol);
5042 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5043 cur_idx = txn->hdr_idx.v[cur_idx].next;
5044 }
5045 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 /*
5048 * Now we quickly check if we have found a full valid response.
5049 * If not so, we check the FD and buffer states before leaving.
5050 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005051 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 * responses are checked first.
5053 *
5054 * Depending on whether the client is still there or not, we
5055 * may send an error response back or not. Note that normally
5056 * we should only check for HTTP status there, and check I/O
5057 * errors somewhere else.
5058 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059
Willy Tarreau655dce92009-11-08 13:10:58 +01005060 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061 /* Invalid response */
5062 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5063 /* we detected a parsing error. We want to archive this response
5064 * in the dedicated proxy area for later troubleshooting.
5065 */
5066 hdr_response_bad:
5067 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005068 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005069
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005070 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005071 if (objt_server(s->target)) {
5072 objt_server(s->target)->counters.failed_resp++;
5073 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005074 }
Willy Tarreau64648412010-03-05 10:41:54 +01005075 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005076 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 rep->analysers = 0;
5078 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005079 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005080 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005081 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082
5083 if (!(s->flags & SN_ERR_MASK))
5084 s->flags |= SN_ERR_PRXCOND;
5085 if (!(s->flags & SN_FINST_MASK))
5086 s->flags |= SN_FINST_H;
5087
5088 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005089 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005090
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005091 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005092 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005093 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005094 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 goto hdr_response_bad;
5096 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005099 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005101 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005103 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005104 if (objt_server(s->target)) {
5105 objt_server(s->target)->counters.failed_resp++;
5106 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005107 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005108
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005109 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005110 rep->analysers = 0;
5111 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005112 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005113 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005114 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005115
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 if (!(s->flags & SN_ERR_MASK))
5117 s->flags |= SN_ERR_SRVCL;
5118 if (!(s->flags & SN_FINST_MASK))
5119 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005120 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005124 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005125 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005126 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005127
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005128 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005129 if (objt_server(s->target)) {
5130 objt_server(s->target)->counters.failed_resp++;
5131 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005132 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005133
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005134 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135 rep->analysers = 0;
5136 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005137 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005138 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005139 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 if (!(s->flags & SN_ERR_MASK))
5142 s->flags |= SN_ERR_SRVTO;
5143 if (!(s->flags & SN_FINST_MASK))
5144 s->flags |= SN_FINST_H;
5145 return 0;
5146 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005147
Willy Tarreauf003d372012-11-26 13:35:37 +01005148 /* client abort with an abortonclose */
5149 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5150 s->fe->fe_counters.cli_aborts++;
5151 s->be->be_counters.cli_aborts++;
5152 if (objt_server(s->target))
5153 objt_server(s->target)->counters.cli_aborts++;
5154
5155 rep->analysers = 0;
5156 channel_auto_close(rep);
5157
5158 txn->status = 400;
5159 bi_erase(rep);
5160 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5161
5162 if (!(s->flags & SN_ERR_MASK))
5163 s->flags |= SN_ERR_CLICL;
5164 if (!(s->flags & SN_FINST_MASK))
5165 s->flags |= SN_FINST_H;
5166
5167 /* process_session() will take care of the error */
5168 return 0;
5169 }
5170
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005171 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005172 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005173 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005174 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005175
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005176 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005177 if (objt_server(s->target)) {
5178 objt_server(s->target)->counters.failed_resp++;
5179 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005180 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005181
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005182 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183 rep->analysers = 0;
5184 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005185 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005186 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005187 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 if (!(s->flags & SN_ERR_MASK))
5190 s->flags |= SN_ERR_SRVCL;
5191 if (!(s->flags & SN_FINST_MASK))
5192 s->flags |= SN_FINST_H;
5193 return 0;
5194 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005195
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005196 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005197 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005199 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005200
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005201 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005203 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204
5205 if (!(s->flags & SN_ERR_MASK))
5206 s->flags |= SN_ERR_CLICL;
5207 if (!(s->flags & SN_FINST_MASK))
5208 s->flags |= SN_FINST_H;
5209
5210 /* process_session() will take care of the error */
5211 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005212 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005213
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005214 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 return 0;
5216 }
5217
5218 /* More interesting part now : we know that we have a complete
5219 * response which at least looks like HTTP. We have an indicator
5220 * of each header's length, so we can parse them quickly.
5221 */
5222
5223 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005224 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226 /*
5227 * 1: get the status code
5228 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005229 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005230 if (n < 1 || n > 5)
5231 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005232 /* when the client triggers a 4xx from the server, it's most often due
5233 * to a missing object or permission. These events should be tracked
5234 * because if they happen often, it may indicate a brute force or a
5235 * vulnerability scan.
5236 */
5237 if (n == 4)
5238 session_inc_http_err_ctr(s);
5239
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005240 if (objt_server(s->target))
5241 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242
Willy Tarreau5b154472009-12-21 20:11:07 +01005243 /* check if the response is HTTP/1.1 or above */
5244 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005245 ((rep->buf->p[5] > '1') ||
5246 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005247 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005248
5249 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005250 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 +01005251
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005252 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005253 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005254
Willy Tarreau9b28e032012-10-12 23:49:43 +02005255 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005256
Willy Tarreau39650402010-03-15 19:44:39 +01005257 /* Adjust server's health based on status code. Note: status codes 501
5258 * and 505 are triggered on demand by client request, so we must not
5259 * count them as server failures.
5260 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005261 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005262 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005263 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005264 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005265 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005266 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005267
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 /*
5269 * 2: check for cacheability.
5270 */
5271
5272 switch (txn->status) {
5273 case 200:
5274 case 203:
5275 case 206:
5276 case 300:
5277 case 301:
5278 case 410:
5279 /* RFC2616 @13.4:
5280 * "A response received with a status code of
5281 * 200, 203, 206, 300, 301 or 410 MAY be stored
5282 * by a cache (...) unless a cache-control
5283 * directive prohibits caching."
5284 *
5285 * RFC2616 @9.5: POST method :
5286 * "Responses to this method are not cacheable,
5287 * unless the response includes appropriate
5288 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005289 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005291 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5293 break;
5294 default:
5295 break;
5296 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005298 /*
5299 * 3: we may need to capture headers
5300 */
5301 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005302 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005303 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 txn->rsp.cap, s->fe->rsp_cap);
5305
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005306 /* 4: determine the transfer-length.
5307 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5308 * the presence of a message-body in a RESPONSE and its transfer length
5309 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005310 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005311 * All responses to the HEAD request method MUST NOT include a
5312 * message-body, even though the presence of entity-header fields
5313 * might lead one to believe they do. All 1xx (informational), 204
5314 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5315 * message-body. All other responses do include a message-body,
5316 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005317 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005318 * 1. Any response which "MUST NOT" include a message-body (such as the
5319 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5320 * always terminated by the first empty line after the header fields,
5321 * regardless of the entity-header fields present in the message.
5322 *
5323 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5324 * the "chunked" transfer-coding (Section 6.2) is used, the
5325 * transfer-length is defined by the use of this transfer-coding.
5326 * If a Transfer-Encoding header field is present and the "chunked"
5327 * transfer-coding is not present, the transfer-length is defined by
5328 * the sender closing the connection.
5329 *
5330 * 3. If a Content-Length header field is present, its decimal value in
5331 * OCTETs represents both the entity-length and the transfer-length.
5332 * If a message is received with both a Transfer-Encoding header
5333 * field and a Content-Length header field, the latter MUST be ignored.
5334 *
5335 * 4. If the message uses the media type "multipart/byteranges", and
5336 * the transfer-length is not otherwise specified, then this self-
5337 * delimiting media type defines the transfer-length. This media
5338 * type MUST NOT be used unless the sender knows that the recipient
5339 * can parse it; the presence in a request of a Range header with
5340 * multiple byte-range specifiers from a 1.1 client implies that the
5341 * client can parse multipart/byteranges responses.
5342 *
5343 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005344 */
5345
5346 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005347 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005348 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005349 * FIXME: should we parse anyway and return an error on chunked encoding ?
5350 */
5351 if (txn->meth == HTTP_METH_HEAD ||
5352 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005353 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005354 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005355 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005356 goto skip_content_length;
5357 }
5358
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005359 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005360 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005361 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005362 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005363 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005364 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5365 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005366 /* bad transfer-encoding (chunked followed by something else) */
5367 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005368 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005369 break;
5370 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005371 }
5372
5373 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5374 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005375 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005376 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005377 signed long long cl;
5378
Willy Tarreauad14f752011-09-02 20:33:27 +02005379 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005380 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005381 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005382 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005383
Willy Tarreauad14f752011-09-02 20:33:27 +02005384 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005385 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005386 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005387 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005388
Willy Tarreauad14f752011-09-02 20:33:27 +02005389 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005390 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005391 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005392 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005393
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005394 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005395 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005396 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005397 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005398
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005399 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005400 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005401 }
5402
William Lallemand82fe75c2012-10-23 10:25:10 +02005403 if (s->fe->comp || s->be->comp)
5404 select_compression_response_header(s, rep->buf);
5405
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005406 /* FIXME: we should also implement the multipart/byterange method.
5407 * For now on, we resort to close mode in this case (unknown length).
5408 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005410
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005411 /* end of job, return OK */
5412 rep->analysers &= ~an_bit;
5413 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005414 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415 return 1;
5416}
5417
5418/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005419 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5420 * and updates t->rep->analysers. It might make sense to explode it into several
5421 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005422 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005423int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005424{
5425 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005426 struct http_msg *msg = &txn->rsp;
5427 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005428 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005430 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 +02005431 now_ms, __FUNCTION__,
5432 t,
5433 rep,
5434 rep->rex, rep->wex,
5435 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005436 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437 rep->analysers);
5438
Willy Tarreau655dce92009-11-08 13:10:58 +01005439 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005440 return 0;
5441
5442 rep->analysers &= ~an_bit;
5443 rep->analyse_exp = TICK_ETERNITY;
5444
Willy Tarreau5b154472009-12-21 20:11:07 +01005445 /* Now we have to check if we need to modify the Connection header.
5446 * This is more difficult on the response than it is on the request,
5447 * because we can have two different HTTP versions and we don't know
5448 * how the client will interprete a response. For instance, let's say
5449 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5450 * HTTP/1.1 response without any header. Maybe it will bound itself to
5451 * HTTP/1.0 because it only knows about it, and will consider the lack
5452 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5453 * the lack of header as a keep-alive. Thus we will use two flags
5454 * indicating how a request MAY be understood by the client. In case
5455 * of multiple possibilities, we'll fix the header to be explicit. If
5456 * ambiguous cases such as both close and keepalive are seen, then we
5457 * will fall back to explicit close. Note that we won't take risks with
5458 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005459 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005460 */
5461
Willy Tarreaudc008c52010-02-01 16:20:08 +01005462 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5463 txn->status == 101)) {
5464 /* Either we've established an explicit tunnel, or we're
5465 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005466 * to understand the next protocols. We have to switch to tunnel
5467 * mode, so that we transfer the request and responses then let
5468 * this protocol pass unmodified. When we later implement specific
5469 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005470 * header which contains information about that protocol for
5471 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005472 */
5473 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5474 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005475 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5476 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5477 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005478 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005479
Willy Tarreau60466522010-01-18 19:08:45 +01005480 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005481 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005482 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5483 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005484
Willy Tarreau60466522010-01-18 19:08:45 +01005485 /* now adjust header transformations depending on current state */
5486 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5487 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5488 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005489 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005490 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005491 }
Willy Tarreau60466522010-01-18 19:08:45 +01005492 else { /* SCL / KAL */
5493 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005494 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005495 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005496 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005497
Willy Tarreau60466522010-01-18 19:08:45 +01005498 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005499 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005500
Willy Tarreau60466522010-01-18 19:08:45 +01005501 /* Some keep-alive responses are converted to Server-close if
5502 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005503 */
Willy Tarreau60466522010-01-18 19:08:45 +01005504 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5505 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005506 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005507 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005508 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005509 }
5510
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005511 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005512 /*
5513 * 3: we will have to evaluate the filters.
5514 * As opposed to version 1.2, now they will be evaluated in the
5515 * filters order and not in the header order. This means that
5516 * each filter has to be validated among all headers.
5517 *
5518 * Filters are tried with ->be first, then with ->fe if it is
5519 * different from ->be.
5520 */
5521
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005522 cur_proxy = t->be;
5523 while (1) {
5524 struct proxy *rule_set = cur_proxy;
5525
5526 /* try headers filters */
5527 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005528 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005529 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005530 if (objt_server(t->target)) {
5531 objt_server(t->target)->counters.failed_resp++;
5532 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005533 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005534 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005535 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005536 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005537 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005538 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005539 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005540 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 if (!(t->flags & SN_ERR_MASK))
5542 t->flags |= SN_ERR_PRXCOND;
5543 if (!(t->flags & SN_FINST_MASK))
5544 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005545 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005546 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005547 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005548
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005549 /* has the response been denied ? */
5550 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005551 if (objt_server(t->target))
5552 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005553
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005554 t->be->be_counters.denied_resp++;
5555 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005556 if (t->listener->counters)
5557 t->listener->counters->denied_resp++;
5558
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005559 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005560 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005561
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005562 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005563 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005564 if (txn->status < 200)
5565 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005566 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005567 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005568 ret = acl_pass(ret);
5569 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5570 ret = !ret;
5571 if (!ret)
5572 continue;
5573 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005574 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005575 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005576 }
5577
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005578 /* check whether we're already working on the frontend */
5579 if (cur_proxy == t->fe)
5580 break;
5581 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005582 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005583
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005584 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005585 * We may be facing a 100-continue response, in which case this
5586 * is not the right response, and we're waiting for the next one.
5587 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005588 * next one.
5589 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005590 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005591 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005592 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005593 msg->msg_state = HTTP_MSG_RPBEFORE;
5594 txn->status = 0;
5595 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5596 return 1;
5597 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005598 else if (unlikely(txn->status < 200))
5599 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005600
5601 /* we don't have any 1xx status code now */
5602
5603 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005604 * 4: check for server cookie.
5605 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005606 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5607 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005608 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005609
Willy Tarreaubaaee002006-06-26 02:48:02 +02005610
Willy Tarreaua15645d2007-03-18 16:22:39 +01005611 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005612 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005613 */
Willy Tarreau67402132012-05-31 20:40:20 +02005614 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005615 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005616
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005617 /*
5618 * 6: add server cookie in the response if needed
5619 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005620 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005621 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005622 (!(t->flags & SN_DIRECT) ||
5623 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5624 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5625 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5626 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005627 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005628 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005629 /* the server is known, it's not the one the client requested, or the
5630 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005631 * insert a set-cookie here, except if we want to insert only on POST
5632 * requests and this one isn't. Note that servers which don't have cookies
5633 * (eg: some backup servers) will return a full cookie removal request.
5634 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005635 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005636 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005637 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5638 t->be->cookie_name);
5639 }
5640 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005641 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005642
5643 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5644 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005645 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5646 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5647 trash.len += 5;
5648
Willy Tarreauef4f3912010-10-07 21:00:29 +02005649 if (t->be->cookie_maxlife) {
5650 /* emit first_date, which is either the original one or
5651 * the current date.
5652 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005653 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005654 s30tob64(txn->cookie_first_date ?
5655 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005656 (date.tv_sec+3) >> 2, trash.str + trash.len);
5657 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005658 }
5659 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005660 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005661 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005662
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005663 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005664 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005665
Willy Tarreau4992dd22012-05-31 21:02:17 +02005666 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005667 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005668
5669 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005670 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005671
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005672 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005673 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005674
Willy Tarreauf1348312010-10-07 15:54:11 +02005675 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005676 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005677 /* the server did not change, only the date was updated */
5678 txn->flags |= TX_SCK_UPDATED;
5679 else
5680 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005682 /* Here, we will tell an eventual cache on the client side that we don't
5683 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5684 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5685 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5686 */
Willy Tarreau67402132012-05-31 20:40:20 +02005687 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005688
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005689 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5690
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005691 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005692 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005693 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694 }
5695 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005697 /*
5698 * 7: check if result will be cacheable with a cookie.
5699 * We'll block the response if security checks have caught
5700 * nasty things such as a cacheable cookie.
5701 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005702 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5703 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005704 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005705
5706 /* we're in presence of a cacheable response containing
5707 * a set-cookie header. We'll block it as requested by
5708 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005709 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005710 if (objt_server(t->target))
5711 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005712
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005713 t->be->be_counters.denied_resp++;
5714 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005715 if (t->listener->counters)
5716 t->listener->counters->denied_resp++;
5717
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005718 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005719 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005720 send_log(t->be, LOG_ALERT,
5721 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005722 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005723 goto return_srv_prx_502;
5724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725
5726 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005727 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005728 * If an "Upgrade" token is found, the header is left untouched in order
5729 * not to have to deal with some client bugs : some of them fail an upgrade
5730 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005732 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5733 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5734 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005735 unsigned int want_flags = 0;
5736
5737 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5738 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5739 /* we want a keep-alive response here. Keep-alive header
5740 * required if either side is not 1.1.
5741 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005742 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005743 want_flags |= TX_CON_KAL_SET;
5744 }
5745 else {
5746 /* we want a close response here. Close header required if
5747 * the server is 1.1, regardless of the client.
5748 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005749 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005750 want_flags |= TX_CON_CLO_SET;
5751 }
5752
5753 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005754 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005755 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005757 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005758 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005759 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005761
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005762 /*************************************************************
5763 * OK, that's finished for the headers. We have done what we *
5764 * could. Let's switch to the DATA state. *
5765 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005768
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769 /* if the user wants to log as soon as possible, without counting
5770 * bytes from the server, then this is the right moment. We have
5771 * to temporarily assign bytes_out to log what we currently have.
5772 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005773 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005774 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5775 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005776 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005777 t->logs.bytes_out = 0;
5778 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005779
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780 /* Note: we must not try to cheat by jumping directly to DATA,
5781 * otherwise we would not let the client side wake up.
5782 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005784 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005786 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005787}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005788
Willy Tarreaud98cf932009-12-27 22:54:55 +01005789/* This function is an analyser which forwards response body (including chunk
5790 * sizes if any). It is called as soon as we must forward, even if we forward
5791 * zero byte. The only situation where it must not be called is when we're in
5792 * tunnel mode and we want to forward till the close. It's used both to forward
5793 * remaining data and to resync after end of body. It expects the msg_state to
5794 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5795 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005796 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005797 * bytes of pending data + the headers if not already done (between sol and sov).
5798 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005799 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005800int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005801{
5802 struct http_txn *txn = &s->txn;
5803 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005804 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005805 static struct buffer *tmpbuf = NULL;
5806 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005807 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005808 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005809
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005810 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5811 return 0;
5812
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005813 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005814 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005815 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005816 /* Output closed while we were sending data. We must abort and
5817 * wake the other side up.
5818 */
5819 msg->msg_state = HTTP_MSG_ERROR;
5820 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005821 return 1;
5822 }
5823
Willy Tarreau4fe41902010-06-07 22:27:41 +02005824 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005825 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005826
William Lallemand82fe75c2012-10-23 10:25:10 +02005827 /* this is the first time we need the compression buffer */
5828 if (s->comp_algo != NULL && tmpbuf == NULL) {
5829 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5830 goto aborted_xfer; /* no memory */
5831 }
5832
Willy Tarreaud98cf932009-12-27 22:54:55 +01005833 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005834 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005835 * rep->buf.p still points to the beginning of the message and msg->sol
5836 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005837 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005838 channel_forward(res, msg->sov);
5839 msg->next = 0;
5840 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005841
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005842 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005843 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005844 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005845 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005846 }
5847
William Lallemand82fe75c2012-10-23 10:25:10 +02005848 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005849 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005850 if (ret < 0)
5851 goto missing_data; /* not enough spaces in buffers */
5852 compressing = 1;
5853 }
5854
Willy Tarreaud98cf932009-12-27 22:54:55 +01005855 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005856 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005857 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005858 if (s->comp_algo == NULL) {
5859 bytes = msg->sov - msg->sol;
5860 if (msg->chunk_len || bytes) {
5861 msg->sol = msg->sov;
5862 msg->next -= bytes; /* will be forwarded */
5863 msg->chunk_len += bytes;
5864 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5865 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005866 }
5867
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005868 switch (msg->msg_state - HTTP_MSG_DATA) {
5869 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005870 if (compressing) {
5871 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5872 if (ret < 0)
5873 goto aborted_xfer;
5874 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005875
5876 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005877 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005878
5879 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005880 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005881 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005882 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005883 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005884 if (compressing && consumed_data) {
5885 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5886 compressing = 0;
5887 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005888 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005889 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005890 /* fall through for HTTP_MSG_CHUNK_CRLF */
5891
5892 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5893 /* we want the CRLF after the data */
5894
5895 ret = http_skip_chunk_crlf(msg);
5896 if (ret == 0)
5897 goto missing_data;
5898 else if (ret < 0) {
5899 if (msg->err_pos >= 0)
5900 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5901 goto return_bad_res;
5902 }
5903 /* skipping data in buffer for compression */
5904 if (compressing) {
5905 b_adv(res->buf, msg->next);
5906 msg->next = 0;
5907 msg->sov = 0;
5908 msg->sol = 0;
5909 }
5910 /* we're in MSG_CHUNK_SIZE now, fall through */
5911
5912 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005913 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005914 * set ->sov and ->next to point to the body and switch to DATA or
5915 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005916 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005917
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005918 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005919 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005920 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005921 else if (ret < 0) {
5922 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005923 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005924 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005925 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005926 if (compressing) {
5927 if (likely(msg->chunk_len > 0)) {
5928 /* skipping data if we are in compression mode */
5929 b_adv(res->buf, msg->next);
5930 msg->next = 0;
5931 msg->sov = 0;
5932 msg->sol = 0;
5933 } else {
5934 if (consumed_data) {
5935 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5936 compressing = 0;
5937 }
5938 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005939 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005940 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005941 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005942
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005943 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5944 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005945 if (ret == 0)
5946 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005947 else if (ret < 0) {
5948 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005949 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005950 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005951 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005952 if (s->comp_algo != NULL) {
5953 /* forwarding trailers */
5954 channel_forward(res, msg->next);
5955 msg->next = 0;
5956 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005957 /* we're in HTTP_MSG_DONE now, but we might still have
5958 * some data pending, so let's loop over once.
5959 */
5960 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005961
5962 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005963 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005964
5965 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005966 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005967 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5968 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005969 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005970 if (http_resync_states(s)) {
5971 http_silent_debug(__LINE__, s);
5972 /* some state changes occurred, maybe the analyser
5973 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005974 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005975 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005976 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005977 /* response errors are most likely due to
5978 * the client aborting the transfer.
5979 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005980 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005981 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005982 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005983 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005984 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005986 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005987 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005988 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005989 }
5990 }
5991
Willy Tarreaud98cf932009-12-27 22:54:55 +01005992 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005993 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005994 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5995 compressing = 0;
5996 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005997
5998 if (res->flags & CF_SHUTW)
5999 goto aborted_xfer;
6000
6001 /* stop waiting for data if the input is closed before the end. If the
6002 * client side was already closed, it means that the client has aborted,
6003 * so we don't want to count this as a server abort. Otherwise it's a
6004 * server abort.
6005 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006006 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006007 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6008 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006009 if (!(s->flags & SN_ERR_MASK))
6010 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006011 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006012 if (objt_server(s->target))
6013 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006014 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006015 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006016
Willy Tarreau40dba092010-03-04 18:14:51 +01006017 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006018 if (!s->req->analysers)
6019 goto return_bad_res;
6020
Willy Tarreauea953162012-05-18 23:41:28 +02006021 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006022 if (s->comp_algo == NULL) {
6023 bytes = msg->sov - msg->sol;
6024 if (msg->chunk_len || bytes) {
6025 msg->sol = msg->sov;
6026 msg->next -= bytes; /* will be forwarded */
6027 msg->chunk_len += bytes;
6028 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6029 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006030 }
6031
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006032 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006033 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006034 * Similarly, with keep-alive on the client side, we don't want to forward a
6035 * close.
6036 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006037 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006038 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6039 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006040 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006041
Willy Tarreau5c620922011-05-11 19:56:11 +02006042 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006043 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006044 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006045 * modes are already handled by the stream sock layer. We must not do
6046 * this in content-length mode because it could present the MSG_MORE
6047 * flag with the last block of forwarded data, which would cause an
6048 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006049 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006050 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006051 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006052
Willy Tarreaud98cf932009-12-27 22:54:55 +01006053 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006054 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006055 return 0;
6056
Willy Tarreau40dba092010-03-04 18:14:51 +01006057 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006058 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006059 if (objt_server(s->target))
6060 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006061
6062 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006063 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006064 /* don't send any error message as we're in the body */
6065 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006066 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006067 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006068 if (objt_server(s->target))
6069 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006070
6071 if (!(s->flags & SN_ERR_MASK))
6072 s->flags |= SN_ERR_PRXCOND;
6073 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006074 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006075 return 0;
6076
6077 aborted_xfer:
6078 txn->rsp.msg_state = HTTP_MSG_ERROR;
6079 /* don't send any error message as we're in the body */
6080 stream_int_retnclose(res->cons, NULL);
6081 res->analysers = 0;
6082 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6083
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006084 s->fe->fe_counters.cli_aborts++;
6085 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006086 if (objt_server(s->target))
6087 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006088
6089 if (!(s->flags & SN_ERR_MASK))
6090 s->flags |= SN_ERR_CLICL;
6091 if (!(s->flags & SN_FINST_MASK))
6092 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006093 return 0;
6094}
6095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006096/* Iterate the same filter through all request headers.
6097 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006098 * Since it can manage the switch to another backend, it updates the per-proxy
6099 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006101int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 char term;
6104 char *cur_ptr, *cur_end, *cur_next;
6105 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006106 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006108 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006110 last_hdr = 0;
6111
Willy Tarreau9b28e032012-10-12 23:49:43 +02006112 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 old_idx = 0;
6114
6115 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006116 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006117 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006118 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 (exp->action == ACT_ALLOW ||
6120 exp->action == ACT_DENY ||
6121 exp->action == ACT_TARPIT))
6122 return 0;
6123
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006124 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006125 if (!cur_idx)
6126 break;
6127
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006128 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006129 cur_ptr = cur_next;
6130 cur_end = cur_ptr + cur_hdr->len;
6131 cur_next = cur_end + cur_hdr->cr + 1;
6132
6133 /* Now we have one header between cur_ptr and cur_end,
6134 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135 */
6136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006137 /* The annoying part is that pattern matching needs
6138 * that we modify the contents to null-terminate all
6139 * strings before testing them.
6140 */
6141
6142 term = *cur_end;
6143 *cur_end = '\0';
6144
6145 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6146 switch (exp->action) {
6147 case ACT_SETBE:
6148 /* It is not possible to jump a second time.
6149 * FIXME: should we return an HTTP/500 here so that
6150 * the admin knows there's a problem ?
6151 */
6152 if (t->be != t->fe)
6153 break;
6154
6155 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006156 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 last_hdr = 1;
6158 break;
6159
6160 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006161 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162 last_hdr = 1;
6163 break;
6164
6165 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006166 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006168
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006169 t->fe->fe_counters.denied_req++;
6170 if (t->fe != t->be)
6171 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006172 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006173 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175 break;
6176
6177 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006178 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006179 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006180
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006181 t->fe->fe_counters.denied_req++;
6182 if (t->fe != t->be)
6183 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006184 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006185 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 break;
6188
6189 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006190 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6191 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006192 /* FIXME: if the user adds a newline in the replacement, the
6193 * index will not be recalculated for now, and the new line
6194 * will not be counted as a new header.
6195 */
6196
6197 cur_end += delta;
6198 cur_next += delta;
6199 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006200 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006201 break;
6202
6203 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006204 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 cur_next += delta;
6206
Willy Tarreaufa355d42009-11-29 18:12:29 +01006207 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006208 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6209 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006210 cur_hdr->len = 0;
6211 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006212 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006213 break;
6214
6215 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 if (cur_end)
6218 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006220 /* keep the link from this header to next one in case of later
6221 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006223 old_idx = cur_idx;
6224 }
6225 return 0;
6226}
6227
6228
6229/* Apply the filter to the request line.
6230 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6231 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006232 * Since it can manage the switch to another backend, it updates the per-proxy
6233 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006234 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006235int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006236{
6237 char term;
6238 char *cur_ptr, *cur_end;
6239 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006240 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006241 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242
Willy Tarreau3d300592007-03-18 18:34:41 +01006243 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006244 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006245 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006246 (exp->action == ACT_ALLOW ||
6247 exp->action == ACT_DENY ||
6248 exp->action == ACT_TARPIT))
6249 return 0;
6250 else if (exp->action == ACT_REMOVE)
6251 return 0;
6252
6253 done = 0;
6254
Willy Tarreau9b28e032012-10-12 23:49:43 +02006255 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006256 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006257
6258 /* Now we have the request line between cur_ptr and cur_end */
6259
6260 /* The annoying part is that pattern matching needs
6261 * that we modify the contents to null-terminate all
6262 * strings before testing them.
6263 */
6264
6265 term = *cur_end;
6266 *cur_end = '\0';
6267
6268 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6269 switch (exp->action) {
6270 case ACT_SETBE:
6271 /* It is not possible to jump a second time.
6272 * FIXME: should we return an HTTP/500 here so that
6273 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006275 if (t->be != t->fe)
6276 break;
6277
6278 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006279 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006280 done = 1;
6281 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006283 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006284 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285 done = 1;
6286 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006287
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006289 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006290
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006291 t->fe->fe_counters.denied_req++;
6292 if (t->fe != t->be)
6293 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006294 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006295 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006297 done = 1;
6298 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006301 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006302
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006303 t->fe->fe_counters.denied_req++;
6304 if (t->fe != t->be)
6305 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006306 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006307 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006309 done = 1;
6310 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 case ACT_REPLACE:
6313 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006314 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6315 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006316 /* FIXME: if the user adds a newline in the replacement, the
6317 * index will not be recalculated for now, and the new line
6318 * will not be counted as a new header.
6319 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006320
Willy Tarreaufa355d42009-11-29 18:12:29 +01006321 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006322 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006323 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006324 HTTP_MSG_RQMETH,
6325 cur_ptr, cur_end + 1,
6326 NULL, NULL);
6327 if (unlikely(!cur_end))
6328 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006330 /* we have a full request and we know that we have either a CR
6331 * or an LF at <ptr>.
6332 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006333 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6334 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006335 /* there is no point trying this regex on headers */
6336 return 1;
6337 }
6338 }
6339 *cur_end = term; /* restore the string terminator */
6340 return done;
6341}
Willy Tarreau97de6242006-12-27 17:18:38 +01006342
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006346 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006348 * unparsable request. Since it can manage the switch to another backend, it
6349 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006351int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006353 struct http_txn *txn = &s->txn;
6354 struct hdr_exp *exp;
6355
6356 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006359 /*
6360 * The interleaving of transformations and verdicts
6361 * makes it difficult to decide to continue or stop
6362 * the evaluation.
6363 */
6364
Willy Tarreau6c123b12010-01-28 20:22:06 +01006365 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6366 break;
6367
Willy Tarreau3d300592007-03-18 18:34:41 +01006368 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006369 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006370 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006372
6373 /* if this filter had a condition, evaluate it now and skip to
6374 * next filter if the condition does not match.
6375 */
6376 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006377 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006378 ret = acl_pass(ret);
6379 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6380 ret = !ret;
6381
6382 if (!ret)
6383 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384 }
6385
6386 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006387 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006388 if (unlikely(ret < 0))
6389 return -1;
6390
6391 if (likely(ret == 0)) {
6392 /* The filter did not match the request, it can be
6393 * iterated through all headers.
6394 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006395 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006399}
6400
6401
Willy Tarreaua15645d2007-03-18 16:22:39 +01006402
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006404 * Try to retrieve the server associated to the appsession.
6405 * If the server is found, it's assigned to the session.
6406 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006407void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006408 struct http_txn *txn = &t->txn;
6409 appsess *asession = NULL;
6410 char *sessid_temp = NULL;
6411
Cyril Bontéb21570a2009-11-29 20:04:48 +01006412 if (len > t->be->appsession_len) {
6413 len = t->be->appsession_len;
6414 }
6415
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006416 if (t->be->options2 & PR_O2_AS_REQL) {
6417 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006418 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006419 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006420 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006421 }
6422
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006423 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006424 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6425 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6426 return;
6427 }
6428
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006429 memcpy(txn->sessid, buf, len);
6430 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006431 }
6432
6433 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6434 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6435 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6436 return;
6437 }
6438
Cyril Bontéb21570a2009-11-29 20:04:48 +01006439 memcpy(sessid_temp, buf, len);
6440 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006441
6442 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6443 /* free previously allocated memory */
6444 pool_free2(apools.sessid, sessid_temp);
6445
6446 if (asession != NULL) {
6447 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6448 if (!(t->be->options2 & PR_O2_AS_REQL))
6449 asession->request_count++;
6450
6451 if (asession->serverid != NULL) {
6452 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006453
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006454 while (srv) {
6455 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006456 if ((srv->state & SRV_RUNNING) ||
6457 (t->be->options & PR_O_PERSIST) ||
6458 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006459 /* we found the server and it's usable */
6460 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006461 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006462 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006463 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006464
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006465 break;
6466 } else {
6467 txn->flags &= ~TX_CK_MASK;
6468 txn->flags |= TX_CK_DOWN;
6469 }
6470 }
6471 srv = srv->next;
6472 }
6473 }
6474 }
6475}
6476
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006477/* Find the end of a cookie value contained between <s> and <e>. It works the
6478 * same way as with headers above except that the semi-colon also ends a token.
6479 * See RFC2965 for more information. Note that it requires a valid header to
6480 * return a valid result.
6481 */
6482char *find_cookie_value_end(char *s, const char *e)
6483{
6484 int quoted, qdpair;
6485
6486 quoted = qdpair = 0;
6487 for (; s < e; s++) {
6488 if (qdpair) qdpair = 0;
6489 else if (quoted) {
6490 if (*s == '\\') qdpair = 1;
6491 else if (*s == '"') quoted = 0;
6492 }
6493 else if (*s == '"') quoted = 1;
6494 else if (*s == ',' || *s == ';') return s;
6495 }
6496 return s;
6497}
6498
6499/* Delete a value in a header between delimiters <from> and <next> in buffer
6500 * <buf>. The number of characters displaced is returned, and the pointer to
6501 * the first delimiter is updated if required. The function tries as much as
6502 * possible to respect the following principles :
6503 * - replace <from> delimiter by the <next> one unless <from> points to a
6504 * colon, in which case <next> is simply removed
6505 * - set exactly one space character after the new first delimiter, unless
6506 * there are not enough characters in the block being moved to do so.
6507 * - remove unneeded spaces before the previous delimiter and after the new
6508 * one.
6509 *
6510 * It is the caller's responsibility to ensure that :
6511 * - <from> points to a valid delimiter or the colon ;
6512 * - <next> points to a valid delimiter or the final CR/LF ;
6513 * - there are non-space chars before <from> ;
6514 * - there is a CR/LF at or after <next>.
6515 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006516int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006517{
6518 char *prev = *from;
6519
6520 if (*prev == ':') {
6521 /* We're removing the first value, preserve the colon and add a
6522 * space if possible.
6523 */
6524 if (!http_is_crlf[(unsigned char)*next])
6525 next++;
6526 prev++;
6527 if (prev < next)
6528 *prev++ = ' ';
6529
6530 while (http_is_spht[(unsigned char)*next])
6531 next++;
6532 } else {
6533 /* Remove useless spaces before the old delimiter. */
6534 while (http_is_spht[(unsigned char)*(prev-1)])
6535 prev--;
6536 *from = prev;
6537
6538 /* copy the delimiter and if possible a space if we're
6539 * not at the end of the line.
6540 */
6541 if (!http_is_crlf[(unsigned char)*next]) {
6542 *prev++ = *next++;
6543 if (prev + 1 < next)
6544 *prev++ = ' ';
6545 while (http_is_spht[(unsigned char)*next])
6546 next++;
6547 }
6548 }
6549 return buffer_replace2(buf, prev, next, NULL, 0);
6550}
6551
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006552/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006553 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 * desirable to call it only when needed. This code is quite complex because
6555 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6556 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006558void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006560 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006561 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006563 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6564 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006567 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006568 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006570 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006572 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006574 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006575 hdr_beg = hdr_next;
6576 hdr_end = hdr_beg + cur_hdr->len;
6577 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 /* We have one full header between hdr_beg and hdr_end, and the
6580 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006581 * "Cookie:" headers.
6582 */
6583
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006584 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006585 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006586 old_idx = cur_idx;
6587 continue;
6588 }
6589
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 del_from = NULL; /* nothing to be deleted */
6591 preserve_hdr = 0; /* assume we may kill the whole header */
6592
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593 /* Now look for cookies. Conforming to RFC2109, we have to support
6594 * attributes whose name begin with a '$', and associate them with
6595 * the right cookie, if we want to delete this cookie.
6596 * So there are 3 cases for each cookie read :
6597 * 1) it's a special attribute, beginning with a '$' : ignore it.
6598 * 2) it's a server id cookie that we *MAY* want to delete : save
6599 * some pointers on it (last semi-colon, beginning of cookie...)
6600 * 3) it's an application cookie : we *MAY* have to delete a previous
6601 * "special" cookie.
6602 * At the end of loop, if a "special" cookie remains, we may have to
6603 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006604 * *MUST* delete it.
6605 *
6606 * Note: RFC2965 is unclear about the processing of spaces around
6607 * the equal sign in the ATTR=VALUE form. A careful inspection of
6608 * the RFC explicitly allows spaces before it, and not within the
6609 * tokens (attrs or values). An inspection of RFC2109 allows that
6610 * too but section 10.1.3 lets one think that spaces may be allowed
6611 * after the equal sign too, resulting in some (rare) buggy
6612 * implementations trying to do that. So let's do what servers do.
6613 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6614 * allowed quoted strings in values, with any possible character
6615 * after a backslash, including control chars and delimitors, which
6616 * causes parsing to become ambiguous. Browsers also allow spaces
6617 * within values even without quotes.
6618 *
6619 * We have to keep multiple pointers in order to support cookie
6620 * removal at the beginning, middle or end of header without
6621 * corrupting the header. All of these headers are valid :
6622 *
6623 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6624 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6625 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6626 * | | | | | | | | |
6627 * | | | | | | | | hdr_end <--+
6628 * | | | | | | | +--> next
6629 * | | | | | | +----> val_end
6630 * | | | | | +-----------> val_beg
6631 * | | | | +--------------> equal
6632 * | | | +----------------> att_end
6633 * | | +---------------------> att_beg
6634 * | +--------------------------> prev
6635 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006636 */
6637
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006638 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6639 /* Iterate through all cookies on this line */
6640
6641 /* find att_beg */
6642 att_beg = prev + 1;
6643 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6644 att_beg++;
6645
6646 /* find att_end : this is the first character after the last non
6647 * space before the equal. It may be equal to hdr_end.
6648 */
6649 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006651 while (equal < hdr_end) {
6652 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006653 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006654 if (http_is_spht[(unsigned char)*equal++])
6655 continue;
6656 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657 }
6658
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006659 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6660 * is between <att_beg> and <equal>, both may be identical.
6661 */
6662
6663 /* look for end of cookie if there is an equal sign */
6664 if (equal < hdr_end && *equal == '=') {
6665 /* look for the beginning of the value */
6666 val_beg = equal + 1;
6667 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6668 val_beg++;
6669
6670 /* find the end of the value, respecting quotes */
6671 next = find_cookie_value_end(val_beg, hdr_end);
6672
6673 /* make val_end point to the first white space or delimitor after the value */
6674 val_end = next;
6675 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6676 val_end--;
6677 } else {
6678 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006679 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 /* We have nothing to do with attributes beginning with '$'. However,
6682 * they will automatically be removed if a header before them is removed,
6683 * since they're supposed to be linked together.
6684 */
6685 if (*att_beg == '$')
6686 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006688 /* Ignore cookies with no equal sign */
6689 if (equal == next) {
6690 /* This is not our cookie, so we must preserve it. But if we already
6691 * scheduled another cookie for removal, we cannot remove the
6692 * complete header, but we can remove the previous block itself.
6693 */
6694 preserve_hdr = 1;
6695 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006696 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697 val_end += delta;
6698 next += delta;
6699 hdr_end += delta;
6700 hdr_next += delta;
6701 cur_hdr->len += delta;
6702 http_msg_move_end(&txn->req, delta);
6703 prev = del_from;
6704 del_from = NULL;
6705 }
6706 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006708
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006709 /* if there are spaces around the equal sign, we need to
6710 * strip them otherwise we'll get trouble for cookie captures,
6711 * or even for rewrites. Since this happens extremely rarely,
6712 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006713 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6715 int stripped_before = 0;
6716 int stripped_after = 0;
6717
6718 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006719 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006720 equal += stripped_before;
6721 val_beg += stripped_before;
6722 }
6723
6724 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006725 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 val_beg += stripped_after;
6727 stripped_before += stripped_after;
6728 }
6729
6730 val_end += stripped_before;
6731 next += stripped_before;
6732 hdr_end += stripped_before;
6733 hdr_next += stripped_before;
6734 cur_hdr->len += stripped_before;
6735 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006736 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 /* First, let's see if we want to capture this cookie. We check
6740 * that we don't already have a client side cookie, because we
6741 * can only capture one. Also as an optimisation, we ignore
6742 * cookies shorter than the declared name.
6743 */
6744 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6745 (val_end - att_beg >= t->fe->capture_namelen) &&
6746 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6747 int log_len = val_end - att_beg;
6748
6749 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6750 Alert("HTTP logging : out of memory.\n");
6751 } else {
6752 if (log_len > t->fe->capture_len)
6753 log_len = t->fe->capture_len;
6754 memcpy(txn->cli_cookie, att_beg, log_len);
6755 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758
Willy Tarreaubca99692010-10-06 19:25:55 +02006759 /* Persistence cookies in passive, rewrite or insert mode have the
6760 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006762 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006763 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006764 * For cookies in prefix mode, the form is :
6765 *
6766 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 */
6768 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6769 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6770 struct server *srv = t->be->srv;
6771 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006773 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6774 * have the server ID between val_beg and delim, and the original cookie between
6775 * delim+1 and val_end. Otherwise, delim==val_end :
6776 *
6777 * Cookie: NAME=SRV; # in all but prefix modes
6778 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6779 * | || || | |+-> next
6780 * | || || | +--> val_end
6781 * | || || +---------> delim
6782 * | || |+------------> val_beg
6783 * | || +-------------> att_end = equal
6784 * | |+-----------------> att_beg
6785 * | +------------------> prev
6786 * +-------------------------> hdr_beg
6787 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788
Willy Tarreau67402132012-05-31 20:40:20 +02006789 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006790 for (delim = val_beg; delim < val_end; delim++)
6791 if (*delim == COOKIE_DELIM)
6792 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006793 } else {
6794 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006796 /* Now check if the cookie contains a date field, which would
6797 * appear after a vertical bar ('|') just after the server name
6798 * and before the delimiter.
6799 */
6800 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6801 if (vbar1) {
6802 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006803 * right is the last seen date. It is a base64 encoded
6804 * 30-bit value representing the UNIX date since the
6805 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006806 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006807 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006808 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006809 if (val_end - vbar1 >= 5) {
6810 val = b64tos30(vbar1);
6811 if (val > 0)
6812 txn->cookie_last_date = val << 2;
6813 }
6814 /* look for a second vertical bar */
6815 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6816 if (vbar1 && (val_end - vbar1 > 5)) {
6817 val = b64tos30(vbar1 + 1);
6818 if (val > 0)
6819 txn->cookie_first_date = val << 2;
6820 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006821 }
6822 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006823
Willy Tarreauf64d1412010-10-07 20:06:11 +02006824 /* if the cookie has an expiration date and the proxy wants to check
6825 * it, then we do that now. We first check if the cookie is too old,
6826 * then only if it has expired. We detect strict overflow because the
6827 * time resolution here is not great (4 seconds). Cookies with dates
6828 * in the future are ignored if their offset is beyond one day. This
6829 * allows an admin to fix timezone issues without expiring everyone
6830 * and at the same time avoids keeping unwanted side effects for too
6831 * long.
6832 */
6833 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006834 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6835 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006836 txn->flags &= ~TX_CK_MASK;
6837 txn->flags |= TX_CK_OLD;
6838 delim = val_beg; // let's pretend we have not found the cookie
6839 txn->cookie_first_date = 0;
6840 txn->cookie_last_date = 0;
6841 }
6842 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006843 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6844 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006845 txn->flags &= ~TX_CK_MASK;
6846 txn->flags |= TX_CK_EXPIRED;
6847 delim = val_beg; // let's pretend we have not found the cookie
6848 txn->cookie_first_date = 0;
6849 txn->cookie_last_date = 0;
6850 }
6851
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006852 /* Here, we'll look for the first running server which supports the cookie.
6853 * This allows to share a same cookie between several servers, for example
6854 * to dedicate backup servers to specific servers only.
6855 * However, to prevent clients from sticking to cookie-less backup server
6856 * when they have incidentely learned an empty cookie, we simply ignore
6857 * empty cookies and mark them as invalid.
6858 * The same behaviour is applied when persistence must be ignored.
6859 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006860 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006862
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006863 while (srv) {
6864 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6865 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6866 if ((srv->state & SRV_RUNNING) ||
6867 (t->be->options & PR_O_PERSIST) ||
6868 (t->flags & SN_FORCE_PRST)) {
6869 /* we found the server and we can use it */
6870 txn->flags &= ~TX_CK_MASK;
6871 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6872 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006873 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 break;
6875 } else {
6876 /* we found a server, but it's down,
6877 * mark it as such and go on in case
6878 * another one is available.
6879 */
6880 txn->flags &= ~TX_CK_MASK;
6881 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006883 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 srv = srv->next;
6885 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006886
Willy Tarreauf64d1412010-10-07 20:06:11 +02006887 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006888 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006890 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6891 txn->flags |= TX_CK_UNUSED;
6892 else
6893 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006894 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006895
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 /* depending on the cookie mode, we may have to either :
6897 * - delete the complete cookie if we're in insert+indirect mode, so that
6898 * the server never sees it ;
6899 * - remove the server id from the cookie value, and tag the cookie as an
6900 * application cookie so that it does not get accidentely removed later,
6901 * if we're in cookie prefix mode
6902 */
Willy Tarreau67402132012-05-31 20:40:20 +02006903 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006905
Willy Tarreau9b28e032012-10-12 23:49:43 +02006906 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 val_end += delta;
6908 next += delta;
6909 hdr_end += delta;
6910 hdr_next += delta;
6911 cur_hdr->len += delta;
6912 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 del_from = NULL;
6915 preserve_hdr = 1; /* we want to keep this cookie */
6916 }
6917 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006918 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 del_from = prev;
6920 }
6921 } else {
6922 /* This is not our cookie, so we must preserve it. But if we already
6923 * scheduled another cookie for removal, we cannot remove the
6924 * complete header, but we can remove the previous block itself.
6925 */
6926 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006929 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006930 if (att_beg >= del_from)
6931 att_beg += delta;
6932 if (att_end >= del_from)
6933 att_end += delta;
6934 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 val_end += delta;
6936 next += delta;
6937 hdr_end += delta;
6938 hdr_next += delta;
6939 cur_hdr->len += delta;
6940 http_msg_move_end(&txn->req, delta);
6941 prev = del_from;
6942 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 /* Look for the appsession cookie unless persistence must be ignored */
6947 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6948 int cmp_len, value_len;
6949 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006951 if (t->be->options2 & PR_O2_AS_PFX) {
6952 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6953 value_begin = att_beg + t->be->appsession_name_len;
6954 value_len = val_end - att_beg - t->be->appsession_name_len;
6955 } else {
6956 cmp_len = att_end - att_beg;
6957 value_begin = val_beg;
6958 value_len = val_end - val_beg;
6959 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006960
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006961 /* let's see if the cookie is our appcookie */
6962 if (cmp_len == t->be->appsession_name_len &&
6963 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6964 manage_client_side_appsession(t, value_begin, value_len);
6965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006966 }
6967
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006968 /* continue with next cookie on this header line */
6969 att_beg = next;
6970 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006972 /* There are no more cookies on this line.
6973 * We may still have one (or several) marked for deletion at the
6974 * end of the line. We must do this now in two ways :
6975 * - if some cookies must be preserved, we only delete from the
6976 * mark to the end of line ;
6977 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006980 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006982 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984 cur_hdr->len += delta;
6985 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006986 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006987
6988 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006989 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6990 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006992 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006995 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996 }
6997
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006998 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007000 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007001}
7002
7003
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004/* Iterate the same filter through all response headers contained in <rtr>.
7005 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7006 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007007int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008{
7009 char term;
7010 char *cur_ptr, *cur_end, *cur_next;
7011 int cur_idx, old_idx, last_hdr;
7012 struct http_txn *txn = &t->txn;
7013 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007014 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015
7016 last_hdr = 0;
7017
Willy Tarreau9b28e032012-10-12 23:49:43 +02007018 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019 old_idx = 0;
7020
7021 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007024 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 (exp->action == ACT_ALLOW ||
7026 exp->action == ACT_DENY))
7027 return 0;
7028
7029 cur_idx = txn->hdr_idx.v[old_idx].next;
7030 if (!cur_idx)
7031 break;
7032
7033 cur_hdr = &txn->hdr_idx.v[cur_idx];
7034 cur_ptr = cur_next;
7035 cur_end = cur_ptr + cur_hdr->len;
7036 cur_next = cur_end + cur_hdr->cr + 1;
7037
7038 /* Now we have one header between cur_ptr and cur_end,
7039 * and the next header starts at cur_next.
7040 */
7041
7042 /* The annoying part is that pattern matching needs
7043 * that we modify the contents to null-terminate all
7044 * strings before testing them.
7045 */
7046
7047 term = *cur_end;
7048 *cur_end = '\0';
7049
7050 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7051 switch (exp->action) {
7052 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007053 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 last_hdr = 1;
7055 break;
7056
7057 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007058 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 last_hdr = 1;
7060 break;
7061
7062 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007063 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7064 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 /* FIXME: if the user adds a newline in the replacement, the
7066 * index will not be recalculated for now, and the new line
7067 * will not be counted as a new header.
7068 */
7069
7070 cur_end += delta;
7071 cur_next += delta;
7072 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007073 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007074 break;
7075
7076 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007077 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078 cur_next += delta;
7079
Willy Tarreaufa355d42009-11-29 18:12:29 +01007080 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007081 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7082 txn->hdr_idx.used--;
7083 cur_hdr->len = 0;
7084 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007085 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086 break;
7087
7088 }
7089 }
7090 if (cur_end)
7091 *cur_end = term; /* restore the string terminator */
7092
7093 /* keep the link from this header to next one in case of later
7094 * removal of next header.
7095 */
7096 old_idx = cur_idx;
7097 }
7098 return 0;
7099}
7100
7101
7102/* Apply the filter to the status line in the response buffer <rtr>.
7103 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7104 * or -1 if a replacement resulted in an invalid status line.
7105 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007106int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107{
7108 char term;
7109 char *cur_ptr, *cur_end;
7110 int done;
7111 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007112 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113
7114
Willy Tarreau3d300592007-03-18 18:34:41 +01007115 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007117 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118 (exp->action == ACT_ALLOW ||
7119 exp->action == ACT_DENY))
7120 return 0;
7121 else if (exp->action == ACT_REMOVE)
7122 return 0;
7123
7124 done = 0;
7125
Willy Tarreau9b28e032012-10-12 23:49:43 +02007126 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007127 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128
7129 /* Now we have the status line between cur_ptr and cur_end */
7130
7131 /* The annoying part is that pattern matching needs
7132 * that we modify the contents to null-terminate all
7133 * strings before testing them.
7134 */
7135
7136 term = *cur_end;
7137 *cur_end = '\0';
7138
7139 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7140 switch (exp->action) {
7141 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007142 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 done = 1;
7144 break;
7145
7146 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007147 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148 done = 1;
7149 break;
7150
7151 case ACT_REPLACE:
7152 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007153 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7154 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 /* FIXME: if the user adds a newline in the replacement, the
7156 * index will not be recalculated for now, and the new line
7157 * will not be counted as a new header.
7158 */
7159
Willy Tarreaufa355d42009-11-29 18:12:29 +01007160 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007162 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007163 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 cur_ptr, cur_end + 1,
7165 NULL, NULL);
7166 if (unlikely(!cur_end))
7167 return -1;
7168
7169 /* we have a full respnse and we know that we have either a CR
7170 * or an LF at <ptr>.
7171 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007172 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007173 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 /* there is no point trying this regex on headers */
7175 return 1;
7176 }
7177 }
7178 *cur_end = term; /* restore the string terminator */
7179 return done;
7180}
7181
7182
7183
7184/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007185 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7187 * unparsable response.
7188 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007189int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007191 struct http_txn *txn = &s->txn;
7192 struct hdr_exp *exp;
7193
7194 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 int ret;
7196
7197 /*
7198 * The interleaving of transformations and verdicts
7199 * makes it difficult to decide to continue or stop
7200 * the evaluation.
7201 */
7202
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007203 if (txn->flags & TX_SVDENY)
7204 break;
7205
Willy Tarreau3d300592007-03-18 18:34:41 +01007206 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7208 exp->action == ACT_PASS)) {
7209 exp = exp->next;
7210 continue;
7211 }
7212
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007213 /* if this filter had a condition, evaluate it now and skip to
7214 * next filter if the condition does not match.
7215 */
7216 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007217 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007218 ret = acl_pass(ret);
7219 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7220 ret = !ret;
7221 if (!ret)
7222 continue;
7223 }
7224
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007226 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 if (unlikely(ret < 0))
7228 return -1;
7229
7230 if (likely(ret == 0)) {
7231 /* The filter did not match the response, it can be
7232 * iterated through all headers.
7233 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007234 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 }
7237 return 0;
7238}
7239
7240
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007242 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007243 * desirable to call it only when needed. This function is also used when we
7244 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007246void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247{
7248 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007249 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007250 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007252 char *hdr_beg, *hdr_end, *hdr_next;
7253 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 /* Iterate through the headers.
7256 * we start with the start line.
7257 */
7258 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007259 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260
7261 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7262 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007263 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
7265 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007266 hdr_beg = hdr_next;
7267 hdr_end = hdr_beg + cur_hdr->len;
7268 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269
Willy Tarreau24581ba2010-08-31 22:39:35 +02007270 /* We have one full header between hdr_beg and hdr_end, and the
7271 * next header starts at hdr_next. We're only interested in
7272 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 */
7274
Willy Tarreau24581ba2010-08-31 22:39:35 +02007275 is_cookie2 = 0;
7276 prev = hdr_beg + 10;
7277 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007278 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007279 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7280 if (!val) {
7281 old_idx = cur_idx;
7282 continue;
7283 }
7284 is_cookie2 = 1;
7285 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286 }
7287
Willy Tarreau24581ba2010-08-31 22:39:35 +02007288 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7289 * <prev> points to the colon.
7290 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007291 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292
Willy Tarreau24581ba2010-08-31 22:39:35 +02007293 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7294 * check-cache is enabled) and we are not interested in checking
7295 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007296 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007297 if (t->be->cookie_name == NULL &&
7298 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007299 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 return;
7301
Willy Tarreau24581ba2010-08-31 22:39:35 +02007302 /* OK so now we know we have to process this response cookie.
7303 * The format of the Set-Cookie header is slightly different
7304 * from the format of the Cookie header in that it does not
7305 * support the comma as a cookie delimiter (thus the header
7306 * cannot be folded) because the Expires attribute described in
7307 * the original Netscape's spec may contain an unquoted date
7308 * with a comma inside. We have to live with this because
7309 * many browsers don't support Max-Age and some browsers don't
7310 * support quoted strings. However the Set-Cookie2 header is
7311 * clean.
7312 *
7313 * We have to keep multiple pointers in order to support cookie
7314 * removal at the beginning, middle or end of header without
7315 * corrupting the header (in case of set-cookie2). A special
7316 * pointer, <scav> points to the beginning of the set-cookie-av
7317 * fields after the first semi-colon. The <next> pointer points
7318 * either to the end of line (set-cookie) or next unquoted comma
7319 * (set-cookie2). All of these headers are valid :
7320 *
7321 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7322 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7323 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7324 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7325 * | | | | | | | | | |
7326 * | | | | | | | | +-> next hdr_end <--+
7327 * | | | | | | | +------------> scav
7328 * | | | | | | +--------------> val_end
7329 * | | | | | +--------------------> val_beg
7330 * | | | | +----------------------> equal
7331 * | | | +------------------------> att_end
7332 * | | +----------------------------> att_beg
7333 * | +------------------------------> prev
7334 * +-----------------------------------------> hdr_beg
7335 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336
Willy Tarreau24581ba2010-08-31 22:39:35 +02007337 for (; prev < hdr_end; prev = next) {
7338 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339
Willy Tarreau24581ba2010-08-31 22:39:35 +02007340 /* find att_beg */
7341 att_beg = prev + 1;
7342 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7343 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
Willy Tarreau24581ba2010-08-31 22:39:35 +02007345 /* find att_end : this is the first character after the last non
7346 * space before the equal. It may be equal to hdr_end.
7347 */
7348 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349
Willy Tarreau24581ba2010-08-31 22:39:35 +02007350 while (equal < hdr_end) {
7351 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7352 break;
7353 if (http_is_spht[(unsigned char)*equal++])
7354 continue;
7355 att_end = equal;
7356 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357
Willy Tarreau24581ba2010-08-31 22:39:35 +02007358 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7359 * is between <att_beg> and <equal>, both may be identical.
7360 */
7361
7362 /* look for end of cookie if there is an equal sign */
7363 if (equal < hdr_end && *equal == '=') {
7364 /* look for the beginning of the value */
7365 val_beg = equal + 1;
7366 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7367 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368
Willy Tarreau24581ba2010-08-31 22:39:35 +02007369 /* find the end of the value, respecting quotes */
7370 next = find_cookie_value_end(val_beg, hdr_end);
7371
7372 /* make val_end point to the first white space or delimitor after the value */
7373 val_end = next;
7374 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7375 val_end--;
7376 } else {
7377 /* <equal> points to next comma, semi-colon or EOL */
7378 val_beg = val_end = next = equal;
7379 }
7380
7381 if (next < hdr_end) {
7382 /* Set-Cookie2 supports multiple cookies, and <next> points to
7383 * a colon or semi-colon before the end. So skip all attr-value
7384 * pairs and look for the next comma. For Set-Cookie, since
7385 * commas are permitted in values, skip to the end.
7386 */
7387 if (is_cookie2)
7388 next = find_hdr_value_end(next, hdr_end);
7389 else
7390 next = hdr_end;
7391 }
7392
7393 /* Now everything is as on the diagram above */
7394
7395 /* Ignore cookies with no equal sign */
7396 if (equal == val_end)
7397 continue;
7398
7399 /* If there are spaces around the equal sign, we need to
7400 * strip them otherwise we'll get trouble for cookie captures,
7401 * or even for rewrites. Since this happens extremely rarely,
7402 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7405 int stripped_before = 0;
7406 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407
Willy Tarreau24581ba2010-08-31 22:39:35 +02007408 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007409 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007410 equal += stripped_before;
7411 val_beg += stripped_before;
7412 }
7413
7414 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007415 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007416 val_beg += stripped_after;
7417 stripped_before += stripped_after;
7418 }
7419
7420 val_end += stripped_before;
7421 next += stripped_before;
7422 hdr_end += stripped_before;
7423 hdr_next += stripped_before;
7424 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007425 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007426 }
7427
7428 /* First, let's see if we want to capture this cookie. We check
7429 * that we don't already have a server side cookie, because we
7430 * can only capture one. Also as an optimisation, we ignore
7431 * cookies shorter than the declared name.
7432 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007433 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007434 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007435 (val_end - att_beg >= t->fe->capture_namelen) &&
7436 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7437 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007438 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439 Alert("HTTP logging : out of memory.\n");
7440 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007441 else {
7442 if (log_len > t->fe->capture_len)
7443 log_len = t->fe->capture_len;
7444 memcpy(txn->srv_cookie, att_beg, log_len);
7445 txn->srv_cookie[log_len] = 0;
7446 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 }
7448
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007449 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007451 if (!(t->flags & SN_IGNORE_PRST) &&
7452 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7453 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007454 /* assume passive cookie by default */
7455 txn->flags &= ~TX_SCK_MASK;
7456 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007457
7458 /* If the cookie is in insert mode on a known server, we'll delete
7459 * this occurrence because we'll insert another one later.
7460 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007461 * a direct access.
7462 */
Willy Tarreau67402132012-05-31 20:40:20 +02007463 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007464 /* The "preserve" flag was set, we don't want to touch the
7465 * server's cookie.
7466 */
7467 }
Willy Tarreau67402132012-05-31 20:40:20 +02007468 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7469 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 /* this cookie must be deleted */
7471 if (*prev == ':' && next == hdr_end) {
7472 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007473 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7475 txn->hdr_idx.used--;
7476 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007477 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007478 hdr_next += delta;
7479 http_msg_move_end(&txn->rsp, delta);
7480 /* note: while both invalid now, <next> and <hdr_end>
7481 * are still equal, so the for() will stop as expected.
7482 */
7483 } else {
7484 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007485 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007486 next = prev;
7487 hdr_end += delta;
7488 hdr_next += delta;
7489 cur_hdr->len += delta;
7490 http_msg_move_end(&txn->rsp, delta);
7491 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007492 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007493 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007494 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495 }
Willy Tarreau67402132012-05-31 20:40:20 +02007496 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007497 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498 * with this server since we know it.
7499 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007500 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007501 next += delta;
7502 hdr_end += delta;
7503 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007505 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506
Willy Tarreauf1348312010-10-07 15:54:11 +02007507 txn->flags &= ~TX_SCK_MASK;
7508 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007510 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007512 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007514 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 next += delta;
7516 hdr_end += delta;
7517 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007519 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau827aee92011-03-10 16:55:02 +01007521 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007522 txn->flags &= ~TX_SCK_MASK;
7523 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524 }
7525 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007526 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7527 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007528 int cmp_len, value_len;
7529 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530
Cyril Bontéb21570a2009-11-29 20:04:48 +01007531 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007532 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7533 value_begin = att_beg + t->be->appsession_name_len;
7534 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007535 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 cmp_len = att_end - att_beg;
7537 value_begin = val_beg;
7538 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007539 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007540
Cyril Bonté17530c32010-04-06 21:11:10 +02007541 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007542 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7543 /* free a possibly previously allocated memory */
7544 pool_free2(apools.sessid, txn->sessid);
7545
Cyril Bontéb21570a2009-11-29 20:04:48 +01007546 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007547 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007548 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7549 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7550 return;
7551 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007552 memcpy(txn->sessid, value_begin, value_len);
7553 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007554 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007555 }
7556 /* that's done for this cookie, check the next one on the same
7557 * line when next != hdr_end (only if is_cookie2).
7558 */
7559 }
7560 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007561 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007562 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007563
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007564 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007565 appsess *asession = NULL;
7566 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007567 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007568 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007569 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007570 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7571 Alert("Not enough Memory process_srv():asession:calloc().\n");
7572 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7573 return;
7574 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007575 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7576
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007577 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7578 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7579 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007580 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007581 return;
7582 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007583 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007584 asession->sessid[t->be->appsession_len] = 0;
7585
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007586 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007587 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007588 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007589 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007590 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007591 return;
7592 }
7593 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007594 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007595
7596 asession->request_count = 0;
7597 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7598 }
7599
7600 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7601 asession->request_count++;
7602 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603}
7604
7605
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606/*
7607 * Check if response is cacheable or not. Updates t->flags.
7608 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007609void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610{
7611 struct http_txn *txn = &t->txn;
7612 char *p1, *p2;
7613
7614 char *cur_ptr, *cur_end, *cur_next;
7615 int cur_idx;
7616
Willy Tarreau5df51872007-11-25 16:20:08 +01007617 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618 return;
7619
7620 /* Iterate through the headers.
7621 * we start with the start line.
7622 */
7623 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007624 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625
7626 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7627 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007628 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629
7630 cur_hdr = &txn->hdr_idx.v[cur_idx];
7631 cur_ptr = cur_next;
7632 cur_end = cur_ptr + cur_hdr->len;
7633 cur_next = cur_end + cur_hdr->cr + 1;
7634
7635 /* We have one full header between cur_ptr and cur_end, and the
7636 * next header starts at cur_next. We're only interested in
7637 * "Cookie:" headers.
7638 */
7639
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007640 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7641 if (val) {
7642 if ((cur_end - (cur_ptr + val) >= 8) &&
7643 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7644 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7645 return;
7646 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 }
7648
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007649 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7650 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007651 continue;
7652
7653 /* OK, right now we know we have a cache-control header at cur_ptr */
7654
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007655 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656
7657 if (p1 >= cur_end) /* no more info */
7658 continue;
7659
7660 /* p1 is at the beginning of the value */
7661 p2 = p1;
7662
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007663 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 p2++;
7665
7666 /* we have a complete value between p1 and p2 */
7667 if (p2 < cur_end && *p2 == '=') {
7668 /* we have something of the form no-cache="set-cookie" */
7669 if ((cur_end - p1 >= 21) &&
7670 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7671 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007672 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007673 continue;
7674 }
7675
7676 /* OK, so we know that either p2 points to the end of string or to a comma */
7677 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7678 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7679 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7680 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007681 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 return;
7683 }
7684
7685 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007686 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687 continue;
7688 }
7689 }
7690}
7691
7692
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693/*
7694 * Try to retrieve a known appsession in the URI, then the associated server.
7695 * If the server is found, it's assigned to the session.
7696 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007697void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007698{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007699 char *end_params, *first_param, *cur_param, *next_param;
7700 char separator;
7701 int value_len;
7702
7703 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007705 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007706 (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 +01007707 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709
Cyril Bontéb21570a2009-11-29 20:04:48 +01007710 first_param = NULL;
7711 switch (mode) {
7712 case PR_O2_AS_M_PP:
7713 first_param = memchr(begin, ';', len);
7714 break;
7715 case PR_O2_AS_M_QS:
7716 first_param = memchr(begin, '?', len);
7717 break;
7718 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007719
Cyril Bontéb21570a2009-11-29 20:04:48 +01007720 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723
Cyril Bontéb21570a2009-11-29 20:04:48 +01007724 switch (mode) {
7725 case PR_O2_AS_M_PP:
7726 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7727 end_params = (char *) begin + len;
7728 }
7729 separator = ';';
7730 break;
7731 case PR_O2_AS_M_QS:
7732 end_params = (char *) begin + len;
7733 separator = '&';
7734 break;
7735 default:
7736 /* unknown mode, shouldn't happen */
7737 return;
7738 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007739
Cyril Bontéb21570a2009-11-29 20:04:48 +01007740 cur_param = next_param = end_params;
7741 while (cur_param > first_param) {
7742 cur_param--;
7743 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7744 /* let's see if this is the appsession parameter */
7745 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7746 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7747 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7748 /* Cool... it's the right one */
7749 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7750 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7751 if (value_len > 0) {
7752 manage_client_side_appsession(t, cur_param, value_len);
7753 }
7754 break;
7755 }
7756 next_param = cur_param;
7757 }
7758 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007759#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007760 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007761 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007763}
7764
Willy Tarreaub2513902006-12-17 14:52:38 +01007765/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007766 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007767 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007768 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007769 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007770 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007771 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007772 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007773 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007774int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007775{
7776 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007777 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007778 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007779 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007780
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007781 if (!uri_auth)
7782 return 0;
7783
Cyril Bonté70be45d2010-10-12 00:14:35 +02007784 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007785 return 0;
7786
Willy Tarreau295a8372011-03-10 11:25:07 +01007787 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007788 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007789 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007791 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007792 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007793 return 0;
7794
Willy Tarreau3a215be2012-03-09 21:39:51 +01007795 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007796 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007797 return 0;
7798
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007799 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007800 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007801 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007802 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007803 break;
7804 }
7805 h++;
7806 }
7807
7808 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007809 h = uri + uri_auth->uri_len;
7810 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007811 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007812 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007813 break;
7814 }
7815 h++;
7816 }
7817 }
7818
Willy Tarreau3a215be2012-03-09 21:39:51 +01007819 h = uri + uri_auth->uri_len;
7820 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007821 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007822 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007823 break;
7824 }
7825 h++;
7826 }
7827
Willy Tarreau3a215be2012-03-09 21:39:51 +01007828 h = uri + uri_auth->uri_len;
7829 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007830 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007831 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007832 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007833 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007834 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7835 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7836 si->applet.ctx.stats.st_code = i;
7837 break;
7838 }
7839 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007840 break;
7841 }
7842 h++;
7843 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007844
7845 si->applet.ctx.stats.scope_str = 0;
7846 si->applet.ctx.stats.scope_len = 0;
7847 h = uri + uri_auth->uri_len;
7848 while (h <= uri + msg->sl.rq.u_l - 8) {
7849 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
7850 int itx = 0;
7851 const char *h2;
7852 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
7853 const char *err;
7854
7855 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
7856 h2 = h;
7857 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
7858 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
7859 itx++;
7860 h++;
7861 }
7862
7863 if (itx > STAT_SCOPE_TXT_MAXLEN)
7864 itx = STAT_SCOPE_TXT_MAXLEN;
7865 si->applet.ctx.stats.scope_len = itx;
7866
7867 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
7868 memcpy(scope_txt, h2, itx);
7869 scope_txt[itx] = '\0';
7870 err = invalid_char(scope_txt);
7871 if (err) {
7872 /* bad char in search text => clear scope */
7873 si->applet.ctx.stats.scope_str = 0;
7874 si->applet.ctx.stats.scope_len = 0;
7875 }
7876 break;
7877 }
7878 h++;
7879 }
7880
7881
Willy Tarreaub2513902006-12-17 14:52:38 +01007882 return 1;
7883}
7884
Willy Tarreau4076a152009-04-02 15:18:36 +02007885/*
7886 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007887 * By default it tries to report the error position as msg->err_pos. However if
7888 * this one is not set, it will then report msg->next, which is the last known
7889 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007890 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007891 */
7892void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007893 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007894 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007895{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007896 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007897 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007898
Willy Tarreau9b28e032012-10-12 23:49:43 +02007899 es->len = MIN(chn->buf->i, sizeof(es->buf));
7900 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007901 len1 = MIN(len1, es->len);
7902 len2 = es->len - len1; /* remaining data if buffer wraps */
7903
Willy Tarreau9b28e032012-10-12 23:49:43 +02007904 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007905 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007906 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007907
Willy Tarreau4076a152009-04-02 15:18:36 +02007908 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007909 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007910 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007911 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007912
Willy Tarreau4076a152009-04-02 15:18:36 +02007913 es->when = date; // user-visible date
7914 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007915 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007916 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007917 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007918 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007919 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007920 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007921 es->s_flags = s->flags;
7922 es->t_flags = s->txn.flags;
7923 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007924 es->b_out = chn->buf->o;
7925 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007926 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007927 es->m_clen = msg->chunk_len;
7928 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007929}
Willy Tarreaub2513902006-12-17 14:52:38 +01007930
Willy Tarreau294c4732011-12-16 21:35:50 +01007931/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7932 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7933 * performed over the whole headers. Otherwise it must contain a valid header
7934 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7935 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7936 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7937 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7938 * -1.
7939 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007940 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007941unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007942 struct hdr_idx *idx, int occ,
7943 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007944{
Willy Tarreau294c4732011-12-16 21:35:50 +01007945 struct hdr_ctx local_ctx;
7946 char *ptr_hist[MAX_HDR_HISTORY];
7947 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007948 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007949 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007950
Willy Tarreau294c4732011-12-16 21:35:50 +01007951 if (!ctx) {
7952 local_ctx.idx = 0;
7953 ctx = &local_ctx;
7954 }
7955
Willy Tarreaubce70882009-09-07 11:51:47 +02007956 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007957 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007958 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007959 occ--;
7960 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007961 *vptr = ctx->line + ctx->val;
7962 *vlen = ctx->vlen;
7963 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007964 }
7965 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007966 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007967 }
7968
7969 /* negative occurrence, we scan all the list then walk back */
7970 if (-occ > MAX_HDR_HISTORY)
7971 return 0;
7972
Willy Tarreau294c4732011-12-16 21:35:50 +01007973 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007974 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007975 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7976 len_hist[hist_ptr] = ctx->vlen;
7977 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007978 hist_ptr = 0;
7979 found++;
7980 }
7981 if (-occ > found)
7982 return 0;
7983 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7984 * find occurrence -occ, so we have to check [hist_ptr+occ].
7985 */
7986 hist_ptr += occ;
7987 if (hist_ptr >= MAX_HDR_HISTORY)
7988 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007989 *vptr = ptr_hist[hist_ptr];
7990 *vlen = len_hist[hist_ptr];
7991 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007992}
7993
Willy Tarreaubaaee002006-06-26 02:48:02 +02007994/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007995 * Print a debug line with a header. Always stop at the first CR or LF char,
7996 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7997 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007998 */
7999void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8000{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008001 int max;
8002 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008003 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8004 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008005
8006 for (max = 0; start + max < end; max++)
8007 if (start[max] == '\r' || start[max] == '\n')
8008 break;
8009
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008010 UBOUND(max, trash.size - trash.len - 3);
8011 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8012 trash.str[trash.len++] = '\n';
8013 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008014}
8015
Willy Tarreau0937bc42009-12-22 15:03:09 +01008016/*
8017 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8018 * the required fields are properly allocated and that we only need to (re)init
8019 * them. This should be used before processing any new request.
8020 */
8021void http_init_txn(struct session *s)
8022{
8023 struct http_txn *txn = &s->txn;
8024 struct proxy *fe = s->fe;
8025
8026 txn->flags = 0;
8027 txn->status = -1;
8028
William Lallemand5f232402012-04-05 18:02:55 +02008029 global.req_count++;
8030
Willy Tarreauf64d1412010-10-07 20:06:11 +02008031 txn->cookie_first_date = 0;
8032 txn->cookie_last_date = 0;
8033
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008034 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008035 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008036 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008037 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008038 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008039 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008040 txn->req.chunk_len = 0LL;
8041 txn->req.body_len = 0LL;
8042 txn->rsp.chunk_len = 0LL;
8043 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008044 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8045 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008046 txn->req.chn = s->req;
8047 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008048
8049 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008050
8051 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8052 if (fe->options2 & PR_O2_REQBUG_OK)
8053 txn->req.err_pos = -1; /* let buggy requests pass */
8054
Willy Tarreau46023632010-01-07 22:51:47 +01008055 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008056 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8057
Willy Tarreau46023632010-01-07 22:51:47 +01008058 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008059 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8060
8061 if (txn->hdr_idx.v)
8062 hdr_idx_init(&txn->hdr_idx);
8063}
8064
8065/* to be used at the end of a transaction */
8066void http_end_txn(struct session *s)
8067{
8068 struct http_txn *txn = &s->txn;
8069
8070 /* these ones will have been dynamically allocated */
8071 pool_free2(pool2_requri, txn->uri);
8072 pool_free2(pool2_capture, txn->cli_cookie);
8073 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008074 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008075 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008076
William Lallemanda73203e2012-03-12 12:48:57 +01008077 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008078 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008079 txn->uri = NULL;
8080 txn->srv_cookie = NULL;
8081 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008082
8083 if (txn->req.cap) {
8084 struct cap_hdr *h;
8085 for (h = s->fe->req_cap; h; h = h->next)
8086 pool_free2(h->pool, txn->req.cap[h->index]);
8087 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8088 }
8089
8090 if (txn->rsp.cap) {
8091 struct cap_hdr *h;
8092 for (h = s->fe->rsp_cap; h; h = h->next)
8093 pool_free2(h->pool, txn->rsp.cap[h->index]);
8094 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8095 }
8096
Willy Tarreau0937bc42009-12-22 15:03:09 +01008097}
8098
8099/* to be used at the end of a transaction to prepare a new one */
8100void http_reset_txn(struct session *s)
8101{
8102 http_end_txn(s);
8103 http_init_txn(s);
8104
8105 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008106 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008107 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008108 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008109 /* re-init store persistence */
8110 s->store_count = 0;
8111
Willy Tarreau0937bc42009-12-22 15:03:09 +01008112 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008113
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008114 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008115
Willy Tarreau739cfba2010-01-25 23:11:14 +01008116 /* We must trim any excess data from the response buffer, because we
8117 * may have blocked an invalid response from a server that we don't
8118 * want to accidentely forward once we disable the analysers, nor do
8119 * we want those data to come along with next response. A typical
8120 * example of such data would be from a buggy server responding to
8121 * a HEAD with some data, or sending more than the advertised
8122 * content-length.
8123 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008124 if (unlikely(s->rep->buf->i))
8125 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008126
Willy Tarreau0937bc42009-12-22 15:03:09 +01008127 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008128 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008129
Willy Tarreaud04e8582010-05-31 12:31:35 +02008130 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008131 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008132
8133 s->req->rex = TICK_ETERNITY;
8134 s->req->wex = TICK_ETERNITY;
8135 s->req->analyse_exp = TICK_ETERNITY;
8136 s->rep->rex = TICK_ETERNITY;
8137 s->rep->wex = TICK_ETERNITY;
8138 s->rep->analyse_exp = TICK_ETERNITY;
8139}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008140
Willy Tarreauff011f22011-01-06 17:51:27 +01008141void free_http_req_rules(struct list *r) {
8142 struct http_req_rule *tr, *pr;
8143
8144 list_for_each_entry_safe(pr, tr, r, list) {
8145 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008146 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008147 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008148
8149 free(pr);
8150 }
8151}
8152
8153struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8154{
8155 struct http_req_rule *rule;
8156 int cur_arg;
8157
8158 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8159 if (!rule) {
8160 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008161 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008162 }
8163
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008164 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008165 rule->action = HTTP_REQ_ACT_ALLOW;
8166 cur_arg = 1;
8167 } else if (!strcmp(args[0], "deny")) {
8168 rule->action = HTTP_REQ_ACT_DENY;
8169 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008170 } else if (!strcmp(args[0], "tarpit")) {
8171 rule->action = HTTP_REQ_ACT_TARPIT;
8172 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008173 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008174 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008175 cur_arg = 1;
8176
8177 while(*args[cur_arg]) {
8178 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008179 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008180 cur_arg+=2;
8181 continue;
8182 } else
8183 break;
8184 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008185 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8186 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8187 cur_arg = 1;
8188
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008189 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8190 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008191 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8192 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008193 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008194 }
8195
8196 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8197 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8198 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008199
8200 proxy->conf.args.ctx = ARGC_HDR;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008201 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8202 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008203 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008204 } else if (strcmp(args[0], "redirect") == 0) {
8205 struct redirect_rule *redir;
8206 char *errmsg;
8207
8208 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8209 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8210 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8211 goto out_err;
8212 }
8213
8214 /* this redirect rule might already contain a parsed condition which
8215 * we'll pass to the http-request rule.
8216 */
8217 rule->action = HTTP_REQ_ACT_REDIR;
8218 rule->arg.redir = redir;
8219 rule->cond = redir->cond;
8220 redir->cond = NULL;
8221 cur_arg = 2;
8222 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008223 } else {
Baptiste Assmann116eefe2013-01-05 16:02:07 +01008224 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008225 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008226 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008227 }
8228
8229 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8230 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008231 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008232
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008233 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8234 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8235 file, linenum, args[0], errmsg);
8236 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008237 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008238 }
8239 rule->cond = cond;
8240 }
8241 else if (*args[cur_arg]) {
8242 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8243 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8244 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008245 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008246 }
8247
8248 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008249 out_err:
8250 free(rule);
8251 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008252}
8253
Willy Tarreau4baae242012-12-27 12:00:31 +01008254/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8255 * with <err> filled with the error message.
8256 */
8257struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8258 const char **args, char **errmsg)
8259{
8260 struct redirect_rule *rule;
8261 int cur_arg;
8262 int type = REDIRECT_TYPE_NONE;
8263 int code = 302;
8264 const char *destination = NULL;
8265 const char *cookie = NULL;
8266 int cookie_set = 0;
8267 unsigned int flags = REDIRECT_FLAG_NONE;
8268 struct acl_cond *cond = NULL;
8269
8270 cur_arg = 0;
8271 while (*(args[cur_arg])) {
8272 if (strcmp(args[cur_arg], "location") == 0) {
8273 if (!*args[cur_arg + 1])
8274 goto missing_arg;
8275
8276 type = REDIRECT_TYPE_LOCATION;
8277 cur_arg++;
8278 destination = args[cur_arg];
8279 }
8280 else if (strcmp(args[cur_arg], "prefix") == 0) {
8281 if (!*args[cur_arg + 1])
8282 goto missing_arg;
8283
8284 type = REDIRECT_TYPE_PREFIX;
8285 cur_arg++;
8286 destination = args[cur_arg];
8287 }
8288 else if (strcmp(args[cur_arg], "scheme") == 0) {
8289 if (!*args[cur_arg + 1])
8290 goto missing_arg;
8291
8292 type = REDIRECT_TYPE_SCHEME;
8293 cur_arg++;
8294 destination = args[cur_arg];
8295 }
8296 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8297 if (!*args[cur_arg + 1])
8298 goto missing_arg;
8299
8300 cur_arg++;
8301 cookie = args[cur_arg];
8302 cookie_set = 1;
8303 }
8304 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8305 if (!*args[cur_arg + 1])
8306 goto missing_arg;
8307
8308 cur_arg++;
8309 cookie = args[cur_arg];
8310 cookie_set = 0;
8311 }
8312 else if (strcmp(args[cur_arg], "code") == 0) {
8313 if (!*args[cur_arg + 1])
8314 goto missing_arg;
8315
8316 cur_arg++;
8317 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008318 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008319 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008320 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008321 args[cur_arg - 1], args[cur_arg]);
8322 return NULL;
8323 }
8324 }
8325 else if (!strcmp(args[cur_arg],"drop-query")) {
8326 flags |= REDIRECT_FLAG_DROP_QS;
8327 }
8328 else if (!strcmp(args[cur_arg],"append-slash")) {
8329 flags |= REDIRECT_FLAG_APPEND_SLASH;
8330 }
8331 else if (strcmp(args[cur_arg], "if") == 0 ||
8332 strcmp(args[cur_arg], "unless") == 0) {
8333 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8334 if (!cond) {
8335 memprintf(errmsg, "error in condition: %s", *errmsg);
8336 return NULL;
8337 }
8338 break;
8339 }
8340 else {
8341 memprintf(errmsg,
8342 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8343 args[cur_arg]);
8344 return NULL;
8345 }
8346 cur_arg++;
8347 }
8348
8349 if (type == REDIRECT_TYPE_NONE) {
8350 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8351 return NULL;
8352 }
8353
8354 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8355 rule->cond = cond;
8356 rule->rdr_str = strdup(destination);
8357 rule->rdr_len = strlen(destination);
8358 if (cookie) {
8359 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8360 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8361 */
8362 rule->cookie_len = strlen(cookie);
8363 if (cookie_set) {
8364 rule->cookie_str = malloc(rule->cookie_len + 10);
8365 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8366 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8367 rule->cookie_len += 9;
8368 } else {
8369 rule->cookie_str = malloc(rule->cookie_len + 21);
8370 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8371 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8372 rule->cookie_len += 20;
8373 }
8374 }
8375 rule->type = type;
8376 rule->code = code;
8377 rule->flags = flags;
8378 LIST_INIT(&rule->list);
8379 return rule;
8380
8381 missing_arg:
8382 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8383 return NULL;
8384}
8385
Willy Tarreau8797c062007-05-07 00:55:35 +02008386/************************************************************************/
8387/* The code below is dedicated to ACL parsing and matching */
8388/************************************************************************/
8389
8390
Willy Tarreau14174bc2012-04-16 14:34:04 +02008391/* This function ensures that the prerequisites for an L7 fetch are ready,
8392 * which means that a request or response is ready. If some data is missing,
8393 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008394 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8395 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008396 *
8397 * The function returns :
8398 * 0 if some data is missing or if the requested data cannot be fetched
8399 * -1 if it is certain that we'll never have any HTTP message there
8400 * 1 if an HTTP message is ready
8401 */
8402static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008403acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008404 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008405{
8406 struct http_txn *txn = l7;
8407 struct http_msg *msg = &txn->req;
8408
8409 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8410 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8411 */
8412
8413 if (unlikely(!s || !txn))
8414 return 0;
8415
8416 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008417 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008418
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008419 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008420 if (unlikely(!s->req))
8421 return 0;
8422
Willy Tarreauaae75e32013-03-29 12:31:49 +01008423 /* If the buffer does not leave enough free space at the end,
8424 * we must first realign it.
8425 */
8426 if (s->req->buf->p > s->req->buf->data &&
8427 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8428 buffer_slow_realign(s->req->buf);
8429
Willy Tarreau14174bc2012-04-16 14:34:04 +02008430 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008431 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008432 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008433 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008434 return -1;
8435 }
8436
8437 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008438 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008439 http_msg_analyzer(msg, &txn->hdr_idx);
8440
8441 /* Still no valid request ? */
8442 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008443 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008444 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008445 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008446 return -1;
8447 }
8448 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008449 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008450 return 0;
8451 }
8452
8453 /* OK we just got a valid HTTP request. We have some minor
8454 * preparation to perform so that further checks can rely
8455 * on HTTP tests.
8456 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008457
8458 /* If the request was parsed but was too large, we must absolutely
8459 * return an error so that it is not processed. At the moment this
8460 * cannot happen, but if the parsers are to change in the future,
8461 * we want this check to be maintained.
8462 */
8463 if (unlikely(s->req->buf->i + s->req->buf->p >
8464 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8465 msg->msg_state = HTTP_MSG_ERROR;
8466 return 1;
8467 }
8468
Willy Tarreau9b28e032012-10-12 23:49:43 +02008469 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008470 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8471 s->flags |= SN_REDIRECTABLE;
8472
8473 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008474 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008475 return -1;
8476 }
8477 }
8478
Willy Tarreau24e32d82012-04-23 23:55:44 +02008479 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008480 return 0; /* data might have moved and indexes changed */
8481
8482 /* otherwise everything's ready for the request */
8483 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008484 else {
8485 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008486 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8487 return 0;
8488 }
8489
8490 /* everything's OK */
8491 return 1;
8492}
Willy Tarreau8797c062007-05-07 00:55:35 +02008493
Willy Tarreauc0239e02012-04-16 14:42:55 +02008494#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008495 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 +02008496
Willy Tarreau24e32d82012-04-23 23:55:44 +02008497#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008498 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 +02008499
Willy Tarreau8797c062007-05-07 00:55:35 +02008500
8501/* 1. Check on METHOD
8502 * We use the pre-parsed method if it is known, and store its number as an
8503 * integer. If it is unknown, we use the pointer and the length.
8504 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008505static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008506{
8507 int len, meth;
8508
Willy Tarreauae8b7962007-06-09 23:10:04 +02008509 len = strlen(*text);
8510 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008511
8512 pattern->val.i = meth;
8513 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008514 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008515 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008516 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008517 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008518 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008519 pattern->len = len;
8520 }
8521 return 1;
8522}
8523
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008524/* This function fetches the method of current HTTP request and stores
8525 * it in the global pattern struct as a chunk. There are two possibilities :
8526 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8527 * in <len> and <ptr> is NULL ;
8528 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8529 * <len> to its length.
8530 * This is intended to be used with acl_match_meth() only.
8531 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008532static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008533smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008534 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008535{
8536 int meth;
8537 struct http_txn *txn = l7;
8538
Willy Tarreau24e32d82012-04-23 23:55:44 +02008539 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008540
Willy Tarreau8797c062007-05-07 00:55:35 +02008541 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008542 smp->type = SMP_T_UINT;
8543 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008544 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008545 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8546 /* ensure the indexes are not affected */
8547 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008548 smp->type = SMP_T_CSTR;
8549 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008550 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008551 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008552 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008553 return 1;
8554}
8555
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008556/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008557static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008558{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008559 int icase;
8560
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008561
Willy Tarreauf853c462012-04-23 18:53:56 +02008562 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008563 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008564 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008565 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008566 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008567 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008568
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008569 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8570 if (pattern->val.i != HTTP_METH_OTHER)
8571 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008572
8573 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008574 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008575 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008576
8577 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008578 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8579 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008580 return ACL_PAT_FAIL;
8581 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008582}
8583
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008584static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008585smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008586 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008587{
8588 struct http_txn *txn = l7;
8589 char *ptr;
8590 int len;
8591
Willy Tarreauc0239e02012-04-16 14:42:55 +02008592 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008593
Willy Tarreau8797c062007-05-07 00:55:35 +02008594 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008595 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008596
8597 while ((len-- > 0) && (*ptr++ != '/'));
8598 if (len <= 0)
8599 return 0;
8600
Willy Tarreauf853c462012-04-23 18:53:56 +02008601 smp->type = SMP_T_CSTR;
8602 smp->data.str.str = ptr;
8603 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008604
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008605 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008606 return 1;
8607}
8608
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008609static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008610smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008611 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008612{
8613 struct http_txn *txn = l7;
8614 char *ptr;
8615 int len;
8616
Willy Tarreauc0239e02012-04-16 14:42:55 +02008617 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008618
Willy Tarreauf26b2522012-12-14 08:33:14 +01008619 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8620 return 0;
8621
Willy Tarreau8797c062007-05-07 00:55:35 +02008622 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008623 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008624
8625 while ((len-- > 0) && (*ptr++ != '/'));
8626 if (len <= 0)
8627 return 0;
8628
Willy Tarreauf853c462012-04-23 18:53:56 +02008629 smp->type = SMP_T_CSTR;
8630 smp->data.str.str = ptr;
8631 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008632
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008633 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008634 return 1;
8635}
8636
8637/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008638static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008639smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008640 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008641{
8642 struct http_txn *txn = l7;
8643 char *ptr;
8644 int len;
8645
Willy Tarreauc0239e02012-04-16 14:42:55 +02008646 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008647
Willy Tarreauf26b2522012-12-14 08:33:14 +01008648 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8649 return 0;
8650
Willy Tarreau8797c062007-05-07 00:55:35 +02008651 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008652 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008653
Willy Tarreauf853c462012-04-23 18:53:56 +02008654 smp->type = SMP_T_UINT;
8655 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008656 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008657 return 1;
8658}
8659
8660/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008661static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008662smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008663 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008664{
8665 struct http_txn *txn = l7;
8666
Willy Tarreauc0239e02012-04-16 14:42:55 +02008667 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008668
Willy Tarreauf853c462012-04-23 18:53:56 +02008669 smp->type = SMP_T_CSTR;
8670 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008671 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008672 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008673 return 1;
8674}
8675
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008676static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008677smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008678 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008679{
8680 struct http_txn *txn = l7;
8681
Willy Tarreauc0239e02012-04-16 14:42:55 +02008682 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008683
8684 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008685 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8686 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008687 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008688 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008689 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008690
8691 /*
8692 * If we are parsing url in frontend space, we prepare backend stage
8693 * to not parse again the same url ! optimization lazyness...
8694 */
8695 if (px->options & PR_O_HTTP_PROXY)
8696 l4->flags |= SN_ADDR_SET;
8697
Willy Tarreau37406352012-04-23 16:16:37 +02008698 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008699 return 1;
8700}
8701
8702static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008703smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008704 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008705{
8706 struct http_txn *txn = l7;
8707
Willy Tarreauc0239e02012-04-16 14:42:55 +02008708 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008709
8710 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008711 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 +02008712 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008713 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008714
8715 if (px->options & PR_O_HTTP_PROXY)
8716 l4->flags |= SN_ADDR_SET;
8717
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008718 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008719 return 1;
8720}
8721
Willy Tarreau185b5c42012-04-26 15:11:51 +02008722/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8723 * Accepts an optional argument of type string containing the header field name,
8724 * and an optional argument of type signed or unsigned integer to request an
8725 * explicit occurrence of the header. Note that in the event of a missing name,
8726 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008727 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008728static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008729smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008730 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008731{
8732 struct http_txn *txn = l7;
8733 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008734 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008735 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008736 int occ = 0;
8737 const char *name_str = NULL;
8738 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008739
Willy Tarreaua890d072013-04-02 12:01:06 +02008740 if (!ctx) {
8741 /* first call */
8742 ctx = &static_hdr_ctx;
8743 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02008744 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008745 }
8746
Willy Tarreau185b5c42012-04-26 15:11:51 +02008747 if (args) {
8748 if (args[0].type != ARGT_STR)
8749 return 0;
8750 name_str = args[0].data.str.str;
8751 name_len = args[0].data.str.len;
8752
8753 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8754 occ = args[1].data.uint;
8755 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008756
Willy Tarreaue333ec92012-04-16 16:26:40 +02008757 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008758
Willy Tarreau185b5c42012-04-26 15:11:51 +02008759 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008760 /* search for header from the beginning */
8761 ctx->idx = 0;
8762
Willy Tarreau185b5c42012-04-26 15:11:51 +02008763 if (!occ && !(opt & SMP_OPT_ITERATE))
8764 /* no explicit occurrence and single fetch => last header by default */
8765 occ = -1;
8766
8767 if (!occ)
8768 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008769 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008770
Willy Tarreau185b5c42012-04-26 15:11:51 +02008771 smp->type = SMP_T_CSTR;
8772 smp->flags |= SMP_F_VOL_HDR;
8773 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 +02008774 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008775
Willy Tarreau37406352012-04-23 16:16:37 +02008776 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008777 return 0;
8778}
8779
Willy Tarreauc11416f2007-06-17 16:58:38 +02008780/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008781 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008782 */
8783static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008784smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008785 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008786{
8787 struct http_txn *txn = l7;
8788 struct hdr_idx *idx = &txn->hdr_idx;
8789 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008790 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008791 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008792
Willy Tarreau24e32d82012-04-23 23:55:44 +02008793 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008794 return 0;
8795
Willy Tarreaue333ec92012-04-16 16:26:40 +02008796 CHECK_HTTP_MESSAGE_FIRST();
8797
Willy Tarreau33a7e692007-06-10 19:45:56 +02008798 ctx.idx = 0;
8799 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008800 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 +02008801 cnt++;
8802
Willy Tarreauf853c462012-04-23 18:53:56 +02008803 smp->type = SMP_T_UINT;
8804 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008805 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008806 return 1;
8807}
8808
Willy Tarreau185b5c42012-04-26 15:11:51 +02008809/* Fetch an HTTP header's integer value. The integer value is returned. It
8810 * takes a mandatory argument of type string and an optional one of type int
8811 * to designate a specific occurrence. It returns an unsigned integer, which
8812 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008813 */
8814static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008815smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008816 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008817{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008818 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008819
Willy Tarreauf853c462012-04-23 18:53:56 +02008820 if (ret > 0) {
8821 smp->type = SMP_T_UINT;
8822 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8823 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008824
Willy Tarreaud53e2422012-04-16 17:21:11 +02008825 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008826}
8827
Cyril Bonté69fa9922012-10-25 00:01:06 +02008828/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8829 * and an optional one of type int to designate a specific occurrence.
8830 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008831 */
8832static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008833smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008834 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008835{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008836 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008837
Willy Tarreau185b5c42012-04-26 15:11:51 +02008838 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008839 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8840 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008841 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008842 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008843 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008844 if (smp->data.str.len < temp->size - 1) {
8845 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8846 temp->str[smp->data.str.len] = '\0';
8847 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8848 smp->type = SMP_T_IPV6;
8849 break;
8850 }
8851 }
8852 }
8853
Willy Tarreaud53e2422012-04-16 17:21:11 +02008854 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008855 if (!(smp->flags & SMP_F_NOT_LAST))
8856 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008857 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008858 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008859}
8860
Willy Tarreau737b0c12007-06-10 21:28:46 +02008861/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8862 * the first '/' after the possible hostname, and ends before the possible '?'.
8863 */
8864static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008865smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008866 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008867{
8868 struct http_txn *txn = l7;
8869 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008870
Willy Tarreauc0239e02012-04-16 14:42:55 +02008871 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008872
Willy Tarreau9b28e032012-10-12 23:49:43 +02008873 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008874 ptr = http_get_path(txn);
8875 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008876 return 0;
8877
8878 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008879 smp->type = SMP_T_CSTR;
8880 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008881
8882 while (ptr < end && *ptr != '?')
8883 ptr++;
8884
Willy Tarreauf853c462012-04-23 18:53:56 +02008885 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008886 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008887 return 1;
8888}
8889
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008890/* This produces a concatenation of the first occurrence of the Host header
8891 * followed by the path component if it begins with a slash ('/'). This means
8892 * that '*' will not be added, resulting in exactly the first Host entry.
8893 * If no Host header is found, then the path is returned as-is. The returned
8894 * value is stored in the trash so it does not need to be marked constant.
8895 */
8896static int
8897smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8898 const struct arg *args, struct sample *smp)
8899{
8900 struct http_txn *txn = l7;
8901 char *ptr, *end, *beg;
8902 struct hdr_ctx ctx;
8903
8904 CHECK_HTTP_MESSAGE_FIRST();
8905
8906 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008907 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 +02008908 !ctx.vlen)
8909 return smp_fetch_path(px, l4, l7, opt, args, smp);
8910
8911 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008912 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008913 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008914 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008915 smp->data.str.len = ctx.vlen;
8916
8917 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008918 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 +02008919 beg = http_get_path(txn);
8920 if (!beg)
8921 beg = end;
8922
8923 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8924
8925 if (beg < ptr && *beg == '/') {
8926 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8927 smp->data.str.len += ptr - beg;
8928 }
8929
8930 smp->flags = SMP_F_VOL_1ST;
8931 return 1;
8932}
8933
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008934/* This produces a 32-bit hash of the concatenation of the first occurrence of
8935 * the Host header followed by the path component if it begins with a slash ('/').
8936 * This means that '*' will not be added, resulting in exactly the first Host
8937 * entry. If no Host header is found, then the path is used. The resulting value
8938 * is hashed using the url hash followed by a full avalanche hash and provides a
8939 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8940 * high-traffic sites without having to store whole paths.
8941 */
8942static int
8943smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8944 const struct arg *args, struct sample *smp)
8945{
8946 struct http_txn *txn = l7;
8947 struct hdr_ctx ctx;
8948 unsigned int hash = 0;
8949 char *ptr, *beg, *end;
8950 int len;
8951
8952 CHECK_HTTP_MESSAGE_FIRST();
8953
8954 ctx.idx = 0;
8955 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8956 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8957 ptr = ctx.line + ctx.val;
8958 len = ctx.vlen;
8959 while (len--)
8960 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8961 }
8962
8963 /* now retrieve the path */
8964 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8965 beg = http_get_path(txn);
8966 if (!beg)
8967 beg = end;
8968
8969 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8970
8971 if (beg < ptr && *beg == '/') {
8972 while (beg < ptr)
8973 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8974 }
8975 hash = full_hash(hash);
8976
8977 smp->type = SMP_T_UINT;
8978 smp->data.uint = hash;
8979 smp->flags = SMP_F_VOL_1ST;
8980 return 1;
8981}
8982
Willy Tarreau4a550602012-12-09 14:53:32 +01008983/* This concatenates the source address with the 32-bit hash of the Host and
8984 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8985 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8986 * on the source address length. The URL hash is stored before the address so
8987 * that in environments where IPv6 is insignificant, truncating the output to
8988 * 8 bytes would still work.
8989 */
8990static int
8991smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8992 const struct arg *args, struct sample *smp)
8993{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008994 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008995
8996 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8997 return 0;
8998
Willy Tarreau47ca5452012-12-23 20:22:19 +01008999 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009000 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9001 temp->len += sizeof(smp->data.uint);
9002
9003 switch (l4->si[0].conn->addr.from.ss_family) {
9004 case AF_INET:
9005 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9006 temp->len += 4;
9007 break;
9008 case AF_INET6:
9009 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9010 temp->len += 16;
9011 break;
9012 default:
9013 return 0;
9014 }
9015
9016 smp->data.str = *temp;
9017 smp->type = SMP_T_BIN;
9018 return 1;
9019}
9020
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009021static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009022smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009023 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009024{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009025 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9026 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9027 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009028
Willy Tarreau24e32d82012-04-23 23:55:44 +02009029 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009030
Willy Tarreauf853c462012-04-23 18:53:56 +02009031 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009032 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009033 return 1;
9034}
9035
Willy Tarreau7f18e522010-10-22 20:04:13 +02009036/* return a valid test if the current request is the first one on the connection */
9037static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009038smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009039 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009040{
9041 if (!s)
9042 return 0;
9043
Willy Tarreauf853c462012-04-23 18:53:56 +02009044 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009045 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009046 return 1;
9047}
9048
Willy Tarreau34db1082012-04-19 17:16:54 +02009049/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009050static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009051smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009052 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009053{
9054
Willy Tarreau24e32d82012-04-23 23:55:44 +02009055 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009056 return 0;
9057
Willy Tarreauc0239e02012-04-16 14:42:55 +02009058 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009059
Willy Tarreauc0239e02012-04-16 14:42:55 +02009060 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009061 return 0;
9062
Willy Tarreauf853c462012-04-23 18:53:56 +02009063 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009064 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 +01009065 return 1;
9066}
Willy Tarreau8797c062007-05-07 00:55:35 +02009067
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009068/* Accepts exactly 1 argument of type userlist */
9069static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009070smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009071 const struct arg *args, struct sample *smp)
9072{
9073
9074 if (!args || args->type != ARGT_USR)
9075 return 0;
9076
9077 CHECK_HTTP_MESSAGE_FIRST();
9078
9079 if (!get_http_auth(l4))
9080 return 0;
9081
9082 /* acl_match_auth() will need several information at once */
9083 smp->ctx.a[0] = args->data.usr; /* user list */
9084 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9085 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9086
9087 /* if the user does not belong to the userlist or has a wrong password,
9088 * report that it unconditionally does not match. Otherwise we return
9089 * a non-zero integer which will be ignored anyway since all the params
9090 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
9091 */
9092 smp->type = SMP_T_BOOL;
9093 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9094 if (smp->data.uint)
9095 smp->type = SMP_T_UINT;
9096
9097 return 1;
9098}
9099
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009100/* Try to find the next occurrence of a cookie name in a cookie header value.
9101 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9102 * the cookie value is returned into *value and *value_l, and the function
9103 * returns a pointer to the next pointer to search from if the value was found.
9104 * Otherwise if the cookie was not found, NULL is returned and neither value
9105 * nor value_l are touched. The input <hdr> string should first point to the
9106 * header's value, and the <hdr_end> pointer must point to the first character
9107 * not part of the value. <list> must be non-zero if value may represent a list
9108 * of values (cookie headers). This makes it faster to abort parsing when no
9109 * list is expected.
9110 */
9111static char *
9112extract_cookie_value(char *hdr, const char *hdr_end,
9113 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009114 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009115{
9116 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9117 char *next;
9118
9119 /* we search at least a cookie name followed by an equal, and more
9120 * generally something like this :
9121 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9122 */
9123 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9124 /* Iterate through all cookies on this line */
9125
9126 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9127 att_beg++;
9128
9129 /* find att_end : this is the first character after the last non
9130 * space before the equal. It may be equal to hdr_end.
9131 */
9132 equal = att_end = att_beg;
9133
9134 while (equal < hdr_end) {
9135 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9136 break;
9137 if (http_is_spht[(unsigned char)*equal++])
9138 continue;
9139 att_end = equal;
9140 }
9141
9142 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9143 * is between <att_beg> and <equal>, both may be identical.
9144 */
9145
9146 /* look for end of cookie if there is an equal sign */
9147 if (equal < hdr_end && *equal == '=') {
9148 /* look for the beginning of the value */
9149 val_beg = equal + 1;
9150 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9151 val_beg++;
9152
9153 /* find the end of the value, respecting quotes */
9154 next = find_cookie_value_end(val_beg, hdr_end);
9155
9156 /* make val_end point to the first white space or delimitor after the value */
9157 val_end = next;
9158 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9159 val_end--;
9160 } else {
9161 val_beg = val_end = next = equal;
9162 }
9163
9164 /* We have nothing to do with attributes beginning with '$'. However,
9165 * they will automatically be removed if a header before them is removed,
9166 * since they're supposed to be linked together.
9167 */
9168 if (*att_beg == '$')
9169 continue;
9170
9171 /* Ignore cookies with no equal sign */
9172 if (equal == next)
9173 continue;
9174
9175 /* Now we have the cookie name between att_beg and att_end, and
9176 * its value between val_beg and val_end.
9177 */
9178
9179 if (att_end - att_beg == cookie_name_l &&
9180 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9181 /* let's return this value and indicate where to go on from */
9182 *value = val_beg;
9183 *value_l = val_end - val_beg;
9184 return next + 1;
9185 }
9186
9187 /* Set-Cookie headers only have the name in the first attr=value part */
9188 if (!list)
9189 break;
9190 }
9191
9192 return NULL;
9193}
9194
Willy Tarreaue333ec92012-04-16 16:26:40 +02009195/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009196 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009197 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009198 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009199 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009200 * Accepts exactly 1 argument of type string. If the input options indicate
9201 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009202 */
9203static int
Willy Tarreau51539362012-05-08 12:46:28 +02009204smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9205 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009206{
9207 struct http_txn *txn = l7;
9208 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009209 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009210 const struct http_msg *msg;
9211 const char *hdr_name;
9212 int hdr_name_len;
9213 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009214 int occ = 0;
9215 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009216
Willy Tarreau24e32d82012-04-23 23:55:44 +02009217 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009218 return 0;
9219
Willy Tarreaua890d072013-04-02 12:01:06 +02009220 if (!ctx) {
9221 /* first call */
9222 ctx = &static_hdr_ctx;
9223 ctx->idx = 0;
9224 smp->ctx.a[2] = ctx;
9225 }
9226
Willy Tarreaue333ec92012-04-16 16:26:40 +02009227 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009228
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009229 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009230 msg = &txn->req;
9231 hdr_name = "Cookie";
9232 hdr_name_len = 6;
9233 } else {
9234 msg = &txn->rsp;
9235 hdr_name = "Set-Cookie";
9236 hdr_name_len = 10;
9237 }
9238
Willy Tarreau28376d62012-04-26 21:26:10 +02009239 if (!occ && !(opt & SMP_OPT_ITERATE))
9240 /* no explicit occurrence and single fetch => last cookie by default */
9241 occ = -1;
9242
9243 /* OK so basically here, either we want only one value and it's the
9244 * last one, or we want to iterate over all of them and we fetch the
9245 * next one.
9246 */
9247
Willy Tarreau9b28e032012-10-12 23:49:43 +02009248 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009249 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009250 /* search for the header from the beginning, we must first initialize
9251 * the search parameters.
9252 */
Willy Tarreau37406352012-04-23 16:16:37 +02009253 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009254 ctx->idx = 0;
9255 }
9256
Willy Tarreau28376d62012-04-26 21:26:10 +02009257 smp->flags |= SMP_F_VOL_HDR;
9258
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009259 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009260 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9261 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009262 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9263 goto out;
9264
Willy Tarreau24e32d82012-04-23 23:55:44 +02009265 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009266 continue;
9267
Willy Tarreau37406352012-04-23 16:16:37 +02009268 smp->ctx.a[0] = ctx->line + ctx->val;
9269 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009270 }
9271
Willy Tarreauf853c462012-04-23 18:53:56 +02009272 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009273 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009274 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009275 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009276 &smp->data.str.str,
9277 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009278 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009279 found = 1;
9280 if (occ >= 0) {
9281 /* one value was returned into smp->data.str.{str,len} */
9282 smp->flags |= SMP_F_NOT_LAST;
9283 return 1;
9284 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009285 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009286 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009287 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009288 /* all cookie headers and values were scanned. If we're looking for the
9289 * last occurrence, we may return it now.
9290 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009291 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009292 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009293 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009294}
9295
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009296/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009297 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009298 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009299 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009300 */
9301static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009302smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009303 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009304{
9305 struct http_txn *txn = l7;
9306 struct hdr_idx *idx = &txn->hdr_idx;
9307 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009308 const struct http_msg *msg;
9309 const char *hdr_name;
9310 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009311 int cnt;
9312 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009313 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009314
Willy Tarreau24e32d82012-04-23 23:55:44 +02009315 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009316 return 0;
9317
Willy Tarreaue333ec92012-04-16 16:26:40 +02009318 CHECK_HTTP_MESSAGE_FIRST();
9319
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009320 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009321 msg = &txn->req;
9322 hdr_name = "Cookie";
9323 hdr_name_len = 6;
9324 } else {
9325 msg = &txn->rsp;
9326 hdr_name = "Set-Cookie";
9327 hdr_name_len = 10;
9328 }
9329
Willy Tarreau9b28e032012-10-12 23:49:43 +02009330 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009331 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009332 ctx.idx = 0;
9333 cnt = 0;
9334
9335 while (1) {
9336 /* Note: val_beg == NULL every time we need to fetch a new header */
9337 if (!val_beg) {
9338 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9339 break;
9340
Willy Tarreau24e32d82012-04-23 23:55:44 +02009341 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009342 continue;
9343
9344 val_beg = ctx.line + ctx.val;
9345 val_end = val_beg + ctx.vlen;
9346 }
9347
Willy Tarreauf853c462012-04-23 18:53:56 +02009348 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009349 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009350 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009351 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009352 &smp->data.str.str,
9353 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009354 cnt++;
9355 }
9356 }
9357
Willy Tarreauf853c462012-04-23 18:53:56 +02009358 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009359 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009360 return 1;
9361}
9362
Willy Tarreau51539362012-05-08 12:46:28 +02009363/* Fetch an cookie's integer value. The integer value is returned. It
9364 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9365 */
9366static int
9367smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9368 const struct arg *args, struct sample *smp)
9369{
9370 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9371
9372 if (ret > 0) {
9373 smp->type = SMP_T_UINT;
9374 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9375 }
9376
9377 return ret;
9378}
9379
Willy Tarreau8797c062007-05-07 00:55:35 +02009380/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009381/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009382/************************************************************************/
9383
David Cournapeau16023ee2010-12-23 20:55:41 +09009384/*
9385 * Given a path string and its length, find the position of beginning of the
9386 * query string. Returns NULL if no query string is found in the path.
9387 *
9388 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9389 *
9390 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9391 */
bedis4c75cca2012-10-05 08:38:24 +02009392static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009393{
9394 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009395
bedis4c75cca2012-10-05 08:38:24 +02009396 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009397 return p ? p + 1 : NULL;
9398}
9399
bedis4c75cca2012-10-05 08:38:24 +02009400static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009401{
bedis4c75cca2012-10-05 08:38:24 +02009402 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009403}
9404
9405/*
9406 * Given a url parameter, find the starting position of the first occurence,
9407 * or NULL if the parameter is not found.
9408 *
9409 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9410 * the function will return query_string+8.
9411 */
9412static char*
9413find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009414 char* url_param_name, size_t url_param_name_l,
9415 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009416{
9417 char *pos, *last;
9418
9419 pos = query_string;
9420 last = query_string + query_string_l - url_param_name_l - 1;
9421
9422 while (pos <= last) {
9423 if (pos[url_param_name_l] == '=') {
9424 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9425 return pos;
9426 pos += url_param_name_l + 1;
9427 }
bedis4c75cca2012-10-05 08:38:24 +02009428 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009429 pos++;
9430 pos++;
9431 }
9432 return NULL;
9433}
9434
9435/*
9436 * Given a url parameter name, returns its value and size into *value and
9437 * *value_l respectively, and returns non-zero. If the parameter is not found,
9438 * zero is returned and value/value_l are not touched.
9439 */
9440static int
9441find_url_param_value(char* path, size_t path_l,
9442 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009443 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009444{
9445 char *query_string, *qs_end;
9446 char *arg_start;
9447 char *value_start, *value_end;
9448
bedis4c75cca2012-10-05 08:38:24 +02009449 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009450 if (!query_string)
9451 return 0;
9452
9453 qs_end = path + path_l;
9454 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009455 url_param_name, url_param_name_l,
9456 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009457 if (!arg_start)
9458 return 0;
9459
9460 value_start = arg_start + url_param_name_l + 1;
9461 value_end = value_start;
9462
bedis4c75cca2012-10-05 08:38:24 +02009463 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009464 value_end++;
9465
9466 *value = value_start;
9467 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009468 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009469}
9470
9471static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009472smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9473 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009474{
bedis4c75cca2012-10-05 08:38:24 +02009475 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009476 struct http_txn *txn = l7;
9477 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009478
bedis4c75cca2012-10-05 08:38:24 +02009479 if (!args || args[0].type != ARGT_STR ||
9480 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009481 return 0;
9482
9483 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009484
bedis4c75cca2012-10-05 08:38:24 +02009485 if (args[1].type)
9486 delim = *args[1].data.str.str;
9487
Willy Tarreau9b28e032012-10-12 23:49:43 +02009488 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009489 args->data.str.str, args->data.str.len,
9490 &smp->data.str.str, &smp->data.str.len,
9491 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009492 return 0;
9493
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009494 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009495 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009496 return 1;
9497}
9498
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009499/* Return the signed integer value for the specified url parameter (see url_param
9500 * above).
9501 */
9502static int
9503smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9504 const struct arg *args, struct sample *smp)
9505{
9506 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9507
9508 if (ret > 0) {
9509 smp->type = SMP_T_UINT;
9510 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9511 }
9512
9513 return ret;
9514}
9515
Willy Tarreau185b5c42012-04-26 15:11:51 +02009516/* This function is used to validate the arguments passed to any "hdr" fetch
9517 * keyword. These keywords support an optional positive or negative occurrence
9518 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9519 * is assumed that the types are already the correct ones. Returns 0 on error,
9520 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9521 * error message in case of error, that the caller is responsible for freeing.
9522 * The initial location must either be freeable or NULL.
9523 */
9524static int val_hdr(struct arg *arg, char **err_msg)
9525{
9526 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009527 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009528 return 0;
9529 }
9530 return 1;
9531}
9532
Willy Tarreau4a568972010-05-12 08:08:50 +02009533/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009534/* All supported ACL keywords must be declared here. */
9535/************************************************************************/
9536
9537/* Note: must not be declared <const> as its list will be overwritten.
9538 * Please take care of keeping this list alphabetically sorted.
9539 */
9540static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009541 { "base", "base", acl_parse_str, acl_match_str },
9542 { "base_beg", "base", acl_parse_str, acl_match_beg },
9543 { "base_dir", "base", acl_parse_str, acl_match_dir },
9544 { "base_dom", "base", acl_parse_str, acl_match_dom },
9545 { "base_end", "base", acl_parse_str, acl_match_end },
9546 { "base_len", "base", acl_parse_int, acl_match_len },
9547 { "base_reg", "base", acl_parse_reg, acl_match_reg },
9548 { "base_sub", "base", acl_parse_str, acl_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009549
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009550 { "cook", "req.cook", acl_parse_str, acl_match_str },
9551 { "cook_beg", "req.cook", acl_parse_str, acl_match_beg },
9552 { "cook_cnt", "req.cook_cnt", acl_parse_int, acl_match_int },
9553 { "cook_dir", "req.cook", acl_parse_str, acl_match_dir },
9554 { "cook_dom", "req.cook", acl_parse_str, acl_match_dom },
9555 { "cook_end", "req.cook", acl_parse_str, acl_match_end },
9556 { "cook_len", "req.cook", acl_parse_int, acl_match_len },
9557 { "cook_reg", "req.cook", acl_parse_reg, acl_match_reg },
9558 { "cook_sub", "req.cook", acl_parse_str, acl_match_sub },
9559 { "cook_val", "req.cook_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009560
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009561 { "hdr", "req.hdr", acl_parse_str, acl_match_str },
9562 { "hdr_beg", "req.hdr", acl_parse_str, acl_match_beg },
9563 { "hdr_cnt", "req.hdr_cnt", acl_parse_int, acl_match_int },
9564 { "hdr_dir", "req.hdr", acl_parse_str, acl_match_dir },
9565 { "hdr_dom", "req.hdr", acl_parse_str, acl_match_dom },
9566 { "hdr_end", "req.hdr", acl_parse_str, acl_match_end },
9567 { "hdr_ip", "req.hdr_ip", acl_parse_ip, acl_match_ip },
9568 { "hdr_len", "req.hdr", acl_parse_int, acl_match_len },
9569 { "hdr_reg", "req.hdr", acl_parse_reg, acl_match_reg },
9570 { "hdr_sub", "req.hdr", acl_parse_str, acl_match_sub },
9571 { "hdr_val", "req.hdr_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009572
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009573 { "http_auth", NULL, acl_parse_nothing, acl_match_nothing },
9574 { "http_auth_group", NULL, acl_parse_strcat, acl_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009575
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009576 { "http_first_req", NULL, acl_parse_nothing, acl_match_nothing },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009577
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009578 { "method", NULL, acl_parse_meth, acl_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009579
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009580 { "path", "path", acl_parse_str, acl_match_str },
9581 { "path_beg", "path", acl_parse_str, acl_match_beg },
9582 { "path_dir", "path", acl_parse_str, acl_match_dir },
9583 { "path_dom", "path", acl_parse_str, acl_match_dom },
9584 { "path_end", "path", acl_parse_str, acl_match_end },
9585 { "path_len", "path", acl_parse_int, acl_match_len },
9586 { "path_reg", "path", acl_parse_reg, acl_match_reg },
9587 { "path_sub", "path", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009588
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009589 { "req_proto_http", "req.proto_http",acl_parse_nothing, acl_match_nothing },
Willy Tarreauff5afcc2013-03-31 18:49:18 +02009590 { "req_ver", "req.ver", acl_parse_str, acl_match_str },
9591 { "resp_ver", "res.ver", acl_parse_str, acl_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009592
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009593 { "scook", "res.cook", acl_parse_str, acl_match_str },
9594 { "scook_beg", "res.cook", acl_parse_str, acl_match_beg },
9595 { "scook_cnt", "res.cook_cnt", acl_parse_int, acl_match_int },
9596 { "scook_dir", "res.cook", acl_parse_str, acl_match_dir },
9597 { "scook_dom", "res.cook", acl_parse_str, acl_match_dom },
9598 { "scook_end", "res.cook", acl_parse_str, acl_match_end },
9599 { "scook_len", "res.cook", acl_parse_int, acl_match_len },
9600 { "scook_reg", "res.cook", acl_parse_reg, acl_match_reg },
9601 { "scook_sub", "res.cook", acl_parse_str, acl_match_sub },
9602 { "scook_val", "res.cook_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009603
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009604 { "shdr", "res.hdr", acl_parse_str, acl_match_str },
9605 { "shdr_beg", "res.hdr", acl_parse_str, acl_match_beg },
9606 { "shdr_cnt", "res.hdr_cnt", acl_parse_int, acl_match_int },
9607 { "shdr_dir", "res.hdr", acl_parse_str, acl_match_dir },
9608 { "shdr_dom", "res.hdr", acl_parse_str, acl_match_dom },
9609 { "shdr_end", "res.hdr", acl_parse_str, acl_match_end },
9610 { "shdr_ip", "res.hdr_ip", acl_parse_ip, acl_match_ip },
9611 { "shdr_len", "res.hdr", acl_parse_int, acl_match_len },
9612 { "shdr_reg", "res.hdr", acl_parse_reg, acl_match_reg },
9613 { "shdr_sub", "res.hdr", acl_parse_str, acl_match_sub },
9614 { "shdr_val", "res.hdr_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009615
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009616 { "status", NULL, acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009617
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009618 { "url", "url", acl_parse_str, acl_match_str },
9619 { "url_beg", "url", acl_parse_str, acl_match_beg },
9620 { "url_dir", "url", acl_parse_str, acl_match_dir },
9621 { "url_dom", "url", acl_parse_str, acl_match_dom },
9622 { "url_end", "url", acl_parse_str, acl_match_end },
9623 { "url_ip", "url_ip", acl_parse_ip, acl_match_ip },
9624 { "url_len", "url", acl_parse_int, acl_match_len },
9625 { "url_port", "url_port", acl_parse_int, acl_match_int },
9626 { "url_reg", "url", acl_parse_reg, acl_match_reg },
9627 { "url_sub", "url", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009628
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009629 { "urlp", "urlp", acl_parse_str, acl_match_str },
9630 { "urlp_beg", "urlp", acl_parse_str, acl_match_beg },
9631 { "urlp_dir", "urlp", acl_parse_str, acl_match_dir },
9632 { "urlp_dom", "urlp", acl_parse_str, acl_match_dom },
9633 { "urlp_end", "urlp", acl_parse_str, acl_match_end },
9634 { "urlp_ip", "urlp", acl_parse_ip, acl_match_ip },
9635 { "urlp_len", "urlp", acl_parse_int, acl_match_len },
9636 { "urlp_reg", "urlp", acl_parse_reg, acl_match_reg },
9637 { "urlp_sub", "urlp", acl_parse_str, acl_match_sub },
9638 { "urlp_val", "urlp_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009639
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009640 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009641}};
9642
9643/************************************************************************/
9644/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009645/************************************************************************/
9646/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009647static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau409bcde2013-01-08 00:31:00 +01009648 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9649 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9650 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
9651
9652 /* cookie is valid in both directions (eg: for "stick ...") but cook*
9653 * are only here to match the ACL's name, are request-only and are used
9654 * for ACL compatibility only.
9655 */
9656 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9657 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9658 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9659 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9660
9661 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
9662 * only here to match the ACL's name, are request-only and are used for
9663 * ACL compatibility only.
9664 */
9665 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9666 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9667 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
9668 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
9669
Willy Tarreau0a0daec2013-04-02 22:44:58 +02009670 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
9671 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009672 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
9673 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
9674 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009675
9676 /* HTTP protocol on the request path */
9677 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009678 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009679
9680 /* HTTP version on the request path */
9681 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009682 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009683
9684 /* HTTP version on the response path */
9685 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009686 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9687
Willy Tarreau18ed2562013-01-14 15:56:36 +01009688 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
9689 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9690 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9691 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9692
9693 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
9694 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9695 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
9696 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
9697
9698 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
9699 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9700 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9701 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9702
9703 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
9704 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9705 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
9706 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
9707
Willy Tarreau409bcde2013-01-08 00:31:00 +01009708 /* scook is valid only on the response and is used for ACL compatibility */
9709 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9710 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9711 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9712 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
9713
9714 /* shdr is valid only on the response and is used for ACL compatibility */
9715 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
9716 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9717 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
9718 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
9719
9720 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
9721 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9722 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
9723 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9724 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9725 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9726 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9727 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +02009728}};
9729
Willy Tarreau8797c062007-05-07 00:55:35 +02009730
9731__attribute__((constructor))
9732static void __http_protocol_init(void)
9733{
9734 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009735 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009736}
9737
9738
Willy Tarreau58f10d72006-12-04 02:26:12 +01009739/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009740 * Local variables:
9741 * c-indent-level: 8
9742 * c-basic-offset: 8
9743 * End:
9744 */