Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/queue.h> |
| 51 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 52 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 53 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/task.h> |
| 55 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_HAP_TCPSPLICE |
| 57 | #include <libtcpsplice.h> |
| 58 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 60 | #define DEBUG_PARSE_NO_SPEEDUP |
| 61 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 62 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 63 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 64 | * instruction. The first argument is the label for normal operation, and the |
| 65 | * second one is the break/continue instruction in the no_speedup mode. |
| 66 | */ |
| 67 | |
| 68 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 69 | #define QUICK_JUMP(x,y) y |
| 70 | #else |
| 71 | #define QUICK_JUMP(x,y) goto x |
| 72 | #endif |
| 73 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 74 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 75 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 76 | "HTTP/1.0 200 OK\r\n" |
| 77 | "Cache-Control: no-cache\r\n" |
| 78 | "Connection: close\r\n" |
| 79 | "Content-Type: text/html\r\n" |
| 80 | "\r\n" |
| 81 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const struct chunk http_200_chunk = { |
| 84 | .str = (char *)&HTTP_200, |
| 85 | .len = sizeof(HTTP_200)-1 |
| 86 | }; |
| 87 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | const char *HTTP_301 = |
| 89 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 90 | "Cache-Control: no-cache\r\n" |
| 91 | "Connection: close\r\n" |
| 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | const char *HTTP_302 = |
| 95 | "HTTP/1.0 302 Found\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
| 98 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 99 | |
| 100 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 101 | const char *HTTP_303 = |
| 102 | "HTTP/1.0 303 See Other\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 108 | const char *HTTP_401_fmt = |
| 109 | "HTTP/1.0 401 Unauthorized\r\n" |
| 110 | "Cache-Control: no-cache\r\n" |
| 111 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 112 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 114 | "\r\n" |
| 115 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 116 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 117 | |
| 118 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 119 | [HTTP_ERR_400] = 400, |
| 120 | [HTTP_ERR_403] = 403, |
| 121 | [HTTP_ERR_408] = 408, |
| 122 | [HTTP_ERR_500] = 500, |
| 123 | [HTTP_ERR_502] = 502, |
| 124 | [HTTP_ERR_503] = 503, |
| 125 | [HTTP_ERR_504] = 504, |
| 126 | }; |
| 127 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 128 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 130 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | "Cache-Control: no-cache\r\n" |
| 132 | "Connection: close\r\n" |
| 133 | "Content-Type: text/html\r\n" |
| 134 | "\r\n" |
| 135 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 136 | |
| 137 | [HTTP_ERR_403] = |
| 138 | "HTTP/1.0 403 Forbidden\r\n" |
| 139 | "Cache-Control: no-cache\r\n" |
| 140 | "Connection: close\r\n" |
| 141 | "Content-Type: text/html\r\n" |
| 142 | "\r\n" |
| 143 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 144 | |
| 145 | [HTTP_ERR_408] = |
| 146 | "HTTP/1.0 408 Request Time-out\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 152 | |
| 153 | [HTTP_ERR_500] = |
| 154 | "HTTP/1.0 500 Server Error\r\n" |
| 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_502] = |
| 162 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 168 | |
| 169 | [HTTP_ERR_503] = |
| 170 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 176 | |
| 177 | [HTTP_ERR_504] = |
| 178 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 184 | |
| 185 | }; |
| 186 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 187 | /* We must put the messages here since GCC cannot initialize consts depending |
| 188 | * on strlen(). |
| 189 | */ |
| 190 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 191 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 192 | #define FD_SETS_ARE_BITFIELDS |
| 193 | #ifdef FD_SETS_ARE_BITFIELDS |
| 194 | /* |
| 195 | * This map is used with all the FD_* macros to check whether a particular bit |
| 196 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 197 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 198 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 199 | * exclusively to the macros. |
| 200 | */ |
| 201 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 203 | |
| 204 | #else |
| 205 | #error "Check if your OS uses bitfields for fd_sets" |
| 206 | #endif |
| 207 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 208 | void init_proto_http() |
| 209 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | int i; |
| 211 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 212 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 215 | if (!http_err_msgs[msg]) { |
| 216 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 217 | abort(); |
| 218 | } |
| 219 | |
| 220 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 221 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 222 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 223 | |
| 224 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 225 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 226 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 227 | * printable characters above. |
| 228 | */ |
| 229 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 230 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 231 | for (i = 0; i < 32; i++) { |
| 232 | FD_SET(i, hdr_encode_map); |
| 233 | FD_SET(i, url_encode_map); |
| 234 | } |
| 235 | for (i = 127; i < 256; i++) { |
| 236 | FD_SET(i, hdr_encode_map); |
| 237 | FD_SET(i, url_encode_map); |
| 238 | } |
| 239 | |
| 240 | tmp = "\"#{|}"; |
| 241 | while (*tmp) { |
| 242 | FD_SET(*tmp, hdr_encode_map); |
| 243 | tmp++; |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, url_encode_map); |
| 249 | tmp++; |
| 250 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 251 | |
| 252 | /* memory allocations */ |
| 253 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 254 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 257 | /* |
| 258 | * We have 26 list of methods (1 per first letter), each of which can have |
| 259 | * up to 3 entries (2 valid, 1 null). |
| 260 | */ |
| 261 | struct http_method_desc { |
| 262 | http_meth_t meth; |
| 263 | int len; |
| 264 | const char text[8]; |
| 265 | }; |
| 266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 267 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 268 | ['C' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 270 | }, |
| 271 | ['D' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 273 | }, |
| 274 | ['G' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 276 | }, |
| 277 | ['H' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 279 | }, |
| 280 | ['P' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 282 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 283 | }, |
| 284 | ['T' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 286 | }, |
| 287 | /* rest is empty like this : |
| 288 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 289 | */ |
| 290 | }; |
| 291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 292 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 293 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 294 | * RFC2616 for those chars. |
| 295 | */ |
| 296 | |
| 297 | const char http_is_spht[256] = { |
| 298 | [' '] = 1, ['\t'] = 1, |
| 299 | }; |
| 300 | |
| 301 | const char http_is_crlf[256] = { |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_lws[256] = { |
| 306 | [' '] = 1, ['\t'] = 1, |
| 307 | ['\r'] = 1, ['\n'] = 1, |
| 308 | }; |
| 309 | |
| 310 | const char http_is_sep[256] = { |
| 311 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 312 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 313 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 314 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 315 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_ctl[256] = { |
| 319 | [0 ... 31] = 1, |
| 320 | [127] = 1, |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 325 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 326 | * them correctly. Instead, define every non-CTL char's status. |
| 327 | */ |
| 328 | const char http_is_token[256] = { |
| 329 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 330 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 331 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 332 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 333 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 334 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 335 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 336 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 337 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 338 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 339 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 340 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 341 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 342 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 343 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 344 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 345 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 346 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 347 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 348 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 349 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 350 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 351 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 352 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 353 | }; |
| 354 | |
| 355 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 356 | /* |
| 357 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 358 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 359 | */ |
| 360 | const char http_is_ver_token[256] = { |
| 361 | ['.'] = 1, ['/'] = 1, |
| 362 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 363 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 364 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 365 | }; |
| 366 | |
| 367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 369 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 538 | /* This function handles a server error at the stream interface level. The |
| 539 | * stream interface is assumed to be already in a closed state. An optional |
| 540 | * message is copied into the input buffer, and an HTTP status code stored. |
| 541 | * The error flags are set to the values in arguments. Any pending request |
| 542 | * is flushed. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 544 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 545 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 547 | buffer_flush(si->ob); |
| 548 | buffer_flush(si->ib); |
| 549 | buffer_write_ena(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 550 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 551 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 552 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | } |
| 554 | if (!(t->flags & SN_ERR_MASK)) |
| 555 | t->flags |= err; |
| 556 | if (!(t->flags & SN_FINST_MASK)) |
| 557 | t->flags |= finst; |
| 558 | } |
| 559 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 560 | /* This function returns the appropriate error location for the given session |
| 561 | * and message. |
| 562 | */ |
| 563 | |
| 564 | struct chunk *error_message(struct session *s, int msgnum) |
| 565 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 566 | if (s->be->errmsg[msgnum].str) |
| 567 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 568 | else if (s->fe->errmsg[msgnum].str) |
| 569 | return &s->fe->errmsg[msgnum]; |
| 570 | else |
| 571 | return &http_err_chunks[msgnum]; |
| 572 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 574 | /* |
| 575 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 576 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 577 | */ |
| 578 | static http_meth_t find_http_meth(const char *str, const int len) |
| 579 | { |
| 580 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 581 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 582 | |
| 583 | m = ((unsigned)*str - 'A'); |
| 584 | |
| 585 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | for (h = http_methods[m]; h->len > 0; h++) { |
| 587 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 589 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 590 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | }; |
| 592 | return HTTP_METH_OTHER; |
| 593 | } |
| 594 | return HTTP_METH_NONE; |
| 595 | |
| 596 | } |
| 597 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 598 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 599 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 600 | * It is returned otherwise. |
| 601 | */ |
| 602 | static char * |
| 603 | http_get_path(struct http_txn *txn) |
| 604 | { |
| 605 | char *ptr, *end; |
| 606 | |
| 607 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 608 | end = ptr + txn->req.sl.rq.u_l; |
| 609 | |
| 610 | if (ptr >= end) |
| 611 | return NULL; |
| 612 | |
| 613 | /* RFC2616, par. 5.1.2 : |
| 614 | * Request-URI = "*" | absuri | abspath | authority |
| 615 | */ |
| 616 | |
| 617 | if (*ptr == '*') |
| 618 | return NULL; |
| 619 | |
| 620 | if (isalpha((unsigned char)*ptr)) { |
| 621 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 622 | ptr++; |
| 623 | while (ptr < end && |
| 624 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 625 | ptr++; |
| 626 | /* skip '://' */ |
| 627 | if (ptr == end || *ptr++ != ':') |
| 628 | return NULL; |
| 629 | if (ptr == end || *ptr++ != '/') |
| 630 | return NULL; |
| 631 | if (ptr == end || *ptr++ != '/') |
| 632 | return NULL; |
| 633 | } |
| 634 | /* skip [user[:passwd]@]host[:[port]] */ |
| 635 | |
| 636 | while (ptr < end && *ptr != '/') |
| 637 | ptr++; |
| 638 | |
| 639 | if (ptr == end) |
| 640 | return NULL; |
| 641 | |
| 642 | /* OK, we got the '/' ! */ |
| 643 | return ptr; |
| 644 | } |
| 645 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 646 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 647 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 648 | * left unchanged and will follow normal proxy processing. |
| 649 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 650 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 651 | { |
| 652 | struct http_txn *txn; |
| 653 | struct chunk rdr; |
| 654 | char *path; |
| 655 | int len; |
| 656 | |
| 657 | /* 1: create the response header */ |
| 658 | rdr.len = strlen(HTTP_302); |
| 659 | rdr.str = trash; |
| 660 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 661 | |
| 662 | /* 2: add the server's prefix */ |
| 663 | if (rdr.len + s->srv->rdr_len > sizeof(trash)) |
| 664 | return; |
| 665 | |
| 666 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 667 | rdr.len += s->srv->rdr_len; |
| 668 | |
| 669 | /* 3: add the request URI */ |
| 670 | txn = &s->txn; |
| 671 | path = http_get_path(txn); |
| 672 | if (!path) |
| 673 | return; |
| 674 | |
| 675 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 676 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 677 | return; |
| 678 | |
| 679 | memcpy(rdr.str + rdr.len, path, len); |
| 680 | rdr.len += len; |
| 681 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 682 | rdr.len += 4; |
| 683 | |
| 684 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 685 | si->shutr(si); |
| 686 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 687 | si->err_type = SI_ET_NONE; |
| 688 | si->err_loc = NULL; |
| 689 | si->state = SI_ST_CLO; |
| 690 | |
| 691 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 692 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 693 | |
| 694 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 695 | if (s->srv) |
| 696 | s->srv->cum_sess++; |
| 697 | } |
| 698 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 699 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 700 | * that the server side is closed. Note that err_type is actually a |
| 701 | * bitmask, where almost only aborts may be cumulated with other |
| 702 | * values. We consider that aborted operations are more important |
| 703 | * than timeouts or errors due to the fact that nobody else in the |
| 704 | * logs might explain incomplete retries. All others should avoid |
| 705 | * being cumulated. It should normally not be possible to have multiple |
| 706 | * aborts at once, but just in case, the first one in sequence is reported. |
| 707 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 708 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 709 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 710 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 711 | |
| 712 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 713 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 714 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 715 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 716 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 717 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 718 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 719 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 720 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 721 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 722 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 723 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 724 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 725 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 726 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 727 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 728 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 729 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 730 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 731 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 732 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 735 | extern const char sess_term_cond[8]; |
| 736 | extern const char sess_fin_state[8]; |
| 737 | extern const char *monthname[12]; |
| 738 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 739 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 740 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 741 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 742 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 743 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 744 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 745 | /* |
| 746 | * send a log for the session when we have enough info about it. |
| 747 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 748 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 749 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 750 | { |
| 751 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 752 | struct proxy *fe = s->fe; |
| 753 | struct proxy *be = s->be; |
| 754 | struct proxy *prx_log; |
| 755 | struct http_txn *txn = &s->txn; |
| 756 | int tolog; |
| 757 | char *uri, *h; |
| 758 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 759 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 760 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 761 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 762 | int hdr; |
| 763 | |
| 764 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 765 | return; |
| 766 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 767 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 768 | if (s->cli_addr.ss_family == AF_INET) |
| 769 | inet_ntop(AF_INET, |
| 770 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 771 | pn, sizeof(pn)); |
| 772 | else |
| 773 | inet_ntop(AF_INET6, |
| 774 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 775 | pn, sizeof(pn)); |
| 776 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 777 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 778 | |
| 779 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 780 | tolog = fe->to_log; |
| 781 | |
| 782 | h = tmpline; |
| 783 | if (fe->to_log & LW_REQHDR && |
| 784 | txn->req.cap && |
| 785 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 786 | *(h++) = ' '; |
| 787 | *(h++) = '{'; |
| 788 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 789 | if (hdr) |
| 790 | *(h++) = '|'; |
| 791 | if (txn->req.cap[hdr] != NULL) |
| 792 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 793 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 794 | } |
| 795 | *(h++) = '}'; |
| 796 | } |
| 797 | |
| 798 | if (fe->to_log & LW_RSPHDR && |
| 799 | txn->rsp.cap && |
| 800 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 801 | *(h++) = ' '; |
| 802 | *(h++) = '{'; |
| 803 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 804 | if (hdr) |
| 805 | *(h++) = '|'; |
| 806 | if (txn->rsp.cap[hdr] != NULL) |
| 807 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 808 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 809 | } |
| 810 | *(h++) = '}'; |
| 811 | } |
| 812 | |
| 813 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 814 | *(h++) = ' '; |
| 815 | *(h++) = '"'; |
| 816 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 817 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 818 | '#', url_encode_map, uri); |
| 819 | *(h++) = '"'; |
| 820 | } |
| 821 | *h = '\0'; |
| 822 | |
| 823 | svid = (tolog & LW_SVID) ? |
| 824 | (s->data_source != DATA_SRC_STATS) ? |
| 825 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 826 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 827 | t_request = -1; |
| 828 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 829 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 830 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 831 | send_log(prx_log, LOG_INFO, |
| 832 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 833 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 834 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 835 | pn, |
| 836 | (s->cli_addr.ss_family == AF_INET) ? |
| 837 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 838 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 839 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 840 | tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 841 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 842 | t_request, |
| 843 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 844 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 845 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 846 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 847 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 848 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 849 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 850 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 851 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 852 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 853 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 854 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 855 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 856 | (s->flags & SN_REDISP)?"+":"", |
| 857 | (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 858 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 859 | |
| 860 | s->logs.logwait = 0; |
| 861 | } |
| 862 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * Capture headers from message starting at <som> according to header list |
| 866 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 867 | */ |
| 868 | void capture_headers(char *som, struct hdr_idx *idx, |
| 869 | char **cap, struct cap_hdr *cap_hdr) |
| 870 | { |
| 871 | char *eol, *sol, *col, *sov; |
| 872 | int cur_idx; |
| 873 | struct cap_hdr *h; |
| 874 | int len; |
| 875 | |
| 876 | sol = som + hdr_idx_first_pos(idx); |
| 877 | cur_idx = hdr_idx_first_idx(idx); |
| 878 | |
| 879 | while (cur_idx) { |
| 880 | eol = sol + idx->v[cur_idx].len; |
| 881 | |
| 882 | col = sol; |
| 883 | while (col < eol && *col != ':') |
| 884 | col++; |
| 885 | |
| 886 | sov = col + 1; |
| 887 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 888 | sov++; |
| 889 | |
| 890 | for (h = cap_hdr; h; h = h->next) { |
| 891 | if ((h->namelen == col - sol) && |
| 892 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 893 | if (cap[h->index] == NULL) |
| 894 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 895 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 896 | |
| 897 | if (cap[h->index] == NULL) { |
| 898 | Alert("HTTP capture : out of memory.\n"); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | len = eol - sov; |
| 903 | if (len > h->len) |
| 904 | len = h->len; |
| 905 | |
| 906 | memcpy(cap[h->index], sov, len); |
| 907 | cap[h->index][len]=0; |
| 908 | } |
| 909 | } |
| 910 | sol = eol + idx->v[cur_idx].cr + 1; |
| 911 | cur_idx = idx->v[cur_idx].next; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 916 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 917 | */ |
| 918 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 919 | |
| 920 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 921 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 922 | */ |
| 923 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 924 | ptr++; \ |
| 925 | if (likely(ptr < end)) \ |
| 926 | goto good; \ |
| 927 | else { \ |
| 928 | state = (st); \ |
| 929 | goto http_msg_ood; \ |
| 930 | } \ |
| 931 | } while (0) |
| 932 | |
| 933 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 935 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 936 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 937 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 938 | * will give undefined results. |
| 939 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 940 | * and that msg->sol points to the beginning of the response. |
| 941 | * If a complete line is found (which implies that at least one CR or LF is |
| 942 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 943 | * returned indicating an incomplete line (which does not mean that parts have |
| 944 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 945 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 946 | * upon next call. |
| 947 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 948 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 949 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 950 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 951 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 952 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 953 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 954 | unsigned int state, const char *ptr, const char *end, |
| 955 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 956 | { |
| 957 | __label__ |
| 958 | http_msg_rpver, |
| 959 | http_msg_rpver_sp, |
| 960 | http_msg_rpcode, |
| 961 | http_msg_rpcode_sp, |
| 962 | http_msg_rpreason, |
| 963 | http_msg_rpline_eol, |
| 964 | http_msg_ood, /* out of data */ |
| 965 | http_msg_invalid; |
| 966 | |
| 967 | switch (state) { |
| 968 | http_msg_rpver: |
| 969 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 970 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 971 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 972 | |
| 973 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 974 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 976 | } |
| 977 | goto http_msg_invalid; |
| 978 | |
| 979 | http_msg_rpver_sp: |
| 980 | case HTTP_MSG_RPVER_SP: |
| 981 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 982 | msg->sl.st.c = ptr - msg_buf; |
| 983 | goto http_msg_rpcode; |
| 984 | } |
| 985 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 986 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 987 | /* so it's a CR/LF, this is invalid */ |
| 988 | goto http_msg_invalid; |
| 989 | |
| 990 | http_msg_rpcode: |
| 991 | case HTTP_MSG_RPCODE: |
| 992 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 993 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 994 | |
| 995 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 996 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 997 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 998 | } |
| 999 | |
| 1000 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1001 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1002 | http_msg_rsp_reason: |
| 1003 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1004 | msg->sl.st.r = ptr - msg_buf; |
| 1005 | msg->sl.st.r_l = 0; |
| 1006 | goto http_msg_rpline_eol; |
| 1007 | |
| 1008 | http_msg_rpcode_sp: |
| 1009 | case HTTP_MSG_RPCODE_SP: |
| 1010 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1011 | msg->sl.st.r = ptr - msg_buf; |
| 1012 | goto http_msg_rpreason; |
| 1013 | } |
| 1014 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1015 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1016 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1017 | goto http_msg_rsp_reason; |
| 1018 | |
| 1019 | http_msg_rpreason: |
| 1020 | case HTTP_MSG_RPREASON: |
| 1021 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1022 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1023 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1024 | http_msg_rpline_eol: |
| 1025 | /* We have seen the end of line. Note that we do not |
| 1026 | * necessarily have the \n yet, but at least we know that we |
| 1027 | * have EITHER \r OR \n, otherwise the response would not be |
| 1028 | * complete. We can then record the response length and return |
| 1029 | * to the caller which will be able to register it. |
| 1030 | */ |
| 1031 | msg->sl.st.l = ptr - msg->sol; |
| 1032 | return ptr; |
| 1033 | |
| 1034 | #ifdef DEBUG_FULL |
| 1035 | default: |
| 1036 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1037 | exit(1); |
| 1038 | #endif |
| 1039 | } |
| 1040 | |
| 1041 | http_msg_ood: |
| 1042 | /* out of data */ |
| 1043 | if (ret_state) |
| 1044 | *ret_state = state; |
| 1045 | if (ret_ptr) |
| 1046 | *ret_ptr = (char *)ptr; |
| 1047 | return NULL; |
| 1048 | |
| 1049 | http_msg_invalid: |
| 1050 | /* invalid message */ |
| 1051 | if (ret_state) |
| 1052 | *ret_state = HTTP_MSG_ERROR; |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1058 | * This function parses a request line between <ptr> and <end>, starting with |
| 1059 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1060 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1061 | * will give undefined results. |
| 1062 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1063 | * and that msg->sol points to the beginning of the request. |
| 1064 | * If a complete line is found (which implies that at least one CR or LF is |
| 1065 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1066 | * returned indicating an incomplete line (which does not mean that parts have |
| 1067 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1068 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1069 | * upon next call. |
| 1070 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1071 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1073 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1074 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1075 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1076 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1077 | unsigned int state, const char *ptr, const char *end, |
| 1078 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1079 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1080 | __label__ |
| 1081 | http_msg_rqmeth, |
| 1082 | http_msg_rqmeth_sp, |
| 1083 | http_msg_rquri, |
| 1084 | http_msg_rquri_sp, |
| 1085 | http_msg_rqver, |
| 1086 | http_msg_rqline_eol, |
| 1087 | http_msg_ood, /* out of data */ |
| 1088 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1089 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1090 | switch (state) { |
| 1091 | http_msg_rqmeth: |
| 1092 | case HTTP_MSG_RQMETH: |
| 1093 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1094 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1095 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1096 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1097 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1099 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1100 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1101 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1102 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1103 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1104 | http_msg_req09_uri: |
| 1105 | msg->sl.rq.u = ptr - msg_buf; |
| 1106 | http_msg_req09_uri_e: |
| 1107 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1108 | http_msg_req09_ver: |
| 1109 | msg->sl.rq.v = ptr - msg_buf; |
| 1110 | msg->sl.rq.v_l = 0; |
| 1111 | goto http_msg_rqline_eol; |
| 1112 | } |
| 1113 | goto http_msg_invalid; |
| 1114 | |
| 1115 | http_msg_rqmeth_sp: |
| 1116 | case HTTP_MSG_RQMETH_SP: |
| 1117 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1118 | msg->sl.rq.u = ptr - msg_buf; |
| 1119 | goto http_msg_rquri; |
| 1120 | } |
| 1121 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1122 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1123 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1124 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1125 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1126 | http_msg_rquri: |
| 1127 | case HTTP_MSG_RQURI: |
| 1128 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1129 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1130 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1131 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1132 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1134 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1135 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1136 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1137 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1138 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1139 | http_msg_rquri_sp: |
| 1140 | case HTTP_MSG_RQURI_SP: |
| 1141 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1142 | msg->sl.rq.v = ptr - msg_buf; |
| 1143 | goto http_msg_rqver; |
| 1144 | } |
| 1145 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1146 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1147 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1148 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1149 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1150 | http_msg_rqver: |
| 1151 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1152 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1153 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1154 | |
| 1155 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1156 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1157 | http_msg_rqline_eol: |
| 1158 | /* We have seen the end of line. Note that we do not |
| 1159 | * necessarily have the \n yet, but at least we know that we |
| 1160 | * have EITHER \r OR \n, otherwise the request would not be |
| 1161 | * complete. We can then record the request length and return |
| 1162 | * to the caller which will be able to register it. |
| 1163 | */ |
| 1164 | msg->sl.rq.l = ptr - msg->sol; |
| 1165 | return ptr; |
| 1166 | } |
| 1167 | |
| 1168 | /* neither an HTTP_VER token nor a CRLF */ |
| 1169 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | #ifdef DEBUG_FULL |
| 1172 | default: |
| 1173 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1174 | exit(1); |
| 1175 | #endif |
| 1176 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | http_msg_ood: |
| 1179 | /* out of data */ |
| 1180 | if (ret_state) |
| 1181 | *ret_state = state; |
| 1182 | if (ret_ptr) |
| 1183 | *ret_ptr = (char *)ptr; |
| 1184 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1185 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1186 | http_msg_invalid: |
| 1187 | /* invalid message */ |
| 1188 | if (ret_state) |
| 1189 | *ret_state = HTTP_MSG_ERROR; |
| 1190 | return NULL; |
| 1191 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1192 | |
| 1193 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | /* |
| 1195 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1196 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1197 | * when data are missing and recalled at the exact same location with no |
| 1198 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1199 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1200 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1201 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1202 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1203 | { |
| 1204 | __label__ |
| 1205 | http_msg_rqbefore, |
| 1206 | http_msg_rqbefore_cr, |
| 1207 | http_msg_rqmeth, |
| 1208 | http_msg_rqline_end, |
| 1209 | http_msg_hdr_first, |
| 1210 | http_msg_hdr_name, |
| 1211 | http_msg_hdr_l1_sp, |
| 1212 | http_msg_hdr_l1_lf, |
| 1213 | http_msg_hdr_l1_lws, |
| 1214 | http_msg_hdr_val, |
| 1215 | http_msg_hdr_l2_lf, |
| 1216 | http_msg_hdr_l2_lws, |
| 1217 | http_msg_complete_header, |
| 1218 | http_msg_last_lf, |
| 1219 | http_msg_ood, /* out of data */ |
| 1220 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1221 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1222 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1225 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | ptr = buf->lr; |
| 1227 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1228 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1229 | if (unlikely(ptr >= end)) |
| 1230 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1232 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1233 | /* |
| 1234 | * First, states that are specific to the response only. |
| 1235 | * We check them first so that request and headers are |
| 1236 | * closer to each other (accessed more often). |
| 1237 | */ |
| 1238 | http_msg_rpbefore: |
| 1239 | case HTTP_MSG_RPBEFORE: |
| 1240 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1241 | if (likely(ptr == buf->data)) { |
| 1242 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1243 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1244 | } else { |
| 1245 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1246 | /* only skip empty leading lines, don't remove them */ |
| 1247 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1248 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1249 | #else |
| 1250 | /* Remove empty leading lines, as recommended by |
| 1251 | * RFC2616. This takes a lot of time because we |
| 1252 | * must move all the buffer backwards, but this |
| 1253 | * is rarely needed. The method above will be |
| 1254 | * cleaner when we'll be able to start sending |
| 1255 | * the request from any place in the buffer. |
| 1256 | */ |
| 1257 | buf->lr = ptr; |
| 1258 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1259 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1260 | msg->sol = buf->data; |
| 1261 | ptr = buf->data; |
| 1262 | end = buf->r; |
| 1263 | #endif |
| 1264 | } |
| 1265 | hdr_idx_init(idx); |
| 1266 | state = HTTP_MSG_RPVER; |
| 1267 | goto http_msg_rpver; |
| 1268 | } |
| 1269 | |
| 1270 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1271 | goto http_msg_invalid; |
| 1272 | |
| 1273 | if (unlikely(*ptr == '\n')) |
| 1274 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1275 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1276 | /* stop here */ |
| 1277 | |
| 1278 | http_msg_rpbefore_cr: |
| 1279 | case HTTP_MSG_RPBEFORE_CR: |
| 1280 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1281 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1282 | /* stop here */ |
| 1283 | |
| 1284 | http_msg_rpver: |
| 1285 | case HTTP_MSG_RPVER: |
| 1286 | case HTTP_MSG_RPVER_SP: |
| 1287 | case HTTP_MSG_RPCODE: |
| 1288 | case HTTP_MSG_RPCODE_SP: |
| 1289 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1290 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1291 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | if (unlikely(!ptr)) |
| 1293 | return; |
| 1294 | |
| 1295 | /* we have a full response and we know that we have either a CR |
| 1296 | * or an LF at <ptr>. |
| 1297 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1298 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1300 | |
| 1301 | msg->sol = ptr; |
| 1302 | if (likely(*ptr == '\r')) |
| 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1304 | goto http_msg_rpline_end; |
| 1305 | |
| 1306 | http_msg_rpline_end: |
| 1307 | case HTTP_MSG_RPLINE_END: |
| 1308 | /* msg->sol must point to the first of CR or LF. */ |
| 1309 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1310 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1311 | /* stop here */ |
| 1312 | |
| 1313 | /* |
| 1314 | * Second, states that are specific to the request only |
| 1315 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | http_msg_rqbefore: |
| 1317 | case HTTP_MSG_RQBEFORE: |
| 1318 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1319 | if (likely(ptr == buf->data)) { |
| 1320 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1321 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1322 | } else { |
| 1323 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1324 | /* only skip empty leading lines, don't remove them */ |
| 1325 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1326 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1327 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | /* Remove empty leading lines, as recommended by |
| 1329 | * RFC2616. This takes a lot of time because we |
| 1330 | * must move all the buffer backwards, but this |
| 1331 | * is rarely needed. The method above will be |
| 1332 | * cleaner when we'll be able to start sending |
| 1333 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1334 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | buf->lr = ptr; |
| 1336 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1337 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | msg->sol = buf->data; |
| 1339 | ptr = buf->data; |
| 1340 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1341 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1343 | /* we will need this when keep-alive will be supported |
| 1344 | hdr_idx_init(idx); |
| 1345 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | state = HTTP_MSG_RQMETH; |
| 1347 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1349 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1351 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1352 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1353 | if (unlikely(*ptr == '\n')) |
| 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1355 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | http_msg_rqbefore_cr: |
| 1359 | case HTTP_MSG_RQBEFORE_CR: |
| 1360 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1361 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | http_msg_rqmeth: |
| 1365 | case HTTP_MSG_RQMETH: |
| 1366 | case HTTP_MSG_RQMETH_SP: |
| 1367 | case HTTP_MSG_RQURI: |
| 1368 | case HTTP_MSG_RQURI_SP: |
| 1369 | case HTTP_MSG_RQVER: |
| 1370 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1371 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | if (unlikely(!ptr)) |
| 1373 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1374 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | /* we have a full request and we know that we have either a CR |
| 1376 | * or an LF at <ptr>. |
| 1377 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1378 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1379 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | msg->sol = ptr; |
| 1382 | if (likely(*ptr == '\r')) |
| 1383 | EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1384 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1385 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | http_msg_rqline_end: |
| 1387 | case HTTP_MSG_RQLINE_END: |
| 1388 | /* check for HTTP/0.9 request : no version information available. |
| 1389 | * msg->sol must point to the first of CR or LF. |
| 1390 | */ |
| 1391 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1392 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1393 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1395 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1396 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1398 | /* |
| 1399 | * Common states below |
| 1400 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | http_msg_hdr_first: |
| 1402 | case HTTP_MSG_HDR_FIRST: |
| 1403 | msg->sol = ptr; |
| 1404 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1405 | goto http_msg_hdr_name; |
| 1406 | } |
| 1407 | |
| 1408 | if (likely(*ptr == '\r')) |
| 1409 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1410 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | http_msg_hdr_name: |
| 1413 | case HTTP_MSG_HDR_NAME: |
| 1414 | /* assumes msg->sol points to the first char */ |
| 1415 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1416 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | if (likely(*ptr == ':')) { |
| 1419 | msg->col = ptr - buf->data; |
| 1420 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1421 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | http_msg_hdr_l1_sp: |
| 1426 | case HTTP_MSG_HDR_L1_SP: |
| 1427 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1428 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1429 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | /* header value can be basically anything except CR/LF */ |
| 1432 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1435 | goto http_msg_hdr_val; |
| 1436 | } |
| 1437 | |
| 1438 | if (likely(*ptr == '\r')) |
| 1439 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1440 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | http_msg_hdr_l1_lf: |
| 1443 | case HTTP_MSG_HDR_L1_LF: |
| 1444 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1445 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | http_msg_hdr_l1_lws: |
| 1448 | case HTTP_MSG_HDR_L1_LWS: |
| 1449 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1450 | /* replace HT,CR,LF with spaces */ |
| 1451 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1452 | buf->data[msg->sov] = ' '; |
| 1453 | goto http_msg_hdr_l1_sp; |
| 1454 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1455 | /* we had a header consisting only in spaces ! */ |
| 1456 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | goto http_msg_complete_header; |
| 1458 | |
| 1459 | http_msg_hdr_val: |
| 1460 | case HTTP_MSG_HDR_VAL: |
| 1461 | /* assumes msg->sol points to the first char, msg->col to the |
| 1462 | * colon, and msg->sov points to the first character of the |
| 1463 | * value. |
| 1464 | */ |
| 1465 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1466 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | msg->eol = ptr; |
| 1469 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1470 | * real header end in case it ends with lots of LWS, but is this |
| 1471 | * really needed ? |
| 1472 | */ |
| 1473 | if (likely(*ptr == '\r')) |
| 1474 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1475 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | http_msg_hdr_l2_lf: |
| 1478 | case HTTP_MSG_HDR_L2_LF: |
| 1479 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1480 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1481 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1482 | http_msg_hdr_l2_lws: |
| 1483 | case HTTP_MSG_HDR_L2_LWS: |
| 1484 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1485 | /* LWS: replace HT,CR,LF with spaces */ |
| 1486 | for (; msg->eol < ptr; msg->eol++) |
| 1487 | *msg->eol = ' '; |
| 1488 | goto http_msg_hdr_val; |
| 1489 | } |
| 1490 | http_msg_complete_header: |
| 1491 | /* |
| 1492 | * It was a new header, so the last one is finished. |
| 1493 | * Assumes msg->sol points to the first char, msg->col to the |
| 1494 | * colon, msg->sov points to the first character of the value |
| 1495 | * and msg->eol to the first CR or LF so we know how the line |
| 1496 | * ends. We insert last header into the index. |
| 1497 | */ |
| 1498 | /* |
| 1499 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1500 | write(2, msg->sol, msg->eol-msg->sol); |
| 1501 | fprintf(stderr,"\n"); |
| 1502 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1504 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1505 | idx, idx->tail) < 0)) |
| 1506 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | msg->sol = ptr; |
| 1509 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1510 | goto http_msg_hdr_name; |
| 1511 | } |
| 1512 | |
| 1513 | if (likely(*ptr == '\r')) |
| 1514 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1515 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | http_msg_last_lf: |
| 1518 | case HTTP_MSG_LAST_LF: |
| 1519 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1520 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1521 | ptr++; |
| 1522 | buf->lr = ptr; |
| 1523 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1524 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | return; |
| 1526 | #ifdef DEBUG_FULL |
| 1527 | default: |
| 1528 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1529 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1530 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | } |
| 1532 | http_msg_ood: |
| 1533 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1534 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | buf->lr = ptr; |
| 1536 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | http_msg_invalid: |
| 1539 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1540 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | return; |
| 1542 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1544 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1545 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 1546 | * needs more data, encounters an error, or wants to immediately abort the |
| 1547 | * request. It relies on buffers flags, and updates s->req->analysers. Its |
| 1548 | * behaviour is rather simple: |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1549 | * - all enabled analysers are called in turn from the lower to the higher |
| 1550 | * bit. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1551 | * - the analyser must check for errors and timeouts, and react as expected. |
| 1552 | * It does not have to close anything upon error, the caller will. |
| 1553 | * - if the analyser does not have enough data, it must return 0without calling |
| 1554 | * other ones. It should also probably do a buffer_write_dis() to ensure |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1555 | * that unprocessed data will not be forwarded. But that probably depends on |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1556 | * the protocol. |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1557 | * - if an analyser has enough data, it just has to pass on to the next |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 1558 | * analyser without using buffer_write_dis() (enabled by default). |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1559 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1560 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1561 | * |
| 1562 | * In the future, analysers should be able to indicate that they want to be |
| 1563 | * called after XXX bytes have been received (or transfered), and the min of |
| 1564 | * all's wishes will be used to ring back (unless a special condition occurs). |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1566 | int http_process_request(struct session *s, struct buffer *req) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1569 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1570 | now_ms, __FUNCTION__, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1571 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1572 | req, |
| 1573 | req->rex, req->wex, |
| 1574 | req->flags, |
| 1575 | req->l, |
| 1576 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1577 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * We will parse the partial (or complete) lines. |
| 1580 | * We will check the request syntax, and also join multi-line |
| 1581 | * headers. An index of all the lines will be elaborated while |
| 1582 | * parsing. |
| 1583 | * |
| 1584 | * For the parsing, we use a 28 states FSM. |
| 1585 | * |
| 1586 | * Here is the information we currently have : |
| 1587 | * req->data + req->som = beginning of request |
| 1588 | * req->data + req->eoh = end of processed headers / start of current one |
| 1589 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1590 | * req->lr = first non-visited byte |
| 1591 | * req->r = end of data |
| 1592 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1593 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1594 | int cur_idx; |
| 1595 | struct http_txn *txn = &s->txn; |
| 1596 | struct http_msg *msg = &txn->req; |
| 1597 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1599 | if (likely(req->lr < req->r)) |
| 1600 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1602 | /* 1: we might have to print this header in debug mode */ |
| 1603 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1604 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 1605 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 1606 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1608 | sol = req->data + msg->som; |
| 1609 | eol = sol + msg->sl.rq.l; |
| 1610 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1611 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1612 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1613 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1615 | while (cur_idx) { |
| 1616 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1617 | debug_hdr("clihdr", s, sol, eol); |
| 1618 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1619 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1621 | } |
| 1622 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1623 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1624 | /* |
| 1625 | * Now we quickly check if we have found a full valid request. |
| 1626 | * If not so, we check the FD and buffer states before leaving. |
| 1627 | * A full request is indicated by the fact that we have seen |
| 1628 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1629 | * requests are checked first. |
| 1630 | * |
| 1631 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1632 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1633 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1634 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1635 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1636 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1637 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 1638 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1640 | /* 1: Since we are in header mode, if there's no space |
| 1641 | * left for headers, we won't be able to free more |
| 1642 | * later, so the session will never terminate. We |
| 1643 | * must terminate it now. |
| 1644 | */ |
| 1645 | if (unlikely(req->flags & BF_FULL)) { |
| 1646 | /* FIXME: check if URI is set and return Status |
| 1647 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1648 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1649 | goto return_bad_req; |
| 1650 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1652 | /* 2: have we encountered a read error ? */ |
| 1653 | else if (req->flags & BF_READ_ERROR) { |
| 1654 | /* we cannot return any message on error */ |
| 1655 | msg->msg_state = HTTP_MSG_ERROR; |
| 1656 | req->analysers = 0; |
| 1657 | s->fe->failed_req++; |
| 1658 | if (!(s->flags & SN_ERR_MASK)) |
| 1659 | s->flags |= SN_ERR_CLICL; |
| 1660 | if (!(s->flags & SN_FINST_MASK)) |
| 1661 | s->flags |= SN_FINST_R; |
| 1662 | return 0; |
| 1663 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1664 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1665 | /* 3: has the read timeout expired ? */ |
| 1666 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
| 1667 | /* read timeout : give up with an error message. */ |
| 1668 | txn->status = 408; |
| 1669 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 1670 | msg->msg_state = HTTP_MSG_ERROR; |
| 1671 | req->analysers = 0; |
| 1672 | s->fe->failed_req++; |
| 1673 | if (!(s->flags & SN_ERR_MASK)) |
| 1674 | s->flags |= SN_ERR_CLITO; |
| 1675 | if (!(s->flags & SN_FINST_MASK)) |
| 1676 | s->flags |= SN_FINST_R; |
| 1677 | return 0; |
| 1678 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1679 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1680 | /* 4: have we encountered a close ? */ |
| 1681 | else if (req->flags & BF_SHUTR) { |
| 1682 | txn->status = 400; |
| 1683 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 1684 | msg->msg_state = HTTP_MSG_ERROR; |
| 1685 | req->analysers = 0; |
| 1686 | s->fe->failed_req++; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1688 | if (!(s->flags & SN_ERR_MASK)) |
| 1689 | s->flags |= SN_ERR_CLICL; |
| 1690 | if (!(s->flags & SN_FINST_MASK)) |
| 1691 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1692 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1695 | buffer_write_dis(req); |
| 1696 | /* just set the request timeout once at the beginning of the request */ |
| 1697 | if (!tick_isset(req->analyse_exp)) |
| 1698 | req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1700 | /* we're not ready yet */ |
| 1701 | return 0; |
| 1702 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1705 | /**************************************************************** |
| 1706 | * More interesting part now : we know that we have a complete * |
| 1707 | * request which at least looks like HTTP. We have an indicator * |
| 1708 | * of each header's length, so we can parse them quickly. * |
| 1709 | ****************************************************************/ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1710 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1711 | req->analysers &= ~AN_REQ_HTTP_HDR; |
| 1712 | req->analyse_exp = TICK_ETERNITY; |
| 1713 | |
| 1714 | /* ensure we keep this pointer to the beginning of the message */ |
| 1715 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1716 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1717 | /* |
| 1718 | * 1: identify the method |
| 1719 | */ |
| 1720 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 1721 | |
| 1722 | /* we can make use of server redirect on GET and HEAD */ |
| 1723 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1724 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1725 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1726 | /* |
| 1727 | * 2: check if the URI matches the monitor_uri. |
| 1728 | * We have to do this for every request which gets in, because |
| 1729 | * the monitor-uri is defined by the frontend. |
| 1730 | */ |
| 1731 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 1732 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1733 | !memcmp(&req->data[msg->sl.rq.u], |
| 1734 | s->fe->monitor_uri, |
| 1735 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1736 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1737 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1738 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1739 | struct acl_cond *cond; |
| 1740 | cur_proxy = s->fe; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1741 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1742 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1744 | /* Check if we want to fail this monitor request or not */ |
| 1745 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1746 | int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1747 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1748 | ret = acl_pass(ret); |
| 1749 | if (cond->pol == ACL_COND_UNLESS) |
| 1750 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1751 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1752 | if (ret) { |
| 1753 | /* we fail this request, let's return 503 service unavail */ |
| 1754 | txn->status = 503; |
| 1755 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 1756 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1757 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1760 | /* nothing to fail, let's reply normaly */ |
| 1761 | txn->status = 200; |
| 1762 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 1763 | goto return_prx_cond; |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1768 | * Note: we cannot log anymore if the request has been |
| 1769 | * classified as invalid. |
| 1770 | */ |
| 1771 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1772 | /* we have a complete HTTP request that we must log */ |
| 1773 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 1774 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1776 | if (urilen >= REQURI_LEN) |
| 1777 | urilen = REQURI_LEN - 1; |
| 1778 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1779 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1781 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 1782 | s->do_log(s); |
| 1783 | } else { |
| 1784 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1785 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1786 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1788 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1789 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1790 | int delta; |
| 1791 | char *cur_end; |
| 1792 | msg->sol = req->data + msg->som; |
| 1793 | cur_end = msg->sol + msg->sl.rq.l; |
| 1794 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1796 | if (msg->sl.rq.u_l == 0) { |
| 1797 | /* if no URI was set, add "/" */ |
| 1798 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | cur_end += delta; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1800 | msg->eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1801 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1802 | /* add HTTP version */ |
| 1803 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1804 | msg->eoh += delta; |
| 1805 | cur_end += delta; |
| 1806 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1807 | HTTP_MSG_RQMETH, |
| 1808 | msg->sol, cur_end + 1, |
| 1809 | NULL, NULL); |
| 1810 | if (unlikely(!cur_end)) |
| 1811 | goto return_bad_req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1813 | /* we have a full HTTP/1.0 request now and we know that |
| 1814 | * we have either a CR or an LF at <ptr>. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1815 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1816 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1817 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1818 | |
| 1819 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1820 | /* 5: we may need to capture headers */ |
| 1821 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 1822 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 1823 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1824 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1825 | /* |
| 1826 | * 6: we will have to evaluate the filters. |
| 1827 | * As opposed to version 1.2, now they will be evaluated in the |
| 1828 | * filters order and not in the header order. This means that |
| 1829 | * each filter has to be validated among all headers. |
| 1830 | * |
| 1831 | * We can now check whether we want to switch to another |
| 1832 | * backend, in which case we will re-check the backend's |
| 1833 | * filters and various options. In order to support 3-level |
| 1834 | * switching, here's how we should proceed : |
| 1835 | * |
| 1836 | * a) run be. |
| 1837 | * if (switch) then switch ->be to the new backend. |
| 1838 | * b) run be if (be != fe). |
| 1839 | * There cannot be any switch from there, so ->be cannot be |
| 1840 | * changed anymore. |
| 1841 | * |
| 1842 | * => filters always apply to ->be, then ->be may change. |
| 1843 | * |
| 1844 | * The response path will be able to apply either ->be, or |
| 1845 | * ->be then ->fe filters in order to match the reverse of |
| 1846 | * the forward sequence. |
| 1847 | */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1848 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1849 | do { |
| 1850 | struct acl_cond *cond; |
| 1851 | struct redirect_rule *rule; |
| 1852 | struct proxy *rule_set = s->be; |
| 1853 | cur_proxy = s->be; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1854 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1855 | /* first check whether we have some ACLs set to redirect this request */ |
| 1856 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 1857 | int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1858 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1859 | ret = acl_pass(ret); |
| 1860 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1861 | ret = !ret; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1862 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1863 | if (ret) { |
| 1864 | struct chunk rdr = { trash, 0 }; |
| 1865 | const char *msg_fmt; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1866 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1867 | /* build redirect message */ |
| 1868 | switch(rule->code) { |
| 1869 | case 303: |
| 1870 | rdr.len = strlen(HTTP_303); |
| 1871 | msg_fmt = HTTP_303; |
| 1872 | break; |
| 1873 | case 301: |
| 1874 | rdr.len = strlen(HTTP_301); |
| 1875 | msg_fmt = HTTP_301; |
| 1876 | break; |
| 1877 | case 302: |
| 1878 | default: |
| 1879 | rdr.len = strlen(HTTP_302); |
| 1880 | msg_fmt = HTTP_302; |
| 1881 | break; |
| 1882 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1883 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1884 | if (unlikely(rdr.len > sizeof(trash))) |
| 1885 | goto return_bad_req; |
| 1886 | memcpy(rdr.str, msg_fmt, rdr.len); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1887 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1888 | switch(rule->type) { |
| 1889 | case REDIRECT_TYPE_PREFIX: { |
| 1890 | const char *path; |
| 1891 | int pathlen; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1892 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1893 | path = http_get_path(txn); |
| 1894 | /* build message using path */ |
| 1895 | if (path) { |
| 1896 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
Willy Tarreau | 79da469 | 2008-11-19 20:03:04 +0100 | [diff] [blame] | 1897 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 1898 | int qs = 0; |
| 1899 | while (qs < pathlen) { |
| 1900 | if (path[qs] == '?') { |
| 1901 | pathlen = qs; |
| 1902 | break; |
| 1903 | } |
| 1904 | qs++; |
| 1905 | } |
| 1906 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1907 | } else { |
| 1908 | path = "/"; |
| 1909 | pathlen = 1; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1912 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 1913 | goto return_bad_req; |
| 1914 | |
Willy Tarreau | fe651a5 | 2008-11-19 21:15:17 +0100 | [diff] [blame] | 1915 | /* add prefix. Note that if prefix == "/", we don't want to |
| 1916 | * add anything, otherwise it makes it hard for the user to |
| 1917 | * configure a self-redirection. |
| 1918 | */ |
| 1919 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 1920 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1921 | rdr.len += rule->rdr_len; |
| 1922 | } |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1923 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1924 | /* add path */ |
| 1925 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 1926 | rdr.len += pathlen; |
| 1927 | break; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1928 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1929 | case REDIRECT_TYPE_LOCATION: |
| 1930 | default: |
| 1931 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 1932 | goto return_bad_req; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1933 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1934 | /* add location */ |
| 1935 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1936 | rdr.len += rule->rdr_len; |
| 1937 | break; |
| 1938 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1939 | |
Willy Tarreau | 0140f25 | 2008-11-19 21:07:09 +0100 | [diff] [blame] | 1940 | if (rule->cookie_len) { |
| 1941 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 1942 | rdr.len += 14; |
| 1943 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 1944 | rdr.len += rule->cookie_len; |
| 1945 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 1946 | rdr.len += 2; |
| 1947 | } |
| 1948 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1949 | /* add end of headers */ |
| 1950 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 1951 | rdr.len += 4; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1952 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1953 | txn->status = rule->code; |
| 1954 | /* let's log the request time */ |
| 1955 | s->logs.tv_request = now; |
| 1956 | stream_int_retnclose(req->prod, &rdr); |
| 1957 | goto return_prx_cond; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1958 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1959 | } |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1960 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1961 | /* first check whether we have some ACLs set to block this request */ |
| 1962 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
| 1963 | int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1964 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1965 | ret = acl_pass(ret); |
| 1966 | if (cond->pol == ACL_COND_UNLESS) |
| 1967 | ret = !ret; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1969 | if (ret) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1970 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1971 | /* let's log the request time */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1972 | s->logs.tv_request = now; |
| 1973 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1974 | goto return_prx_cond; |
| 1975 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /* try headers filters */ |
| 1979 | if (rule_set->req_exp != NULL) { |
| 1980 | if (apply_filters_to_request(s, req, rule_set->req_exp) < 0) |
| 1981 | goto return_bad_req; |
| 1982 | } |
| 1983 | |
| 1984 | if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) { |
| 1985 | /* to ensure correct connection accounting on |
| 1986 | * the backend, we count the connection for the |
| 1987 | * one managing the queue. |
| 1988 | */ |
| 1989 | s->be->beconn++; |
| 1990 | if (s->be->beconn > s->be->beconn_max) |
| 1991 | s->be->beconn_max = s->be->beconn; |
| 1992 | s->be->cum_beconn++; |
| 1993 | s->flags |= SN_BE_ASSIGNED; |
| 1994 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1995 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1996 | /* has the request been denied ? */ |
| 1997 | if (txn->flags & TX_CLDENY) { |
| 1998 | /* no need to go further */ |
| 1999 | txn->status = 403; |
| 2000 | /* let's log the request time */ |
| 2001 | s->logs.tv_request = now; |
| 2002 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2003 | goto return_prx_cond; |
| 2004 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2005 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2006 | /* We might have to check for "Connection:" */ |
| 2007 | if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2008 | !(s->flags & SN_CONN_CLOSED)) { |
| 2009 | char *cur_ptr, *cur_end, *cur_next; |
| 2010 | int cur_idx, old_idx, delta, val; |
| 2011 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2012 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2013 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2014 | old_idx = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2015 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2016 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2017 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2018 | cur_ptr = cur_next; |
| 2019 | cur_end = cur_ptr + cur_hdr->len; |
| 2020 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2021 | |
| 2022 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2023 | if (val) { |
| 2024 | /* 3 possibilities : |
| 2025 | * - we have already set Connection: close, |
| 2026 | * so we remove this line. |
| 2027 | * - we have not yet set Connection: close, |
| 2028 | * but this line indicates close. We leave |
| 2029 | * it untouched and set the flag. |
| 2030 | * - we have not yet set Connection: close, |
| 2031 | * and this line indicates non-close. We |
| 2032 | * replace it. |
| 2033 | */ |
| 2034 | if (s->flags & SN_CONN_CLOSED) { |
| 2035 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2036 | txn->req.eoh += delta; |
| 2037 | cur_next += delta; |
| 2038 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2039 | txn->hdr_idx.used--; |
| 2040 | cur_hdr->len = 0; |
| 2041 | } else { |
| 2042 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2043 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2044 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2045 | cur_next += delta; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2046 | cur_hdr->len += delta; |
| 2047 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2048 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2049 | s->flags |= SN_CONN_CLOSED; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2050 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2051 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2052 | old_idx = cur_idx; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2053 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2054 | } |
| 2055 | /* add request headers from the rule sets in the same order */ |
| 2056 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2057 | if (unlikely(http_header_add_tail(req, |
| 2058 | &txn->req, |
| 2059 | &txn->hdr_idx, |
| 2060 | rule_set->req_add[cur_idx])) < 0) |
| 2061 | goto return_bad_req; |
| 2062 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2063 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2064 | /* check if stats URI was requested, and if an auth is needed */ |
| 2065 | if (rule_set->uri_auth != NULL && |
| 2066 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2067 | /* we have to check the URI and auth for this request. |
| 2068 | * FIXME!!! that one is rather dangerous, we want to |
| 2069 | * make it follow standard rules (eg: clear req->analysers). |
| 2070 | */ |
| 2071 | if (stats_check_uri_auth(s, rule_set)) { |
| 2072 | req->analysers = 0; |
| 2073 | return 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2074 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2075 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2076 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2077 | /* now check whether we have some switching rules for this request */ |
| 2078 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 2079 | struct switching_rule *rule; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2080 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2081 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2082 | int ret; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2083 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2084 | ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2085 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2086 | ret = acl_pass(ret); |
| 2087 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2088 | ret = !ret; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2089 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2090 | if (ret) { |
| 2091 | s->be = rule->be.backend; |
| 2092 | s->be->beconn++; |
| 2093 | if (s->be->beconn > s->be->beconn_max) |
| 2094 | s->be->beconn_max = s->be->beconn; |
| 2095 | s->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2096 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2097 | /* assign new parameters to the session from the new backend */ |
| 2098 | s->rep->rto = s->req->wto = s->be->timeout.server; |
| 2099 | s->req->cto = s->be->timeout.connect; |
| 2100 | s->conn_retries = s->be->conn_retries; |
| 2101 | s->flags |= SN_BE_ASSIGNED; |
| 2102 | break; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2103 | } |
| 2104 | } |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2107 | if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2108 | /* No backend was set, but there was a default |
| 2109 | * backend set in the frontend, so we use it and |
| 2110 | * loop again. |
| 2111 | */ |
| 2112 | s->be = cur_proxy->defbe.be; |
| 2113 | s->be->beconn++; |
| 2114 | if (s->be->beconn > s->be->beconn_max) |
| 2115 | s->be->beconn_max = s->be->beconn; |
| 2116 | s->be->cum_beconn++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2117 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2118 | /* assign new parameters to the session from the new backend */ |
| 2119 | s->rep->rto = s->req->wto = s->be->timeout.server; |
| 2120 | s->req->cto = s->be->timeout.connect; |
| 2121 | s->conn_retries = s->be->conn_retries; |
| 2122 | s->flags |= SN_BE_ASSIGNED; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2123 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2124 | } while (s->be != cur_proxy); /* we loop only if s->be has changed */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2125 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2126 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 2127 | /* To ensure correct connection accounting on |
| 2128 | * the backend, we count the connection for the |
| 2129 | * one managing the queue. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2130 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2131 | s->be->beconn++; |
| 2132 | if (s->be->beconn > s->be->beconn_max) |
| 2133 | s->be->beconn_max = s->be->beconn; |
| 2134 | s->be->cum_beconn++; |
| 2135 | s->flags |= SN_BE_ASSIGNED; |
| 2136 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2137 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2138 | /* |
| 2139 | * Right now, we know that we have processed the entire headers |
| 2140 | * and that unwanted requests have been filtered out. We can do |
| 2141 | * whatever we want with the remaining request. Also, now we |
| 2142 | * may have separate values for ->fe, ->be. |
| 2143 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2144 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2145 | /* |
| 2146 | * If HTTP PROXY is set we simply get remote server address |
| 2147 | * parsing incoming request. |
| 2148 | */ |
| 2149 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2150 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2151 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2152 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2153 | /* |
| 2154 | * 7: the appsession cookie was looked up very early in 1.2, |
| 2155 | * so let's do the same now. |
| 2156 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2157 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2158 | /* It needs to look into the URI */ |
| 2159 | if (s->be->appsession_name) { |
| 2160 | get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l); |
| 2161 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2162 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2163 | /* |
| 2164 | * 8: Now we can work with the cookies. |
| 2165 | * Note that doing so might move headers in the request, but |
| 2166 | * the fields will stay coherent and the URI will not move. |
| 2167 | * This should only be performed in the backend. |
| 2168 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2169 | if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2171 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2172 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2173 | /* |
| 2174 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2175 | * asks for it. |
| 2176 | */ |
| 2177 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2178 | if (s->cli_addr.ss_family == AF_INET) { |
| 2179 | /* Add an X-Forwarded-For header unless the source IP is |
| 2180 | * in the 'except' network range. |
| 2181 | */ |
| 2182 | if ((!s->fe->except_mask.s_addr || |
| 2183 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2184 | != s->fe->except_net.s_addr) && |
| 2185 | (!s->be->except_mask.s_addr || |
| 2186 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2187 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2188 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2189 | unsigned char *pn; |
| 2190 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2191 | |
| 2192 | /* Note: we rely on the backend to get the header name to be used for |
| 2193 | * x-forwarded-for, because the header is really meant for the backends. |
| 2194 | * However, if the backend did not specify any option, we have to rely |
| 2195 | * on the frontend's header name. |
| 2196 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2197 | if (s->be->fwdfor_hdr_len) { |
| 2198 | len = s->be->fwdfor_hdr_len; |
| 2199 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2200 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2201 | len = s->fe->fwdfor_hdr_len; |
| 2202 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
| 2203 | } |
| 2204 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 2205 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2206 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2207 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2208 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2209 | } |
| 2210 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 2212 | /* FIXME: for the sake of completeness, we should also support |
| 2213 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2214 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2215 | int len; |
| 2216 | char pn[INET6_ADDRSTRLEN]; |
| 2217 | inet_ntop(AF_INET6, |
| 2218 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2219 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2220 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2221 | /* Note: we rely on the backend to get the header name to be used for |
| 2222 | * x-forwarded-for, because the header is really meant for the backends. |
| 2223 | * However, if the backend did not specify any option, we have to rely |
| 2224 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2225 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2226 | if (s->be->fwdfor_hdr_len) { |
| 2227 | len = s->be->fwdfor_hdr_len; |
| 2228 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 2229 | } else { |
| 2230 | len = s->fe->fwdfor_hdr_len; |
| 2231 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2232 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2233 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2234 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2235 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2236 | &txn->hdr_idx, trash, len)) < 0) |
| 2237 | goto return_bad_req; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | /* |
| 2242 | * 10: add "Connection: close" if needed and not yet set. |
| 2243 | * Note that we do not need to add it in case of HTTP/1.0. |
| 2244 | */ |
| 2245 | if (!(s->flags & SN_CONN_CLOSED) && |
| 2246 | ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 2247 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2248 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2249 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
| 2250 | "Connection: close", 17)) < 0) |
| 2251 | goto return_bad_req; |
| 2252 | s->flags |= SN_CONN_CLOSED; |
| 2253 | } |
| 2254 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2255 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2256 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2257 | * (unless headers overflowed the buffer?) |
| 2258 | */ |
| 2259 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2260 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
| 2261 | s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
| 2262 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2263 | /* are there enough bytes here? total == l || r || rlim ? |
| 2264 | * len is unsigned, but eoh is int, |
| 2265 | * how many bytes of body have we received? |
| 2266 | * eoh is the first empty line of the header |
| 2267 | */ |
| 2268 | /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */ |
| 2269 | unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1); |
| 2270 | |
| 2271 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2272 | * We can't assume responsibility for the server's decision, |
| 2273 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2274 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2275 | */ |
| 2276 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2277 | struct hdr_ctx ctx; |
| 2278 | ctx.idx = 0; |
| 2279 | /* Expect is allowed in 1.1, look for it */ |
| 2280 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2281 | if (ctx.idx != 0 && |
| 2282 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
| 2283 | /* We can't reliablly stall and wait for data, because of |
| 2284 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2285 | * the next block to check length expectations. |
| 2286 | * We could send 100 status back to the client, but then we need to |
| 2287 | * re-write headers, and send the message. And this isn't the right |
| 2288 | * place for that action. |
| 2289 | * TODO: support Expect elsewhere and delete this block. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2290 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2291 | goto end_check_maybe_wait_for_body; |
| 2292 | } |
| 2293 | |
| 2294 | if (likely(len > s->be->url_param_post_limit)) { |
| 2295 | /* nothing to do, we got enough */ |
| 2296 | } else { |
| 2297 | /* limit implies we are supposed to need this many bytes |
| 2298 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2299 | */ |
| 2300 | long long hint = len; |
| 2301 | struct hdr_ctx ctx; |
| 2302 | ctx.idx = 0; |
| 2303 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
| 2304 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2305 | buffer_write_dis(req); |
| 2306 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2307 | } |
| 2308 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2309 | ctx.idx = 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2310 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2311 | /* now if we have a length, we'll take the hint */ |
| 2312 | if (ctx.idx) { |
| 2313 | /* We have Content-Length */ |
| 2314 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
| 2315 | hint = 0; /* parse failure, untrusted client */ |
| 2316 | else { |
| 2317 | if (hint > 0) |
| 2318 | msg->hdr_content_len = hint; |
| 2319 | else |
| 2320 | hint = 0; /* bad client, sent negative length */ |
| 2321 | } |
| 2322 | } |
| 2323 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
| 2324 | if (s->be->url_param_post_limit < hint) |
| 2325 | hint = s->be->url_param_post_limit; |
| 2326 | /* now do we really need to buffer more data? */ |
| 2327 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2328 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2329 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2330 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2331 | /* else... There are no body bytes to wait for */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2332 | } |
| 2333 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2334 | } |
| 2335 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2336 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2337 | /************************************************************* |
| 2338 | * OK, that's finished for the headers. We have done what we * |
| 2339 | * could. Let's switch to the DATA state. * |
| 2340 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2341 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2342 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
| 2343 | s->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2346 | * which may be the same as the connect timeout if unspecified. |
| 2347 | * If unset, then set it to zero because we really want it to |
| 2348 | * eventually expire. We build the tarpit as an analyser. |
| 2349 | */ |
| 2350 | if (txn->flags & TX_CLTARPIT) { |
| 2351 | buffer_flush(s->req); |
| 2352 | /* flush the request so that we can drop the connection early |
| 2353 | * if the client closes first. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2354 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2355 | buffer_write_dis(req); |
| 2356 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2357 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2358 | if (!req->analyse_exp) |
| 2359 | req->analyse_exp = now_ms; |
| 2360 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2361 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2362 | /* OK let's go on with the BODY now */ |
| 2363 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2364 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2365 | return_bad_req: /* let's centralize all bad requests */ |
| 2366 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2367 | txn->status = 400; |
| 2368 | req->analysers = 0; |
| 2369 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2370 | s->fe->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2371 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2372 | return_prx_cond: |
| 2373 | if (!(s->flags & SN_ERR_MASK)) |
| 2374 | s->flags |= SN_ERR_PRXCOND; |
| 2375 | if (!(s->flags & SN_FINST_MASK)) |
| 2376 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2377 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2378 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2379 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2380 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 2381 | * returns zero, at the beginning because it prevents any other processing |
| 2382 | * from occurring, and at the end because it terminates the request. |
| 2383 | */ |
| 2384 | int http_process_tarpit(struct session *s, struct buffer *req) |
| 2385 | { |
| 2386 | struct http_txn *txn = &s->txn; |
| 2387 | |
| 2388 | /* This connection is being tarpitted. The CLIENT side has |
| 2389 | * already set the connect expiration date to the right |
| 2390 | * timeout. We just have to check that the client is still |
| 2391 | * there and that the timeout has not expired. |
| 2392 | */ |
| 2393 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 2394 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2395 | return 0; |
| 2396 | |
| 2397 | /* We will set the queue timer to the time spent, just for |
| 2398 | * logging purposes. We fake a 500 server error, so that the |
| 2399 | * attacker will not suspect his connection has been tarpitted. |
| 2400 | * It will not cause trouble to the logs because we can exclude |
| 2401 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2402 | */ |
| 2403 | trace_term(s, TT_HTTP_SRV_2); |
| 2404 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 2405 | |
| 2406 | txn->status = 500; |
| 2407 | if (req->flags != BF_READ_ERROR) |
| 2408 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 2409 | |
| 2410 | req->analysers = 0; |
| 2411 | req->analyse_exp = TICK_ETERNITY; |
| 2412 | |
| 2413 | s->fe->failed_req++; |
| 2414 | if (!(s->flags & SN_ERR_MASK)) |
| 2415 | s->flags |= SN_ERR_PRXCOND; |
| 2416 | if (!(s->flags & SN_FINST_MASK)) |
| 2417 | s->flags |= SN_FINST_T; |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2421 | /* This function is an analyser which processes the HTTP request body. It looks |
| 2422 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 2423 | * must only be called after the standard HTTP request processing has occurred, |
| 2424 | * because it expects the request to be parsed. It returns zero if it needs to |
| 2425 | * read more data, or 1 once it has completed its analysis. |
| 2426 | */ |
| 2427 | int http_process_request_body(struct session *s, struct buffer *req) |
| 2428 | { |
| 2429 | struct http_msg *msg = &s->txn.req; |
| 2430 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2431 | long long limit = s->be->url_param_post_limit; |
| 2432 | struct hdr_ctx ctx; |
| 2433 | |
| 2434 | /* We have to parse the HTTP request body to find any required data. |
| 2435 | * "balance url_param check_post" should have been the only way to get |
| 2436 | * into this. We were brought here after HTTP header analysis, so all |
| 2437 | * related structures are ready. |
| 2438 | */ |
| 2439 | |
| 2440 | ctx.idx = 0; |
| 2441 | |
| 2442 | /* now if we have a length, we'll take the hint */ |
| 2443 | http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx); |
| 2444 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2445 | unsigned int chunk = 0; |
| 2446 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2447 | char c = msg->sol[body]; |
| 2448 | if (ishex(c)) { |
| 2449 | unsigned int hex = toupper(c) - '0'; |
| 2450 | if (hex > 9) |
| 2451 | hex -= 'A' - '9' - 1; |
| 2452 | chunk = (chunk << 4) | hex; |
| 2453 | } else |
| 2454 | break; |
| 2455 | body++; |
| 2456 | } |
| 2457 | if (body + 2 >= req->l) /* we want CRLF too */ |
| 2458 | goto http_body_end; /* end of buffer? data missing! */ |
| 2459 | |
| 2460 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
| 2461 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
| 2462 | |
| 2463 | body += 2; // skip CRLF |
| 2464 | |
| 2465 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2466 | * 1. how much entity data do we have? new var |
| 2467 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2468 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2469 | */ |
| 2470 | |
| 2471 | if (chunk < limit) |
| 2472 | limit = chunk; /* only reading one chunk */ |
| 2473 | } else { |
| 2474 | if (msg->hdr_content_len < limit) |
| 2475 | limit = msg->hdr_content_len; |
| 2476 | } |
| 2477 | |
| 2478 | http_body_end: |
| 2479 | /* we leave once we know we have nothing left to do. This means that we have |
| 2480 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2481 | * buffer closed). |
| 2482 | */ |
| 2483 | if (req->l - body >= limit || /* enough bytes! */ |
| 2484 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
| 2485 | tick_is_expired(req->analyse_exp, now_ms)) { |
| 2486 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 2487 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2488 | req->analysers &= ~AN_REQ_HTTP_BODY; |
| 2489 | req->analyse_exp = TICK_ETERNITY; |
| 2490 | return 1; |
| 2491 | } |
| 2492 | else { |
| 2493 | /* Not enough data. We'll re-use the http-request |
| 2494 | * timeout here. Ideally, we should set the timeout |
| 2495 | * relative to the accept() date. We just set the |
| 2496 | * request timeout once at the beginning of the |
| 2497 | * request. |
| 2498 | */ |
| 2499 | buffer_write_dis(req); |
| 2500 | if (!tick_isset(req->analyse_exp)) |
| 2501 | req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | } |
| 2505 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2506 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2507 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2508 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2509 | * t->rep->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2510 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2511 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2512 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2513 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2514 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2515 | struct buffer *req = t->req; |
| 2516 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2517 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2518 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2519 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2520 | t, |
| 2521 | rep, |
| 2522 | rep->rex, rep->wex, |
| 2523 | rep->flags, |
| 2524 | rep->l, |
| 2525 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2526 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2527 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2528 | /* |
| 2529 | * Now parse the partial (or complete) lines. |
| 2530 | * We will check the response syntax, and also join multi-line |
| 2531 | * headers. An index of all the lines will be elaborated while |
| 2532 | * parsing. |
| 2533 | * |
| 2534 | * For the parsing, we use a 28 states FSM. |
| 2535 | * |
| 2536 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2537 | * rep->data + rep->som = beginning of response |
| 2538 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2539 | * rep->data + rep->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2540 | * rep->lr = first non-visited byte |
| 2541 | * rep->r = end of data |
| 2542 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2544 | int cur_idx; |
| 2545 | struct http_msg *msg = &txn->rsp; |
| 2546 | struct proxy *cur_proxy; |
| 2547 | |
| 2548 | if (likely(rep->lr < rep->r)) |
| 2549 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2550 | |
| 2551 | /* 1: we might have to print this header in debug mode */ |
| 2552 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2553 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2554 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2555 | char *eol, *sol; |
| 2556 | |
| 2557 | sol = rep->data + msg->som; |
| 2558 | eol = sol + msg->sl.rq.l; |
| 2559 | debug_hdr("srvrep", t, sol, eol); |
| 2560 | |
| 2561 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2562 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2563 | |
| 2564 | while (cur_idx) { |
| 2565 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2566 | debug_hdr("srvhdr", t, sol, eol); |
| 2567 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2568 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2569 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2570 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2571 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2572 | /* |
| 2573 | * Now we quickly check if we have found a full valid response. |
| 2574 | * If not so, we check the FD and buffer states before leaving. |
| 2575 | * A full response is indicated by the fact that we have seen |
| 2576 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2577 | * responses are checked first. |
| 2578 | * |
| 2579 | * Depending on whether the client is still there or not, we |
| 2580 | * may send an error response back or not. Note that normally |
| 2581 | * we should only check for HTTP status there, and check I/O |
| 2582 | * errors somewhere else. |
| 2583 | */ |
| 2584 | |
| 2585 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2586 | /* Invalid response */ |
| 2587 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2588 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2589 | //buffer_shutr(rep); |
| 2590 | //buffer_shutw(req); |
| 2591 | //fd_delete(req->cons->fd); |
| 2592 | //req->cons->state = SI_ST_CLO; |
| 2593 | buffer_shutr_now(rep); |
| 2594 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2595 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2596 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2597 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2598 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2599 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2600 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2601 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2602 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2603 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2604 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2605 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2606 | if (!(t->flags & SN_FINST_MASK)) |
| 2607 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2609 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2610 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2611 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2612 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2613 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2614 | /* too large response does not fit in buffer. */ |
| 2615 | else if (rep->flags & BF_FULL) { |
| 2616 | goto hdr_response_bad; |
| 2617 | } |
| 2618 | /* read error */ |
| 2619 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2620 | buffer_shutr_now(rep); |
| 2621 | buffer_shutw_now(req); |
| 2622 | //fd_delete(req->cons->fd); |
| 2623 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2624 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2625 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2626 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2627 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2628 | //} |
| 2629 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2630 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2631 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2632 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2633 | if (!(t->flags & SN_ERR_MASK)) |
| 2634 | t->flags |= SN_ERR_SRVCL; |
| 2635 | if (!(t->flags & SN_FINST_MASK)) |
| 2636 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2638 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2639 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2640 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2641 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2642 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2643 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2644 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2645 | buffer_shutr_now(rep); |
| 2646 | buffer_shutw_now(req); |
| 2647 | //fd_delete(req->cons->fd); |
| 2648 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2649 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2650 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2651 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2652 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2653 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2654 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2655 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2656 | txn->status = 504; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2657 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_504)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2658 | if (!(t->flags & SN_ERR_MASK)) |
| 2659 | t->flags |= SN_ERR_SRVTO; |
| 2660 | if (!(t->flags & SN_FINST_MASK)) |
| 2661 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2663 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2664 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2665 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2666 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2667 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2668 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2669 | buffer_shutr_now(rep); |
| 2670 | buffer_shutw_now(req); |
| 2671 | //fd_delete(req->cons->fd); |
| 2672 | //req->cons->state = SI_ST_CLO; |
| 2673 | if (t->srv) { |
| 2674 | //t->srv->cur_sess--; |
| 2675 | t->srv->failed_resp++; |
| 2676 | //sess_change_server(t, NULL); |
| 2677 | } |
| 2678 | t->be->failed_resp++; |
| 2679 | rep->analysers = 0; |
| 2680 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2681 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2682 | if (!(t->flags & SN_ERR_MASK)) |
| 2683 | t->flags |= SN_ERR_SRVCL; |
| 2684 | if (!(t->flags & SN_FINST_MASK)) |
| 2685 | t->flags |= SN_FINST_H; |
| 2686 | |
| 2687 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2688 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2689 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2690 | return 0; |
| 2691 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2692 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2693 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2694 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2695 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2696 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2697 | /***************************************************************** |
| 2698 | * More interesting part now : we know that we have a complete * |
| 2699 | * response which at least looks like HTTP. We have an indicator * |
| 2700 | * of each header's length, so we can parse them quickly. * |
| 2701 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2702 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2703 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2704 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2705 | /* ensure we keep this pointer to the beginning of the message */ |
| 2706 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2707 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2708 | /* |
| 2709 | * 1: get the status code and check for cacheability. |
| 2710 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2711 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2712 | t->logs.logwait &= ~LW_RESP; |
| 2713 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2714 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2715 | switch (txn->status) { |
| 2716 | case 200: |
| 2717 | case 203: |
| 2718 | case 206: |
| 2719 | case 300: |
| 2720 | case 301: |
| 2721 | case 410: |
| 2722 | /* RFC2616 @13.4: |
| 2723 | * "A response received with a status code of |
| 2724 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2725 | * by a cache (...) unless a cache-control |
| 2726 | * directive prohibits caching." |
| 2727 | * |
| 2728 | * RFC2616 @9.5: POST method : |
| 2729 | * "Responses to this method are not cacheable, |
| 2730 | * unless the response includes appropriate |
| 2731 | * Cache-Control or Expires header fields." |
| 2732 | */ |
| 2733 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2734 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2735 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2736 | break; |
| 2737 | default: |
| 2738 | break; |
| 2739 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2741 | /* |
| 2742 | * 2: we may need to capture headers |
| 2743 | */ |
| 2744 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2745 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2746 | txn->rsp.cap, t->fe->rsp_cap); |
| 2747 | |
| 2748 | /* |
| 2749 | * 3: we will have to evaluate the filters. |
| 2750 | * As opposed to version 1.2, now they will be evaluated in the |
| 2751 | * filters order and not in the header order. This means that |
| 2752 | * each filter has to be validated among all headers. |
| 2753 | * |
| 2754 | * Filters are tried with ->be first, then with ->fe if it is |
| 2755 | * different from ->be. |
| 2756 | */ |
| 2757 | |
| 2758 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2759 | |
| 2760 | cur_proxy = t->be; |
| 2761 | while (1) { |
| 2762 | struct proxy *rule_set = cur_proxy; |
| 2763 | |
| 2764 | /* try headers filters */ |
| 2765 | if (rule_set->rsp_exp != NULL) { |
| 2766 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2767 | return_bad_resp: |
| 2768 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2769 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2770 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2771 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2772 | } |
| 2773 | cur_proxy->failed_resp++; |
| 2774 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2775 | buffer_shutr_now(rep); |
| 2776 | buffer_shutw_now(req); |
| 2777 | //fd_delete(req->cons->fd); |
| 2778 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2779 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2780 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2781 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2782 | if (!(t->flags & SN_ERR_MASK)) |
| 2783 | t->flags |= SN_ERR_PRXCOND; |
| 2784 | if (!(t->flags & SN_FINST_MASK)) |
| 2785 | t->flags |= SN_FINST_H; |
| 2786 | /* We used to have a free connection slot. Since we'll never use it, |
| 2787 | * we have to inform the server that it may be used by another session. |
| 2788 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2789 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2790 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2791 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2792 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2793 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2794 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2795 | /* has the response been denied ? */ |
| 2796 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2797 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2798 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2799 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2800 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2801 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2802 | cur_proxy->denied_resp++; |
| 2803 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2804 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2805 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2806 | /* We might have to check for "Connection:" */ |
| 2807 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2808 | !(t->flags & SN_CONN_CLOSED)) { |
| 2809 | char *cur_ptr, *cur_end, *cur_next; |
| 2810 | int cur_idx, old_idx, delta, val; |
| 2811 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2812 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2813 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2814 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2815 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2816 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2817 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2818 | cur_ptr = cur_next; |
| 2819 | cur_end = cur_ptr + cur_hdr->len; |
| 2820 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2822 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2823 | if (val) { |
| 2824 | /* 3 possibilities : |
| 2825 | * - we have already set Connection: close, |
| 2826 | * so we remove this line. |
| 2827 | * - we have not yet set Connection: close, |
| 2828 | * but this line indicates close. We leave |
| 2829 | * it untouched and set the flag. |
| 2830 | * - we have not yet set Connection: close, |
| 2831 | * and this line indicates non-close. We |
| 2832 | * replace it. |
| 2833 | */ |
| 2834 | if (t->flags & SN_CONN_CLOSED) { |
| 2835 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2836 | txn->rsp.eoh += delta; |
| 2837 | cur_next += delta; |
| 2838 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2839 | txn->hdr_idx.used--; |
| 2840 | cur_hdr->len = 0; |
| 2841 | } else { |
| 2842 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2843 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2844 | "close", 5); |
| 2845 | cur_next += delta; |
| 2846 | cur_hdr->len += delta; |
| 2847 | txn->rsp.eoh += delta; |
| 2848 | } |
| 2849 | t->flags |= SN_CONN_CLOSED; |
| 2850 | } |
| 2851 | } |
| 2852 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2853 | } |
| 2854 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2856 | /* add response headers from the rule sets in the same order */ |
| 2857 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 2858 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2859 | rule_set->rsp_add[cur_idx])) < 0) |
| 2860 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2863 | /* check whether we're already working on the frontend */ |
| 2864 | if (cur_proxy == t->fe) |
| 2865 | break; |
| 2866 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2867 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2868 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2869 | /* |
| 2870 | * 4: check for server cookie. |
| 2871 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2872 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2873 | || (t->be->options & PR_O_CHK_CACHE)) |
| 2874 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2876 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2877 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2878 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2879 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2880 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 2881 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2882 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2883 | /* |
| 2884 | * 6: add server cookie in the response if needed |
| 2885 | */ |
| 2886 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2887 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 2888 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2889 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2890 | /* the server is known, it's not the one the client requested, we have to |
| 2891 | * insert a set-cookie here, except if we want to insert only on POST |
| 2892 | * requests and this one isn't. Note that servers which don't have cookies |
| 2893 | * (eg: some backup servers) will return a full cookie removal request. |
| 2894 | */ |
| 2895 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 2896 | t->be->cookie_name, |
| 2897 | t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2899 | if (t->be->cookie_domain) |
| 2900 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2901 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2902 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2903 | trash, len)) < 0) |
| 2904 | goto return_bad_resp; |
| 2905 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2906 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2907 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2908 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2909 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2910 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2911 | */ |
| 2912 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2913 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2914 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2915 | |
| 2916 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2917 | "Cache-control: private", 22)) < 0) |
| 2918 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2919 | } |
| 2920 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2921 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2922 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2923 | /* |
| 2924 | * 7: check if result will be cacheable with a cookie. |
| 2925 | * We'll block the response if security checks have caught |
| 2926 | * nasty things such as a cacheable cookie. |
| 2927 | */ |
| 2928 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 2929 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 2930 | (t->be->options & PR_O_CHK_CACHE)) { |
| 2931 | |
| 2932 | /* we're in presence of a cacheable response containing |
| 2933 | * a set-cookie header. We'll block it as requested by |
| 2934 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2935 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2936 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2937 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2938 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2939 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2940 | } |
| 2941 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2942 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2943 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2944 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 2945 | send_log(t->be, LOG_ALERT, |
| 2946 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2947 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 2948 | goto return_srv_prx_502; |
| 2949 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2950 | |
| 2951 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2952 | * 8: add "Connection: close" if needed and not yet set. |
| 2953 | * Note that we do not need to add it in case of HTTP/1.0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2954 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2955 | if (!(t->flags & SN_CONN_CLOSED) && |
| 2956 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 2957 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 2958 | unlikely(req->data[msg->som + 7] != '0')) && |
| 2959 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2960 | "Connection: close", 17)) < 0) |
| 2961 | goto return_bad_resp; |
| 2962 | t->flags |= SN_CONN_CLOSED; |
| 2963 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2964 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2965 | /************************************************************* |
| 2966 | * OK, that's finished for the headers. We have done what we * |
| 2967 | * could. Let's switch to the DATA state. * |
| 2968 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2969 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2970 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2971 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2972 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2973 | #ifdef CONFIG_HAP_TCPSPLICE |
| 2974 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 2975 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2976 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2977 | } |
| 2978 | #endif |
| 2979 | /* if the user wants to log as soon as possible, without counting |
| 2980 | * bytes from the server, then this is the right moment. We have |
| 2981 | * to temporarily assign bytes_out to log what we currently have. |
| 2982 | */ |
| 2983 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 2984 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 2985 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2986 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2987 | t->logs.bytes_out = 0; |
| 2988 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2989 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2990 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 2991 | * otherwise we would not let the client side wake up. |
| 2992 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2993 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2994 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2995 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2996 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2997 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2998 | * probably reduce one day's debugging session. |
| 2999 | */ |
| 3000 | #ifdef DEBUG_DEV |
| 3001 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3002 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3003 | __FILE__, __LINE__, rep->analysers); |
| 3004 | ABORT_NOW(); |
| 3005 | } |
| 3006 | #endif |
| 3007 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3008 | return 0; |
| 3009 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3010 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3011 | /* |
| 3012 | * Produces data for the session <s> depending on its source. Expects to be |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3013 | * called with client socket shut down on input. Right now, only statistics can |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3014 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 3015 | * buffer, which it uses to keep on being called when there is free space in |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3016 | * the buffer, or simply by letting an empty buffer upon return. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3017 | */ |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3018 | void produce_content(struct session *s, struct buffer *rep) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3019 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3020 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3021 | buffer_stop_hijack(rep); |
| 3022 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3023 | } |
| 3024 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3025 | /* dump server statistics */ |
Willy Tarreau | 0a46489 | 2008-12-07 18:30:00 +0100 | [diff] [blame] | 3026 | int ret = stats_dump_http(s, rep, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3027 | if (ret >= 0) |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3028 | return; |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3029 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3030 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3031 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3032 | /* unknown data source or internal error */ |
| 3033 | s->txn.status = 500; |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3034 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3035 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3036 | if (!(s->flags & SN_ERR_MASK)) |
| 3037 | s->flags |= SN_ERR_PRXCOND; |
| 3038 | if (!(s->flags & SN_FINST_MASK)) |
| 3039 | s->flags |= SN_FINST_R; |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 3040 | buffer_stop_hijack(rep); |
| 3041 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3045 | /* Iterate the same filter through all request headers. |
| 3046 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3047 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3048 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3049 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3050 | int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3051 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3052 | char term; |
| 3053 | char *cur_ptr, *cur_end, *cur_next; |
| 3054 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3055 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3056 | struct hdr_idx_elem *cur_hdr; |
| 3057 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3058 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3059 | last_hdr = 0; |
| 3060 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3061 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3062 | old_idx = 0; |
| 3063 | |
| 3064 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3065 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3066 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3067 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3068 | (exp->action == ACT_ALLOW || |
| 3069 | exp->action == ACT_DENY || |
| 3070 | exp->action == ACT_TARPIT)) |
| 3071 | return 0; |
| 3072 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3073 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3074 | if (!cur_idx) |
| 3075 | break; |
| 3076 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3077 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3078 | cur_ptr = cur_next; |
| 3079 | cur_end = cur_ptr + cur_hdr->len; |
| 3080 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3081 | |
| 3082 | /* Now we have one header between cur_ptr and cur_end, |
| 3083 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3084 | */ |
| 3085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3086 | /* The annoying part is that pattern matching needs |
| 3087 | * that we modify the contents to null-terminate all |
| 3088 | * strings before testing them. |
| 3089 | */ |
| 3090 | |
| 3091 | term = *cur_end; |
| 3092 | *cur_end = '\0'; |
| 3093 | |
| 3094 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3095 | switch (exp->action) { |
| 3096 | case ACT_SETBE: |
| 3097 | /* It is not possible to jump a second time. |
| 3098 | * FIXME: should we return an HTTP/500 here so that |
| 3099 | * the admin knows there's a problem ? |
| 3100 | */ |
| 3101 | if (t->be != t->fe) |
| 3102 | break; |
| 3103 | |
| 3104 | /* Swithing Proxy */ |
| 3105 | t->be = (struct proxy *) exp->replace; |
| 3106 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3107 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3108 | * because we have associated req_cap and rsp_cap to the |
| 3109 | * frontend, and the beconn will be updated later. |
| 3110 | */ |
| 3111 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3112 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3113 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3114 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3115 | last_hdr = 1; |
| 3116 | break; |
| 3117 | |
| 3118 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3119 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3120 | last_hdr = 1; |
| 3121 | break; |
| 3122 | |
| 3123 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3124 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3125 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3126 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3127 | break; |
| 3128 | |
| 3129 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3130 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3131 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3132 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3133 | break; |
| 3134 | |
| 3135 | case ACT_REPLACE: |
| 3136 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3137 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3138 | /* FIXME: if the user adds a newline in the replacement, the |
| 3139 | * index will not be recalculated for now, and the new line |
| 3140 | * will not be counted as a new header. |
| 3141 | */ |
| 3142 | |
| 3143 | cur_end += delta; |
| 3144 | cur_next += delta; |
| 3145 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3146 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3147 | break; |
| 3148 | |
| 3149 | case ACT_REMOVE: |
| 3150 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3151 | cur_next += delta; |
| 3152 | |
| 3153 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3154 | txn->req.eoh += delta; |
| 3155 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3156 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3157 | cur_hdr->len = 0; |
| 3158 | cur_end = NULL; /* null-term has been rewritten */ |
| 3159 | break; |
| 3160 | |
| 3161 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3162 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3163 | if (cur_end) |
| 3164 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3165 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3166 | /* keep the link from this header to next one in case of later |
| 3167 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3168 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3169 | old_idx = cur_idx; |
| 3170 | } |
| 3171 | return 0; |
| 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | /* Apply the filter to the request line. |
| 3176 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3177 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3178 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3179 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3180 | */ |
| 3181 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3182 | { |
| 3183 | char term; |
| 3184 | char *cur_ptr, *cur_end; |
| 3185 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3186 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3187 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3189 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3190 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3191 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3192 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3193 | (exp->action == ACT_ALLOW || |
| 3194 | exp->action == ACT_DENY || |
| 3195 | exp->action == ACT_TARPIT)) |
| 3196 | return 0; |
| 3197 | else if (exp->action == ACT_REMOVE) |
| 3198 | return 0; |
| 3199 | |
| 3200 | done = 0; |
| 3201 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3202 | cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3203 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3204 | |
| 3205 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3206 | |
| 3207 | /* The annoying part is that pattern matching needs |
| 3208 | * that we modify the contents to null-terminate all |
| 3209 | * strings before testing them. |
| 3210 | */ |
| 3211 | |
| 3212 | term = *cur_end; |
| 3213 | *cur_end = '\0'; |
| 3214 | |
| 3215 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3216 | switch (exp->action) { |
| 3217 | case ACT_SETBE: |
| 3218 | /* It is not possible to jump a second time. |
| 3219 | * FIXME: should we return an HTTP/500 here so that |
| 3220 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3221 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3222 | if (t->be != t->fe) |
| 3223 | break; |
| 3224 | |
| 3225 | /* Swithing Proxy */ |
| 3226 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3227 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3228 | /* right now, the backend switch is not too much complicated |
| 3229 | * because we have associated req_cap and rsp_cap to the |
| 3230 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3231 | */ |
| 3232 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3233 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3234 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3235 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3236 | done = 1; |
| 3237 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3238 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3239 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3240 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3241 | done = 1; |
| 3242 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3243 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3244 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3245 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3246 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3247 | done = 1; |
| 3248 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3249 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3250 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3251 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3252 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3253 | done = 1; |
| 3254 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3255 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3256 | case ACT_REPLACE: |
| 3257 | *cur_end = term; /* restore the string terminator */ |
| 3258 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3259 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3260 | /* FIXME: if the user adds a newline in the replacement, the |
| 3261 | * index will not be recalculated for now, and the new line |
| 3262 | * will not be counted as a new header. |
| 3263 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3264 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3265 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3266 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3267 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3268 | txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3269 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3270 | HTTP_MSG_RQMETH, |
| 3271 | cur_ptr, cur_end + 1, |
| 3272 | NULL, NULL); |
| 3273 | if (unlikely(!cur_end)) |
| 3274 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3275 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3276 | /* we have a full request and we know that we have either a CR |
| 3277 | * or an LF at <ptr>. |
| 3278 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3279 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3280 | hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3281 | /* there is no point trying this regex on headers */ |
| 3282 | return 1; |
| 3283 | } |
| 3284 | } |
| 3285 | *cur_end = term; /* restore the string terminator */ |
| 3286 | return done; |
| 3287 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3288 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3289 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3291 | /* |
| 3292 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3293 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3294 | * unparsable request. Since it can manage the switch to another backend, it |
| 3295 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3296 | */ |
| 3297 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3298 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3299 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3300 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3301 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3302 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3304 | /* |
| 3305 | * The interleaving of transformations and verdicts |
| 3306 | * makes it difficult to decide to continue or stop |
| 3307 | * the evaluation. |
| 3308 | */ |
| 3309 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3310 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3311 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3312 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3313 | exp = exp->next; |
| 3314 | continue; |
| 3315 | } |
| 3316 | |
| 3317 | /* Apply the filter to the request line. */ |
| 3318 | ret = apply_filter_to_req_line(t, req, exp); |
| 3319 | if (unlikely(ret < 0)) |
| 3320 | return -1; |
| 3321 | |
| 3322 | if (likely(ret == 0)) { |
| 3323 | /* The filter did not match the request, it can be |
| 3324 | * iterated through all headers. |
| 3325 | */ |
| 3326 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3327 | } |
| 3328 | exp = exp->next; |
| 3329 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3330 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3334 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3335 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3336 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3337 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3338 | */ |
| 3339 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3340 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3341 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3342 | char *p1, *p2, *p3, *p4; |
| 3343 | char *del_colon, *del_cookie, *colon; |
| 3344 | int app_cookies; |
| 3345 | |
| 3346 | appsess *asession_temp = NULL; |
| 3347 | appsess local_asession; |
| 3348 | |
| 3349 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3350 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3351 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3352 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3353 | * we start with the start line. |
| 3354 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3355 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3356 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3357 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3358 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3359 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3360 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3361 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3362 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3363 | cur_ptr = cur_next; |
| 3364 | cur_end = cur_ptr + cur_hdr->len; |
| 3365 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3366 | |
| 3367 | /* We have one full header between cur_ptr and cur_end, and the |
| 3368 | * next header starts at cur_next. We're only interested in |
| 3369 | * "Cookie:" headers. |
| 3370 | */ |
| 3371 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3372 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3373 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3374 | old_idx = cur_idx; |
| 3375 | continue; |
| 3376 | } |
| 3377 | |
| 3378 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3379 | * attributes whose name begin with a '$', and associate them with |
| 3380 | * the right cookie, if we want to delete this cookie. |
| 3381 | * So there are 3 cases for each cookie read : |
| 3382 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3383 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3384 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3385 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3386 | * "special" cookie. |
| 3387 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3388 | * remove it. If no application cookie persists in the header, we |
| 3389 | * *MUST* delete it |
| 3390 | */ |
| 3391 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3392 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3393 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3394 | /* del_cookie == NULL => nothing to be deleted */ |
| 3395 | del_colon = del_cookie = NULL; |
| 3396 | app_cookies = 0; |
| 3397 | |
| 3398 | while (p1 < cur_end) { |
| 3399 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3400 | while (p1 < cur_end) { |
| 3401 | if (*p1 == ';' || *p1 == ',') |
| 3402 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3403 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3404 | break; |
| 3405 | p1++; |
| 3406 | } |
| 3407 | |
| 3408 | if (p1 == cur_end) |
| 3409 | break; |
| 3410 | |
| 3411 | /* p1 is at the beginning of the cookie name */ |
| 3412 | p2 = p1; |
| 3413 | while (p2 < cur_end && *p2 != '=') |
| 3414 | p2++; |
| 3415 | |
| 3416 | if (p2 == cur_end) |
| 3417 | break; |
| 3418 | |
| 3419 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3420 | if (p3 == cur_end) |
| 3421 | break; |
| 3422 | |
| 3423 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3424 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3425 | p4++; |
| 3426 | |
| 3427 | /* here, we have the cookie name between p1 and p2, |
| 3428 | * and its value between p3 and p4. |
| 3429 | * we can process it : |
| 3430 | * |
| 3431 | * Cookie: NAME=VALUE; |
| 3432 | * | || || | |
| 3433 | * | || || +--> p4 |
| 3434 | * | || |+-------> p3 |
| 3435 | * | || +--------> p2 |
| 3436 | * | |+------------> p1 |
| 3437 | * | +-------------> colon |
| 3438 | * +--------------------> cur_ptr |
| 3439 | */ |
| 3440 | |
| 3441 | if (*p1 == '$') { |
| 3442 | /* skip this one */ |
| 3443 | } |
| 3444 | else { |
| 3445 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3446 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3447 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3448 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3449 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3450 | int log_len = p4 - p1; |
| 3451 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3452 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3453 | Alert("HTTP logging : out of memory.\n"); |
| 3454 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3455 | if (log_len > t->fe->capture_len) |
| 3456 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3457 | memcpy(txn->cli_cookie, p1, log_len); |
| 3458 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3462 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3463 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3464 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3465 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3466 | char *delim; |
| 3467 | |
| 3468 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3469 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3470 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3471 | * |
| 3472 | * Cookie: NAME=SRV~VALUE; |
| 3473 | * | || || | | |
| 3474 | * | || || | +--> p4 |
| 3475 | * | || || +--------> delim |
| 3476 | * | || |+-----------> p3 |
| 3477 | * | || +------------> p2 |
| 3478 | * | |+----------------> p1 |
| 3479 | * | +-----------------> colon |
| 3480 | * +------------------------> cur_ptr |
| 3481 | */ |
| 3482 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3483 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3484 | for (delim = p3; delim < p4; delim++) |
| 3485 | if (*delim == COOKIE_DELIM) |
| 3486 | break; |
| 3487 | } |
| 3488 | else |
| 3489 | delim = p4; |
| 3490 | |
| 3491 | |
| 3492 | /* Here, we'll look for the first running server which supports the cookie. |
| 3493 | * This allows to share a same cookie between several servers, for example |
| 3494 | * to dedicate backup servers to specific servers only. |
| 3495 | * However, to prevent clients from sticking to cookie-less backup server |
| 3496 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3497 | * empty cookies and mark them as invalid. |
| 3498 | */ |
| 3499 | if (delim == p3) |
| 3500 | srv = NULL; |
| 3501 | |
| 3502 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3503 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3504 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3505 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3506 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3507 | txn->flags &= ~TX_CK_MASK; |
| 3508 | txn->flags |= TX_CK_VALID; |
| 3509 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3510 | t->srv = srv; |
| 3511 | break; |
| 3512 | } else { |
| 3513 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3514 | txn->flags &= ~TX_CK_MASK; |
| 3515 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3516 | } |
| 3517 | } |
| 3518 | srv = srv->next; |
| 3519 | } |
| 3520 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3521 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3522 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3523 | txn->flags &= ~TX_CK_MASK; |
| 3524 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /* depending on the cookie mode, we may have to either : |
| 3528 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3529 | * the server never sees it ; |
| 3530 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3531 | * application cookie so that it does not get accidentely removed later, |
| 3532 | * if we're in cookie prefix mode |
| 3533 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3534 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3535 | int delta; /* negative */ |
| 3536 | |
| 3537 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3538 | p4 += delta; |
| 3539 | cur_end += delta; |
| 3540 | cur_next += delta; |
| 3541 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3542 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3543 | |
| 3544 | del_cookie = del_colon = NULL; |
| 3545 | app_cookies++; /* protect the header from deletion */ |
| 3546 | } |
| 3547 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3548 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3549 | del_cookie = p1; |
| 3550 | del_colon = colon; |
| 3551 | } |
| 3552 | } else { |
| 3553 | /* now we know that we must keep this cookie since it's |
| 3554 | * not ours. But if we wanted to delete our cookie |
| 3555 | * earlier, we cannot remove the complete header, but we |
| 3556 | * can remove the previous block itself. |
| 3557 | */ |
| 3558 | app_cookies++; |
| 3559 | |
| 3560 | if (del_cookie != NULL) { |
| 3561 | int delta; /* negative */ |
| 3562 | |
| 3563 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3564 | p4 += delta; |
| 3565 | cur_end += delta; |
| 3566 | cur_next += delta; |
| 3567 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3568 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3569 | del_cookie = del_colon = NULL; |
| 3570 | } |
| 3571 | } |
| 3572 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3573 | if ((t->be->appsession_name != NULL) && |
| 3574 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3575 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3576 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3577 | /* Cool... it's the right one */ |
| 3578 | |
| 3579 | asession_temp = &local_asession; |
| 3580 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3581 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3582 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3583 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3584 | return; |
| 3585 | } |
| 3586 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3587 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3588 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3589 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3590 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3591 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3592 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3593 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3594 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3595 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3596 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3597 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3598 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3599 | return; |
| 3600 | } |
| 3601 | |
| 3602 | asession_temp->sessid = local_asession.sessid; |
| 3603 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3604 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3605 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3606 | } else { |
| 3607 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3608 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3609 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3610 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3611 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3612 | Alert("Found Application Session without matching server.\n"); |
| 3613 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3614 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3615 | while (srv) { |
| 3616 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3617 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3618 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3619 | txn->flags &= ~TX_CK_MASK; |
| 3620 | txn->flags |= TX_CK_VALID; |
| 3621 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3622 | t->srv = srv; |
| 3623 | break; |
| 3624 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3625 | txn->flags &= ~TX_CK_MASK; |
| 3626 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | srv = srv->next; |
| 3630 | }/* end while(srv) */ |
| 3631 | }/* end else if server == NULL */ |
| 3632 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3633 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3634 | asession_temp->request_count++; |
| 3635 | #if defined(DEBUG_HASH) |
| 3636 | Alert("manage_client_side_cookies\n"); |
| 3637 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 3638 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3639 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 3640 | } |
| 3641 | |
| 3642 | /* we'll have to look for another cookie ... */ |
| 3643 | p1 = p4; |
| 3644 | } /* while (p1 < cur_end) */ |
| 3645 | |
| 3646 | /* There's no more cookie on this line. |
| 3647 | * We may have marked the last one(s) for deletion. |
| 3648 | * We must do this now in two ways : |
| 3649 | * - if there is no app cookie, we simply delete the header ; |
| 3650 | * - if there are app cookies, we must delete the end of the |
| 3651 | * string properly, including the colon/semi-colon before |
| 3652 | * the cookie name. |
| 3653 | */ |
| 3654 | if (del_cookie != NULL) { |
| 3655 | int delta; |
| 3656 | if (app_cookies) { |
| 3657 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 3658 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3659 | cur_hdr->len += delta; |
| 3660 | } else { |
| 3661 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3662 | |
| 3663 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3664 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3665 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3666 | cur_hdr->len = 0; |
| 3667 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 3668 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3669 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | /* keep the link from this header to next one */ |
| 3673 | old_idx = cur_idx; |
| 3674 | } /* end of cookie processing on this header */ |
| 3675 | } |
| 3676 | |
| 3677 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3678 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 3679 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 3680 | */ |
| 3681 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3682 | { |
| 3683 | char term; |
| 3684 | char *cur_ptr, *cur_end, *cur_next; |
| 3685 | int cur_idx, old_idx, last_hdr; |
| 3686 | struct http_txn *txn = &t->txn; |
| 3687 | struct hdr_idx_elem *cur_hdr; |
| 3688 | int len, delta; |
| 3689 | |
| 3690 | last_hdr = 0; |
| 3691 | |
| 3692 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3693 | old_idx = 0; |
| 3694 | |
| 3695 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3696 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3697 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3698 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3699 | (exp->action == ACT_ALLOW || |
| 3700 | exp->action == ACT_DENY)) |
| 3701 | return 0; |
| 3702 | |
| 3703 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 3704 | if (!cur_idx) |
| 3705 | break; |
| 3706 | |
| 3707 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3708 | cur_ptr = cur_next; |
| 3709 | cur_end = cur_ptr + cur_hdr->len; |
| 3710 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3711 | |
| 3712 | /* Now we have one header between cur_ptr and cur_end, |
| 3713 | * and the next header starts at cur_next. |
| 3714 | */ |
| 3715 | |
| 3716 | /* The annoying part is that pattern matching needs |
| 3717 | * that we modify the contents to null-terminate all |
| 3718 | * strings before testing them. |
| 3719 | */ |
| 3720 | |
| 3721 | term = *cur_end; |
| 3722 | *cur_end = '\0'; |
| 3723 | |
| 3724 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3725 | switch (exp->action) { |
| 3726 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3727 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3728 | last_hdr = 1; |
| 3729 | break; |
| 3730 | |
| 3731 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3732 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3733 | last_hdr = 1; |
| 3734 | break; |
| 3735 | |
| 3736 | case ACT_REPLACE: |
| 3737 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3738 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 3739 | /* FIXME: if the user adds a newline in the replacement, the |
| 3740 | * index will not be recalculated for now, and the new line |
| 3741 | * will not be counted as a new header. |
| 3742 | */ |
| 3743 | |
| 3744 | cur_end += delta; |
| 3745 | cur_next += delta; |
| 3746 | cur_hdr->len += delta; |
| 3747 | txn->rsp.eoh += delta; |
| 3748 | break; |
| 3749 | |
| 3750 | case ACT_REMOVE: |
| 3751 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 3752 | cur_next += delta; |
| 3753 | |
| 3754 | /* FIXME: this should be a separate function */ |
| 3755 | txn->rsp.eoh += delta; |
| 3756 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3757 | txn->hdr_idx.used--; |
| 3758 | cur_hdr->len = 0; |
| 3759 | cur_end = NULL; /* null-term has been rewritten */ |
| 3760 | break; |
| 3761 | |
| 3762 | } |
| 3763 | } |
| 3764 | if (cur_end) |
| 3765 | *cur_end = term; /* restore the string terminator */ |
| 3766 | |
| 3767 | /* keep the link from this header to next one in case of later |
| 3768 | * removal of next header. |
| 3769 | */ |
| 3770 | old_idx = cur_idx; |
| 3771 | } |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
| 3775 | |
| 3776 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 3777 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3778 | * or -1 if a replacement resulted in an invalid status line. |
| 3779 | */ |
| 3780 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3781 | { |
| 3782 | char term; |
| 3783 | char *cur_ptr, *cur_end; |
| 3784 | int done; |
| 3785 | struct http_txn *txn = &t->txn; |
| 3786 | int len, delta; |
| 3787 | |
| 3788 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3789 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3790 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3791 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3792 | (exp->action == ACT_ALLOW || |
| 3793 | exp->action == ACT_DENY)) |
| 3794 | return 0; |
| 3795 | else if (exp->action == ACT_REMOVE) |
| 3796 | return 0; |
| 3797 | |
| 3798 | done = 0; |
| 3799 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3800 | cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3801 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 3802 | |
| 3803 | /* Now we have the status line between cur_ptr and cur_end */ |
| 3804 | |
| 3805 | /* The annoying part is that pattern matching needs |
| 3806 | * that we modify the contents to null-terminate all |
| 3807 | * strings before testing them. |
| 3808 | */ |
| 3809 | |
| 3810 | term = *cur_end; |
| 3811 | *cur_end = '\0'; |
| 3812 | |
| 3813 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3814 | switch (exp->action) { |
| 3815 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3816 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3817 | done = 1; |
| 3818 | break; |
| 3819 | |
| 3820 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3821 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3822 | done = 1; |
| 3823 | break; |
| 3824 | |
| 3825 | case ACT_REPLACE: |
| 3826 | *cur_end = term; /* restore the string terminator */ |
| 3827 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3828 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 3829 | /* FIXME: if the user adds a newline in the replacement, the |
| 3830 | * index will not be recalculated for now, and the new line |
| 3831 | * will not be counted as a new header. |
| 3832 | */ |
| 3833 | |
| 3834 | txn->rsp.eoh += delta; |
| 3835 | cur_end += delta; |
| 3836 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3837 | txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3838 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 3839 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3840 | cur_ptr, cur_end + 1, |
| 3841 | NULL, NULL); |
| 3842 | if (unlikely(!cur_end)) |
| 3843 | return -1; |
| 3844 | |
| 3845 | /* we have a full respnse and we know that we have either a CR |
| 3846 | * or an LF at <ptr>. |
| 3847 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3848 | txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3849 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 3850 | /* there is no point trying this regex on headers */ |
| 3851 | return 1; |
| 3852 | } |
| 3853 | } |
| 3854 | *cur_end = term; /* restore the string terminator */ |
| 3855 | return done; |
| 3856 | } |
| 3857 | |
| 3858 | |
| 3859 | |
| 3860 | /* |
| 3861 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 3862 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 3863 | * unparsable response. |
| 3864 | */ |
| 3865 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3866 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3867 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3868 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3869 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3870 | int ret; |
| 3871 | |
| 3872 | /* |
| 3873 | * The interleaving of transformations and verdicts |
| 3874 | * makes it difficult to decide to continue or stop |
| 3875 | * the evaluation. |
| 3876 | */ |
| 3877 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3878 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3879 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3880 | exp->action == ACT_PASS)) { |
| 3881 | exp = exp->next; |
| 3882 | continue; |
| 3883 | } |
| 3884 | |
| 3885 | /* Apply the filter to the status line. */ |
| 3886 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 3887 | if (unlikely(ret < 0)) |
| 3888 | return -1; |
| 3889 | |
| 3890 | if (likely(ret == 0)) { |
| 3891 | /* The filter did not match the response, it can be |
| 3892 | * iterated through all headers. |
| 3893 | */ |
| 3894 | apply_filter_to_resp_headers(t, rtr, exp); |
| 3895 | } |
| 3896 | exp = exp->next; |
| 3897 | } |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | |
| 3903 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3904 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3905 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3906 | */ |
| 3907 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 3908 | { |
| 3909 | struct http_txn *txn = &t->txn; |
| 3910 | char *p1, *p2, *p3, *p4; |
| 3911 | |
| 3912 | appsess *asession_temp = NULL; |
| 3913 | appsess local_asession; |
| 3914 | |
| 3915 | char *cur_ptr, *cur_end, *cur_next; |
| 3916 | int cur_idx, old_idx, delta; |
| 3917 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3918 | /* Iterate through the headers. |
| 3919 | * we start with the start line. |
| 3920 | */ |
| 3921 | old_idx = 0; |
| 3922 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3923 | |
| 3924 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3925 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3926 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3927 | |
| 3928 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3929 | cur_ptr = cur_next; |
| 3930 | cur_end = cur_ptr + cur_hdr->len; |
| 3931 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3932 | |
| 3933 | /* We have one full header between cur_ptr and cur_end, and the |
| 3934 | * next header starts at cur_next. We're only interested in |
| 3935 | * "Cookie:" headers. |
| 3936 | */ |
| 3937 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3938 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 3939 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3940 | old_idx = cur_idx; |
| 3941 | continue; |
| 3942 | } |
| 3943 | |
| 3944 | /* OK, right now we know we have a set-cookie at cur_ptr */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3945 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3946 | |
| 3947 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3948 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 3949 | * the cookie capture is declared in the fronend. |
| 3950 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3951 | if (t->be->cookie_name == NULL && |
| 3952 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3953 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3954 | return; |
| 3955 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3956 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3957 | |
| 3958 | while (p1 < cur_end) { /* in fact, we'll break after the first cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3959 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 3960 | break; |
| 3961 | |
| 3962 | /* p1 is at the beginning of the cookie name */ |
| 3963 | p2 = p1; |
| 3964 | |
| 3965 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 3966 | p2++; |
| 3967 | |
| 3968 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 3969 | break; |
| 3970 | |
| 3971 | p3 = p2 + 1; /* skip the '=' sign */ |
| 3972 | if (p3 == cur_end) |
| 3973 | break; |
| 3974 | |
| 3975 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3976 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3977 | p4++; |
| 3978 | |
| 3979 | /* here, we have the cookie name between p1 and p2, |
| 3980 | * and its value between p3 and p4. |
| 3981 | * we can process it. |
| 3982 | */ |
| 3983 | |
| 3984 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3985 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3986 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3987 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3988 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3989 | int log_len = p4 - p1; |
| 3990 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3991 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3992 | Alert("HTTP logging : out of memory.\n"); |
| 3993 | } |
| 3994 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3995 | if (log_len > t->fe->capture_len) |
| 3996 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3997 | memcpy(txn->srv_cookie, p1, log_len); |
| 3998 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4002 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4003 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4004 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4005 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4006 | |
| 4007 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4008 | * this occurrence because we'll insert another one later. |
| 4009 | * We'll delete it too if the "indirect" option is set and we're in |
| 4010 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4011 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4012 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4013 | /* this header must be deleted */ |
| 4014 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4015 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4016 | txn->hdr_idx.used--; |
| 4017 | cur_hdr->len = 0; |
| 4018 | cur_next += delta; |
| 4019 | txn->rsp.eoh += delta; |
| 4020 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4021 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4022 | } |
| 4023 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4024 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4025 | /* replace bytes p3->p4 with the cookie name associated |
| 4026 | * with this server since we know it. |
| 4027 | */ |
| 4028 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4029 | cur_hdr->len += delta; |
| 4030 | cur_next += delta; |
| 4031 | txn->rsp.eoh += delta; |
| 4032 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4033 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4034 | } |
| 4035 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4036 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4037 | /* insert the cookie name associated with this server |
| 4038 | * before existing cookie, and insert a delimitor between them.. |
| 4039 | */ |
| 4040 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4041 | cur_hdr->len += delta; |
| 4042 | cur_next += delta; |
| 4043 | txn->rsp.eoh += delta; |
| 4044 | |
| 4045 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4046 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4047 | } |
| 4048 | } |
| 4049 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4050 | else if ((t->be->appsession_name != NULL) && |
| 4051 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4052 | |
| 4053 | /* Cool... it's the right one */ |
| 4054 | |
| 4055 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4056 | asession_temp = &local_asession; |
| 4057 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4058 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4059 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4060 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4061 | return; |
| 4062 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4063 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4064 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4065 | asession_temp->serverid = NULL; |
| 4066 | |
| 4067 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4068 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4069 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4070 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4071 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4072 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4073 | return; |
| 4074 | } |
| 4075 | asession_temp->sessid = local_asession.sessid; |
| 4076 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4077 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4078 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4079 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4080 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4081 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4084 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4085 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4086 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4087 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4088 | return; |
| 4089 | } |
| 4090 | asession_temp->serverid[0] = '\0'; |
| 4091 | } |
| 4092 | |
| 4093 | if (asession_temp->serverid[0] == '\0') |
| 4094 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4095 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4096 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4097 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4098 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4099 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4100 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4101 | #endif |
| 4102 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4103 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4104 | } /* we're now at the end of the cookie value */ |
| 4105 | |
| 4106 | /* keep the link from this header to next one */ |
| 4107 | old_idx = cur_idx; |
| 4108 | } /* end of cookie processing on this header */ |
| 4109 | } |
| 4110 | |
| 4111 | |
| 4112 | |
| 4113 | /* |
| 4114 | * Check if response is cacheable or not. Updates t->flags. |
| 4115 | */ |
| 4116 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4117 | { |
| 4118 | struct http_txn *txn = &t->txn; |
| 4119 | char *p1, *p2; |
| 4120 | |
| 4121 | char *cur_ptr, *cur_end, *cur_next; |
| 4122 | int cur_idx; |
| 4123 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4124 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4125 | return; |
| 4126 | |
| 4127 | /* Iterate through the headers. |
| 4128 | * we start with the start line. |
| 4129 | */ |
| 4130 | cur_idx = 0; |
| 4131 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4132 | |
| 4133 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4134 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4135 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4136 | |
| 4137 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4138 | cur_ptr = cur_next; |
| 4139 | cur_end = cur_ptr + cur_hdr->len; |
| 4140 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4141 | |
| 4142 | /* We have one full header between cur_ptr and cur_end, and the |
| 4143 | * next header starts at cur_next. We're only interested in |
| 4144 | * "Cookie:" headers. |
| 4145 | */ |
| 4146 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4147 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4148 | if (val) { |
| 4149 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4150 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4151 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4152 | return; |
| 4153 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4154 | } |
| 4155 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4156 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4157 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4158 | continue; |
| 4159 | |
| 4160 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4161 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4162 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4163 | |
| 4164 | if (p1 >= cur_end) /* no more info */ |
| 4165 | continue; |
| 4166 | |
| 4167 | /* p1 is at the beginning of the value */ |
| 4168 | p2 = p1; |
| 4169 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4170 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4171 | p2++; |
| 4172 | |
| 4173 | /* we have a complete value between p1 and p2 */ |
| 4174 | if (p2 < cur_end && *p2 == '=') { |
| 4175 | /* we have something of the form no-cache="set-cookie" */ |
| 4176 | if ((cur_end - p1 >= 21) && |
| 4177 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4178 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4179 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4180 | continue; |
| 4181 | } |
| 4182 | |
| 4183 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4184 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4185 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4186 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4187 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4188 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4189 | return; |
| 4190 | } |
| 4191 | |
| 4192 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4193 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4194 | continue; |
| 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4200 | /* |
| 4201 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4202 | * If the server is found, it's assigned to the session. |
| 4203 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4204 | void get_srv_from_appsession(struct session *t, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4205 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4206 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4207 | appsess *asession_temp = NULL; |
| 4208 | appsess local_asession; |
| 4209 | char *request_line; |
| 4210 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4211 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4212 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4213 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4214 | ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line))) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4215 | return; |
| 4216 | |
| 4217 | /* skip ';' */ |
| 4218 | request_line++; |
| 4219 | |
| 4220 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4221 | if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4222 | return; |
| 4223 | |
| 4224 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4225 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4226 | |
| 4227 | /* First try if we already have an appsession */ |
| 4228 | asession_temp = &local_asession; |
| 4229 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4230 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4231 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4232 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4233 | return; |
| 4234 | } |
| 4235 | |
| 4236 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4237 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4238 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4239 | asession_temp->serverid = NULL; |
| 4240 | |
| 4241 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4242 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4243 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4244 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4245 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4246 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4247 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4248 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4249 | return; |
| 4250 | } |
| 4251 | asession_temp->sessid = local_asession.sessid; |
| 4252 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4253 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4254 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4255 | } |
| 4256 | else { |
| 4257 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4258 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4259 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4260 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4261 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4262 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4263 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4264 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4265 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4266 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4267 | #endif |
| 4268 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4269 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4270 | Alert("Found Application Session without matching server.\n"); |
| 4271 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4272 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4273 | while (srv) { |
| 4274 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4275 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4276 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4277 | txn->flags &= ~TX_CK_MASK; |
| 4278 | txn->flags |= TX_CK_VALID; |
| 4279 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4280 | t->srv = srv; |
| 4281 | break; |
| 4282 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4283 | txn->flags &= ~TX_CK_MASK; |
| 4284 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4285 | } |
| 4286 | } |
| 4287 | srv = srv->next; |
| 4288 | } |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4293 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4294 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4295 | * for the current backend, and if an authorization has been passed and is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4296 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4297 | * It is assumed that the request is either a HEAD or GET and that the |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4298 | * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4299 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4300 | * |
| 4301 | * Returns 1 if the session's state changes, otherwise 0. |
| 4302 | */ |
| 4303 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4304 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4305 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4306 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4307 | struct user_auth *user; |
| 4308 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4309 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4310 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4311 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 4312 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4313 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4314 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4315 | return 0; |
| 4316 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4317 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4318 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4319 | /* the URI is in h */ |
| 4320 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4321 | return 0; |
| 4322 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4323 | h += uri_auth->uri_len; |
| 4324 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4325 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4326 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4327 | break; |
| 4328 | } |
| 4329 | h++; |
| 4330 | } |
| 4331 | |
| 4332 | if (uri_auth->refresh) { |
| 4333 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4334 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4335 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4336 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4337 | break; |
| 4338 | } |
| 4339 | h++; |
| 4340 | } |
| 4341 | } |
| 4342 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4343 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4344 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4345 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4346 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4347 | break; |
| 4348 | } |
| 4349 | h++; |
| 4350 | } |
| 4351 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4352 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 4353 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4354 | /* we are in front of a interceptable URI. Let's check |
| 4355 | * if there's an authentication and if it's valid. |
| 4356 | */ |
| 4357 | user = uri_auth->users; |
| 4358 | if (!user) { |
| 4359 | /* no user auth required, it's OK */ |
| 4360 | authenticated = 1; |
| 4361 | } else { |
| 4362 | authenticated = 0; |
| 4363 | |
| 4364 | /* a user list is defined, we have to check. |
| 4365 | * skip 21 chars for "Authorization: Basic ". |
| 4366 | */ |
| 4367 | |
| 4368 | /* FIXME: this should move to an earlier place */ |
| 4369 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4370 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4371 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4372 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4373 | if (len > 14 && |
| 4374 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4375 | txn->auth_hdr.str = h; |
| 4376 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4377 | break; |
| 4378 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4379 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4382 | if (txn->auth_hdr.len < 21 || |
| 4383 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4384 | user = NULL; |
| 4385 | |
| 4386 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4387 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4388 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4389 | user->user_pwd, user->user_len)) { |
| 4390 | authenticated = 1; |
| 4391 | break; |
| 4392 | } |
| 4393 | user = user->next; |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4398 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4399 | |
| 4400 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4401 | msg.str = trash; |
| 4402 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4403 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 4404 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4405 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4406 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4407 | if (!(t->flags & SN_ERR_MASK)) |
| 4408 | t->flags |= SN_ERR_PRXCOND; |
| 4409 | if (!(t->flags & SN_FINST_MASK)) |
| 4410 | t->flags |= SN_FINST_R; |
| 4411 | return 1; |
| 4412 | } |
| 4413 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4414 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4415 | * data. |
| 4416 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 4417 | buffer_write_dis(t->req); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4418 | buffer_shutw_now(t->req); |
| 4419 | buffer_shutr_now(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 4420 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4421 | t->data_source = DATA_SRC_STATS; |
| 4422 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 4423 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 4424 | buffer_install_hijacker(t, t->rep, produce_content); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4425 | return 1; |
| 4426 | } |
| 4427 | |
| 4428 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4429 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4430 | * Print a debug line with a header |
| 4431 | */ |
| 4432 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4433 | { |
| 4434 | int len, max; |
| 4435 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4436 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4437 | max = end - start; |
| 4438 | UBOUND(max, sizeof(trash) - len - 1); |
| 4439 | len += strlcpy2(trash + len, start, max + 1); |
| 4440 | trash[len++] = '\n'; |
| 4441 | write(1, trash, len); |
| 4442 | } |
| 4443 | |
| 4444 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4445 | /************************************************************************/ |
| 4446 | /* The code below is dedicated to ACL parsing and matching */ |
| 4447 | /************************************************************************/ |
| 4448 | |
| 4449 | |
| 4450 | |
| 4451 | |
| 4452 | /* 1. Check on METHOD |
| 4453 | * We use the pre-parsed method if it is known, and store its number as an |
| 4454 | * integer. If it is unknown, we use the pointer and the length. |
| 4455 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4456 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4457 | { |
| 4458 | int len, meth; |
| 4459 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4460 | len = strlen(*text); |
| 4461 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4462 | |
| 4463 | pattern->val.i = meth; |
| 4464 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4465 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4466 | if (!pattern->ptr.str) |
| 4467 | return 0; |
| 4468 | pattern->len = len; |
| 4469 | } |
| 4470 | return 1; |
| 4471 | } |
| 4472 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4473 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4474 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4475 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4476 | { |
| 4477 | int meth; |
| 4478 | struct http_txn *txn = l7; |
| 4479 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4480 | if (!txn) |
| 4481 | return 0; |
| 4482 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4483 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4484 | return 0; |
| 4485 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4486 | meth = txn->meth; |
| 4487 | test->i = meth; |
| 4488 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4489 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4490 | /* ensure the indexes are not affected */ |
| 4491 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4492 | test->len = txn->req.sl.rq.m_l; |
| 4493 | test->ptr = txn->req.sol; |
| 4494 | } |
| 4495 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4496 | return 1; |
| 4497 | } |
| 4498 | |
| 4499 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4500 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4501 | int icase; |
| 4502 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4503 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4504 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4505 | |
| 4506 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4507 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4508 | |
| 4509 | /* Other method, we must compare the strings */ |
| 4510 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4511 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4512 | |
| 4513 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4514 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4515 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4516 | return ACL_PAT_FAIL; |
| 4517 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* 2. Check on Request/Status Version |
| 4521 | * We simply compare strings here. |
| 4522 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4523 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4524 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4525 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4526 | if (!pattern->ptr.str) |
| 4527 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4528 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4529 | return 1; |
| 4530 | } |
| 4531 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4532 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4533 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4534 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4535 | { |
| 4536 | struct http_txn *txn = l7; |
| 4537 | char *ptr; |
| 4538 | int len; |
| 4539 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4540 | if (!txn) |
| 4541 | return 0; |
| 4542 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4543 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4544 | return 0; |
| 4545 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4546 | len = txn->req.sl.rq.v_l; |
| 4547 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4548 | |
| 4549 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4550 | if (len <= 0) |
| 4551 | return 0; |
| 4552 | |
| 4553 | test->ptr = ptr; |
| 4554 | test->len = len; |
| 4555 | |
| 4556 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4557 | return 1; |
| 4558 | } |
| 4559 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4560 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4561 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4562 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4563 | { |
| 4564 | struct http_txn *txn = l7; |
| 4565 | char *ptr; |
| 4566 | int len; |
| 4567 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4568 | if (!txn) |
| 4569 | return 0; |
| 4570 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4571 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4572 | return 0; |
| 4573 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4574 | len = txn->rsp.sl.st.v_l; |
| 4575 | ptr = txn->rsp.sol; |
| 4576 | |
| 4577 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4578 | if (len <= 0) |
| 4579 | return 0; |
| 4580 | |
| 4581 | test->ptr = ptr; |
| 4582 | test->len = len; |
| 4583 | |
| 4584 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4585 | return 1; |
| 4586 | } |
| 4587 | |
| 4588 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4589 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4590 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4591 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4592 | { |
| 4593 | struct http_txn *txn = l7; |
| 4594 | char *ptr; |
| 4595 | int len; |
| 4596 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4597 | if (!txn) |
| 4598 | return 0; |
| 4599 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4600 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4601 | return 0; |
| 4602 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4603 | len = txn->rsp.sl.st.c_l; |
| 4604 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4605 | |
| 4606 | test->i = __strl2ui(ptr, len); |
| 4607 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4608 | return 1; |
| 4609 | } |
| 4610 | |
| 4611 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4612 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4613 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4614 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4615 | { |
| 4616 | struct http_txn *txn = l7; |
| 4617 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4618 | if (!txn) |
| 4619 | return 0; |
| 4620 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4621 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4622 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4623 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4624 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4625 | /* ensure the indexes are not affected */ |
| 4626 | return 0; |
| 4627 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4628 | test->len = txn->req.sl.rq.u_l; |
| 4629 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4630 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4631 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4632 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4633 | return 1; |
| 4634 | } |
| 4635 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4636 | static int |
| 4637 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4638 | struct acl_expr *expr, struct acl_test *test) |
| 4639 | { |
| 4640 | struct http_txn *txn = l7; |
| 4641 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4642 | if (!txn) |
| 4643 | return 0; |
| 4644 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4645 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4646 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4647 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4648 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4649 | /* ensure the indexes are not affected */ |
| 4650 | return 0; |
| 4651 | |
| 4652 | /* Parse HTTP request */ |
| 4653 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4654 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 4655 | test->i = AF_INET; |
| 4656 | |
| 4657 | /* |
| 4658 | * If we are parsing url in frontend space, we prepare backend stage |
| 4659 | * to not parse again the same url ! optimization lazyness... |
| 4660 | */ |
| 4661 | if (px->options & PR_O_HTTP_PROXY) |
| 4662 | l4->flags |= SN_ADDR_SET; |
| 4663 | |
| 4664 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
| 4668 | static int |
| 4669 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4670 | struct acl_expr *expr, struct acl_test *test) |
| 4671 | { |
| 4672 | struct http_txn *txn = l7; |
| 4673 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4674 | if (!txn) |
| 4675 | return 0; |
| 4676 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4677 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4678 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4679 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4680 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4681 | /* ensure the indexes are not affected */ |
| 4682 | return 0; |
| 4683 | |
| 4684 | /* Same optimization as url_ip */ |
| 4685 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4686 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 4687 | |
| 4688 | if (px->options & PR_O_HTTP_PROXY) |
| 4689 | l4->flags |= SN_ADDR_SET; |
| 4690 | |
| 4691 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4692 | return 1; |
| 4693 | } |
| 4694 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4695 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 4696 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 4697 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4698 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4699 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4700 | struct acl_expr *expr, struct acl_test *test) |
| 4701 | { |
| 4702 | struct http_txn *txn = l7; |
| 4703 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4704 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4705 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4706 | if (!txn) |
| 4707 | return 0; |
| 4708 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4709 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4710 | /* search for header from the beginning */ |
| 4711 | ctx->idx = 0; |
| 4712 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4713 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4714 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4715 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4716 | test->len = ctx->vlen; |
| 4717 | test->ptr = (char *)ctx->line + ctx->val; |
| 4718 | return 1; |
| 4719 | } |
| 4720 | |
| 4721 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4722 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4726 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4727 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4728 | struct acl_expr *expr, struct acl_test *test) |
| 4729 | { |
| 4730 | struct http_txn *txn = l7; |
| 4731 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4732 | if (!txn) |
| 4733 | return 0; |
| 4734 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4735 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4736 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4737 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4738 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4739 | /* ensure the indexes are not affected */ |
| 4740 | return 0; |
| 4741 | |
| 4742 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 4743 | } |
| 4744 | |
| 4745 | static int |
| 4746 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4747 | struct acl_expr *expr, struct acl_test *test) |
| 4748 | { |
| 4749 | struct http_txn *txn = l7; |
| 4750 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4751 | if (!txn) |
| 4752 | return 0; |
| 4753 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4754 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4755 | return 0; |
| 4756 | |
| 4757 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 4758 | } |
| 4759 | |
| 4760 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 4761 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 4762 | */ |
| 4763 | static int |
| 4764 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4765 | struct acl_expr *expr, struct acl_test *test) |
| 4766 | { |
| 4767 | struct http_txn *txn = l7; |
| 4768 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4769 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4770 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4771 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4772 | if (!txn) |
| 4773 | return 0; |
| 4774 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4775 | ctx.idx = 0; |
| 4776 | cnt = 0; |
| 4777 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 4778 | cnt++; |
| 4779 | |
| 4780 | test->i = cnt; |
| 4781 | test->flags = ACL_TEST_F_VOL_HDR; |
| 4782 | return 1; |
| 4783 | } |
| 4784 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4785 | static int |
| 4786 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4787 | struct acl_expr *expr, struct acl_test *test) |
| 4788 | { |
| 4789 | struct http_txn *txn = l7; |
| 4790 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4791 | if (!txn) |
| 4792 | return 0; |
| 4793 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4794 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4795 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4796 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4797 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4798 | /* ensure the indexes are not affected */ |
| 4799 | return 0; |
| 4800 | |
| 4801 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 4802 | } |
| 4803 | |
| 4804 | static int |
| 4805 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4806 | struct acl_expr *expr, struct acl_test *test) |
| 4807 | { |
| 4808 | struct http_txn *txn = l7; |
| 4809 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4810 | if (!txn) |
| 4811 | return 0; |
| 4812 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4813 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4814 | return 0; |
| 4815 | |
| 4816 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 4817 | } |
| 4818 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4819 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 4820 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4821 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4822 | */ |
| 4823 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4824 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4825 | struct acl_expr *expr, struct acl_test *test) |
| 4826 | { |
| 4827 | struct http_txn *txn = l7; |
| 4828 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4829 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4830 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4831 | if (!txn) |
| 4832 | return 0; |
| 4833 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4834 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4835 | /* search for header from the beginning */ |
| 4836 | ctx->idx = 0; |
| 4837 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4838 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4839 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4840 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4841 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 4842 | return 1; |
| 4843 | } |
| 4844 | |
| 4845 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4846 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4847 | return 0; |
| 4848 | } |
| 4849 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4850 | static int |
| 4851 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4852 | struct acl_expr *expr, struct acl_test *test) |
| 4853 | { |
| 4854 | struct http_txn *txn = l7; |
| 4855 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4856 | if (!txn) |
| 4857 | return 0; |
| 4858 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4859 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4860 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4861 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4862 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4863 | /* ensure the indexes are not affected */ |
| 4864 | return 0; |
| 4865 | |
| 4866 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 4867 | } |
| 4868 | |
| 4869 | static int |
| 4870 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4871 | struct acl_expr *expr, struct acl_test *test) |
| 4872 | { |
| 4873 | struct http_txn *txn = l7; |
| 4874 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4875 | if (!txn) |
| 4876 | return 0; |
| 4877 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4878 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4879 | return 0; |
| 4880 | |
| 4881 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 4882 | } |
| 4883 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4884 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 4885 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 4886 | */ |
| 4887 | static int |
| 4888 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4889 | struct acl_expr *expr, struct acl_test *test) |
| 4890 | { |
| 4891 | struct http_txn *txn = l7; |
| 4892 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4893 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4894 | if (!txn) |
| 4895 | return 0; |
| 4896 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4897 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4898 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4899 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4900 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4901 | /* ensure the indexes are not affected */ |
| 4902 | return 0; |
| 4903 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4904 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4905 | ptr = http_get_path(txn); |
| 4906 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4907 | return 0; |
| 4908 | |
| 4909 | /* OK, we got the '/' ! */ |
| 4910 | test->ptr = ptr; |
| 4911 | |
| 4912 | while (ptr < end && *ptr != '?') |
| 4913 | ptr++; |
| 4914 | |
| 4915 | test->len = ptr - test->ptr; |
| 4916 | |
| 4917 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4918 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4919 | return 1; |
| 4920 | } |
| 4921 | |
| 4922 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4923 | |
| 4924 | /************************************************************************/ |
| 4925 | /* All supported keywords must be declared here. */ |
| 4926 | /************************************************************************/ |
| 4927 | |
| 4928 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 4929 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 4930 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 4931 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 4932 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 4933 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4934 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 4935 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 4936 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 4937 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 4938 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 4939 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 4940 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 4941 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 4942 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 4943 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4944 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 4945 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 4946 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 4947 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 4948 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 4949 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 4950 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 4951 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 4952 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 4953 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 4954 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4955 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 4956 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 4957 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 4958 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 4959 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 4960 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 4961 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 4962 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 4963 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 4964 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4965 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 4966 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 4967 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 4968 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 4969 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 4970 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 4971 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 4972 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4973 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4974 | { NULL, NULL, NULL, NULL }, |
| 4975 | |
| 4976 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4977 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 4978 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 4979 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 4980 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 4981 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 4982 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 4983 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 4984 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4985 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 4986 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 4987 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 4988 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 4989 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 4990 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 4991 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 4992 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 4993 | |
| 4994 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 4995 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 4996 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 4997 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 4998 | { NULL, NULL, NULL, NULL }, |
| 4999 | #endif |
| 5000 | }}; |
| 5001 | |
| 5002 | |
| 5003 | __attribute__((constructor)) |
| 5004 | static void __http_protocol_init(void) |
| 5005 | { |
| 5006 | acl_register_keywords(&acl_kws); |
| 5007 | } |
| 5008 | |
| 5009 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5010 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5011 | * Local variables: |
| 5012 | * c-indent-level: 8 |
| 5013 | * c-basic-offset: 8 |
| 5014 | * End: |
| 5015 | */ |