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> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/session.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 | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 372 | static void http_sess_log(struct session *s); |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 376 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 377 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 378 | * of headers is automatically adjusted. The number of bytes added is returned |
| 379 | * on success, otherwise <0 is returned indicating an error. |
| 380 | */ |
| 381 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 382 | struct hdr_idx *hdr_idx, const char *text) |
| 383 | { |
| 384 | int bytes, len; |
| 385 | |
| 386 | len = strlen(text); |
| 387 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 388 | if (!bytes) |
| 389 | return -1; |
| 390 | msg->eoh += bytes; |
| 391 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 396 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 397 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 398 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 399 | * of headers is automatically adjusted. The number of bytes added is returned |
| 400 | * on success, otherwise <0 is returned indicating an error. |
| 401 | */ |
| 402 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 403 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 404 | { |
| 405 | int bytes; |
| 406 | |
| 407 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 408 | if (!bytes) |
| 409 | return -1; |
| 410 | msg->eoh += bytes; |
| 411 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 415 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 416 | * If so, returns the position of the first non-space character relative to |
| 417 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 418 | * to return a pointer to the place after the first space. Returns 0 if the |
| 419 | * header name does not match. Checks are case-insensitive. |
| 420 | */ |
| 421 | int http_header_match2(const char *hdr, const char *end, |
| 422 | const char *name, int len) |
| 423 | { |
| 424 | const char *val; |
| 425 | |
| 426 | if (hdr + len >= end) |
| 427 | return 0; |
| 428 | if (hdr[len] != ':') |
| 429 | return 0; |
| 430 | if (strncasecmp(hdr, name, len) != 0) |
| 431 | return 0; |
| 432 | val = hdr + len + 1; |
| 433 | while (val < end && HTTP_IS_SPHT(*val)) |
| 434 | val++; |
| 435 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 436 | return len + 2; /* we may replace starting from second space */ |
| 437 | return val - hdr; |
| 438 | } |
| 439 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 440 | /* Find the end of the header value contained between <s> and <e>. |
| 441 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 442 | * a valid header to return a valid result. |
| 443 | */ |
| 444 | const char *find_hdr_value_end(const char *s, const char *e) |
| 445 | { |
| 446 | int quoted, qdpair; |
| 447 | |
| 448 | quoted = qdpair = 0; |
| 449 | for (; s < e; s++) { |
| 450 | if (qdpair) qdpair = 0; |
| 451 | else if (quoted && *s == '\\') qdpair = 1; |
| 452 | else if (quoted && *s == '"') quoted = 0; |
| 453 | else if (*s == '"') quoted = 1; |
| 454 | else if (*s == ',') return s; |
| 455 | } |
| 456 | return s; |
| 457 | } |
| 458 | |
| 459 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 460 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 461 | * structure holds everything necessary to use the header and find next |
| 462 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 463 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 464 | * 1 when it finds a value, and 0 when there is no more. |
| 465 | */ |
| 466 | int http_find_header2(const char *name, int len, |
| 467 | const char *sol, struct hdr_idx *idx, |
| 468 | struct hdr_ctx *ctx) |
| 469 | { |
| 470 | __label__ return_hdr, next_hdr; |
| 471 | const char *eol, *sov; |
| 472 | int cur_idx; |
| 473 | |
| 474 | if (ctx->idx) { |
| 475 | /* We have previously returned a value, let's search |
| 476 | * another one on the same line. |
| 477 | */ |
| 478 | cur_idx = ctx->idx; |
| 479 | sol = ctx->line; |
| 480 | sov = sol + ctx->val + ctx->vlen; |
| 481 | eol = sol + idx->v[cur_idx].len; |
| 482 | |
| 483 | if (sov >= eol) |
| 484 | /* no more values in this header */ |
| 485 | goto next_hdr; |
| 486 | |
| 487 | /* values remaining for this header, skip the comma */ |
| 488 | sov++; |
| 489 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 490 | sov++; |
| 491 | |
| 492 | goto return_hdr; |
| 493 | } |
| 494 | |
| 495 | /* first request for this header */ |
| 496 | sol += hdr_idx_first_pos(idx); |
| 497 | cur_idx = hdr_idx_first_idx(idx); |
| 498 | |
| 499 | while (cur_idx) { |
| 500 | eol = sol + idx->v[cur_idx].len; |
| 501 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 502 | if (len == 0) { |
| 503 | /* No argument was passed, we want any header. |
| 504 | * To achieve this, we simply build a fake request. */ |
| 505 | while (sol + len < eol && sol[len] != ':') |
| 506 | len++; |
| 507 | name = sol; |
| 508 | } |
| 509 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | if ((len < eol - sol) && |
| 511 | (sol[len] == ':') && |
| 512 | (strncasecmp(sol, name, len) == 0)) { |
| 513 | |
| 514 | sov = sol + len + 1; |
| 515 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 516 | sov++; |
| 517 | return_hdr: |
| 518 | ctx->line = sol; |
| 519 | ctx->idx = cur_idx; |
| 520 | ctx->val = sov - sol; |
| 521 | |
| 522 | eol = find_hdr_value_end(sov, eol); |
| 523 | ctx->vlen = eol - sov; |
| 524 | return 1; |
| 525 | } |
| 526 | next_hdr: |
| 527 | sol = eol + idx->v[cur_idx].cr + 1; |
| 528 | cur_idx = idx->v[cur_idx].next; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int http_find_header(const char *name, |
| 534 | const char *sol, struct hdr_idx *idx, |
| 535 | struct hdr_ctx *ctx) |
| 536 | { |
| 537 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 538 | } |
| 539 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 540 | /* This function shuts down the buffers on the server side, and sets indicators |
| 541 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 542 | * <status> is 0, or if the message pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
| 544 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 545 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 547 | t->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 548 | buffer_shutw(t->req); |
| 549 | buffer_shutr(t->rep); |
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 | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 552 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 553 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 554 | } |
| 555 | if (!(t->flags & SN_ERR_MASK)) |
| 556 | t->flags |= err; |
| 557 | if (!(t->flags & SN_FINST_MASK)) |
| 558 | t->flags |= finst; |
| 559 | } |
| 560 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 561 | /* This function returns the appropriate error location for the given session |
| 562 | * and message. |
| 563 | */ |
| 564 | |
| 565 | struct chunk *error_message(struct session *s, int msgnum) |
| 566 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 567 | if (s->be->errmsg[msgnum].str) |
| 568 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 569 | else if (s->fe->errmsg[msgnum].str) |
| 570 | return &s->fe->errmsg[msgnum]; |
| 571 | else |
| 572 | return &http_err_chunks[msgnum]; |
| 573 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 575 | /* |
| 576 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 577 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 578 | */ |
| 579 | static http_meth_t find_http_meth(const char *str, const int len) |
| 580 | { |
| 581 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 582 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 583 | |
| 584 | m = ((unsigned)*str - 'A'); |
| 585 | |
| 586 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 587 | for (h = http_methods[m]; h->len > 0; h++) { |
| 588 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 589 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 590 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 592 | }; |
| 593 | return HTTP_METH_OTHER; |
| 594 | } |
| 595 | return HTTP_METH_NONE; |
| 596 | |
| 597 | } |
| 598 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 599 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 600 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 601 | * It is returned otherwise. |
| 602 | */ |
| 603 | static char * |
| 604 | http_get_path(struct http_txn *txn) |
| 605 | { |
| 606 | char *ptr, *end; |
| 607 | |
| 608 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 609 | end = ptr + txn->req.sl.rq.u_l; |
| 610 | |
| 611 | if (ptr >= end) |
| 612 | return NULL; |
| 613 | |
| 614 | /* RFC2616, par. 5.1.2 : |
| 615 | * Request-URI = "*" | absuri | abspath | authority |
| 616 | */ |
| 617 | |
| 618 | if (*ptr == '*') |
| 619 | return NULL; |
| 620 | |
| 621 | if (isalpha((unsigned char)*ptr)) { |
| 622 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 623 | ptr++; |
| 624 | while (ptr < end && |
| 625 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 626 | ptr++; |
| 627 | /* skip '://' */ |
| 628 | if (ptr == end || *ptr++ != ':') |
| 629 | return NULL; |
| 630 | if (ptr == end || *ptr++ != '/') |
| 631 | return NULL; |
| 632 | if (ptr == end || *ptr++ != '/') |
| 633 | return NULL; |
| 634 | } |
| 635 | /* skip [user[:passwd]@]host[:[port]] */ |
| 636 | |
| 637 | while (ptr < end && *ptr != '/') |
| 638 | ptr++; |
| 639 | |
| 640 | if (ptr == end) |
| 641 | return NULL; |
| 642 | |
| 643 | /* OK, we got the '/' ! */ |
| 644 | return ptr; |
| 645 | } |
| 646 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 647 | /* Processes the client, server, request and response jobs of a session task, |
| 648 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 649 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 650 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 651 | * nothing too many times, the request and response buffers flags are monitored |
| 652 | * and each function is called only if at least another function has changed at |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 653 | * least one flag it is interested in. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 655 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | { |
| 657 | struct session *s = t->context; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 658 | int resync; |
| 659 | unsigned int rqf_cli, rpf_cli; |
| 660 | unsigned int rqf_srv, rpf_srv; |
| 661 | unsigned int rqf_req, rpf_rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 663 | /* check server-side errors during data phase */ |
| 664 | if (s->req->cons->state == SI_ST_EST) { |
| 665 | stream_sock_data_check_errors(s->req->cons->fd); |
| 666 | /* When a server-side connection is released, we have to |
| 667 | * count it and check for pending connections on this server. |
| 668 | */ |
| 669 | if (unlikely(s->req->cons->state == SI_ST_CLO)) { |
| 670 | /* Count server-side errors (but not timeouts). */ |
| 671 | if (s->req->flags & BF_WRITE_ERROR) { |
| 672 | s->be->failed_resp++; |
| 673 | if (s->srv) |
| 674 | s->srv->failed_resp++; |
| 675 | } |
| 676 | |
| 677 | if (s->srv) { |
| 678 | s->srv->cur_sess--; |
| 679 | sess_change_server(s, NULL); |
| 680 | if (may_dequeue_tasks(s->srv, s->be)) |
| 681 | process_srv_queue(s->srv); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* check client-side errors during data phase */ |
| 687 | if (s->rep->cons->state == SI_ST_EST) |
| 688 | stream_sock_data_check_errors(s->rep->cons->fd); |
| 689 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 690 | /* force one first pass everywhere */ |
| 691 | rqf_cli = rqf_srv = rqf_req = ~s->req->flags; |
| 692 | rpf_cli = rpf_srv = rpf_rep = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 693 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 694 | do { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 695 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 696 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 697 | if (s->rep->cons->state != SI_ST_CLO) { |
| 698 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 699 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
| 700 | resync = 1; |
| 701 | stream_sock_data_update(s->rep->cons->fd); |
| 702 | rqf_cli = s->req->flags; |
| 703 | rpf_cli = s->rep->flags; |
| 704 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 705 | if (unlikely((s->rep->cons->state == SI_ST_CLO) && |
| 706 | (global.mode & MODE_DEBUG) && |
| 707 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 708 | int len; |
| 709 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 710 | s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 711 | write(1, trash, len); |
| 712 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 713 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 714 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 715 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 716 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 717 | if (s->req->cons->state != SI_ST_CLO) { |
| 718 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 719 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
| 720 | resync = 1; |
| 721 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 722 | if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_MAY_FORWARD) |
| 723 | process_srv_conn(s); |
| 724 | |
| 725 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 726 | if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD) && |
| 727 | s->be->options & PR_O_FORCE_CLO && |
| 728 | s->rep->flags & BF_READ_STATUS) { |
| 729 | /* We want to force the connection to the server to close, |
| 730 | * and the server has begun to respond. That's the right |
| 731 | * time. |
| 732 | */ |
| 733 | buffer_shutw_now(s->req); |
| 734 | } |
| 735 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 736 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 737 | |
| 738 | /* When a server-side connection is released, we have to |
| 739 | * count it and check for pending connections on this server. |
| 740 | */ |
| 741 | if (s->req->cons->state == SI_ST_CLO) { |
| 742 | if (s->srv) { |
| 743 | s->srv->cur_sess--; |
| 744 | sess_change_server(s, NULL); |
| 745 | if (may_dequeue_tasks(s->srv, s->be)) |
| 746 | process_srv_queue(s->srv); |
| 747 | } |
| 748 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 749 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 750 | rqf_srv = s->req->flags; |
| 751 | rpf_srv = s->rep->flags; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 752 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 753 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 754 | (global.mode & MODE_DEBUG) && |
| 755 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 756 | int len; |
| 757 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 758 | s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 759 | write(1, trash, len); |
| 760 | } |
| 761 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if ((rqf_req ^ s->req->flags) & BF_MASK_ANALYSER) { |
| 765 | resync = 1; |
| 766 | /* the analysers must block it themselves */ |
| 767 | s->req->flags |= BF_MAY_FORWARD; |
| 768 | if (s->req->analysers) { |
| 769 | process_request(s); |
| 770 | } |
| 771 | rqf_req = s->req->flags; |
| 772 | } |
| 773 | |
| 774 | if ((rpf_rep ^ s->rep->flags) & BF_MASK_ANALYSER) { |
| 775 | resync = 1; |
| 776 | /* the analysers must block it themselves */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 777 | if (s->req->cons->state >= SI_ST_EST) |
| 778 | s->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 779 | |
| 780 | if (s->rep->analysers) { |
| 781 | process_response(s); |
| 782 | } |
| 783 | rpf_rep = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 784 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 785 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 786 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 788 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 789 | (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 790 | |
| 791 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 792 | session_process_counters(s); |
| 793 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 794 | if (s->rep->cons->state == SI_ST_EST) |
| 795 | stream_sock_data_finish(s->rep->cons->fd); |
| 796 | |
| 797 | if (s->req->cons->state == SI_ST_EST) |
| 798 | stream_sock_data_finish(s->req->cons->fd); |
| 799 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 800 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 801 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 802 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 803 | /* Trick: if a request is being waiting for the server to respond, |
| 804 | * and if we know the server can timeout, we don't want the timeout |
| 805 | * to expire on the client side first, but we're still interested |
| 806 | * in passing data from the client to the server (eg: POST). Thus, |
| 807 | * we can cancel the client's request timeout if the server's |
| 808 | * request timeout is set and the server has not yet sent a response. |
| 809 | */ |
| 810 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 811 | if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 812 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 813 | s->req->rex = TICK_ETERNITY; |
| 814 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 815 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 816 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 817 | if (s->req->analysers) |
| 818 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 819 | |
| 820 | /* restore t to its place in the task list */ |
| 821 | task_queue(t); |
| 822 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 823 | #ifdef DEBUG_DEV |
| 824 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 825 | if (!t->expire) |
| 826 | ABORT_NOW(); |
| 827 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 828 | *next = t->expire; |
| 829 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 832 | s->fe->feconn--; |
| 833 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 834 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 835 | actconn--; |
| 836 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 837 | if (unlikely((global.mode & MODE_DEBUG) && |
| 838 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 839 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 840 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 841 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 842 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 843 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | write(1, trash, len); |
| 845 | } |
| 846 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 847 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 848 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 849 | |
| 850 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 851 | if (s->logs.logwait && |
| 852 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 853 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 854 | if (s->fe->to_log & LW_REQ) |
| 855 | http_sess_log(s); |
| 856 | else |
| 857 | tcp_sess_log(s); |
| 858 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 859 | |
| 860 | /* the task MUST not be in the run queue anymore */ |
| 861 | task_delete(t); |
| 862 | session_free(s); |
| 863 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 864 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 868 | extern const char sess_term_cond[8]; |
| 869 | extern const char sess_fin_state[8]; |
| 870 | extern const char *monthname[12]; |
| 871 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 872 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 873 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 874 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 875 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 876 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 877 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 878 | /* |
| 879 | * send a log for the session when we have enough info about it. |
| 880 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 881 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 882 | static void http_sess_log(struct session *s) |
| 883 | { |
| 884 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 885 | struct proxy *fe = s->fe; |
| 886 | struct proxy *be = s->be; |
| 887 | struct proxy *prx_log; |
| 888 | struct http_txn *txn = &s->txn; |
| 889 | int tolog; |
| 890 | char *uri, *h; |
| 891 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 892 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 893 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 894 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 895 | int hdr; |
| 896 | |
| 897 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 898 | return; |
| 899 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 900 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 901 | if (s->cli_addr.ss_family == AF_INET) |
| 902 | inet_ntop(AF_INET, |
| 903 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 904 | pn, sizeof(pn)); |
| 905 | else |
| 906 | inet_ntop(AF_INET6, |
| 907 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 908 | pn, sizeof(pn)); |
| 909 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 910 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 911 | |
| 912 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 913 | tolog = fe->to_log; |
| 914 | |
| 915 | h = tmpline; |
| 916 | if (fe->to_log & LW_REQHDR && |
| 917 | txn->req.cap && |
| 918 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 919 | *(h++) = ' '; |
| 920 | *(h++) = '{'; |
| 921 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 922 | if (hdr) |
| 923 | *(h++) = '|'; |
| 924 | if (txn->req.cap[hdr] != NULL) |
| 925 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 926 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 927 | } |
| 928 | *(h++) = '}'; |
| 929 | } |
| 930 | |
| 931 | if (fe->to_log & LW_RSPHDR && |
| 932 | txn->rsp.cap && |
| 933 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 934 | *(h++) = ' '; |
| 935 | *(h++) = '{'; |
| 936 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 937 | if (hdr) |
| 938 | *(h++) = '|'; |
| 939 | if (txn->rsp.cap[hdr] != NULL) |
| 940 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 941 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 942 | } |
| 943 | *(h++) = '}'; |
| 944 | } |
| 945 | |
| 946 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 947 | *(h++) = ' '; |
| 948 | *(h++) = '"'; |
| 949 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 950 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 951 | '#', url_encode_map, uri); |
| 952 | *(h++) = '"'; |
| 953 | } |
| 954 | *h = '\0'; |
| 955 | |
| 956 | svid = (tolog & LW_SVID) ? |
| 957 | (s->data_source != DATA_SRC_STATS) ? |
| 958 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 959 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 960 | t_request = -1; |
| 961 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 962 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 963 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 964 | send_log(prx_log, LOG_INFO, |
| 965 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 966 | " %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] | 967 | " %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] | 968 | pn, |
| 969 | (s->cli_addr.ss_family == AF_INET) ? |
| 970 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 971 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 972 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 973 | 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] | 974 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 975 | t_request, |
| 976 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 977 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 978 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 979 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 980 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 981 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 982 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 983 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 984 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 985 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 986 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 987 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 988 | 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] | 989 | (s->flags & SN_REDISP)?"+":"", |
| 990 | (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] | 991 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 992 | |
| 993 | s->logs.logwait = 0; |
| 994 | } |
| 995 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Capture headers from message starting at <som> according to header list |
| 999 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1000 | */ |
| 1001 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1002 | char **cap, struct cap_hdr *cap_hdr) |
| 1003 | { |
| 1004 | char *eol, *sol, *col, *sov; |
| 1005 | int cur_idx; |
| 1006 | struct cap_hdr *h; |
| 1007 | int len; |
| 1008 | |
| 1009 | sol = som + hdr_idx_first_pos(idx); |
| 1010 | cur_idx = hdr_idx_first_idx(idx); |
| 1011 | |
| 1012 | while (cur_idx) { |
| 1013 | eol = sol + idx->v[cur_idx].len; |
| 1014 | |
| 1015 | col = sol; |
| 1016 | while (col < eol && *col != ':') |
| 1017 | col++; |
| 1018 | |
| 1019 | sov = col + 1; |
| 1020 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1021 | sov++; |
| 1022 | |
| 1023 | for (h = cap_hdr; h; h = h->next) { |
| 1024 | if ((h->namelen == col - sol) && |
| 1025 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1026 | if (cap[h->index] == NULL) |
| 1027 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1028 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1029 | |
| 1030 | if (cap[h->index] == NULL) { |
| 1031 | Alert("HTTP capture : out of memory.\n"); |
| 1032 | continue; |
| 1033 | } |
| 1034 | |
| 1035 | len = eol - sov; |
| 1036 | if (len > h->len) |
| 1037 | len = h->len; |
| 1038 | |
| 1039 | memcpy(cap[h->index], sov, len); |
| 1040 | cap[h->index][len]=0; |
| 1041 | } |
| 1042 | } |
| 1043 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1044 | cur_idx = idx->v[cur_idx].next; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1049 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1050 | */ |
| 1051 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1052 | |
| 1053 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1054 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1055 | */ |
| 1056 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1057 | ptr++; \ |
| 1058 | if (likely(ptr < end)) \ |
| 1059 | goto good; \ |
| 1060 | else { \ |
| 1061 | state = (st); \ |
| 1062 | goto http_msg_ood; \ |
| 1063 | } \ |
| 1064 | } while (0) |
| 1065 | |
| 1066 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1067 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1068 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1069 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1070 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1071 | * will give undefined results. |
| 1072 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1073 | * and that msg->sol points to the beginning of the response. |
| 1074 | * If a complete line is found (which implies that at least one CR or LF is |
| 1075 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1076 | * returned indicating an incomplete line (which does not mean that parts have |
| 1077 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1078 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1079 | * upon next call. |
| 1080 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1081 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1082 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1083 | * 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] | 1084 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1085 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1086 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1087 | unsigned int state, const char *ptr, const char *end, |
| 1088 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1089 | { |
| 1090 | __label__ |
| 1091 | http_msg_rpver, |
| 1092 | http_msg_rpver_sp, |
| 1093 | http_msg_rpcode, |
| 1094 | http_msg_rpcode_sp, |
| 1095 | http_msg_rpreason, |
| 1096 | http_msg_rpline_eol, |
| 1097 | http_msg_ood, /* out of data */ |
| 1098 | http_msg_invalid; |
| 1099 | |
| 1100 | switch (state) { |
| 1101 | http_msg_rpver: |
| 1102 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1103 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1104 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1105 | |
| 1106 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1107 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1108 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1109 | } |
| 1110 | goto http_msg_invalid; |
| 1111 | |
| 1112 | http_msg_rpver_sp: |
| 1113 | case HTTP_MSG_RPVER_SP: |
| 1114 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1115 | msg->sl.st.c = ptr - msg_buf; |
| 1116 | goto http_msg_rpcode; |
| 1117 | } |
| 1118 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1119 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1120 | /* so it's a CR/LF, this is invalid */ |
| 1121 | goto http_msg_invalid; |
| 1122 | |
| 1123 | http_msg_rpcode: |
| 1124 | case HTTP_MSG_RPCODE: |
| 1125 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1126 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1127 | |
| 1128 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1129 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1130 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1131 | } |
| 1132 | |
| 1133 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1134 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1135 | http_msg_rsp_reason: |
| 1136 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1137 | msg->sl.st.r = ptr - msg_buf; |
| 1138 | msg->sl.st.r_l = 0; |
| 1139 | goto http_msg_rpline_eol; |
| 1140 | |
| 1141 | http_msg_rpcode_sp: |
| 1142 | case HTTP_MSG_RPCODE_SP: |
| 1143 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1144 | msg->sl.st.r = ptr - msg_buf; |
| 1145 | goto http_msg_rpreason; |
| 1146 | } |
| 1147 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1148 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1149 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1150 | goto http_msg_rsp_reason; |
| 1151 | |
| 1152 | http_msg_rpreason: |
| 1153 | case HTTP_MSG_RPREASON: |
| 1154 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1155 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1156 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1157 | http_msg_rpline_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 response would not be |
| 1161 | * complete. We can then record the response length and return |
| 1162 | * to the caller which will be able to register it. |
| 1163 | */ |
| 1164 | msg->sl.st.l = ptr - msg->sol; |
| 1165 | return ptr; |
| 1166 | |
| 1167 | #ifdef DEBUG_FULL |
| 1168 | default: |
| 1169 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1170 | exit(1); |
| 1171 | #endif |
| 1172 | } |
| 1173 | |
| 1174 | http_msg_ood: |
| 1175 | /* out of data */ |
| 1176 | if (ret_state) |
| 1177 | *ret_state = state; |
| 1178 | if (ret_ptr) |
| 1179 | *ret_ptr = (char *)ptr; |
| 1180 | return NULL; |
| 1181 | |
| 1182 | http_msg_invalid: |
| 1183 | /* invalid message */ |
| 1184 | if (ret_state) |
| 1185 | *ret_state = HTTP_MSG_ERROR; |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1191 | * This function parses a request line between <ptr> and <end>, starting with |
| 1192 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1193 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1194 | * will give undefined results. |
| 1195 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1196 | * and that msg->sol points to the beginning of the request. |
| 1197 | * If a complete line is found (which implies that at least one CR or LF is |
| 1198 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1199 | * returned indicating an incomplete line (which does not mean that parts have |
| 1200 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1201 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1202 | * upon next call. |
| 1203 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1204 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1205 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1206 | * 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] | 1207 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1208 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1209 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1210 | unsigned int state, const char *ptr, const char *end, |
| 1211 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1212 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1213 | __label__ |
| 1214 | http_msg_rqmeth, |
| 1215 | http_msg_rqmeth_sp, |
| 1216 | http_msg_rquri, |
| 1217 | http_msg_rquri_sp, |
| 1218 | http_msg_rqver, |
| 1219 | http_msg_rqline_eol, |
| 1220 | http_msg_ood, /* out of data */ |
| 1221 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1222 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | switch (state) { |
| 1224 | http_msg_rqmeth: |
| 1225 | case HTTP_MSG_RQMETH: |
| 1226 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1227 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
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 (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1230 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1231 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1232 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1234 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1235 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1236 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | http_msg_req09_uri: |
| 1238 | msg->sl.rq.u = ptr - msg_buf; |
| 1239 | http_msg_req09_uri_e: |
| 1240 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1241 | http_msg_req09_ver: |
| 1242 | msg->sl.rq.v = ptr - msg_buf; |
| 1243 | msg->sl.rq.v_l = 0; |
| 1244 | goto http_msg_rqline_eol; |
| 1245 | } |
| 1246 | goto http_msg_invalid; |
| 1247 | |
| 1248 | http_msg_rqmeth_sp: |
| 1249 | case HTTP_MSG_RQMETH_SP: |
| 1250 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1251 | msg->sl.rq.u = ptr - msg_buf; |
| 1252 | goto http_msg_rquri; |
| 1253 | } |
| 1254 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1255 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1256 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1257 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1258 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1259 | http_msg_rquri: |
| 1260 | case HTTP_MSG_RQURI: |
| 1261 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1262 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1265 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1267 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1268 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1269 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1270 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1271 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1272 | http_msg_rquri_sp: |
| 1273 | case HTTP_MSG_RQURI_SP: |
| 1274 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1275 | msg->sl.rq.v = ptr - msg_buf; |
| 1276 | goto http_msg_rqver; |
| 1277 | } |
| 1278 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1279 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1280 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1281 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1282 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | http_msg_rqver: |
| 1284 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1285 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1286 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1287 | |
| 1288 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1289 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1290 | http_msg_rqline_eol: |
| 1291 | /* We have seen the end of line. Note that we do not |
| 1292 | * necessarily have the \n yet, but at least we know that we |
| 1293 | * have EITHER \r OR \n, otherwise the request would not be |
| 1294 | * complete. We can then record the request length and return |
| 1295 | * to the caller which will be able to register it. |
| 1296 | */ |
| 1297 | msg->sl.rq.l = ptr - msg->sol; |
| 1298 | return ptr; |
| 1299 | } |
| 1300 | |
| 1301 | /* neither an HTTP_VER token nor a CRLF */ |
| 1302 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1304 | #ifdef DEBUG_FULL |
| 1305 | default: |
| 1306 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1307 | exit(1); |
| 1308 | #endif |
| 1309 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | http_msg_ood: |
| 1312 | /* out of data */ |
| 1313 | if (ret_state) |
| 1314 | *ret_state = state; |
| 1315 | if (ret_ptr) |
| 1316 | *ret_ptr = (char *)ptr; |
| 1317 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1318 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | http_msg_invalid: |
| 1320 | /* invalid message */ |
| 1321 | if (ret_state) |
| 1322 | *ret_state = HTTP_MSG_ERROR; |
| 1323 | return NULL; |
| 1324 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1325 | |
| 1326 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1329 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | * when data are missing and recalled at the exact same location with no |
| 1331 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1332 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1333 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1336 | { |
| 1337 | __label__ |
| 1338 | http_msg_rqbefore, |
| 1339 | http_msg_rqbefore_cr, |
| 1340 | http_msg_rqmeth, |
| 1341 | http_msg_rqline_end, |
| 1342 | http_msg_hdr_first, |
| 1343 | http_msg_hdr_name, |
| 1344 | http_msg_hdr_l1_sp, |
| 1345 | http_msg_hdr_l1_lf, |
| 1346 | http_msg_hdr_l1_lws, |
| 1347 | http_msg_hdr_val, |
| 1348 | http_msg_hdr_l2_lf, |
| 1349 | http_msg_hdr_l2_lws, |
| 1350 | http_msg_complete_header, |
| 1351 | http_msg_last_lf, |
| 1352 | http_msg_ood, /* out of data */ |
| 1353 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1354 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1355 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1356 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1358 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1359 | ptr = buf->lr; |
| 1360 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | if (unlikely(ptr >= end)) |
| 1363 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1364 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1366 | /* |
| 1367 | * First, states that are specific to the response only. |
| 1368 | * We check them first so that request and headers are |
| 1369 | * closer to each other (accessed more often). |
| 1370 | */ |
| 1371 | http_msg_rpbefore: |
| 1372 | case HTTP_MSG_RPBEFORE: |
| 1373 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1374 | if (likely(ptr == buf->data)) { |
| 1375 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1376 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | } else { |
| 1378 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1379 | /* only skip empty leading lines, don't remove them */ |
| 1380 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1381 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1382 | #else |
| 1383 | /* Remove empty leading lines, as recommended by |
| 1384 | * RFC2616. This takes a lot of time because we |
| 1385 | * must move all the buffer backwards, but this |
| 1386 | * is rarely needed. The method above will be |
| 1387 | * cleaner when we'll be able to start sending |
| 1388 | * the request from any place in the buffer. |
| 1389 | */ |
| 1390 | buf->lr = ptr; |
| 1391 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1392 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1393 | msg->sol = buf->data; |
| 1394 | ptr = buf->data; |
| 1395 | end = buf->r; |
| 1396 | #endif |
| 1397 | } |
| 1398 | hdr_idx_init(idx); |
| 1399 | state = HTTP_MSG_RPVER; |
| 1400 | goto http_msg_rpver; |
| 1401 | } |
| 1402 | |
| 1403 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1404 | goto http_msg_invalid; |
| 1405 | |
| 1406 | if (unlikely(*ptr == '\n')) |
| 1407 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1408 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1409 | /* stop here */ |
| 1410 | |
| 1411 | http_msg_rpbefore_cr: |
| 1412 | case HTTP_MSG_RPBEFORE_CR: |
| 1413 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1414 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1415 | /* stop here */ |
| 1416 | |
| 1417 | http_msg_rpver: |
| 1418 | case HTTP_MSG_RPVER: |
| 1419 | case HTTP_MSG_RPVER_SP: |
| 1420 | case HTTP_MSG_RPCODE: |
| 1421 | case HTTP_MSG_RPCODE_SP: |
| 1422 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1423 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1424 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1425 | if (unlikely(!ptr)) |
| 1426 | return; |
| 1427 | |
| 1428 | /* we have a full response and we know that we have either a CR |
| 1429 | * or an LF at <ptr>. |
| 1430 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1431 | //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] | 1432 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1433 | |
| 1434 | msg->sol = ptr; |
| 1435 | if (likely(*ptr == '\r')) |
| 1436 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1437 | goto http_msg_rpline_end; |
| 1438 | |
| 1439 | http_msg_rpline_end: |
| 1440 | case HTTP_MSG_RPLINE_END: |
| 1441 | /* msg->sol must point to the first of CR or LF. */ |
| 1442 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1443 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1444 | /* stop here */ |
| 1445 | |
| 1446 | /* |
| 1447 | * Second, states that are specific to the request only |
| 1448 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | http_msg_rqbefore: |
| 1450 | case HTTP_MSG_RQBEFORE: |
| 1451 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1452 | if (likely(ptr == buf->data)) { |
| 1453 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1454 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | } else { |
| 1456 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1457 | /* only skip empty leading lines, don't remove them */ |
| 1458 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1459 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1460 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | /* Remove empty leading lines, as recommended by |
| 1462 | * RFC2616. This takes a lot of time because we |
| 1463 | * must move all the buffer backwards, but this |
| 1464 | * is rarely needed. The method above will be |
| 1465 | * cleaner when we'll be able to start sending |
| 1466 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1467 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | buf->lr = ptr; |
| 1469 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1470 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | msg->sol = buf->data; |
| 1472 | ptr = buf->data; |
| 1473 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1474 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1476 | /* we will need this when keep-alive will be supported |
| 1477 | hdr_idx_init(idx); |
| 1478 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1479 | state = HTTP_MSG_RQMETH; |
| 1480 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1484 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | if (unlikely(*ptr == '\n')) |
| 1487 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1488 | 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] | 1489 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | http_msg_rqbefore_cr: |
| 1492 | case HTTP_MSG_RQBEFORE_CR: |
| 1493 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1494 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1495 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1496 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | http_msg_rqmeth: |
| 1498 | case HTTP_MSG_RQMETH: |
| 1499 | case HTTP_MSG_RQMETH_SP: |
| 1500 | case HTTP_MSG_RQURI: |
| 1501 | case HTTP_MSG_RQURI_SP: |
| 1502 | case HTTP_MSG_RQVER: |
| 1503 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1504 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | if (unlikely(!ptr)) |
| 1506 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | /* we have a full request and we know that we have either a CR |
| 1509 | * or an LF at <ptr>. |
| 1510 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1511 | //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] | 1512 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1513 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | msg->sol = ptr; |
| 1515 | if (likely(*ptr == '\r')) |
| 1516 | 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] | 1517 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1518 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | http_msg_rqline_end: |
| 1520 | case HTTP_MSG_RQLINE_END: |
| 1521 | /* check for HTTP/0.9 request : no version information available. |
| 1522 | * msg->sol must point to the first of CR or LF. |
| 1523 | */ |
| 1524 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1525 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1526 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1528 | 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] | 1529 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1531 | /* |
| 1532 | * Common states below |
| 1533 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | http_msg_hdr_first: |
| 1535 | case HTTP_MSG_HDR_FIRST: |
| 1536 | msg->sol = ptr; |
| 1537 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1538 | goto http_msg_hdr_name; |
| 1539 | } |
| 1540 | |
| 1541 | if (likely(*ptr == '\r')) |
| 1542 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1543 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1545 | http_msg_hdr_name: |
| 1546 | case HTTP_MSG_HDR_NAME: |
| 1547 | /* assumes msg->sol points to the first char */ |
| 1548 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1549 | 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] | 1550 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | if (likely(*ptr == ':')) { |
| 1552 | msg->col = ptr - buf->data; |
| 1553 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1554 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1555 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1557 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | http_msg_hdr_l1_sp: |
| 1559 | case HTTP_MSG_HDR_L1_SP: |
| 1560 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1561 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1562 | 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] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | /* header value can be basically anything except CR/LF */ |
| 1565 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1566 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1568 | goto http_msg_hdr_val; |
| 1569 | } |
| 1570 | |
| 1571 | if (likely(*ptr == '\r')) |
| 1572 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1573 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1574 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | http_msg_hdr_l1_lf: |
| 1576 | case HTTP_MSG_HDR_L1_LF: |
| 1577 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1578 | 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] | 1579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | http_msg_hdr_l1_lws: |
| 1581 | case HTTP_MSG_HDR_L1_LWS: |
| 1582 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1583 | /* replace HT,CR,LF with spaces */ |
| 1584 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1585 | buf->data[msg->sov] = ' '; |
| 1586 | goto http_msg_hdr_l1_sp; |
| 1587 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1588 | /* we had a header consisting only in spaces ! */ |
| 1589 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | goto http_msg_complete_header; |
| 1591 | |
| 1592 | http_msg_hdr_val: |
| 1593 | case HTTP_MSG_HDR_VAL: |
| 1594 | /* assumes msg->sol points to the first char, msg->col to the |
| 1595 | * colon, and msg->sov points to the first character of the |
| 1596 | * value. |
| 1597 | */ |
| 1598 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1599 | 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] | 1600 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | msg->eol = ptr; |
| 1602 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1603 | * real header end in case it ends with lots of LWS, but is this |
| 1604 | * really needed ? |
| 1605 | */ |
| 1606 | if (likely(*ptr == '\r')) |
| 1607 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1608 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | http_msg_hdr_l2_lf: |
| 1611 | case HTTP_MSG_HDR_L2_LF: |
| 1612 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1613 | 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] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | http_msg_hdr_l2_lws: |
| 1616 | case HTTP_MSG_HDR_L2_LWS: |
| 1617 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1618 | /* LWS: replace HT,CR,LF with spaces */ |
| 1619 | for (; msg->eol < ptr; msg->eol++) |
| 1620 | *msg->eol = ' '; |
| 1621 | goto http_msg_hdr_val; |
| 1622 | } |
| 1623 | http_msg_complete_header: |
| 1624 | /* |
| 1625 | * It was a new header, so the last one is finished. |
| 1626 | * Assumes msg->sol points to the first char, msg->col to the |
| 1627 | * colon, msg->sov points to the first character of the value |
| 1628 | * and msg->eol to the first CR or LF so we know how the line |
| 1629 | * ends. We insert last header into the index. |
| 1630 | */ |
| 1631 | /* |
| 1632 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1633 | write(2, msg->sol, msg->eol-msg->sol); |
| 1634 | fprintf(stderr,"\n"); |
| 1635 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1638 | idx, idx->tail) < 0)) |
| 1639 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1640 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | msg->sol = ptr; |
| 1642 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1643 | goto http_msg_hdr_name; |
| 1644 | } |
| 1645 | |
| 1646 | if (likely(*ptr == '\r')) |
| 1647 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1648 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1649 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1650 | http_msg_last_lf: |
| 1651 | case HTTP_MSG_LAST_LF: |
| 1652 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1653 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1654 | ptr++; |
| 1655 | buf->lr = ptr; |
| 1656 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1657 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | return; |
| 1659 | #ifdef DEBUG_FULL |
| 1660 | default: |
| 1661 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1662 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1663 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | } |
| 1665 | http_msg_ood: |
| 1666 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1667 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | buf->lr = ptr; |
| 1669 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | http_msg_invalid: |
| 1672 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1673 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | return; |
| 1675 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1677 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1678 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1679 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1680 | * t->req->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1681 | * functions. Its behaviour is rather simple : |
| 1682 | * - all enabled analysers are called in turn from the lower to the higher |
| 1683 | * bit. |
| 1684 | * - if an analyser does not have enough data, it must return without calling |
| 1685 | * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure |
| 1686 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1687 | * the protocol. Generally it is not reset in case of errors. |
| 1688 | * - if an analyser has enough data, it just has to pass on to the next |
| 1689 | * analyser without touching BF_MAY_FORWARD (it is enabled prior to |
| 1690 | * analysis). |
| 1691 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1692 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1693 | * |
| 1694 | * In the future, analysers should be able to indicate that they want to be |
| 1695 | * called after XXX bytes have been received (or transfered), and the min of |
| 1696 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1697 | * |
| 1698 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1699 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1700 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | struct buffer *req = t->req; |
| 1703 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1704 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1705 | 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] | 1706 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1707 | t, |
| 1708 | req, |
| 1709 | req->rex, req->wex, |
| 1710 | req->flags, |
| 1711 | req->l, |
| 1712 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1714 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1715 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1716 | struct tcp_rule *rule; |
| 1717 | int partial; |
| 1718 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1719 | /* We will abort if we encounter a read error. In theory, we |
| 1720 | * should not abort if we get a close, it might be valid, |
| 1721 | * although very unlikely. FIXME: we'll abort for now, this |
| 1722 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1723 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1724 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1725 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1726 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1727 | if (!(t->flags & SN_ERR_MASK)) |
| 1728 | t->flags |= SN_ERR_CLICL; |
| 1729 | if (!(t->flags & SN_FINST_MASK)) |
| 1730 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1731 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1735 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1736 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1737 | t->fe->failed_req++; |
| 1738 | if (!(t->flags & SN_ERR_MASK)) |
| 1739 | t->flags |= SN_ERR_CLITO; |
| 1740 | if (!(t->flags & SN_FINST_MASK)) |
| 1741 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1742 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /* We don't know whether we have enough data, so must proceed |
| 1746 | * this way : |
| 1747 | * - iterate through all rules in their declaration order |
| 1748 | * - if one rule returns MISS, it means the inspect delay is |
| 1749 | * not over yet, then return immediately, otherwise consider |
| 1750 | * it as a non-match. |
| 1751 | * - if one rule returns OK, then return OK |
| 1752 | * - if one rule returns KO, then return KO |
| 1753 | */ |
| 1754 | |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1755 | if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1756 | partial = 0; |
| 1757 | else |
| 1758 | partial = ACL_PARTIAL; |
| 1759 | |
| 1760 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1761 | int ret = ACL_PAT_PASS; |
| 1762 | |
| 1763 | if (rule->cond) { |
| 1764 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1765 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1766 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1767 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1768 | if (!tick_isset(req->analyse_exp)) |
| 1769 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1770 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1771 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1772 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1773 | ret = acl_pass(ret); |
| 1774 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1775 | ret = !ret; |
| 1776 | } |
| 1777 | |
| 1778 | if (ret) { |
| 1779 | /* we have a matching rule. */ |
| 1780 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1781 | buffer_abort(req); |
| 1782 | buffer_abort(rep); |
| 1783 | //FIXME: this delete this |
| 1784 | //fd_delete(t->cli_fd); |
| 1785 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1786 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1787 | t->fe->failed_req++; |
| 1788 | if (!(t->flags & SN_ERR_MASK)) |
| 1789 | t->flags |= SN_ERR_PRXCOND; |
| 1790 | if (!(t->flags & SN_FINST_MASK)) |
| 1791 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1792 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1793 | } |
| 1794 | /* otherwise accept */ |
| 1795 | break; |
| 1796 | } |
| 1797 | } |
| 1798 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1799 | /* if we get there, it means we have no rule which matches, or |
| 1800 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1801 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1802 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1803 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1804 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1805 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1806 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1807 | /* |
| 1808 | * Now parse the partial (or complete) lines. |
| 1809 | * We will check the request syntax, and also join multi-line |
| 1810 | * headers. An index of all the lines will be elaborated while |
| 1811 | * parsing. |
| 1812 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1813 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1816 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | * req->data + req->eoh = end of processed headers / start of current one |
| 1818 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1819 | * req->lr = first non-visited byte |
| 1820 | * req->r = end of data |
| 1821 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1824 | struct http_txn *txn = &t->txn; |
| 1825 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1829 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1830 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | /* 1: we might have to print this header in debug mode */ |
| 1832 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1833 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1834 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1837 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | eol = sol + msg->sl.rq.l; |
| 1839 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1840 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1841 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1842 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1845 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1847 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1848 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | } |
| 1850 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1851 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1855 | * If not so, we check the FD and buffer states before leaving. |
| 1856 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1857 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1858 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1859 | * |
| 1860 | */ |
| 1861 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1862 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | /* |
| 1864 | * First, let's catch bad requests. |
| 1865 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1866 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* 1: Since we are in header mode, if there's no space |
| 1870 | * left for headers, we won't be able to free more |
| 1871 | * later, so the session will never terminate. We |
| 1872 | * must terminate it now. |
| 1873 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1874 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | /* FIXME: check if URI is set and return Status |
| 1876 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1877 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1878 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1881 | /* 2: have we encountered a read error ? */ |
| 1882 | else if (req->flags & BF_READ_ERROR) { |
| 1883 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1884 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1885 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1886 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1887 | if (!(t->flags & SN_ERR_MASK)) |
| 1888 | t->flags |= SN_ERR_CLICL; |
| 1889 | if (!(t->flags & SN_FINST_MASK)) |
| 1890 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1891 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1895 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1896 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1897 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1898 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1899 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1900 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1901 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1902 | if (!(t->flags & SN_ERR_MASK)) |
| 1903 | t->flags |= SN_ERR_CLITO; |
| 1904 | if (!(t->flags & SN_FINST_MASK)) |
| 1905 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1906 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1907 | } |
| 1908 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1909 | /* 4: have we encountered a close ? */ |
| 1910 | else if (req->flags & (BF_READ_NULL | BF_SHUTR)) { |
| 1911 | txn->status = 400; |
| 1912 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1913 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1914 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1915 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1916 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1917 | if (!(t->flags & SN_ERR_MASK)) |
| 1918 | t->flags |= SN_ERR_CLICL; |
| 1919 | if (!(t->flags & SN_FINST_MASK)) |
| 1920 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1921 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1923 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1924 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1925 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1926 | if (!tick_isset(req->analyse_exp)) |
| 1927 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1928 | |
| 1929 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1930 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | /**************************************************************** |
| 1935 | * More interesting part now : we know that we have a complete * |
| 1936 | * request which at least looks like HTTP. We have an indicator * |
| 1937 | * of each header's length, so we can parse them quickly. * |
| 1938 | ****************************************************************/ |
| 1939 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1940 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1941 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1942 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1943 | /* ensure we keep this pointer to the beginning of the message */ |
| 1944 | msg->sol = req->data + msg->som; |
| 1945 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1946 | /* |
| 1947 | * 1: identify the method |
| 1948 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1949 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1950 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1951 | /* we can make use of server redirect on GET and HEAD */ |
| 1952 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1953 | t->flags |= SN_REDIRECTABLE; |
| 1954 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1955 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1957 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1958 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1959 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1960 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1961 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1962 | !memcmp(&req->data[msg->sl.rq.u], |
| 1963 | t->fe->monitor_uri, |
| 1964 | t->fe->monitor_uri_len))) { |
| 1965 | /* |
| 1966 | * We have found the monitor URI |
| 1967 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1968 | struct acl_cond *cond; |
| 1969 | cur_proxy = t->fe; |
| 1970 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1971 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* Check if we want to fail this monitor request or not */ |
| 1974 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1975 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1976 | |
| 1977 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1978 | if (cond->pol == ACL_COND_UNLESS) |
| 1979 | ret = !ret; |
| 1980 | |
| 1981 | if (ret) { |
| 1982 | /* we fail this request, let's return 503 service unavail */ |
| 1983 | txn->status = 503; |
| 1984 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1985 | goto return_prx_cond; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1990 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1991 | client_retnclose(t, &http_200_chunk); |
| 1992 | goto return_prx_cond; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1997 | * Note: we cannot log anymore if the request has been |
| 1998 | * classified as invalid. |
| 1999 | */ |
| 2000 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2001 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2002 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2003 | int urilen = msg->sl.rq.l; |
| 2004 | |
| 2005 | if (urilen >= REQURI_LEN) |
| 2006 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2007 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2008 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2009 | |
| 2010 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2011 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2012 | } else { |
| 2013 | Alert("HTTP logging : out of memory.\n"); |
| 2014 | } |
| 2015 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2016 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2017 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2018 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2019 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2020 | int delta; |
| 2021 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2022 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2023 | cur_end = msg->sol + msg->sl.rq.l; |
| 2024 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2025 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2026 | if (msg->sl.rq.u_l == 0) { |
| 2027 | /* if no URI was set, add "/" */ |
| 2028 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2029 | cur_end += delta; |
| 2030 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2031 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2032 | /* add HTTP version */ |
| 2033 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2034 | msg->eoh += delta; |
| 2035 | cur_end += delta; |
| 2036 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2037 | HTTP_MSG_RQMETH, |
| 2038 | msg->sol, cur_end + 1, |
| 2039 | NULL, NULL); |
| 2040 | if (unlikely(!cur_end)) |
| 2041 | goto return_bad_req; |
| 2042 | |
| 2043 | /* we have a full HTTP/1.0 request now and we know that |
| 2044 | * we have either a CR or an LF at <ptr>. |
| 2045 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2046 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2047 | } |
| 2048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2051 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2052 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2053 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2056 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2057 | * As opposed to version 1.2, now they will be evaluated in the |
| 2058 | * filters order and not in the header order. This means that |
| 2059 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2060 | * |
| 2061 | * We can now check whether we want to switch to another |
| 2062 | * backend, in which case we will re-check the backend's |
| 2063 | * filters and various options. In order to support 3-level |
| 2064 | * switching, here's how we should proceed : |
| 2065 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2066 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2067 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2068 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2069 | * There cannot be any switch from there, so ->be cannot be |
| 2070 | * changed anymore. |
| 2071 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2072 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2073 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2074 | * The response path will be able to apply either ->be, or |
| 2075 | * ->be then ->fe filters in order to match the reverse of |
| 2076 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2077 | */ |
| 2078 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2079 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2080 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2081 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2082 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2083 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2084 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2085 | /* first check whether we have some ACLs set to redirect this request */ |
| 2086 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2087 | int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2088 | |
| 2089 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2090 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2091 | ret = !ret; |
| 2092 | |
| 2093 | if (ret) { |
| 2094 | struct chunk rdr = { trash, 0 }; |
| 2095 | const char *msg_fmt; |
| 2096 | |
| 2097 | /* build redirect message */ |
| 2098 | switch(rule->code) { |
| 2099 | case 303: |
| 2100 | rdr.len = strlen(HTTP_303); |
| 2101 | msg_fmt = HTTP_303; |
| 2102 | break; |
| 2103 | case 301: |
| 2104 | rdr.len = strlen(HTTP_301); |
| 2105 | msg_fmt = HTTP_301; |
| 2106 | break; |
| 2107 | case 302: |
| 2108 | default: |
| 2109 | rdr.len = strlen(HTTP_302); |
| 2110 | msg_fmt = HTTP_302; |
| 2111 | break; |
| 2112 | } |
| 2113 | |
| 2114 | if (unlikely(rdr.len > sizeof(trash))) |
| 2115 | goto return_bad_req; |
| 2116 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2117 | |
| 2118 | switch(rule->type) { |
| 2119 | case REDIRECT_TYPE_PREFIX: { |
| 2120 | const char *path; |
| 2121 | int pathlen; |
| 2122 | |
| 2123 | path = http_get_path(txn); |
| 2124 | /* build message using path */ |
| 2125 | if (path) { |
| 2126 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2127 | } else { |
| 2128 | path = "/"; |
| 2129 | pathlen = 1; |
| 2130 | } |
| 2131 | |
| 2132 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2133 | goto return_bad_req; |
| 2134 | |
| 2135 | /* add prefix */ |
| 2136 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2137 | rdr.len += rule->rdr_len; |
| 2138 | |
| 2139 | /* add path */ |
| 2140 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2141 | rdr.len += pathlen; |
| 2142 | break; |
| 2143 | } |
| 2144 | case REDIRECT_TYPE_LOCATION: |
| 2145 | default: |
| 2146 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2147 | goto return_bad_req; |
| 2148 | |
| 2149 | /* add location */ |
| 2150 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2151 | rdr.len += rule->rdr_len; |
| 2152 | break; |
| 2153 | } |
| 2154 | |
| 2155 | /* add end of headers */ |
| 2156 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2157 | rdr.len += 4; |
| 2158 | |
| 2159 | txn->status = rule->code; |
| 2160 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2161 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2162 | client_retnclose(t, &rdr); |
| 2163 | goto return_prx_cond; |
| 2164 | } |
| 2165 | } |
| 2166 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2167 | /* first check whether we have some ACLs set to block this request */ |
| 2168 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2169 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2170 | |
| 2171 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2172 | if (cond->pol == ACL_COND_UNLESS) |
| 2173 | ret = !ret; |
| 2174 | |
| 2175 | if (ret) { |
| 2176 | txn->status = 403; |
| 2177 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2178 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2179 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2180 | goto return_prx_cond; |
| 2181 | } |
| 2182 | } |
| 2183 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2184 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2185 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2186 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2187 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2190 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2191 | /* to ensure correct connection accounting on |
| 2192 | * the backend, we count the connection for the |
| 2193 | * one managing the queue. |
| 2194 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2195 | t->be->beconn++; |
| 2196 | if (t->be->beconn > t->be->beconn_max) |
| 2197 | t->be->beconn_max = t->be->beconn; |
| 2198 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2199 | t->flags |= SN_BE_ASSIGNED; |
| 2200 | } |
| 2201 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2202 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2203 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2204 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2205 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2206 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2207 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2208 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2209 | goto return_prx_cond; |
| 2210 | } |
| 2211 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2212 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2213 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2214 | !(t->flags & SN_CONN_CLOSED)) { |
| 2215 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2216 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2217 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2218 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2219 | 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] | 2220 | old_idx = 0; |
| 2221 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2222 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2223 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2224 | cur_ptr = cur_next; |
| 2225 | cur_end = cur_ptr + cur_hdr->len; |
| 2226 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2227 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2228 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2229 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2230 | /* 3 possibilities : |
| 2231 | * - we have already set Connection: close, |
| 2232 | * so we remove this line. |
| 2233 | * - we have not yet set Connection: close, |
| 2234 | * but this line indicates close. We leave |
| 2235 | * it untouched and set the flag. |
| 2236 | * - we have not yet set Connection: close, |
| 2237 | * and this line indicates non-close. We |
| 2238 | * replace it. |
| 2239 | */ |
| 2240 | if (t->flags & SN_CONN_CLOSED) { |
| 2241 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2242 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2243 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2244 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2245 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2246 | cur_hdr->len = 0; |
| 2247 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2248 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2249 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2250 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2251 | cur_next += delta; |
| 2252 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2253 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2254 | } |
| 2255 | t->flags |= SN_CONN_CLOSED; |
| 2256 | } |
| 2257 | } |
| 2258 | old_idx = cur_idx; |
| 2259 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2260 | } |
| 2261 | /* add request headers from the rule sets in the same order */ |
| 2262 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2263 | if (unlikely(http_header_add_tail(req, |
| 2264 | &txn->req, |
| 2265 | &txn->hdr_idx, |
| 2266 | rule_set->req_add[cur_idx])) < 0) |
| 2267 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2268 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2269 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2270 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2271 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2272 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2273 | /* we have to check the URI and auth for this request. |
| 2274 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2275 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2276 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2277 | if (stats_check_uri_auth(t, rule_set)) |
| 2278 | return 1; |
| 2279 | } |
| 2280 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2281 | /* now check whether we have some switching rules for this request */ |
| 2282 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2283 | struct switching_rule *rule; |
| 2284 | |
| 2285 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2286 | int ret; |
| 2287 | |
| 2288 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2289 | |
| 2290 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2291 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2292 | ret = !ret; |
| 2293 | |
| 2294 | if (ret) { |
| 2295 | t->be = rule->be.backend; |
| 2296 | t->be->beconn++; |
| 2297 | if (t->be->beconn > t->be->beconn_max) |
| 2298 | t->be->beconn_max = t->be->beconn; |
| 2299 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2300 | |
| 2301 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2302 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2303 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2304 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2305 | t->flags |= SN_BE_ASSIGNED; |
| 2306 | break; |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2311 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2312 | /* No backend was set, but there was a default |
| 2313 | * backend set in the frontend, so we use it and |
| 2314 | * loop again. |
| 2315 | */ |
| 2316 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2317 | t->be->beconn++; |
| 2318 | if (t->be->beconn > t->be->beconn_max) |
| 2319 | t->be->beconn_max = t->be->beconn; |
| 2320 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2321 | |
| 2322 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2323 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2324 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2325 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2326 | t->flags |= SN_BE_ASSIGNED; |
| 2327 | } |
| 2328 | } while (t->be != cur_proxy); /* we loop only if t->be has changed */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2329 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2330 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2331 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2332 | /* To ensure correct connection accounting on |
| 2333 | * the backend, we count the connection for the |
| 2334 | * one managing the queue. |
| 2335 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2336 | t->be->beconn++; |
| 2337 | if (t->be->beconn > t->be->beconn_max) |
| 2338 | t->be->beconn_max = t->be->beconn; |
| 2339 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2340 | t->flags |= SN_BE_ASSIGNED; |
| 2341 | } |
| 2342 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2343 | /* |
| 2344 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2345 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2346 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2347 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2348 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2349 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2350 | /* |
| 2351 | * If HTTP PROXY is set we simply get remote server address |
| 2352 | * parsing incoming request. |
| 2353 | */ |
| 2354 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2355 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2356 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2357 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2358 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2359 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2360 | * so let's do the same now. |
| 2361 | */ |
| 2362 | |
| 2363 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2364 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2365 | get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | |
| 2369 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2370 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2371 | * Note that doing so might move headers in the request, but |
| 2372 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2373 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2374 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2375 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2376 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2377 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2378 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2379 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2380 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2381 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2382 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2383 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2384 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2385 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2386 | /* Add an X-Forwarded-For header unless the source IP is |
| 2387 | * in the 'except' network range. |
| 2388 | */ |
| 2389 | if ((!t->fe->except_mask.s_addr || |
| 2390 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2391 | != t->fe->except_net.s_addr) && |
| 2392 | (!t->be->except_mask.s_addr || |
| 2393 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2394 | != t->be->except_net.s_addr)) { |
| 2395 | int len; |
| 2396 | unsigned char *pn; |
| 2397 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2398 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2399 | /* Note: we rely on the backend to get the header name to be used for |
| 2400 | * x-forwarded-for, because the header is really meant for the backends. |
| 2401 | * However, if the backend did not specify any option, we have to rely |
| 2402 | * on the frontend's header name. |
| 2403 | */ |
| 2404 | if (t->be->fwdfor_hdr_len) { |
| 2405 | len = t->be->fwdfor_hdr_len; |
| 2406 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2407 | } else { |
| 2408 | len = t->fe->fwdfor_hdr_len; |
| 2409 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2410 | } |
| 2411 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2412 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2413 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2414 | &txn->hdr_idx, trash, len)) < 0) |
| 2415 | goto return_bad_req; |
| 2416 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2417 | } |
| 2418 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2419 | /* FIXME: for the sake of completeness, we should also support |
| 2420 | * 'except' here, although it is mostly useless in this case. |
| 2421 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2422 | int len; |
| 2423 | char pn[INET6_ADDRSTRLEN]; |
| 2424 | inet_ntop(AF_INET6, |
| 2425 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2426 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* Note: we rely on the backend to get the header name to be used for |
| 2429 | * x-forwarded-for, because the header is really meant for the backends. |
| 2430 | * However, if the backend did not specify any option, we have to rely |
| 2431 | * on the frontend's header name. |
| 2432 | */ |
| 2433 | if (t->be->fwdfor_hdr_len) { |
| 2434 | len = t->be->fwdfor_hdr_len; |
| 2435 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2436 | } else { |
| 2437 | len = t->fe->fwdfor_hdr_len; |
| 2438 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2439 | } |
| 2440 | len += sprintf(trash + len, ": %s", pn); |
| 2441 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2442 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2443 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2444 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2445 | } |
| 2446 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2447 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2448 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2449 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2450 | * Note that we do not need to add it in case of HTTP/1.0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2451 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2452 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2453 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2454 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2455 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2456 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2457 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2458 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2459 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2460 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2461 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2462 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2463 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2464 | * (unless headers overflowed the buffer?) |
| 2465 | */ |
| 2466 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2467 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2468 | t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2469 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2470 | /* are there enough bytes here? total == l || r || rlim ? |
| 2471 | * len is unsigned, but eoh is int, |
| 2472 | * how many bytes of body have we received? |
| 2473 | * eoh is the first empty line of the header |
| 2474 | */ |
| 2475 | /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */ |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 2476 | unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2477 | |
| 2478 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2479 | * We can't assume responsibility for the server's decision, |
| 2480 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2481 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2482 | */ |
| 2483 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2484 | struct hdr_ctx ctx; |
| 2485 | ctx.idx = 0; |
| 2486 | /* Expect is allowed in 1.1, look for it */ |
| 2487 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2488 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2489 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2490 | /* We can't reliablly stall and wait for data, because of |
| 2491 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2492 | * the next block to check length expectations. |
| 2493 | * We could send 100 status back to the client, but then we need to |
| 2494 | * re-write headers, and send the message. And this isn't the right |
| 2495 | * place for that action. |
| 2496 | * TODO: support Expect elsewhere and delete this block. |
| 2497 | */ |
| 2498 | goto end_check_maybe_wait_for_body; |
| 2499 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2500 | |
| 2501 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2502 | /* nothing to do, we got enough */ |
| 2503 | } else { |
| 2504 | /* limit implies we are supposed to need this many bytes |
| 2505 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2506 | */ |
| 2507 | long long hint = len; |
| 2508 | struct hdr_ctx ctx; |
| 2509 | ctx.idx = 0; |
| 2510 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2511 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2512 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2513 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2514 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2515 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2516 | ctx.idx = 0; |
| 2517 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2518 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2519 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2520 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2521 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2522 | hint = 0; /* parse failure, untrusted client */ |
| 2523 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2524 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2525 | msg->hdr_content_len = hint; |
| 2526 | else |
| 2527 | hint = 0; /* bad client, sent negative length */ |
| 2528 | } |
| 2529 | } |
| 2530 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2531 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2532 | hint = t->be->url_param_post_limit; |
| 2533 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2534 | if (len < hint) { |
| 2535 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2536 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2537 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2538 | /* else... There are no body bytes to wait for */ |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2544 | /************************************************************* |
| 2545 | * OK, that's finished for the headers. We have done what we * |
| 2546 | * could. Let's switch to the DATA state. * |
| 2547 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2548 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2549 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2550 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2551 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2552 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2553 | * which may be the same as the connect timeout if unspecified. |
| 2554 | * If unset, then set it to zero because we really want it to |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2555 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2556 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2557 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2558 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2559 | /* flush the request so that we can drop the connection early |
| 2560 | * if the client closes first. |
| 2561 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2562 | req->flags &= ~BF_MAY_FORWARD; |
| 2563 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2564 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2565 | if (!req->analyse_exp) |
| 2566 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2567 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2569 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2570 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2571 | |
| 2572 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2573 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2574 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2575 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2576 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2577 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2578 | return_prx_cond: |
| 2579 | if (!(t->flags & SN_ERR_MASK)) |
| 2580 | t->flags |= SN_ERR_PRXCOND; |
| 2581 | if (!(t->flags & SN_FINST_MASK)) |
| 2582 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2583 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2584 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2585 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2588 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2589 | struct http_txn *txn = &t->txn; |
| 2590 | |
| 2591 | /* This connection is being tarpitted. The CLIENT side has |
| 2592 | * already set the connect expiration date to the right |
| 2593 | * timeout. We just have to check that the client is still |
| 2594 | * there and that the timeout has not expired. |
| 2595 | */ |
| 2596 | if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 && |
| 2597 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2598 | return 0; |
| 2599 | |
| 2600 | /* We will set the queue timer to the time spent, just for |
| 2601 | * logging purposes. We fake a 500 server error, so that the |
| 2602 | * attacker will not suspect his connection has been tarpitted. |
| 2603 | * It will not cause trouble to the logs because we can exclude |
| 2604 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2605 | */ |
| 2606 | trace_term(t, TT_HTTP_SRV_2); |
| 2607 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2608 | |
| 2609 | txn->status = 500; |
| 2610 | if (req->flags != BF_READ_ERROR) |
| 2611 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2612 | |
| 2613 | req->analysers = 0; |
| 2614 | req->analyse_exp = TICK_ETERNITY; |
| 2615 | |
| 2616 | t->fe->failed_req++; |
| 2617 | if (!(t->flags & SN_ERR_MASK)) |
| 2618 | t->flags |= SN_ERR_PRXCOND; |
| 2619 | if (!(t->flags & SN_FINST_MASK)) |
| 2620 | t->flags |= SN_FINST_T; |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2624 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2625 | /* We have to parse the HTTP request body to find any required data. |
| 2626 | * "balance url_param check_post" should have been the only way to get |
| 2627 | * into this. We were brought here after HTTP header analysis, so all |
| 2628 | * related structures are ready. |
| 2629 | */ |
| 2630 | struct http_msg *msg = &t->txn.req; |
| 2631 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2632 | long long limit = t->be->url_param_post_limit; |
| 2633 | struct hdr_ctx ctx; |
| 2634 | |
| 2635 | ctx.idx = 0; |
| 2636 | |
| 2637 | /* now if we have a length, we'll take the hint */ |
| 2638 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2639 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2640 | unsigned int chunk = 0; |
| 2641 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2642 | char c = msg->sol[body]; |
| 2643 | if (ishex(c)) { |
| 2644 | unsigned int hex = toupper(c) - '0'; |
| 2645 | if (hex > 9) |
| 2646 | hex -= 'A' - '9' - 1; |
| 2647 | chunk = (chunk << 4) | hex; |
| 2648 | } else |
| 2649 | break; |
| 2650 | body++; |
| 2651 | } |
| 2652 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2653 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2654 | |
| 2655 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2656 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2657 | |
| 2658 | body += 2; // skip CRLF |
| 2659 | |
| 2660 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2661 | * 1. how much entity data do we have? new var |
| 2662 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2663 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2664 | */ |
| 2665 | |
| 2666 | if (chunk < limit) |
| 2667 | limit = chunk; /* only reading one chunk */ |
| 2668 | } else { |
| 2669 | if (msg->hdr_content_len < limit) |
| 2670 | limit = msg->hdr_content_len; |
| 2671 | } |
| 2672 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2673 | http_body_end: |
| 2674 | /* we leave once we know we have nothing left to do. This means that we have |
| 2675 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2676 | * buffer closed). |
| 2677 | */ |
| 2678 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2679 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2680 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2681 | /* The situation will not evolve, so let's give up on the analysis. */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2682 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2683 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2684 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2685 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2686 | else { |
| 2687 | /* Not enough data. We'll re-use the http-request |
| 2688 | * timeout here. Ideally, we should set the timeout |
| 2689 | * relative to the accept() date. We just set the |
| 2690 | * request timeout once at the beginning of the |
| 2691 | * request. |
| 2692 | */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2693 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2694 | if (!tick_isset(req->analyse_exp)) |
| 2695 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2696 | return 0; |
| 2697 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2698 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2699 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2700 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2701 | * probably reduce one day's debugging session. |
| 2702 | */ |
| 2703 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2704 | if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2705 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2706 | __FILE__, __LINE__, req->analysers); |
| 2707 | ABORT_NOW(); |
| 2708 | } |
| 2709 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2710 | req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2711 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2712 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2713 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2714 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2715 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2716 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2717 | * 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] | 2718 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2719 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2720 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2721 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2722 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2723 | struct buffer *req = t->req; |
| 2724 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2726 | 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] | 2727 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2728 | t, |
| 2729 | rep, |
| 2730 | rep->rex, rep->wex, |
| 2731 | rep->flags, |
| 2732 | rep->l, |
| 2733 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2734 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2735 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2736 | /* |
| 2737 | * Now parse the partial (or complete) lines. |
| 2738 | * We will check the response syntax, and also join multi-line |
| 2739 | * headers. An index of all the lines will be elaborated while |
| 2740 | * parsing. |
| 2741 | * |
| 2742 | * For the parsing, we use a 28 states FSM. |
| 2743 | * |
| 2744 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2745 | * rep->data + rep->som = beginning of response |
| 2746 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2747 | * 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] | 2748 | * rep->lr = first non-visited byte |
| 2749 | * rep->r = end of data |
| 2750 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2751 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2752 | int cur_idx; |
| 2753 | struct http_msg *msg = &txn->rsp; |
| 2754 | struct proxy *cur_proxy; |
| 2755 | |
| 2756 | if (likely(rep->lr < rep->r)) |
| 2757 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2758 | |
| 2759 | /* 1: we might have to print this header in debug mode */ |
| 2760 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2761 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2762 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2763 | char *eol, *sol; |
| 2764 | |
| 2765 | sol = rep->data + msg->som; |
| 2766 | eol = sol + msg->sl.rq.l; |
| 2767 | debug_hdr("srvrep", t, sol, eol); |
| 2768 | |
| 2769 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2770 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2771 | |
| 2772 | while (cur_idx) { |
| 2773 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2774 | debug_hdr("srvhdr", t, sol, eol); |
| 2775 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2776 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2777 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2778 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2780 | /* |
| 2781 | * Now we quickly check if we have found a full valid response. |
| 2782 | * If not so, we check the FD and buffer states before leaving. |
| 2783 | * A full response is indicated by the fact that we have seen |
| 2784 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2785 | * responses are checked first. |
| 2786 | * |
| 2787 | * Depending on whether the client is still there or not, we |
| 2788 | * may send an error response back or not. Note that normally |
| 2789 | * we should only check for HTTP status there, and check I/O |
| 2790 | * errors somewhere else. |
| 2791 | */ |
| 2792 | |
| 2793 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2794 | /* Invalid response */ |
| 2795 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2796 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2797 | //buffer_shutr(rep); |
| 2798 | //buffer_shutw(req); |
| 2799 | //fd_delete(req->cons->fd); |
| 2800 | //req->cons->state = SI_ST_CLO; |
| 2801 | buffer_shutr_now(rep); |
| 2802 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2803 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2804 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2805 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2806 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2807 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2808 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2809 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2810 | txn->status = 502; |
| 2811 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2812 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2813 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2814 | if (!(t->flags & SN_FINST_MASK)) |
| 2815 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2817 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2818 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2819 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2820 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2821 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2822 | /* too large response does not fit in buffer. */ |
| 2823 | else if (rep->flags & BF_FULL) { |
| 2824 | goto hdr_response_bad; |
| 2825 | } |
| 2826 | /* read error */ |
| 2827 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2828 | buffer_shutr_now(rep); |
| 2829 | buffer_shutw_now(req); |
| 2830 | //fd_delete(req->cons->fd); |
| 2831 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2832 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2833 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2834 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2835 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2836 | //} |
| 2837 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2838 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2839 | txn->status = 502; |
| 2840 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2841 | if (!(t->flags & SN_ERR_MASK)) |
| 2842 | t->flags |= SN_ERR_SRVCL; |
| 2843 | if (!(t->flags & SN_FINST_MASK)) |
| 2844 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2845 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2846 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2847 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2849 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2850 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2851 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2852 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2853 | buffer_shutr_now(rep); |
| 2854 | buffer_shutw_now(req); |
| 2855 | //fd_delete(req->cons->fd); |
| 2856 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2857 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2858 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2859 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2860 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2861 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2862 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2863 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2864 | txn->status = 504; |
| 2865 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2866 | if (!(t->flags & SN_ERR_MASK)) |
| 2867 | t->flags |= SN_ERR_SRVTO; |
| 2868 | if (!(t->flags & SN_FINST_MASK)) |
| 2869 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2870 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2871 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2872 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2873 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2874 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2875 | /* write error to client, or close from server */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2876 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR|BF_READ_NULL)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2877 | buffer_shutr_now(rep); |
| 2878 | buffer_shutw_now(req); |
| 2879 | //fd_delete(req->cons->fd); |
| 2880 | //req->cons->state = SI_ST_CLO; |
| 2881 | if (t->srv) { |
| 2882 | //t->srv->cur_sess--; |
| 2883 | t->srv->failed_resp++; |
| 2884 | //sess_change_server(t, NULL); |
| 2885 | } |
| 2886 | t->be->failed_resp++; |
| 2887 | rep->analysers = 0; |
| 2888 | txn->status = 502; |
| 2889 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2890 | if (!(t->flags & SN_ERR_MASK)) |
| 2891 | t->flags |= SN_ERR_SRVCL; |
| 2892 | if (!(t->flags & SN_FINST_MASK)) |
| 2893 | t->flags |= SN_FINST_H; |
| 2894 | |
| 2895 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2896 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2897 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2898 | return 0; |
| 2899 | } |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2900 | rep->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2901 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2902 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2903 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2904 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2905 | /***************************************************************** |
| 2906 | * More interesting part now : we know that we have a complete * |
| 2907 | * response which at least looks like HTTP. We have an indicator * |
| 2908 | * of each header's length, so we can parse them quickly. * |
| 2909 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2910 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2911 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2912 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2913 | /* ensure we keep this pointer to the beginning of the message */ |
| 2914 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2915 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2916 | /* |
| 2917 | * 1: get the status code and check for cacheability. |
| 2918 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2919 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2920 | t->logs.logwait &= ~LW_RESP; |
| 2921 | 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] | 2922 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2923 | switch (txn->status) { |
| 2924 | case 200: |
| 2925 | case 203: |
| 2926 | case 206: |
| 2927 | case 300: |
| 2928 | case 301: |
| 2929 | case 410: |
| 2930 | /* RFC2616 @13.4: |
| 2931 | * "A response received with a status code of |
| 2932 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2933 | * by a cache (...) unless a cache-control |
| 2934 | * directive prohibits caching." |
| 2935 | * |
| 2936 | * RFC2616 @9.5: POST method : |
| 2937 | * "Responses to this method are not cacheable, |
| 2938 | * unless the response includes appropriate |
| 2939 | * Cache-Control or Expires header fields." |
| 2940 | */ |
| 2941 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2942 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2943 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2944 | break; |
| 2945 | default: |
| 2946 | break; |
| 2947 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2948 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2949 | /* |
| 2950 | * 2: we may need to capture headers |
| 2951 | */ |
| 2952 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2953 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2954 | txn->rsp.cap, t->fe->rsp_cap); |
| 2955 | |
| 2956 | /* |
| 2957 | * 3: we will have to evaluate the filters. |
| 2958 | * As opposed to version 1.2, now they will be evaluated in the |
| 2959 | * filters order and not in the header order. This means that |
| 2960 | * each filter has to be validated among all headers. |
| 2961 | * |
| 2962 | * Filters are tried with ->be first, then with ->fe if it is |
| 2963 | * different from ->be. |
| 2964 | */ |
| 2965 | |
| 2966 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2967 | |
| 2968 | cur_proxy = t->be; |
| 2969 | while (1) { |
| 2970 | struct proxy *rule_set = cur_proxy; |
| 2971 | |
| 2972 | /* try headers filters */ |
| 2973 | if (rule_set->rsp_exp != NULL) { |
| 2974 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2975 | return_bad_resp: |
| 2976 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2977 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2978 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2979 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2980 | } |
| 2981 | cur_proxy->failed_resp++; |
| 2982 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2983 | buffer_shutr_now(rep); |
| 2984 | buffer_shutw_now(req); |
| 2985 | //fd_delete(req->cons->fd); |
| 2986 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2987 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2988 | txn->status = 502; |
| 2989 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2990 | if (!(t->flags & SN_ERR_MASK)) |
| 2991 | t->flags |= SN_ERR_PRXCOND; |
| 2992 | if (!(t->flags & SN_FINST_MASK)) |
| 2993 | t->flags |= SN_FINST_H; |
| 2994 | /* We used to have a free connection slot. Since we'll never use it, |
| 2995 | * we have to inform the server that it may be used by another session. |
| 2996 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2997 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2998 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2999 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3000 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3001 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3002 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3003 | /* has the response been denied ? */ |
| 3004 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3005 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3006 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3007 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3008 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3009 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3010 | cur_proxy->denied_resp++; |
| 3011 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3012 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3013 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3014 | /* We might have to check for "Connection:" */ |
| 3015 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3016 | !(t->flags & SN_CONN_CLOSED)) { |
| 3017 | char *cur_ptr, *cur_end, *cur_next; |
| 3018 | int cur_idx, old_idx, delta, val; |
| 3019 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3020 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3021 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3022 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3023 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3024 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3025 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3026 | cur_ptr = cur_next; |
| 3027 | cur_end = cur_ptr + cur_hdr->len; |
| 3028 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3029 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3030 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3031 | if (val) { |
| 3032 | /* 3 possibilities : |
| 3033 | * - we have already set Connection: close, |
| 3034 | * so we remove this line. |
| 3035 | * - we have not yet set Connection: close, |
| 3036 | * but this line indicates close. We leave |
| 3037 | * it untouched and set the flag. |
| 3038 | * - we have not yet set Connection: close, |
| 3039 | * and this line indicates non-close. We |
| 3040 | * replace it. |
| 3041 | */ |
| 3042 | if (t->flags & SN_CONN_CLOSED) { |
| 3043 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3044 | txn->rsp.eoh += delta; |
| 3045 | cur_next += delta; |
| 3046 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3047 | txn->hdr_idx.used--; |
| 3048 | cur_hdr->len = 0; |
| 3049 | } else { |
| 3050 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3051 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3052 | "close", 5); |
| 3053 | cur_next += delta; |
| 3054 | cur_hdr->len += delta; |
| 3055 | txn->rsp.eoh += delta; |
| 3056 | } |
| 3057 | t->flags |= SN_CONN_CLOSED; |
| 3058 | } |
| 3059 | } |
| 3060 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3061 | } |
| 3062 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3063 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3064 | /* add response headers from the rule sets in the same order */ |
| 3065 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3066 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3067 | rule_set->rsp_add[cur_idx])) < 0) |
| 3068 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3071 | /* check whether we're already working on the frontend */ |
| 3072 | if (cur_proxy == t->fe) |
| 3073 | break; |
| 3074 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3075 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3076 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3077 | /* |
| 3078 | * 4: check for server cookie. |
| 3079 | */ |
| 3080 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3081 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3082 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3083 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3084 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3085 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3086 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3087 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3088 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3089 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3090 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3091 | /* |
| 3092 | * 6: add server cookie in the response if needed |
| 3093 | */ |
| 3094 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3095 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3096 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3097 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3098 | /* the server is known, it's not the one the client requested, we have to |
| 3099 | * insert a set-cookie here, except if we want to insert only on POST |
| 3100 | * requests and this one isn't. Note that servers which don't have cookies |
| 3101 | * (eg: some backup servers) will return a full cookie removal request. |
| 3102 | */ |
| 3103 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3104 | t->be->cookie_name, |
| 3105 | 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] | 3106 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3107 | if (t->be->cookie_domain) |
| 3108 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3109 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3110 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3111 | trash, len)) < 0) |
| 3112 | goto return_bad_resp; |
| 3113 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3114 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3115 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3116 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3117 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3118 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3119 | */ |
| 3120 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3121 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3122 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3123 | |
| 3124 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3125 | "Cache-control: private", 22)) < 0) |
| 3126 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3127 | } |
| 3128 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3129 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3130 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3131 | /* |
| 3132 | * 7: check if result will be cacheable with a cookie. |
| 3133 | * We'll block the response if security checks have caught |
| 3134 | * nasty things such as a cacheable cookie. |
| 3135 | */ |
| 3136 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3137 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3138 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3139 | |
| 3140 | /* we're in presence of a cacheable response containing |
| 3141 | * a set-cookie header. We'll block it as requested by |
| 3142 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3143 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3144 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3145 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3146 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3147 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3148 | } |
| 3149 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3150 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3151 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3152 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3153 | send_log(t->be, LOG_ALERT, |
| 3154 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3155 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3156 | goto return_srv_prx_502; |
| 3157 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3158 | |
| 3159 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3160 | * 8: add "Connection: close" if needed and not yet set. |
| 3161 | * 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] | 3162 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3163 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3164 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3165 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3166 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3167 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3168 | "Connection: close", 17)) < 0) |
| 3169 | goto return_bad_resp; |
| 3170 | t->flags |= SN_CONN_CLOSED; |
| 3171 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3172 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3173 | /************************************************************* |
| 3174 | * OK, that's finished for the headers. We have done what we * |
| 3175 | * could. Let's switch to the DATA state. * |
| 3176 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3177 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3178 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3179 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3180 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3181 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3182 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3183 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3184 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3185 | } |
| 3186 | #endif |
| 3187 | /* if the user wants to log as soon as possible, without counting |
| 3188 | * bytes from the server, then this is the right moment. We have |
| 3189 | * to temporarily assign bytes_out to log what we currently have. |
| 3190 | */ |
| 3191 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3192 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3193 | t->logs.bytes_out = txn->rsp.eoh; |
| 3194 | if (t->fe->to_log & LW_REQ) |
| 3195 | http_sess_log(t); |
| 3196 | else |
| 3197 | tcp_sess_log(t); |
| 3198 | t->logs.bytes_out = 0; |
| 3199 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3200 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3201 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3202 | * otherwise we would not let the client side wake up. |
| 3203 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3204 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3205 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3206 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3207 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3208 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3209 | * probably reduce one day's debugging session. |
| 3210 | */ |
| 3211 | #ifdef DEBUG_DEV |
| 3212 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3213 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3214 | __FILE__, __LINE__, rep->analysers); |
| 3215 | ABORT_NOW(); |
| 3216 | } |
| 3217 | #endif |
| 3218 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3219 | return 0; |
| 3220 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3221 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3222 | ///* |
| 3223 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3224 | // * return 1 if it absolutely wants to be called again. |
| 3225 | // * |
| 3226 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3227 | // * but CL_STCLOSE. |
| 3228 | // */ |
| 3229 | //int process_cli(struct session *t) |
| 3230 | //{ |
| 3231 | // struct buffer *req = t->req; |
| 3232 | // struct buffer *rep = t->rep; |
| 3233 | // |
| 3234 | // DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3235 | // now_ms, __FUNCTION__, |
| 3236 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3237 | // cli_stnames[t->cli_state], |
| 3238 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3239 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3240 | // req->rex, rep->wex, |
| 3241 | // req->flags, rep->flags, |
| 3242 | // req->l, rep->l); |
| 3243 | // |
| 3244 | // update_state: |
| 3245 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3246 | // * still set this state (and will do until unix sockets are converted). |
| 3247 | // */ |
| 3248 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3249 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3250 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3251 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3252 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3253 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3254 | // (!(rep->flags & BF_SHUTW) && |
| 3255 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3256 | // goto update_timeouts; |
| 3257 | // |
| 3258 | // /* read or write error */ |
| 3259 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3260 | // buffer_shutr(req); |
| 3261 | // req->flags |= BF_READ_ERROR; |
| 3262 | // buffer_shutw(rep); |
| 3263 | // rep->flags |= BF_WRITE_ERROR; |
| 3264 | // fd_delete(t->cli_fd); |
| 3265 | // t->cli_state = CL_STCLOSE; |
| 3266 | // trace_term(t, TT_HTTP_CLI_1); |
| 3267 | // if (!req->analysers) { |
| 3268 | // if (!(t->flags & SN_ERR_MASK)) |
| 3269 | // t->flags |= SN_ERR_CLICL; |
| 3270 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3271 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3272 | // t->flags |= SN_FINST_Q; |
| 3273 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3274 | // t->flags |= SN_FINST_C; |
| 3275 | // else |
| 3276 | // t->flags |= SN_FINST_D; |
| 3277 | // } |
| 3278 | // } |
| 3279 | // goto update_state; |
| 3280 | // } |
| 3281 | // /* last read, or end of server write */ |
| 3282 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3283 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3284 | // buffer_shutr(req); |
| 3285 | // if (!(rep->flags & BF_SHUTW)) { |
| 3286 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3287 | // trace_term(t, TT_HTTP_CLI_2); |
| 3288 | // } else { |
| 3289 | // /* output was already closed */ |
| 3290 | // fd_delete(t->cli_fd); |
| 3291 | // t->cli_state = CL_STCLOSE; |
| 3292 | // trace_term(t, TT_HTTP_CLI_3); |
| 3293 | // } |
| 3294 | // goto update_state; |
| 3295 | // } |
| 3296 | // /* last server read and buffer empty : we only check them when we're |
| 3297 | // * allowed to forward the data. |
| 3298 | // */ |
| 3299 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3300 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3301 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3302 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3303 | // buffer_shutw(rep); |
| 3304 | // if (!(req->flags & BF_SHUTR)) { |
| 3305 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3306 | // shutdown(t->cli_fd, SHUT_WR); |
| 3307 | // trace_term(t, TT_HTTP_CLI_4); |
| 3308 | // } else { |
| 3309 | // fd_delete(t->cli_fd); |
| 3310 | // t->cli_state = CL_STCLOSE; |
| 3311 | // trace_term(t, TT_HTTP_CLI_5); |
| 3312 | // } |
| 3313 | // goto update_state; |
| 3314 | // } |
| 3315 | // /* read timeout */ |
| 3316 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3317 | // buffer_shutr(req); |
| 3318 | // if (!(rep->flags & BF_SHUTW)) { |
| 3319 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3320 | // trace_term(t, TT_HTTP_CLI_6); |
| 3321 | // } else { |
| 3322 | // /* output was already closed */ |
| 3323 | // fd_delete(t->cli_fd); |
| 3324 | // t->cli_state = CL_STCLOSE; |
| 3325 | // trace_term(t, TT_HTTP_CLI_7); |
| 3326 | // } |
| 3327 | // if (!req->analysers) { |
| 3328 | // if (!(t->flags & SN_ERR_MASK)) |
| 3329 | // t->flags |= SN_ERR_CLITO; |
| 3330 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3331 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3332 | // t->flags |= SN_FINST_Q; |
| 3333 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3334 | // t->flags |= SN_FINST_C; |
| 3335 | // else |
| 3336 | // t->flags |= SN_FINST_D; |
| 3337 | // } |
| 3338 | // } |
| 3339 | // goto update_state; |
| 3340 | // } |
| 3341 | // /* write timeout */ |
| 3342 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3343 | // buffer_shutw(rep); |
| 3344 | // if (!(req->flags & BF_SHUTR)) { |
| 3345 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3346 | // shutdown(t->cli_fd, SHUT_WR); |
| 3347 | // trace_term(t, TT_HTTP_CLI_8); |
| 3348 | // } else { |
| 3349 | // fd_delete(t->cli_fd); |
| 3350 | // t->cli_state = CL_STCLOSE; |
| 3351 | // trace_term(t, TT_HTTP_CLI_9); |
| 3352 | // } |
| 3353 | // if (!req->analysers) { |
| 3354 | // if (!(t->flags & SN_ERR_MASK)) |
| 3355 | // t->flags |= SN_ERR_CLITO; |
| 3356 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3357 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3358 | // t->flags |= SN_FINST_Q; |
| 3359 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3360 | // t->flags |= SN_FINST_C; |
| 3361 | // else |
| 3362 | // t->flags |= SN_FINST_D; |
| 3363 | // } |
| 3364 | // } |
| 3365 | // goto update_state; |
| 3366 | // } |
| 3367 | // |
| 3368 | // update_timeouts: |
| 3369 | // /* manage read timeout */ |
| 3370 | // if (!(req->flags & BF_SHUTR)) { |
| 3371 | // if (req->flags & BF_FULL) { |
| 3372 | // /* no room to read more data */ |
| 3373 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3374 | // /* stop reading until we get some space */ |
| 3375 | // req->rex = TICK_ETERNITY; |
| 3376 | // } |
| 3377 | // } else { |
| 3378 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3379 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3380 | // } |
| 3381 | // } |
| 3382 | // |
| 3383 | // /* manage write timeout */ |
| 3384 | // if (!(rep->flags & BF_SHUTW)) { |
| 3385 | // /* first, we may have to produce data (eg: stats). |
| 3386 | // * right now, this is limited to the SHUTR state. |
| 3387 | // */ |
| 3388 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3389 | // produce_content(t); |
| 3390 | // if (rep->flags & BF_EMPTY) { |
| 3391 | // buffer_shutw(rep); |
| 3392 | // fd_delete(t->cli_fd); |
| 3393 | // t->cli_state = CL_STCLOSE; |
| 3394 | // trace_term(t, TT_HTTP_CLI_10); |
| 3395 | // goto update_state; |
| 3396 | // } |
| 3397 | // } |
| 3398 | // |
| 3399 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3400 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3401 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3402 | // /* stop writing */ |
| 3403 | // rep->wex = TICK_ETERNITY; |
| 3404 | // } |
| 3405 | // } else { |
| 3406 | // /* buffer not empty */ |
| 3407 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3408 | // if (!tick_isset(rep->wex)) { |
| 3409 | // /* restart writing */ |
| 3410 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3411 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3412 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3413 | // * we refresh it, except if it was already infinite. */ |
| 3414 | // req->rex = rep->wex; |
| 3415 | // } |
| 3416 | // } |
| 3417 | // } |
| 3418 | // } |
| 3419 | // return 0; /* other cases change nothing */ |
| 3420 | // } |
| 3421 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3422 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3423 | // int len; |
| 3424 | // len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd); |
| 3425 | // write(1, trash, len); |
| 3426 | // } |
| 3427 | // return 0; |
| 3428 | // } |
| 3429 | //#ifdef DEBUG_DEV |
| 3430 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3431 | // ABORT_NOW(); |
| 3432 | //#endif |
| 3433 | // return 0; |
| 3434 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3435 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3436 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3437 | /* Return 1 if the pending connection has failed and should be retried, |
| 3438 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3439 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3440 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3441 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3442 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3443 | struct buffer *req = t->req; |
| 3444 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3445 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3446 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3447 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3448 | now_ms, __FUNCTION__, |
| 3449 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3450 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3451 | req->flags, rep->flags, |
| 3452 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3453 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3454 | if ((req->flags & BF_SHUTW_NOW) || |
| 3455 | (rep->flags & BF_SHUTW) || |
| 3456 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3457 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
| 3458 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3459 | |
| 3460 | trace_term(t, TT_HTTP_SRV_5); |
| 3461 | req->wex = TICK_ETERNITY; |
| 3462 | fd_delete(req->cons->fd); |
| 3463 | if (t->srv) { |
| 3464 | t->srv->cur_sess--; |
| 3465 | sess_change_server(t, NULL); |
| 3466 | } |
| 3467 | /* note that this must not return any error because it would be able to |
| 3468 | * overwrite the client_retnclose() output. |
| 3469 | */ |
| 3470 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3471 | |
| 3472 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3473 | buffer_shutw(req); |
| 3474 | buffer_shutr(rep); |
| 3475 | req->cons->state = SI_ST_CLO; |
| 3476 | if (!req->cons->err_type) |
| 3477 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3478 | req->cons->err_loc = t->srv; |
| 3479 | return 0; |
| 3480 | } |
| 3481 | |
| 3482 | /* check for timeouts and asynchronous connect errors */ |
| 3483 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3484 | conn_err = SI_ET_CONN_ERR; |
| 3485 | if (!req->cons->err_type) |
| 3486 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3487 | } |
| 3488 | else if (!(req->flags & BF_WRITE_STATUS)) { |
| 3489 | /* nothing happened, maybe we timed out */ |
| 3490 | if (tick_is_expired(req->wex, now_ms)) { |
| 3491 | conn_err = SI_ET_CONN_TO; |
| 3492 | if (!req->cons->err_type) |
| 3493 | req->cons->err_type = SI_ET_CONN_TO; |
| 3494 | } |
| 3495 | else |
| 3496 | return 0; /* let's wait a bit more */ |
| 3497 | } |
| 3498 | |
| 3499 | if (conn_err) { |
| 3500 | fd_delete(req->cons->fd); |
| 3501 | req->cons->state = SI_ST_CLO; |
| 3502 | |
| 3503 | if (t->srv) { |
| 3504 | t->srv->cur_sess--; |
| 3505 | sess_change_server(t, NULL); |
| 3506 | req->cons->err_loc = t->srv; |
| 3507 | } |
| 3508 | |
| 3509 | /* ensure that we have enough retries left */ |
| 3510 | if (srv_count_retry_down(t, conn_err)) |
| 3511 | return 0; |
| 3512 | |
| 3513 | if (conn_err == SI_ET_CONN_ERR) { |
| 3514 | /* we encountered an immediate connection error, and we |
| 3515 | * will have to retry connecting to the same server, most |
| 3516 | * likely leading to the same result. To avoid this, we |
| 3517 | * fake a connection timeout to retry after a turn-around |
| 3518 | * time of 1 second. We will wait in the previous if block. |
| 3519 | */ |
| 3520 | req->cons->state = SI_ST_TAR; |
| 3521 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 3522 | return 0; |
| 3523 | } |
| 3524 | |
| 3525 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3526 | /* We're on our last chance, and the REDISP option was specified. |
| 3527 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3528 | */ |
| 3529 | /* let's try to offer this slot to anybody */ |
| 3530 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3531 | process_srv_queue(t->srv); |
| 3532 | |
| 3533 | /* it's left to the dispatcher to choose a server */ |
| 3534 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3535 | t->prev_srv = t->srv; |
| 3536 | } else { |
| 3537 | /* we just want to retry */ |
| 3538 | if (t->srv) |
| 3539 | t->srv->retries++; |
| 3540 | t->be->retries++; |
| 3541 | |
| 3542 | /* Now we will try to either reconnect to the same server or |
| 3543 | * connect to another server. If the connection gets queued |
| 3544 | * because all servers are saturated, then we will go back to |
| 3545 | * the idle state where the buffer's consumer is marked as |
| 3546 | * unknown. |
| 3547 | */ |
| 3548 | if (srv_retryable_connect(t)) { |
| 3549 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3550 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3551 | return 0; |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | /* We'll rely on the caller to try to get a connection again */ |
| 3556 | return 1; |
| 3557 | } |
| 3558 | else { |
| 3559 | /* no error and write OK : connection succeeded */ |
| 3560 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3561 | req->cons->state = SI_ST_EST; |
| 3562 | req->cons->err_type = SI_ET_NONE; |
| 3563 | req->cons->err_loc = NULL; |
| 3564 | |
| 3565 | if (req->flags & BF_EMPTY) { |
| 3566 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3567 | req->wex = TICK_ETERNITY; |
| 3568 | } else { |
| 3569 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3570 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3571 | if (tick_isset(req->wex)) { |
| 3572 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3573 | * we refresh it. */ |
| 3574 | rep->rex = req->wex; |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3579 | if (!(rep->flags & BF_HIJACK)) { |
| 3580 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3581 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3582 | } |
| 3583 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3584 | |
| 3585 | /* if the user wants to log as soon as possible, without counting |
| 3586 | bytes from the server, then this is the right moment. */ |
| 3587 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3588 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3589 | tcp_sess_log(t); |
| 3590 | } |
| 3591 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3592 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3593 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3594 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3595 | } |
| 3596 | #endif |
| 3597 | } |
| 3598 | else { |
| 3599 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3600 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3601 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3602 | /* reset hdr_idx which was already initialized by the request. |
| 3603 | * right now, the http parser does it. |
| 3604 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3605 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | if (!rep->analysers) |
| 3609 | t->rep->flags |= BF_MAY_FORWARD; |
| 3610 | req->wex = TICK_ETERNITY; |
| 3611 | return 0; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | |
| 3616 | /* |
| 3617 | * This function tries to assign a server to a stream_sock interface. |
| 3618 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3619 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3620 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3621 | * it returns SI_ST_ASS, otherwise zero. |
| 3622 | */ |
| 3623 | int stream_sock_assign_server(struct session *t) |
| 3624 | { |
| 3625 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3626 | now_ms, __FUNCTION__, |
| 3627 | cli_stnames[t->cli_state], |
| 3628 | t->rep->rex, t->req->wex, |
| 3629 | t->req->flags, t->rep->flags, |
| 3630 | t->req->l, t->rep->l); |
| 3631 | |
| 3632 | if (t->req->cons->state == SI_ST_TAR) { |
| 3633 | /* connection might be aborted */ |
| 3634 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3635 | (t->rep->flags & BF_SHUTW) || |
| 3636 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3637 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3638 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3639 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3640 | t->req->wex = TICK_ETERNITY; |
| 3641 | |
| 3642 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3643 | buffer_shutr(t->rep); |
| 3644 | buffer_shutw(t->req); |
| 3645 | if (!t->req->cons->err_type) |
| 3646 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3647 | t->req->cons->state = SI_ST_CLO; |
| 3648 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3649 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3650 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3651 | if (!tick_is_expired(t->req->wex, now_ms)) |
| 3652 | return 0; /* still in turn-around */ |
| 3653 | |
| 3654 | t->req->cons->state = SI_ST_INI; |
| 3655 | } |
| 3656 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3657 | if (t->pend_pos) { |
| 3658 | /* request still in queue... */ |
| 3659 | if (tick_is_expired(t->req->wex, now_ms)) { |
| 3660 | /* ... and timeout expired */ |
| 3661 | trace_term(t, TT_HTTP_SRV_3); |
| 3662 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3663 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3664 | if (t->srv) |
| 3665 | t->srv->failed_conns++; |
| 3666 | t->be->failed_conns++; |
| 3667 | |
| 3668 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3669 | buffer_shutr(t->rep); |
| 3670 | buffer_shutw(t->req); |
| 3671 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3672 | if (!t->req->cons->err_type) |
| 3673 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3674 | t->req->cons->state = SI_ST_CLO; |
| 3675 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3676 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3677 | /* connection remains in queue, check if we have to abort it */ |
| 3678 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3679 | (t->rep->flags & BF_SHUTW) || |
| 3680 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3681 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3682 | /* give up */ |
| 3683 | trace_term(t, TT_HTTP_SRV_1); |
| 3684 | t->req->wex = TICK_ETERNITY; |
| 3685 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3686 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3687 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3688 | buffer_shutr(t->rep); |
| 3689 | buffer_shutw(t->req); |
| 3690 | if (!t->req->cons->err_type) |
| 3691 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3692 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3693 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3694 | return 0; |
| 3695 | } |
| 3696 | /* The connection is not in the queue anymore */ |
| 3697 | t->req->cons->state = SI_ST_INI; |
| 3698 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3699 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3700 | /* we may get here from above */ |
| 3701 | if (t->req->cons->state == SI_ST_INI) { |
| 3702 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3703 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3704 | /* first, check if the connection has been aborted */ |
| 3705 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3706 | (t->rep->flags & BF_SHUTW) || |
| 3707 | ((t->req->flags & BF_SHUTR) && |
| 3708 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3709 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3710 | trace_term(t, TT_HTTP_SRV_1); |
| 3711 | t->req->wex = TICK_ETERNITY; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3712 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3713 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3714 | buffer_shutr(t->rep); |
| 3715 | buffer_shutw(t->req); |
| 3716 | if (!t->req->cons->err_type) |
| 3717 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3718 | t->req->cons->state = SI_ST_CLO; |
| 3719 | return 0; |
| 3720 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3722 | /* try to get a server assigned */ |
| 3723 | if (srv_redispatch_connect(t) != 0) { |
| 3724 | /* we did not get any server, let's check the cause */ |
| 3725 | if (t->req->cons->state == SI_ST_QUE) { |
| 3726 | /* the connection was queued, that's OK */ |
| 3727 | return 0; |
| 3728 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3729 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3730 | trace_term(t, TT_HTTP_SRV_2); |
| 3731 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3733 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3734 | buffer_shutr(t->rep); |
| 3735 | buffer_shutw(t->req); |
| 3736 | t->req->flags |= BF_WRITE_ERROR; |
| 3737 | if (!t->req->cons->err_type) |
| 3738 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3739 | t->req->cons->state = SI_ST_CLO; |
| 3740 | return 0; |
| 3741 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3742 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3743 | t->req->cons->state = SI_ST_ASS; |
| 3744 | /* Once the server is assigned, we have to return because |
| 3745 | * the caller might be interested in checking several |
| 3746 | * things before connecting. |
| 3747 | */ |
| 3748 | return 1; |
| 3749 | } |
| 3750 | return 0; |
| 3751 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3752 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3753 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3754 | /* |
| 3755 | * This function tries to establish a connection to an assigned server. It also |
| 3756 | * performs connection retries. It may only be called with t->req->cons->state |
| 3757 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3758 | * SI_ST_EST, or SI_ST_CLO. |
| 3759 | */ |
| 3760 | int stream_sock_connect_server(struct session *t) |
| 3761 | { |
| 3762 | if (t->req->cons->state == SI_ST_ASS) { |
| 3763 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3764 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3765 | if (!srv_retryable_connect(t)) { |
| 3766 | /* we need to redispatch */ |
| 3767 | t->req->cons->state = SI_ST_INI; |
| 3768 | return 0; |
| 3769 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3770 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3771 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3772 | if (t->req->cons->state != SI_ST_CON) { |
| 3773 | /* it was an error */ |
| 3774 | trace_term(t, TT_HTTP_SRV_4); |
| 3775 | t->req->wex = TICK_ETERNITY; |
| 3776 | |
| 3777 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3778 | buffer_shutr(t->rep); |
| 3779 | buffer_shutw(t->req); |
| 3780 | t->req->flags |= BF_WRITE_ERROR; |
| 3781 | if (!t->req->cons->err_type) |
| 3782 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3783 | t->req->cons->state = SI_ST_CLO; |
| 3784 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3785 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3786 | /* We have a socket and switched to SI_ST_CON */ |
| 3787 | } |
| 3788 | |
| 3789 | /* we may also get here from above */ |
| 3790 | if (t->req->cons->state == SI_ST_CON) { |
| 3791 | /* connection in progress or just completed */ |
| 3792 | if (!tcp_connection_status(t)) |
| 3793 | return 0; |
| 3794 | } |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
| 3798 | |
| 3799 | /* |
| 3800 | * Tries to establish a connection to the server and associate it to the |
| 3801 | * request buffer's consumer side. It is assumed that this function will not be |
| 3802 | * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally |
| 3803 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3804 | */ |
| 3805 | int process_srv_conn(struct session *t) |
| 3806 | { |
| 3807 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3808 | now_ms, __FUNCTION__, |
| 3809 | cli_stnames[t->cli_state], |
| 3810 | t->rep->rex, t->req->wex, |
| 3811 | t->req->flags, t->rep->flags, |
| 3812 | t->req->l, t->rep->l); |
| 3813 | |
| 3814 | do { |
| 3815 | if (t->req->cons->state == SI_ST_INI || |
| 3816 | t->req->cons->state == SI_ST_TAR || |
| 3817 | t->req->cons->state == SI_ST_QUE) { |
| 3818 | /* try to assign a server */ |
| 3819 | if (!stream_sock_assign_server(t)) |
| 3820 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3821 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3822 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3823 | if (t->req->cons->state == SI_ST_ASS && |
| 3824 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3825 | /* Server supporting redirection and it is possible. |
| 3826 | * Invalid requests are reported as such. It concerns all |
| 3827 | * the largest ones. |
| 3828 | */ |
| 3829 | struct http_txn *txn = &t->txn; |
| 3830 | struct chunk rdr; |
| 3831 | char *path; |
| 3832 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3834 | /* 1: create the response header */ |
| 3835 | rdr.len = strlen(HTTP_302); |
| 3836 | rdr.str = trash; |
| 3837 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3838 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3839 | /* 2: add the server's prefix */ |
| 3840 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3841 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3842 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3843 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3844 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3845 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3846 | /* 3: add the request URI */ |
| 3847 | path = http_get_path(txn); |
| 3848 | if (!path) |
| 3849 | goto cancel_redir; |
| 3850 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3851 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3852 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3853 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3854 | memcpy(rdr.str + rdr.len, path, len); |
| 3855 | rdr.len += len; |
| 3856 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3857 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3858 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3859 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3860 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3861 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3862 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3863 | if (t->srv) |
| 3864 | t->srv->cum_sess++; |
| 3865 | |
| 3866 | t->req->cons->state = SI_ST_CLO; |
| 3867 | return 0; |
| 3868 | cancel_redir: |
| 3869 | //txn->status = 400; |
| 3870 | //t->fe->failed_req++; |
| 3871 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3872 | // 400, error_message(t, HTTP_ERR_400)); |
| 3873 | trace_term(t, TT_HTTP_SRV_4); |
| 3874 | |
| 3875 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3876 | buffer_shutw(t->req); |
| 3877 | buffer_shutr(t->rep); |
| 3878 | if (!t->req->cons->err_type) |
| 3879 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3880 | t->req->cons->state = SI_ST_CLO; |
| 3881 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3882 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3883 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3884 | if (t->req->cons->state == SI_ST_CON || |
| 3885 | t->req->cons->state == SI_ST_ASS) { |
| 3886 | stream_sock_connect_server(t); |
| 3887 | } |
| 3888 | } while (t->req->cons->state != SI_ST_CLO && |
| 3889 | t->req->cons->state != SI_ST_CON && |
| 3890 | t->req->cons->state != SI_ST_EST); |
| 3891 | return 0; |
| 3892 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3893 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3894 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3895 | /* |
| 3896 | * 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] | 3897 | * called with client socket shut down on input. Right now, only statistics can |
| 3898 | * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3899 | * session, which it uses to keep on being called when there is free space in |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3900 | * the buffer, or simply by letting an empty buffer upon return. It returns 1 |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3901 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3902 | */ |
| 3903 | int produce_content(struct session *s) |
| 3904 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3905 | if (s->data_source == DATA_SRC_NONE) { |
| 3906 | s->flags &= ~SN_SELF_GEN; |
| 3907 | return 1; |
| 3908 | } |
| 3909 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3910 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3911 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3912 | if (ret >= 0) |
| 3913 | return ret; |
| 3914 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3915 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3916 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3917 | /* unknown data source or internal error */ |
| 3918 | s->txn.status = 500; |
| 3919 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3920 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3921 | if (!(s->flags & SN_ERR_MASK)) |
| 3922 | s->flags |= SN_ERR_PRXCOND; |
| 3923 | if (!(s->flags & SN_FINST_MASK)) |
| 3924 | s->flags |= SN_FINST_R; |
| 3925 | s->flags &= ~SN_SELF_GEN; |
| 3926 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3930 | /* Iterate the same filter through all request headers. |
| 3931 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3932 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3933 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3934 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3935 | 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] | 3936 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3937 | char term; |
| 3938 | char *cur_ptr, *cur_end, *cur_next; |
| 3939 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3940 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3941 | struct hdr_idx_elem *cur_hdr; |
| 3942 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3943 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3944 | last_hdr = 0; |
| 3945 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3946 | 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] | 3947 | old_idx = 0; |
| 3948 | |
| 3949 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3950 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3951 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3952 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3953 | (exp->action == ACT_ALLOW || |
| 3954 | exp->action == ACT_DENY || |
| 3955 | exp->action == ACT_TARPIT)) |
| 3956 | return 0; |
| 3957 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3958 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3959 | if (!cur_idx) |
| 3960 | break; |
| 3961 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3962 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3963 | cur_ptr = cur_next; |
| 3964 | cur_end = cur_ptr + cur_hdr->len; |
| 3965 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3966 | |
| 3967 | /* Now we have one header between cur_ptr and cur_end, |
| 3968 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3969 | */ |
| 3970 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3971 | /* The annoying part is that pattern matching needs |
| 3972 | * that we modify the contents to null-terminate all |
| 3973 | * strings before testing them. |
| 3974 | */ |
| 3975 | |
| 3976 | term = *cur_end; |
| 3977 | *cur_end = '\0'; |
| 3978 | |
| 3979 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3980 | switch (exp->action) { |
| 3981 | case ACT_SETBE: |
| 3982 | /* It is not possible to jump a second time. |
| 3983 | * FIXME: should we return an HTTP/500 here so that |
| 3984 | * the admin knows there's a problem ? |
| 3985 | */ |
| 3986 | if (t->be != t->fe) |
| 3987 | break; |
| 3988 | |
| 3989 | /* Swithing Proxy */ |
| 3990 | t->be = (struct proxy *) exp->replace; |
| 3991 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3992 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3993 | * because we have associated req_cap and rsp_cap to the |
| 3994 | * frontend, and the beconn will be updated later. |
| 3995 | */ |
| 3996 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3997 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3998 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3999 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4000 | last_hdr = 1; |
| 4001 | break; |
| 4002 | |
| 4003 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4004 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4005 | last_hdr = 1; |
| 4006 | break; |
| 4007 | |
| 4008 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4009 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4010 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4011 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4012 | break; |
| 4013 | |
| 4014 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4015 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4016 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4017 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4018 | break; |
| 4019 | |
| 4020 | case ACT_REPLACE: |
| 4021 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4022 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4023 | /* FIXME: if the user adds a newline in the replacement, the |
| 4024 | * index will not be recalculated for now, and the new line |
| 4025 | * will not be counted as a new header. |
| 4026 | */ |
| 4027 | |
| 4028 | cur_end += delta; |
| 4029 | cur_next += delta; |
| 4030 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4031 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4032 | break; |
| 4033 | |
| 4034 | case ACT_REMOVE: |
| 4035 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4036 | cur_next += delta; |
| 4037 | |
| 4038 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4039 | txn->req.eoh += delta; |
| 4040 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4041 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4042 | cur_hdr->len = 0; |
| 4043 | cur_end = NULL; /* null-term has been rewritten */ |
| 4044 | break; |
| 4045 | |
| 4046 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4047 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4048 | if (cur_end) |
| 4049 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4050 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4051 | /* keep the link from this header to next one in case of later |
| 4052 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4053 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4054 | old_idx = cur_idx; |
| 4055 | } |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | |
| 4060 | /* Apply the filter to the request line. |
| 4061 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4062 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4063 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4064 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4065 | */ |
| 4066 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4067 | { |
| 4068 | char term; |
| 4069 | char *cur_ptr, *cur_end; |
| 4070 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4071 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4072 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4073 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4074 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4075 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4076 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4077 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4078 | (exp->action == ACT_ALLOW || |
| 4079 | exp->action == ACT_DENY || |
| 4080 | exp->action == ACT_TARPIT)) |
| 4081 | return 0; |
| 4082 | else if (exp->action == ACT_REMOVE) |
| 4083 | return 0; |
| 4084 | |
| 4085 | done = 0; |
| 4086 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4087 | 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] | 4088 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4089 | |
| 4090 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4091 | |
| 4092 | /* The annoying part is that pattern matching needs |
| 4093 | * that we modify the contents to null-terminate all |
| 4094 | * strings before testing them. |
| 4095 | */ |
| 4096 | |
| 4097 | term = *cur_end; |
| 4098 | *cur_end = '\0'; |
| 4099 | |
| 4100 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4101 | switch (exp->action) { |
| 4102 | case ACT_SETBE: |
| 4103 | /* It is not possible to jump a second time. |
| 4104 | * FIXME: should we return an HTTP/500 here so that |
| 4105 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4106 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4107 | if (t->be != t->fe) |
| 4108 | break; |
| 4109 | |
| 4110 | /* Swithing Proxy */ |
| 4111 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4113 | /* right now, the backend switch is not too much complicated |
| 4114 | * because we have associated req_cap and rsp_cap to the |
| 4115 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4116 | */ |
| 4117 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4118 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4119 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4120 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4121 | done = 1; |
| 4122 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4124 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4125 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4126 | done = 1; |
| 4127 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4128 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4129 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4130 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4131 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4132 | done = 1; |
| 4133 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4135 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4136 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4137 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4138 | done = 1; |
| 4139 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4140 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4141 | case ACT_REPLACE: |
| 4142 | *cur_end = term; /* restore the string terminator */ |
| 4143 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4144 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4145 | /* FIXME: if the user adds a newline in the replacement, the |
| 4146 | * index will not be recalculated for now, and the new line |
| 4147 | * will not be counted as a new header. |
| 4148 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4149 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4150 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4151 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4152 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4153 | 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] | 4154 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4155 | HTTP_MSG_RQMETH, |
| 4156 | cur_ptr, cur_end + 1, |
| 4157 | NULL, NULL); |
| 4158 | if (unlikely(!cur_end)) |
| 4159 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4160 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4161 | /* we have a full request and we know that we have either a CR |
| 4162 | * or an LF at <ptr>. |
| 4163 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4164 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4165 | 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] | 4166 | /* there is no point trying this regex on headers */ |
| 4167 | return 1; |
| 4168 | } |
| 4169 | } |
| 4170 | *cur_end = term; /* restore the string terminator */ |
| 4171 | return done; |
| 4172 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4173 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4174 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4175 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4176 | /* |
| 4177 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4178 | * 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] | 4179 | * unparsable request. Since it can manage the switch to another backend, it |
| 4180 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4181 | */ |
| 4182 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4183 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4184 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4185 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4186 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4187 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4188 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4189 | /* |
| 4190 | * The interleaving of transformations and verdicts |
| 4191 | * makes it difficult to decide to continue or stop |
| 4192 | * the evaluation. |
| 4193 | */ |
| 4194 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4195 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4196 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4197 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4198 | exp = exp->next; |
| 4199 | continue; |
| 4200 | } |
| 4201 | |
| 4202 | /* Apply the filter to the request line. */ |
| 4203 | ret = apply_filter_to_req_line(t, req, exp); |
| 4204 | if (unlikely(ret < 0)) |
| 4205 | return -1; |
| 4206 | |
| 4207 | if (likely(ret == 0)) { |
| 4208 | /* The filter did not match the request, it can be |
| 4209 | * iterated through all headers. |
| 4210 | */ |
| 4211 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4212 | } |
| 4213 | exp = exp->next; |
| 4214 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4215 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4219 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4220 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4221 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4222 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4223 | */ |
| 4224 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4225 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4226 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4227 | char *p1, *p2, *p3, *p4; |
| 4228 | char *del_colon, *del_cookie, *colon; |
| 4229 | int app_cookies; |
| 4230 | |
| 4231 | appsess *asession_temp = NULL; |
| 4232 | appsess local_asession; |
| 4233 | |
| 4234 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4235 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4236 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4237 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4238 | * we start with the start line. |
| 4239 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4240 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4241 | 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] | 4242 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4243 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4244 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4245 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4246 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4247 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4248 | cur_ptr = cur_next; |
| 4249 | cur_end = cur_ptr + cur_hdr->len; |
| 4250 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4251 | |
| 4252 | /* We have one full header between cur_ptr and cur_end, and the |
| 4253 | * next header starts at cur_next. We're only interested in |
| 4254 | * "Cookie:" headers. |
| 4255 | */ |
| 4256 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4257 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4258 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4259 | old_idx = cur_idx; |
| 4260 | continue; |
| 4261 | } |
| 4262 | |
| 4263 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4264 | * attributes whose name begin with a '$', and associate them with |
| 4265 | * the right cookie, if we want to delete this cookie. |
| 4266 | * So there are 3 cases for each cookie read : |
| 4267 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4268 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4269 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4270 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4271 | * "special" cookie. |
| 4272 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4273 | * remove it. If no application cookie persists in the header, we |
| 4274 | * *MUST* delete it |
| 4275 | */ |
| 4276 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4277 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4278 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4279 | /* del_cookie == NULL => nothing to be deleted */ |
| 4280 | del_colon = del_cookie = NULL; |
| 4281 | app_cookies = 0; |
| 4282 | |
| 4283 | while (p1 < cur_end) { |
| 4284 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4285 | while (p1 < cur_end) { |
| 4286 | if (*p1 == ';' || *p1 == ',') |
| 4287 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4288 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4289 | break; |
| 4290 | p1++; |
| 4291 | } |
| 4292 | |
| 4293 | if (p1 == cur_end) |
| 4294 | break; |
| 4295 | |
| 4296 | /* p1 is at the beginning of the cookie name */ |
| 4297 | p2 = p1; |
| 4298 | while (p2 < cur_end && *p2 != '=') |
| 4299 | p2++; |
| 4300 | |
| 4301 | if (p2 == cur_end) |
| 4302 | break; |
| 4303 | |
| 4304 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4305 | if (p3 == cur_end) |
| 4306 | break; |
| 4307 | |
| 4308 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4309 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4310 | p4++; |
| 4311 | |
| 4312 | /* here, we have the cookie name between p1 and p2, |
| 4313 | * and its value between p3 and p4. |
| 4314 | * we can process it : |
| 4315 | * |
| 4316 | * Cookie: NAME=VALUE; |
| 4317 | * | || || | |
| 4318 | * | || || +--> p4 |
| 4319 | * | || |+-------> p3 |
| 4320 | * | || +--------> p2 |
| 4321 | * | |+------------> p1 |
| 4322 | * | +-------------> colon |
| 4323 | * +--------------------> cur_ptr |
| 4324 | */ |
| 4325 | |
| 4326 | if (*p1 == '$') { |
| 4327 | /* skip this one */ |
| 4328 | } |
| 4329 | else { |
| 4330 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4331 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4332 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4333 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4334 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4335 | int log_len = p4 - p1; |
| 4336 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4337 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | Alert("HTTP logging : out of memory.\n"); |
| 4339 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4340 | if (log_len > t->fe->capture_len) |
| 4341 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4342 | memcpy(txn->cli_cookie, p1, log_len); |
| 4343 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4347 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4348 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4349 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4350 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4351 | char *delim; |
| 4352 | |
| 4353 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4354 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4355 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4356 | * |
| 4357 | * Cookie: NAME=SRV~VALUE; |
| 4358 | * | || || | | |
| 4359 | * | || || | +--> p4 |
| 4360 | * | || || +--------> delim |
| 4361 | * | || |+-----------> p3 |
| 4362 | * | || +------------> p2 |
| 4363 | * | |+----------------> p1 |
| 4364 | * | +-----------------> colon |
| 4365 | * +------------------------> cur_ptr |
| 4366 | */ |
| 4367 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4368 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4369 | for (delim = p3; delim < p4; delim++) |
| 4370 | if (*delim == COOKIE_DELIM) |
| 4371 | break; |
| 4372 | } |
| 4373 | else |
| 4374 | delim = p4; |
| 4375 | |
| 4376 | |
| 4377 | /* Here, we'll look for the first running server which supports the cookie. |
| 4378 | * This allows to share a same cookie between several servers, for example |
| 4379 | * to dedicate backup servers to specific servers only. |
| 4380 | * However, to prevent clients from sticking to cookie-less backup server |
| 4381 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4382 | * empty cookies and mark them as invalid. |
| 4383 | */ |
| 4384 | if (delim == p3) |
| 4385 | srv = NULL; |
| 4386 | |
| 4387 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4388 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4389 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4390 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4391 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4392 | txn->flags &= ~TX_CK_MASK; |
| 4393 | txn->flags |= TX_CK_VALID; |
| 4394 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4395 | t->srv = srv; |
| 4396 | break; |
| 4397 | } else { |
| 4398 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4399 | txn->flags &= ~TX_CK_MASK; |
| 4400 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | srv = srv->next; |
| 4404 | } |
| 4405 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4406 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4407 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4408 | txn->flags &= ~TX_CK_MASK; |
| 4409 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | /* depending on the cookie mode, we may have to either : |
| 4413 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4414 | * the server never sees it ; |
| 4415 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4416 | * application cookie so that it does not get accidentely removed later, |
| 4417 | * if we're in cookie prefix mode |
| 4418 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4419 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4420 | int delta; /* negative */ |
| 4421 | |
| 4422 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4423 | p4 += delta; |
| 4424 | cur_end += delta; |
| 4425 | cur_next += delta; |
| 4426 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4427 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4428 | |
| 4429 | del_cookie = del_colon = NULL; |
| 4430 | app_cookies++; /* protect the header from deletion */ |
| 4431 | } |
| 4432 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4433 | (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] | 4434 | del_cookie = p1; |
| 4435 | del_colon = colon; |
| 4436 | } |
| 4437 | } else { |
| 4438 | /* now we know that we must keep this cookie since it's |
| 4439 | * not ours. But if we wanted to delete our cookie |
| 4440 | * earlier, we cannot remove the complete header, but we |
| 4441 | * can remove the previous block itself. |
| 4442 | */ |
| 4443 | app_cookies++; |
| 4444 | |
| 4445 | if (del_cookie != NULL) { |
| 4446 | int delta; /* negative */ |
| 4447 | |
| 4448 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4449 | p4 += delta; |
| 4450 | cur_end += delta; |
| 4451 | cur_next += delta; |
| 4452 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4453 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | del_cookie = del_colon = NULL; |
| 4455 | } |
| 4456 | } |
| 4457 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4458 | if ((t->be->appsession_name != NULL) && |
| 4459 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4460 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4461 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4462 | /* Cool... it's the right one */ |
| 4463 | |
| 4464 | asession_temp = &local_asession; |
| 4465 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4466 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4467 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4468 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4469 | return; |
| 4470 | } |
| 4471 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4472 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4473 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4474 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4475 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4476 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4477 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4478 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4479 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4480 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4481 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4482 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4483 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4484 | return; |
| 4485 | } |
| 4486 | |
| 4487 | asession_temp->sessid = local_asession.sessid; |
| 4488 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4489 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4490 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4491 | } else { |
| 4492 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4493 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4494 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4495 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4496 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4497 | Alert("Found Application Session without matching server.\n"); |
| 4498 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4499 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4500 | while (srv) { |
| 4501 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4502 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4503 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4504 | txn->flags &= ~TX_CK_MASK; |
| 4505 | txn->flags |= TX_CK_VALID; |
| 4506 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4507 | t->srv = srv; |
| 4508 | break; |
| 4509 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4510 | txn->flags &= ~TX_CK_MASK; |
| 4511 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | srv = srv->next; |
| 4515 | }/* end while(srv) */ |
| 4516 | }/* end else if server == NULL */ |
| 4517 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4518 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4519 | asession_temp->request_count++; |
| 4520 | #if defined(DEBUG_HASH) |
| 4521 | Alert("manage_client_side_cookies\n"); |
| 4522 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4523 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4524 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4525 | } |
| 4526 | |
| 4527 | /* we'll have to look for another cookie ... */ |
| 4528 | p1 = p4; |
| 4529 | } /* while (p1 < cur_end) */ |
| 4530 | |
| 4531 | /* There's no more cookie on this line. |
| 4532 | * We may have marked the last one(s) for deletion. |
| 4533 | * We must do this now in two ways : |
| 4534 | * - if there is no app cookie, we simply delete the header ; |
| 4535 | * - if there are app cookies, we must delete the end of the |
| 4536 | * string properly, including the colon/semi-colon before |
| 4537 | * the cookie name. |
| 4538 | */ |
| 4539 | if (del_cookie != NULL) { |
| 4540 | int delta; |
| 4541 | if (app_cookies) { |
| 4542 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4543 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4544 | cur_hdr->len += delta; |
| 4545 | } else { |
| 4546 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4547 | |
| 4548 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4549 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4550 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4551 | cur_hdr->len = 0; |
| 4552 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4553 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4554 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | /* keep the link from this header to next one */ |
| 4558 | old_idx = cur_idx; |
| 4559 | } /* end of cookie processing on this header */ |
| 4560 | } |
| 4561 | |
| 4562 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4563 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4564 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4565 | */ |
| 4566 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4567 | { |
| 4568 | char term; |
| 4569 | char *cur_ptr, *cur_end, *cur_next; |
| 4570 | int cur_idx, old_idx, last_hdr; |
| 4571 | struct http_txn *txn = &t->txn; |
| 4572 | struct hdr_idx_elem *cur_hdr; |
| 4573 | int len, delta; |
| 4574 | |
| 4575 | last_hdr = 0; |
| 4576 | |
| 4577 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4578 | old_idx = 0; |
| 4579 | |
| 4580 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4581 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4582 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4583 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4584 | (exp->action == ACT_ALLOW || |
| 4585 | exp->action == ACT_DENY)) |
| 4586 | return 0; |
| 4587 | |
| 4588 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4589 | if (!cur_idx) |
| 4590 | break; |
| 4591 | |
| 4592 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4593 | cur_ptr = cur_next; |
| 4594 | cur_end = cur_ptr + cur_hdr->len; |
| 4595 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4596 | |
| 4597 | /* Now we have one header between cur_ptr and cur_end, |
| 4598 | * and the next header starts at cur_next. |
| 4599 | */ |
| 4600 | |
| 4601 | /* The annoying part is that pattern matching needs |
| 4602 | * that we modify the contents to null-terminate all |
| 4603 | * strings before testing them. |
| 4604 | */ |
| 4605 | |
| 4606 | term = *cur_end; |
| 4607 | *cur_end = '\0'; |
| 4608 | |
| 4609 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4610 | switch (exp->action) { |
| 4611 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4612 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4613 | last_hdr = 1; |
| 4614 | break; |
| 4615 | |
| 4616 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4617 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4618 | last_hdr = 1; |
| 4619 | break; |
| 4620 | |
| 4621 | case ACT_REPLACE: |
| 4622 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4623 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4624 | /* FIXME: if the user adds a newline in the replacement, the |
| 4625 | * index will not be recalculated for now, and the new line |
| 4626 | * will not be counted as a new header. |
| 4627 | */ |
| 4628 | |
| 4629 | cur_end += delta; |
| 4630 | cur_next += delta; |
| 4631 | cur_hdr->len += delta; |
| 4632 | txn->rsp.eoh += delta; |
| 4633 | break; |
| 4634 | |
| 4635 | case ACT_REMOVE: |
| 4636 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4637 | cur_next += delta; |
| 4638 | |
| 4639 | /* FIXME: this should be a separate function */ |
| 4640 | txn->rsp.eoh += delta; |
| 4641 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4642 | txn->hdr_idx.used--; |
| 4643 | cur_hdr->len = 0; |
| 4644 | cur_end = NULL; /* null-term has been rewritten */ |
| 4645 | break; |
| 4646 | |
| 4647 | } |
| 4648 | } |
| 4649 | if (cur_end) |
| 4650 | *cur_end = term; /* restore the string terminator */ |
| 4651 | |
| 4652 | /* keep the link from this header to next one in case of later |
| 4653 | * removal of next header. |
| 4654 | */ |
| 4655 | old_idx = cur_idx; |
| 4656 | } |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
| 4660 | |
| 4661 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4662 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4663 | * or -1 if a replacement resulted in an invalid status line. |
| 4664 | */ |
| 4665 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4666 | { |
| 4667 | char term; |
| 4668 | char *cur_ptr, *cur_end; |
| 4669 | int done; |
| 4670 | struct http_txn *txn = &t->txn; |
| 4671 | int len, delta; |
| 4672 | |
| 4673 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4674 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4675 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4676 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4677 | (exp->action == ACT_ALLOW || |
| 4678 | exp->action == ACT_DENY)) |
| 4679 | return 0; |
| 4680 | else if (exp->action == ACT_REMOVE) |
| 4681 | return 0; |
| 4682 | |
| 4683 | done = 0; |
| 4684 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4685 | 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] | 4686 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4687 | |
| 4688 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4689 | |
| 4690 | /* The annoying part is that pattern matching needs |
| 4691 | * that we modify the contents to null-terminate all |
| 4692 | * strings before testing them. |
| 4693 | */ |
| 4694 | |
| 4695 | term = *cur_end; |
| 4696 | *cur_end = '\0'; |
| 4697 | |
| 4698 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4699 | switch (exp->action) { |
| 4700 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4701 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4702 | done = 1; |
| 4703 | break; |
| 4704 | |
| 4705 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4706 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4707 | done = 1; |
| 4708 | break; |
| 4709 | |
| 4710 | case ACT_REPLACE: |
| 4711 | *cur_end = term; /* restore the string terminator */ |
| 4712 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4713 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4714 | /* FIXME: if the user adds a newline in the replacement, the |
| 4715 | * index will not be recalculated for now, and the new line |
| 4716 | * will not be counted as a new header. |
| 4717 | */ |
| 4718 | |
| 4719 | txn->rsp.eoh += delta; |
| 4720 | cur_end += delta; |
| 4721 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4722 | 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] | 4723 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4724 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4725 | cur_ptr, cur_end + 1, |
| 4726 | NULL, NULL); |
| 4727 | if (unlikely(!cur_end)) |
| 4728 | return -1; |
| 4729 | |
| 4730 | /* we have a full respnse and we know that we have either a CR |
| 4731 | * or an LF at <ptr>. |
| 4732 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4733 | 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] | 4734 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4735 | /* there is no point trying this regex on headers */ |
| 4736 | return 1; |
| 4737 | } |
| 4738 | } |
| 4739 | *cur_end = term; /* restore the string terminator */ |
| 4740 | return done; |
| 4741 | } |
| 4742 | |
| 4743 | |
| 4744 | |
| 4745 | /* |
| 4746 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4747 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4748 | * unparsable response. |
| 4749 | */ |
| 4750 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4751 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4752 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4753 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4754 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4755 | int ret; |
| 4756 | |
| 4757 | /* |
| 4758 | * The interleaving of transformations and verdicts |
| 4759 | * makes it difficult to decide to continue or stop |
| 4760 | * the evaluation. |
| 4761 | */ |
| 4762 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4763 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4764 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4765 | exp->action == ACT_PASS)) { |
| 4766 | exp = exp->next; |
| 4767 | continue; |
| 4768 | } |
| 4769 | |
| 4770 | /* Apply the filter to the status line. */ |
| 4771 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4772 | if (unlikely(ret < 0)) |
| 4773 | return -1; |
| 4774 | |
| 4775 | if (likely(ret == 0)) { |
| 4776 | /* The filter did not match the response, it can be |
| 4777 | * iterated through all headers. |
| 4778 | */ |
| 4779 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4780 | } |
| 4781 | exp = exp->next; |
| 4782 | } |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | |
| 4787 | |
| 4788 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4789 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4790 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4791 | */ |
| 4792 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4793 | { |
| 4794 | struct http_txn *txn = &t->txn; |
| 4795 | char *p1, *p2, *p3, *p4; |
| 4796 | |
| 4797 | appsess *asession_temp = NULL; |
| 4798 | appsess local_asession; |
| 4799 | |
| 4800 | char *cur_ptr, *cur_end, *cur_next; |
| 4801 | int cur_idx, old_idx, delta; |
| 4802 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4803 | /* Iterate through the headers. |
| 4804 | * we start with the start line. |
| 4805 | */ |
| 4806 | old_idx = 0; |
| 4807 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4808 | |
| 4809 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4810 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4811 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4812 | |
| 4813 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4814 | cur_ptr = cur_next; |
| 4815 | cur_end = cur_ptr + cur_hdr->len; |
| 4816 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4817 | |
| 4818 | /* We have one full header between cur_ptr and cur_end, and the |
| 4819 | * next header starts at cur_next. We're only interested in |
| 4820 | * "Cookie:" headers. |
| 4821 | */ |
| 4822 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4823 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4824 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4825 | old_idx = cur_idx; |
| 4826 | continue; |
| 4827 | } |
| 4828 | |
| 4829 | /* 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] | 4830 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4831 | |
| 4832 | |
| 4833 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4834 | if (t->be->cookie_name == NULL && |
| 4835 | t->be->appsession_name == NULL && |
| 4836 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4837 | return; |
| 4838 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4839 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4840 | |
| 4841 | 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] | 4842 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4843 | break; |
| 4844 | |
| 4845 | /* p1 is at the beginning of the cookie name */ |
| 4846 | p2 = p1; |
| 4847 | |
| 4848 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4849 | p2++; |
| 4850 | |
| 4851 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4852 | break; |
| 4853 | |
| 4854 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4855 | if (p3 == cur_end) |
| 4856 | break; |
| 4857 | |
| 4858 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4859 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4860 | p4++; |
| 4861 | |
| 4862 | /* here, we have the cookie name between p1 and p2, |
| 4863 | * and its value between p3 and p4. |
| 4864 | * we can process it. |
| 4865 | */ |
| 4866 | |
| 4867 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4868 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4869 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4870 | (p4 - p1 >= t->be->capture_namelen) && |
| 4871 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4872 | int log_len = p4 - p1; |
| 4873 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4874 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4875 | Alert("HTTP logging : out of memory.\n"); |
| 4876 | } |
| 4877 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4878 | if (log_len > t->be->capture_len) |
| 4879 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4880 | memcpy(txn->srv_cookie, p1, log_len); |
| 4881 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4885 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4886 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4887 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4888 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4889 | |
| 4890 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4891 | * this occurrence because we'll insert another one later. |
| 4892 | * We'll delete it too if the "indirect" option is set and we're in |
| 4893 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4894 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4895 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4896 | /* this header must be deleted */ |
| 4897 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4898 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4899 | txn->hdr_idx.used--; |
| 4900 | cur_hdr->len = 0; |
| 4901 | cur_next += delta; |
| 4902 | txn->rsp.eoh += delta; |
| 4903 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4904 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4905 | } |
| 4906 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4907 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4908 | /* replace bytes p3->p4 with the cookie name associated |
| 4909 | * with this server since we know it. |
| 4910 | */ |
| 4911 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4912 | cur_hdr->len += delta; |
| 4913 | cur_next += delta; |
| 4914 | txn->rsp.eoh += delta; |
| 4915 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4916 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | } |
| 4918 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4919 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4920 | /* insert the cookie name associated with this server |
| 4921 | * before existing cookie, and insert a delimitor between them.. |
| 4922 | */ |
| 4923 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4924 | cur_hdr->len += delta; |
| 4925 | cur_next += delta; |
| 4926 | txn->rsp.eoh += delta; |
| 4927 | |
| 4928 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4929 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4930 | } |
| 4931 | } |
| 4932 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4933 | else if ((t->be->appsession_name != NULL) && |
| 4934 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4935 | |
| 4936 | /* Cool... it's the right one */ |
| 4937 | |
| 4938 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4939 | asession_temp = &local_asession; |
| 4940 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4941 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4942 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4943 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4944 | return; |
| 4945 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4946 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4947 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4948 | asession_temp->serverid = NULL; |
| 4949 | |
| 4950 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4951 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4952 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4953 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4954 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4955 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4956 | return; |
| 4957 | } |
| 4958 | asession_temp->sessid = local_asession.sessid; |
| 4959 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4960 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4961 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4962 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4963 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4964 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4965 | } |
| 4966 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4967 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4968 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4969 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4970 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4971 | return; |
| 4972 | } |
| 4973 | asession_temp->serverid[0] = '\0'; |
| 4974 | } |
| 4975 | |
| 4976 | if (asession_temp->serverid[0] == '\0') |
| 4977 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4978 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4979 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4980 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4981 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4982 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4983 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4984 | #endif |
| 4985 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4986 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4987 | } /* we're now at the end of the cookie value */ |
| 4988 | |
| 4989 | /* keep the link from this header to next one */ |
| 4990 | old_idx = cur_idx; |
| 4991 | } /* end of cookie processing on this header */ |
| 4992 | } |
| 4993 | |
| 4994 | |
| 4995 | |
| 4996 | /* |
| 4997 | * Check if response is cacheable or not. Updates t->flags. |
| 4998 | */ |
| 4999 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5000 | { |
| 5001 | struct http_txn *txn = &t->txn; |
| 5002 | char *p1, *p2; |
| 5003 | |
| 5004 | char *cur_ptr, *cur_end, *cur_next; |
| 5005 | int cur_idx; |
| 5006 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5007 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5008 | return; |
| 5009 | |
| 5010 | /* Iterate through the headers. |
| 5011 | * we start with the start line. |
| 5012 | */ |
| 5013 | cur_idx = 0; |
| 5014 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5015 | |
| 5016 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5017 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5018 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5019 | |
| 5020 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5021 | cur_ptr = cur_next; |
| 5022 | cur_end = cur_ptr + cur_hdr->len; |
| 5023 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5024 | |
| 5025 | /* We have one full header between cur_ptr and cur_end, and the |
| 5026 | * next header starts at cur_next. We're only interested in |
| 5027 | * "Cookie:" headers. |
| 5028 | */ |
| 5029 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5030 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5031 | if (val) { |
| 5032 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5033 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5034 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5035 | return; |
| 5036 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5037 | } |
| 5038 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5039 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5040 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5041 | continue; |
| 5042 | |
| 5043 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5044 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5045 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5046 | |
| 5047 | if (p1 >= cur_end) /* no more info */ |
| 5048 | continue; |
| 5049 | |
| 5050 | /* p1 is at the beginning of the value */ |
| 5051 | p2 = p1; |
| 5052 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5053 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5054 | p2++; |
| 5055 | |
| 5056 | /* we have a complete value between p1 and p2 */ |
| 5057 | if (p2 < cur_end && *p2 == '=') { |
| 5058 | /* we have something of the form no-cache="set-cookie" */ |
| 5059 | if ((cur_end - p1 >= 21) && |
| 5060 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5061 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5062 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5063 | continue; |
| 5064 | } |
| 5065 | |
| 5066 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5067 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5068 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5069 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5070 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5071 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5072 | return; |
| 5073 | } |
| 5074 | |
| 5075 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5076 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5077 | continue; |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5083 | /* |
| 5084 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5085 | * If the server is found, it's assigned to the session. |
| 5086 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5087 | 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] | 5088 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5089 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5090 | appsess *asession_temp = NULL; |
| 5091 | appsess local_asession; |
| 5092 | char *request_line; |
| 5093 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5094 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5095 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5096 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5097 | ((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] | 5098 | return; |
| 5099 | |
| 5100 | /* skip ';' */ |
| 5101 | request_line++; |
| 5102 | |
| 5103 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5104 | 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] | 5105 | return; |
| 5106 | |
| 5107 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5108 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5109 | |
| 5110 | /* First try if we already have an appsession */ |
| 5111 | asession_temp = &local_asession; |
| 5112 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5113 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5114 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5115 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5116 | return; |
| 5117 | } |
| 5118 | |
| 5119 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5120 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5121 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5122 | asession_temp->serverid = NULL; |
| 5123 | |
| 5124 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5125 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5126 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5127 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5128 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5129 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5130 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5131 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5132 | return; |
| 5133 | } |
| 5134 | asession_temp->sessid = local_asession.sessid; |
| 5135 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5136 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5137 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5138 | } |
| 5139 | else { |
| 5140 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5141 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5142 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5143 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5144 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5145 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5146 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5147 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5148 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5149 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5150 | #endif |
| 5151 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5152 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5153 | Alert("Found Application Session without matching server.\n"); |
| 5154 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5155 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5156 | while (srv) { |
| 5157 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5158 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5159 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5160 | txn->flags &= ~TX_CK_MASK; |
| 5161 | txn->flags |= TX_CK_VALID; |
| 5162 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5163 | t->srv = srv; |
| 5164 | break; |
| 5165 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5166 | txn->flags &= ~TX_CK_MASK; |
| 5167 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5168 | } |
| 5169 | } |
| 5170 | srv = srv->next; |
| 5171 | } |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5176 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5177 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5178 | * 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] | 5179 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5180 | * 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] | 5181 | * 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] | 5182 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5183 | * |
| 5184 | * Returns 1 if the session's state changes, otherwise 0. |
| 5185 | */ |
| 5186 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5187 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5188 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5189 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5190 | struct user_auth *user; |
| 5191 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5192 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5193 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5194 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5195 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5196 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5197 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5198 | return 0; |
| 5199 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5200 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5201 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5202 | /* the URI is in h */ |
| 5203 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5204 | return 0; |
| 5205 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5206 | h += uri_auth->uri_len; |
| 5207 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5208 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5209 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5210 | break; |
| 5211 | } |
| 5212 | h++; |
| 5213 | } |
| 5214 | |
| 5215 | if (uri_auth->refresh) { |
| 5216 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5217 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5218 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5219 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5220 | break; |
| 5221 | } |
| 5222 | h++; |
| 5223 | } |
| 5224 | } |
| 5225 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5226 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5227 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5228 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5229 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5230 | break; |
| 5231 | } |
| 5232 | h++; |
| 5233 | } |
| 5234 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5235 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5236 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5237 | /* we are in front of a interceptable URI. Let's check |
| 5238 | * if there's an authentication and if it's valid. |
| 5239 | */ |
| 5240 | user = uri_auth->users; |
| 5241 | if (!user) { |
| 5242 | /* no user auth required, it's OK */ |
| 5243 | authenticated = 1; |
| 5244 | } else { |
| 5245 | authenticated = 0; |
| 5246 | |
| 5247 | /* a user list is defined, we have to check. |
| 5248 | * skip 21 chars for "Authorization: Basic ". |
| 5249 | */ |
| 5250 | |
| 5251 | /* FIXME: this should move to an earlier place */ |
| 5252 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5253 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5254 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5255 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5256 | if (len > 14 && |
| 5257 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5258 | txn->auth_hdr.str = h; |
| 5259 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5260 | break; |
| 5261 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5262 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5263 | } |
| 5264 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5265 | if (txn->auth_hdr.len < 21 || |
| 5266 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5267 | user = NULL; |
| 5268 | |
| 5269 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5270 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5271 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5272 | user->user_pwd, user->user_len)) { |
| 5273 | authenticated = 1; |
| 5274 | break; |
| 5275 | } |
| 5276 | user = user->next; |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5281 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5282 | |
| 5283 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5284 | msg.str = trash; |
| 5285 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5286 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5287 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5288 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5289 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5290 | if (!(t->flags & SN_ERR_MASK)) |
| 5291 | t->flags |= SN_ERR_PRXCOND; |
| 5292 | if (!(t->flags & SN_FINST_MASK)) |
| 5293 | t->flags |= SN_FINST_R; |
| 5294 | return 1; |
| 5295 | } |
| 5296 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5297 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5298 | * data. |
| 5299 | */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5300 | EV_FD_CLR(t->req->prod->fd, DIR_RD); |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 5301 | buffer_shutr(t->req); |
| 5302 | buffer_shutr(t->rep); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 5303 | buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5304 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5305 | t->data_source = DATA_SRC_STATS; |
| 5306 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5307 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5308 | produce_content(t); |
| 5309 | return 1; |
| 5310 | } |
| 5311 | |
| 5312 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5313 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5314 | * Print a debug line with a header |
| 5315 | */ |
| 5316 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5317 | { |
| 5318 | int len, max; |
| 5319 | 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] | 5320 | 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] | 5321 | max = end - start; |
| 5322 | UBOUND(max, sizeof(trash) - len - 1); |
| 5323 | len += strlcpy2(trash + len, start, max + 1); |
| 5324 | trash[len++] = '\n'; |
| 5325 | write(1, trash, len); |
| 5326 | } |
| 5327 | |
| 5328 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5329 | /************************************************************************/ |
| 5330 | /* The code below is dedicated to ACL parsing and matching */ |
| 5331 | /************************************************************************/ |
| 5332 | |
| 5333 | |
| 5334 | |
| 5335 | |
| 5336 | /* 1. Check on METHOD |
| 5337 | * We use the pre-parsed method if it is known, and store its number as an |
| 5338 | * integer. If it is unknown, we use the pointer and the length. |
| 5339 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5340 | 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] | 5341 | { |
| 5342 | int len, meth; |
| 5343 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5344 | len = strlen(*text); |
| 5345 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5346 | |
| 5347 | pattern->val.i = meth; |
| 5348 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5349 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5350 | if (!pattern->ptr.str) |
| 5351 | return 0; |
| 5352 | pattern->len = len; |
| 5353 | } |
| 5354 | return 1; |
| 5355 | } |
| 5356 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5357 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5358 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5359 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5360 | { |
| 5361 | int meth; |
| 5362 | struct http_txn *txn = l7; |
| 5363 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5364 | if (!txn) |
| 5365 | return 0; |
| 5366 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5367 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5368 | return 0; |
| 5369 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5370 | meth = txn->meth; |
| 5371 | test->i = meth; |
| 5372 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5373 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5374 | /* ensure the indexes are not affected */ |
| 5375 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5376 | test->len = txn->req.sl.rq.m_l; |
| 5377 | test->ptr = txn->req.sol; |
| 5378 | } |
| 5379 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5380 | return 1; |
| 5381 | } |
| 5382 | |
| 5383 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5384 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5385 | int icase; |
| 5386 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5387 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5388 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5389 | |
| 5390 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5391 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5392 | |
| 5393 | /* Other method, we must compare the strings */ |
| 5394 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5395 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5396 | |
| 5397 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5398 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5399 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5400 | return ACL_PAT_FAIL; |
| 5401 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | /* 2. Check on Request/Status Version |
| 5405 | * We simply compare strings here. |
| 5406 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5407 | 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] | 5408 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5409 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5410 | if (!pattern->ptr.str) |
| 5411 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5412 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5413 | return 1; |
| 5414 | } |
| 5415 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5416 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5417 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5418 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5419 | { |
| 5420 | struct http_txn *txn = l7; |
| 5421 | char *ptr; |
| 5422 | int len; |
| 5423 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5424 | if (!txn) |
| 5425 | return 0; |
| 5426 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5427 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5428 | return 0; |
| 5429 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5430 | len = txn->req.sl.rq.v_l; |
| 5431 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5432 | |
| 5433 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5434 | if (len <= 0) |
| 5435 | return 0; |
| 5436 | |
| 5437 | test->ptr = ptr; |
| 5438 | test->len = len; |
| 5439 | |
| 5440 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5441 | return 1; |
| 5442 | } |
| 5443 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5444 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5445 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5446 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5447 | { |
| 5448 | struct http_txn *txn = l7; |
| 5449 | char *ptr; |
| 5450 | int len; |
| 5451 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5452 | if (!txn) |
| 5453 | return 0; |
| 5454 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5455 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5456 | return 0; |
| 5457 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5458 | len = txn->rsp.sl.st.v_l; |
| 5459 | ptr = txn->rsp.sol; |
| 5460 | |
| 5461 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5462 | if (len <= 0) |
| 5463 | return 0; |
| 5464 | |
| 5465 | test->ptr = ptr; |
| 5466 | test->len = len; |
| 5467 | |
| 5468 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5469 | return 1; |
| 5470 | } |
| 5471 | |
| 5472 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5473 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5474 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5475 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5476 | { |
| 5477 | struct http_txn *txn = l7; |
| 5478 | char *ptr; |
| 5479 | int len; |
| 5480 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5481 | if (!txn) |
| 5482 | return 0; |
| 5483 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5484 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5485 | return 0; |
| 5486 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5487 | len = txn->rsp.sl.st.c_l; |
| 5488 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5489 | |
| 5490 | test->i = __strl2ui(ptr, len); |
| 5491 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5492 | return 1; |
| 5493 | } |
| 5494 | |
| 5495 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5496 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5497 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5498 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5499 | { |
| 5500 | struct http_txn *txn = l7; |
| 5501 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5502 | if (!txn) |
| 5503 | return 0; |
| 5504 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5505 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5506 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5507 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5508 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5509 | /* ensure the indexes are not affected */ |
| 5510 | return 0; |
| 5511 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5512 | test->len = txn->req.sl.rq.u_l; |
| 5513 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5514 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5515 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5516 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5517 | return 1; |
| 5518 | } |
| 5519 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5520 | static int |
| 5521 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5522 | struct acl_expr *expr, struct acl_test *test) |
| 5523 | { |
| 5524 | struct http_txn *txn = l7; |
| 5525 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5526 | if (!txn) |
| 5527 | return 0; |
| 5528 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5529 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5530 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5531 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5532 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5533 | /* ensure the indexes are not affected */ |
| 5534 | return 0; |
| 5535 | |
| 5536 | /* Parse HTTP request */ |
| 5537 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5538 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5539 | test->i = AF_INET; |
| 5540 | |
| 5541 | /* |
| 5542 | * If we are parsing url in frontend space, we prepare backend stage |
| 5543 | * to not parse again the same url ! optimization lazyness... |
| 5544 | */ |
| 5545 | if (px->options & PR_O_HTTP_PROXY) |
| 5546 | l4->flags |= SN_ADDR_SET; |
| 5547 | |
| 5548 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5549 | return 1; |
| 5550 | } |
| 5551 | |
| 5552 | static int |
| 5553 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5554 | struct acl_expr *expr, struct acl_test *test) |
| 5555 | { |
| 5556 | struct http_txn *txn = l7; |
| 5557 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5558 | if (!txn) |
| 5559 | return 0; |
| 5560 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5561 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5562 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5563 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5564 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5565 | /* ensure the indexes are not affected */ |
| 5566 | return 0; |
| 5567 | |
| 5568 | /* Same optimization as url_ip */ |
| 5569 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5570 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5571 | |
| 5572 | if (px->options & PR_O_HTTP_PROXY) |
| 5573 | l4->flags |= SN_ADDR_SET; |
| 5574 | |
| 5575 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5576 | return 1; |
| 5577 | } |
| 5578 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5579 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5580 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5581 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5582 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5583 | 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] | 5584 | struct acl_expr *expr, struct acl_test *test) |
| 5585 | { |
| 5586 | struct http_txn *txn = l7; |
| 5587 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5588 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5589 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5590 | if (!txn) |
| 5591 | return 0; |
| 5592 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5593 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5594 | /* search for header from the beginning */ |
| 5595 | ctx->idx = 0; |
| 5596 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5597 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5598 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5599 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5600 | test->len = ctx->vlen; |
| 5601 | test->ptr = (char *)ctx->line + ctx->val; |
| 5602 | return 1; |
| 5603 | } |
| 5604 | |
| 5605 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5606 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5607 | return 0; |
| 5608 | } |
| 5609 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5610 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5611 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5612 | struct acl_expr *expr, struct acl_test *test) |
| 5613 | { |
| 5614 | struct http_txn *txn = l7; |
| 5615 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5616 | if (!txn) |
| 5617 | return 0; |
| 5618 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5619 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5620 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5621 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5622 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5623 | /* ensure the indexes are not affected */ |
| 5624 | return 0; |
| 5625 | |
| 5626 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5627 | } |
| 5628 | |
| 5629 | static int |
| 5630 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5631 | struct acl_expr *expr, struct acl_test *test) |
| 5632 | { |
| 5633 | struct http_txn *txn = l7; |
| 5634 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5635 | if (!txn) |
| 5636 | return 0; |
| 5637 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5638 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5639 | return 0; |
| 5640 | |
| 5641 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5642 | } |
| 5643 | |
| 5644 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5645 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5646 | */ |
| 5647 | static int |
| 5648 | 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] | 5649 | struct acl_expr *expr, struct acl_test *test) |
| 5650 | { |
| 5651 | struct http_txn *txn = l7; |
| 5652 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5653 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5654 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5655 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5656 | if (!txn) |
| 5657 | return 0; |
| 5658 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5659 | ctx.idx = 0; |
| 5660 | cnt = 0; |
| 5661 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5662 | cnt++; |
| 5663 | |
| 5664 | test->i = cnt; |
| 5665 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5666 | return 1; |
| 5667 | } |
| 5668 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5669 | static int |
| 5670 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5671 | struct acl_expr *expr, struct acl_test *test) |
| 5672 | { |
| 5673 | struct http_txn *txn = l7; |
| 5674 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5675 | if (!txn) |
| 5676 | return 0; |
| 5677 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5678 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5679 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5680 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5681 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5682 | /* ensure the indexes are not affected */ |
| 5683 | return 0; |
| 5684 | |
| 5685 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5686 | } |
| 5687 | |
| 5688 | static int |
| 5689 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5690 | struct acl_expr *expr, struct acl_test *test) |
| 5691 | { |
| 5692 | struct http_txn *txn = l7; |
| 5693 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5694 | if (!txn) |
| 5695 | return 0; |
| 5696 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5697 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5698 | return 0; |
| 5699 | |
| 5700 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5701 | } |
| 5702 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5703 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5704 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5705 | * 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] | 5706 | */ |
| 5707 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5708 | 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] | 5709 | struct acl_expr *expr, struct acl_test *test) |
| 5710 | { |
| 5711 | struct http_txn *txn = l7; |
| 5712 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5713 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5714 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5715 | if (!txn) |
| 5716 | return 0; |
| 5717 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5718 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5719 | /* search for header from the beginning */ |
| 5720 | ctx->idx = 0; |
| 5721 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5722 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5723 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5724 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5725 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5726 | return 1; |
| 5727 | } |
| 5728 | |
| 5729 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5730 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5734 | static int |
| 5735 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5736 | struct acl_expr *expr, struct acl_test *test) |
| 5737 | { |
| 5738 | struct http_txn *txn = l7; |
| 5739 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5740 | if (!txn) |
| 5741 | return 0; |
| 5742 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5743 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5744 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5745 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5746 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5747 | /* ensure the indexes are not affected */ |
| 5748 | return 0; |
| 5749 | |
| 5750 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5751 | } |
| 5752 | |
| 5753 | static int |
| 5754 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5755 | struct acl_expr *expr, struct acl_test *test) |
| 5756 | { |
| 5757 | struct http_txn *txn = l7; |
| 5758 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5759 | if (!txn) |
| 5760 | return 0; |
| 5761 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5762 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5763 | return 0; |
| 5764 | |
| 5765 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5766 | } |
| 5767 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5768 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5769 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5770 | */ |
| 5771 | static int |
| 5772 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5773 | struct acl_expr *expr, struct acl_test *test) |
| 5774 | { |
| 5775 | struct http_txn *txn = l7; |
| 5776 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5777 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5778 | if (!txn) |
| 5779 | return 0; |
| 5780 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5781 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5782 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5783 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5784 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5785 | /* ensure the indexes are not affected */ |
| 5786 | return 0; |
| 5787 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5788 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5789 | ptr = http_get_path(txn); |
| 5790 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5791 | return 0; |
| 5792 | |
| 5793 | /* OK, we got the '/' ! */ |
| 5794 | test->ptr = ptr; |
| 5795 | |
| 5796 | while (ptr < end && *ptr != '?') |
| 5797 | ptr++; |
| 5798 | |
| 5799 | test->len = ptr - test->ptr; |
| 5800 | |
| 5801 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5802 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5803 | return 1; |
| 5804 | } |
| 5805 | |
| 5806 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5807 | |
| 5808 | /************************************************************************/ |
| 5809 | /* All supported keywords must be declared here. */ |
| 5810 | /************************************************************************/ |
| 5811 | |
| 5812 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5813 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5814 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5815 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5816 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5817 | { "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] | 5818 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5819 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5820 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5821 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5822 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5823 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5824 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5825 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5826 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5827 | { "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] | 5828 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5829 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5830 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5831 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5832 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5833 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5834 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5835 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5836 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5837 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5838 | { "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] | 5839 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5840 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5841 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5842 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5843 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5844 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5845 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5846 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5847 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5848 | { "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] | 5849 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5850 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5851 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5852 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5853 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5854 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5855 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5856 | { "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] | 5857 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5858 | { NULL, NULL, NULL, NULL }, |
| 5859 | |
| 5860 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5861 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5862 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5863 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5864 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5865 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5866 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5867 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5868 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5869 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5870 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5871 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5872 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5873 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5874 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5875 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5876 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5877 | |
| 5878 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5879 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5880 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5881 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5882 | { NULL, NULL, NULL, NULL }, |
| 5883 | #endif |
| 5884 | }}; |
| 5885 | |
| 5886 | |
| 5887 | __attribute__((constructor)) |
| 5888 | static void __http_protocol_init(void) |
| 5889 | { |
| 5890 | acl_register_keywords(&acl_kws); |
| 5891 | } |
| 5892 | |
| 5893 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5894 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5895 | * Local variables: |
| 5896 | * c-indent-level: 8 |
| 5897 | * c-basic-offset: 8 |
| 5898 | * End: |
| 5899 | */ |