Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 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> |
| 33 | #include <common/time.h> |
| 34 | #include <common/uri_auth.h> |
| 35 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 37 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <types/capture.h> |
| 39 | #include <types/client.h> |
| 40 | #include <types/global.h> |
| 41 | #include <types/httperr.h> |
| 42 | #include <types/polling.h> |
| 43 | #include <types/proxy.h> |
| 44 | #include <types/server.h> |
| 45 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 46 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
| 51 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 52 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
| 54 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 55 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/session.h> |
| 57 | #include <proto/task.h> |
| 58 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_HAP_TCPSPLICE |
| 60 | #include <libtcpsplice.h> |
| 61 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 63 | #define DEBUG_PARSE_NO_SPEEDUP |
| 64 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 65 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 66 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 67 | * instruction. The first argument is the label for normal operation, and the |
| 68 | * second one is the break/continue instruction in the no_speedup mode. |
| 69 | */ |
| 70 | |
| 71 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 72 | #define QUICK_JUMP(x,y) y |
| 73 | #else |
| 74 | #define QUICK_JUMP(x,y) goto x |
| 75 | #endif |
| 76 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 79 | "HTTP/1.0 200 OK\r\n" |
| 80 | "Cache-Control: no-cache\r\n" |
| 81 | "Connection: close\r\n" |
| 82 | "Content-Type: text/html\r\n" |
| 83 | "\r\n" |
| 84 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 85 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | const struct chunk http_200_chunk = { |
| 87 | .str = (char *)&HTTP_200, |
| 88 | .len = sizeof(HTTP_200)-1 |
| 89 | }; |
| 90 | |
| 91 | const char *HTTP_302 = |
| 92 | "HTTP/1.0 302 Found\r\n" |
| 93 | "Cache-Control: no-cache\r\n" |
| 94 | "Connection: close\r\n" |
| 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
| 99 | "HTTP/1.0 303 See Other\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Connection: close\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 114 | |
| 115 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 133 | |
| 134 | [HTTP_ERR_403] = |
| 135 | "HTTP/1.0 403 Forbidden\r\n" |
| 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_408] = |
| 143 | "HTTP/1.0 408 Request Time-out\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_500] = |
| 151 | "HTTP/1.0 500 Server Error\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_502] = |
| 159 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_503] = |
| 167 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_504] = |
| 175 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 181 | |
| 182 | }; |
| 183 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 184 | /* We must put the messages here since GCC cannot initialize consts depending |
| 185 | * on strlen(). |
| 186 | */ |
| 187 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 188 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 189 | #define FD_SETS_ARE_BITFIELDS |
| 190 | #ifdef FD_SETS_ARE_BITFIELDS |
| 191 | /* |
| 192 | * This map is used with all the FD_* macros to check whether a particular bit |
| 193 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 194 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 195 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 196 | * exclusively to the macros. |
| 197 | */ |
| 198 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 199 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 200 | |
| 201 | #else |
| 202 | #error "Check if your OS uses bitfields for fd_sets" |
| 203 | #endif |
| 204 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 205 | void init_proto_http() |
| 206 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 207 | int i; |
| 208 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 212 | if (!http_err_msgs[msg]) { |
| 213 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 214 | abort(); |
| 215 | } |
| 216 | |
| 217 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 218 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 219 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
| 221 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 222 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 223 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 224 | * printable characters above. |
| 225 | */ |
| 226 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 227 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 228 | for (i = 0; i < 32; i++) { |
| 229 | FD_SET(i, hdr_encode_map); |
| 230 | FD_SET(i, url_encode_map); |
| 231 | } |
| 232 | for (i = 127; i < 256; i++) { |
| 233 | FD_SET(i, hdr_encode_map); |
| 234 | FD_SET(i, url_encode_map); |
| 235 | } |
| 236 | |
| 237 | tmp = "\"#{|}"; |
| 238 | while (*tmp) { |
| 239 | FD_SET(*tmp, hdr_encode_map); |
| 240 | tmp++; |
| 241 | } |
| 242 | |
| 243 | tmp = "\"#"; |
| 244 | while (*tmp) { |
| 245 | FD_SET(*tmp, url_encode_map); |
| 246 | tmp++; |
| 247 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 248 | |
| 249 | /* memory allocations */ |
| 250 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 251 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 252 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 254 | /* |
| 255 | * We have 26 list of methods (1 per first letter), each of which can have |
| 256 | * up to 3 entries (2 valid, 1 null). |
| 257 | */ |
| 258 | struct http_method_desc { |
| 259 | http_meth_t meth; |
| 260 | int len; |
| 261 | const char text[8]; |
| 262 | }; |
| 263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 264 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 265 | ['C' - 'A'] = { |
| 266 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 267 | }, |
| 268 | ['D' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 270 | }, |
| 271 | ['G' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 273 | }, |
| 274 | ['H' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 276 | }, |
| 277 | ['P' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 279 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 280 | }, |
| 281 | ['T' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 283 | }, |
| 284 | /* rest is empty like this : |
| 285 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 286 | */ |
| 287 | }; |
| 288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 289 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 290 | * table than two perform a boolean AND or OR between two tests. Refer to |
| 291 | * RFC2616 for those chars. |
| 292 | */ |
| 293 | |
| 294 | const char http_is_spht[256] = { |
| 295 | [' '] = 1, ['\t'] = 1, |
| 296 | }; |
| 297 | |
| 298 | const char http_is_crlf[256] = { |
| 299 | ['\r'] = 1, ['\n'] = 1, |
| 300 | }; |
| 301 | |
| 302 | const char http_is_lws[256] = { |
| 303 | [' '] = 1, ['\t'] = 1, |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_sep[256] = { |
| 308 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 309 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 310 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 311 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 312 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 313 | }; |
| 314 | |
| 315 | const char http_is_ctl[256] = { |
| 316 | [0 ... 31] = 1, |
| 317 | [127] = 1, |
| 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 322 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 323 | * them correctly. Instead, define every non-CTL char's status. |
| 324 | */ |
| 325 | const char http_is_token[256] = { |
| 326 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 327 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 328 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 329 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 330 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 331 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 332 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 333 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 334 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 335 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 336 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 337 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 338 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 339 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 340 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 341 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 342 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 343 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 344 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 345 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 346 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 347 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 348 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 349 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 350 | }; |
| 351 | |
| 352 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 353 | /* |
| 354 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 355 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 356 | */ |
| 357 | const char http_is_ver_token[256] = { |
| 358 | ['.'] = 1, ['/'] = 1, |
| 359 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 360 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 361 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 362 | }; |
| 363 | |
| 364 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | #ifdef DEBUG_FULL |
| 366 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 367 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 368 | #endif |
| 369 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 370 | static void http_sess_log(struct session *s); |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 538 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 539 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 540 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 541 | */ |
| 542 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 543 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | { |
| 545 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 546 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 547 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 548 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 549 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 550 | } |
| 551 | if (!(t->flags & SN_ERR_MASK)) |
| 552 | t->flags |= err; |
| 553 | if (!(t->flags & SN_FINST_MASK)) |
| 554 | t->flags |= finst; |
| 555 | } |
| 556 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 557 | /* This function returns the appropriate error location for the given session |
| 558 | * and message. |
| 559 | */ |
| 560 | |
| 561 | struct chunk *error_message(struct session *s, int msgnum) |
| 562 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 563 | if (s->be->errmsg[msgnum].str) |
| 564 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 565 | else if (s->fe->errmsg[msgnum].str) |
| 566 | return &s->fe->errmsg[msgnum]; |
| 567 | else |
| 568 | return &http_err_chunks[msgnum]; |
| 569 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 571 | /* |
| 572 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 573 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 574 | */ |
| 575 | static http_meth_t find_http_meth(const char *str, const int len) |
| 576 | { |
| 577 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 578 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 579 | |
| 580 | m = ((unsigned)*str - 'A'); |
| 581 | |
| 582 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 583 | for (h = http_methods[m]; h->len > 0; h++) { |
| 584 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 585 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 587 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | }; |
| 589 | return HTTP_METH_OTHER; |
| 590 | } |
| 591 | return HTTP_METH_NONE; |
| 592 | |
| 593 | } |
| 594 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | /* Processes the client and server jobs of a session task, then |
| 596 | * puts it back to the wait queue in a clean state, or |
| 597 | * cleans up its resources if it must be deleted. Returns |
| 598 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 599 | * infinity. |
| 600 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 601 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 602 | { |
| 603 | struct session *s = t->context; |
| 604 | int fsm_resync = 0; |
| 605 | |
| 606 | do { |
| 607 | fsm_resync = 0; |
| 608 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 609 | fsm_resync |= process_cli(s); |
| 610 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 611 | fsm_resync |= process_srv(s); |
| 612 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 613 | } while (fsm_resync); |
| 614 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 615 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 616 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 617 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 618 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 619 | t->expire = s->req->rex; |
| 620 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 621 | tv_bound(&t->expire, &s->req->cex); |
| 622 | tv_bound(&t->expire, &s->rep->rex); |
| 623 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 624 | |
| 625 | /* restore t to its place in the task list */ |
| 626 | task_queue(t); |
| 627 | |
| 628 | #ifdef DEBUG_FULL |
| 629 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 630 | * that a task still has something to do and will provoke a quick loop. |
| 631 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 632 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | exit(100); |
| 634 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 635 | *next = t->expire; |
| 636 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 639 | s->fe->feconn--; |
| 640 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 641 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | actconn--; |
| 643 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 644 | if (unlikely((global.mode & MODE_DEBUG) && |
| 645 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 646 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 647 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 648 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 649 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | write(1, trash, len); |
| 651 | } |
| 652 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 653 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 654 | if (s->req != NULL) |
| 655 | s->logs.bytes_in = s->req->total; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | if (s->rep != NULL) |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 657 | s->logs.bytes_out = s->rep->total; |
| 658 | |
| 659 | s->fe->bytes_in += s->logs.bytes_in; |
| 660 | s->fe->bytes_out += s->logs.bytes_out; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 661 | if (s->be != s->fe) { |
| 662 | s->be->bytes_in += s->logs.bytes_in; |
| 663 | s->be->bytes_out += s->logs.bytes_out; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 664 | } |
| 665 | if (s->srv) { |
| 666 | s->srv->bytes_in += s->logs.bytes_in; |
| 667 | s->srv->bytes_out += s->logs.bytes_out; |
| 668 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | |
| 670 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 671 | if (s->logs.logwait && |
| 672 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 673 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 674 | if (s->fe->to_log & LW_REQ) |
| 675 | http_sess_log(s); |
| 676 | else |
| 677 | tcp_sess_log(s); |
| 678 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | |
| 680 | /* the task MUST not be in the run queue anymore */ |
| 681 | task_delete(t); |
| 682 | session_free(s); |
| 683 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 684 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 688 | extern const char sess_term_cond[8]; |
| 689 | extern const char sess_fin_state[8]; |
| 690 | extern const char *monthname[12]; |
| 691 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 692 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 693 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 694 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 695 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 696 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 698 | /* |
| 699 | * send a log for the session when we have enough info about it. |
| 700 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 701 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 702 | static void http_sess_log(struct session *s) |
| 703 | { |
| 704 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 705 | struct proxy *fe = s->fe; |
| 706 | struct proxy *be = s->be; |
| 707 | struct proxy *prx_log; |
| 708 | struct http_txn *txn = &s->txn; |
| 709 | int tolog; |
| 710 | char *uri, *h; |
| 711 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 712 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 713 | static char tmpline[MAX_SYSLOG_LEN]; |
| 714 | int hdr; |
| 715 | |
| 716 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 717 | return; |
| 718 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 719 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 720 | if (s->cli_addr.ss_family == AF_INET) |
| 721 | inet_ntop(AF_INET, |
| 722 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 723 | pn, sizeof(pn)); |
| 724 | else |
| 725 | inet_ntop(AF_INET6, |
| 726 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 727 | pn, sizeof(pn)); |
| 728 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 729 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 730 | |
| 731 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 732 | tolog = fe->to_log; |
| 733 | |
| 734 | h = tmpline; |
| 735 | if (fe->to_log & LW_REQHDR && |
| 736 | txn->req.cap && |
| 737 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 738 | *(h++) = ' '; |
| 739 | *(h++) = '{'; |
| 740 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 741 | if (hdr) |
| 742 | *(h++) = '|'; |
| 743 | if (txn->req.cap[hdr] != NULL) |
| 744 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 745 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 746 | } |
| 747 | *(h++) = '}'; |
| 748 | } |
| 749 | |
| 750 | if (fe->to_log & LW_RSPHDR && |
| 751 | txn->rsp.cap && |
| 752 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 753 | *(h++) = ' '; |
| 754 | *(h++) = '{'; |
| 755 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 756 | if (hdr) |
| 757 | *(h++) = '|'; |
| 758 | if (txn->rsp.cap[hdr] != NULL) |
| 759 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 760 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 761 | } |
| 762 | *(h++) = '}'; |
| 763 | } |
| 764 | |
| 765 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 766 | *(h++) = ' '; |
| 767 | *(h++) = '"'; |
| 768 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 769 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 770 | '#', url_encode_map, uri); |
| 771 | *(h++) = '"'; |
| 772 | } |
| 773 | *h = '\0'; |
| 774 | |
| 775 | svid = (tolog & LW_SVID) ? |
| 776 | (s->data_source != DATA_SRC_STATS) ? |
| 777 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 778 | |
| 779 | send_log(prx_log, LOG_INFO, |
| 780 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 781 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
| 782 | " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n", |
| 783 | pn, |
| 784 | (s->cli_addr.ss_family == AF_INET) ? |
| 785 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 786 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 787 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 788 | tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 789 | fe->id, be->id, svid, |
| 790 | s->logs.t_request, |
| 791 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 792 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 793 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 794 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 795 | txn->status, |
| 796 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in, |
| 797 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 798 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 799 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 800 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 801 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 802 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 803 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 804 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 805 | |
| 806 | s->logs.logwait = 0; |
| 807 | } |
| 808 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * Capture headers from message starting at <som> according to header list |
| 812 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 813 | */ |
| 814 | void capture_headers(char *som, struct hdr_idx *idx, |
| 815 | char **cap, struct cap_hdr *cap_hdr) |
| 816 | { |
| 817 | char *eol, *sol, *col, *sov; |
| 818 | int cur_idx; |
| 819 | struct cap_hdr *h; |
| 820 | int len; |
| 821 | |
| 822 | sol = som + hdr_idx_first_pos(idx); |
| 823 | cur_idx = hdr_idx_first_idx(idx); |
| 824 | |
| 825 | while (cur_idx) { |
| 826 | eol = sol + idx->v[cur_idx].len; |
| 827 | |
| 828 | col = sol; |
| 829 | while (col < eol && *col != ':') |
| 830 | col++; |
| 831 | |
| 832 | sov = col + 1; |
| 833 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 834 | sov++; |
| 835 | |
| 836 | for (h = cap_hdr; h; h = h->next) { |
| 837 | if ((h->namelen == col - sol) && |
| 838 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 839 | if (cap[h->index] == NULL) |
| 840 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 841 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 842 | |
| 843 | if (cap[h->index] == NULL) { |
| 844 | Alert("HTTP capture : out of memory.\n"); |
| 845 | continue; |
| 846 | } |
| 847 | |
| 848 | len = eol - sov; |
| 849 | if (len > h->len) |
| 850 | len = h->len; |
| 851 | |
| 852 | memcpy(cap[h->index], sov, len); |
| 853 | cap[h->index][len]=0; |
| 854 | } |
| 855 | } |
| 856 | sol = eol + idx->v[cur_idx].cr + 1; |
| 857 | cur_idx = idx->v[cur_idx].next; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 862 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 863 | */ |
| 864 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 865 | |
| 866 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 867 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 868 | */ |
| 869 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 870 | ptr++; \ |
| 871 | if (likely(ptr < end)) \ |
| 872 | goto good; \ |
| 873 | else { \ |
| 874 | state = (st); \ |
| 875 | goto http_msg_ood; \ |
| 876 | } \ |
| 877 | } while (0) |
| 878 | |
| 879 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 880 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 881 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 882 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 883 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 884 | * will give undefined results. |
| 885 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 886 | * and that msg->sol points to the beginning of the response. |
| 887 | * If a complete line is found (which implies that at least one CR or LF is |
| 888 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 889 | * returned indicating an incomplete line (which does not mean that parts have |
| 890 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 891 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 892 | * upon next call. |
| 893 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 894 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 895 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 896 | * 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] | 897 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 898 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 899 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 900 | const char *ptr, const char *end, |
| 901 | char **ret_ptr, int *ret_state) |
| 902 | { |
| 903 | __label__ |
| 904 | http_msg_rpver, |
| 905 | http_msg_rpver_sp, |
| 906 | http_msg_rpcode, |
| 907 | http_msg_rpcode_sp, |
| 908 | http_msg_rpreason, |
| 909 | http_msg_rpline_eol, |
| 910 | http_msg_ood, /* out of data */ |
| 911 | http_msg_invalid; |
| 912 | |
| 913 | switch (state) { |
| 914 | http_msg_rpver: |
| 915 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 916 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 917 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 918 | |
| 919 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 920 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 921 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 922 | } |
| 923 | goto http_msg_invalid; |
| 924 | |
| 925 | http_msg_rpver_sp: |
| 926 | case HTTP_MSG_RPVER_SP: |
| 927 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 928 | msg->sl.st.c = ptr - msg_buf; |
| 929 | goto http_msg_rpcode; |
| 930 | } |
| 931 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 932 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 933 | /* so it's a CR/LF, this is invalid */ |
| 934 | goto http_msg_invalid; |
| 935 | |
| 936 | http_msg_rpcode: |
| 937 | case HTTP_MSG_RPCODE: |
| 938 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 939 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 940 | |
| 941 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 942 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 943 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 944 | } |
| 945 | |
| 946 | /* so it's a CR/LF, so there is no reason phrase */ |
| 947 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 948 | http_msg_rsp_reason: |
| 949 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 950 | msg->sl.st.r = ptr - msg_buf; |
| 951 | msg->sl.st.r_l = 0; |
| 952 | goto http_msg_rpline_eol; |
| 953 | |
| 954 | http_msg_rpcode_sp: |
| 955 | case HTTP_MSG_RPCODE_SP: |
| 956 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 957 | msg->sl.st.r = ptr - msg_buf; |
| 958 | goto http_msg_rpreason; |
| 959 | } |
| 960 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 961 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 962 | /* so it's a CR/LF, so there is no reason phrase */ |
| 963 | goto http_msg_rsp_reason; |
| 964 | |
| 965 | http_msg_rpreason: |
| 966 | case HTTP_MSG_RPREASON: |
| 967 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 968 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 969 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 970 | http_msg_rpline_eol: |
| 971 | /* We have seen the end of line. Note that we do not |
| 972 | * necessarily have the \n yet, but at least we know that we |
| 973 | * have EITHER \r OR \n, otherwise the response would not be |
| 974 | * complete. We can then record the response length and return |
| 975 | * to the caller which will be able to register it. |
| 976 | */ |
| 977 | msg->sl.st.l = ptr - msg->sol; |
| 978 | return ptr; |
| 979 | |
| 980 | #ifdef DEBUG_FULL |
| 981 | default: |
| 982 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 983 | exit(1); |
| 984 | #endif |
| 985 | } |
| 986 | |
| 987 | http_msg_ood: |
| 988 | /* out of data */ |
| 989 | if (ret_state) |
| 990 | *ret_state = state; |
| 991 | if (ret_ptr) |
| 992 | *ret_ptr = (char *)ptr; |
| 993 | return NULL; |
| 994 | |
| 995 | http_msg_invalid: |
| 996 | /* invalid message */ |
| 997 | if (ret_state) |
| 998 | *ret_state = HTTP_MSG_ERROR; |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1004 | * This function parses a request line between <ptr> and <end>, starting with |
| 1005 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1006 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1007 | * will give undefined results. |
| 1008 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1009 | * and that msg->sol points to the beginning of the request. |
| 1010 | * If a complete line is found (which implies that at least one CR or LF is |
| 1011 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1012 | * returned indicating an incomplete line (which does not mean that parts have |
| 1013 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1014 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1015 | * upon next call. |
| 1016 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1017 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1018 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1019 | * 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] | 1020 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1021 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1022 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1023 | const char *ptr, const char *end, |
| 1024 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1025 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1026 | __label__ |
| 1027 | http_msg_rqmeth, |
| 1028 | http_msg_rqmeth_sp, |
| 1029 | http_msg_rquri, |
| 1030 | http_msg_rquri_sp, |
| 1031 | http_msg_rqver, |
| 1032 | http_msg_rqline_eol, |
| 1033 | http_msg_ood, /* out of data */ |
| 1034 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1035 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1036 | switch (state) { |
| 1037 | http_msg_rqmeth: |
| 1038 | case HTTP_MSG_RQMETH: |
| 1039 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1040 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1041 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1042 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1043 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1044 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1045 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1046 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1047 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1048 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1049 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1050 | http_msg_req09_uri: |
| 1051 | msg->sl.rq.u = ptr - msg_buf; |
| 1052 | http_msg_req09_uri_e: |
| 1053 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1054 | http_msg_req09_ver: |
| 1055 | msg->sl.rq.v = ptr - msg_buf; |
| 1056 | msg->sl.rq.v_l = 0; |
| 1057 | goto http_msg_rqline_eol; |
| 1058 | } |
| 1059 | goto http_msg_invalid; |
| 1060 | |
| 1061 | http_msg_rqmeth_sp: |
| 1062 | case HTTP_MSG_RQMETH_SP: |
| 1063 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1064 | msg->sl.rq.u = ptr - msg_buf; |
| 1065 | goto http_msg_rquri; |
| 1066 | } |
| 1067 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1068 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1069 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1070 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1071 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | http_msg_rquri: |
| 1073 | case HTTP_MSG_RQURI: |
| 1074 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1078 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1079 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1080 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1081 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1083 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1084 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | http_msg_rquri_sp: |
| 1086 | case HTTP_MSG_RQURI_SP: |
| 1087 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1088 | msg->sl.rq.v = ptr - msg_buf; |
| 1089 | goto http_msg_rqver; |
| 1090 | } |
| 1091 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1092 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1093 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1094 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1095 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1096 | http_msg_rqver: |
| 1097 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1098 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1099 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1100 | |
| 1101 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1102 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1103 | http_msg_rqline_eol: |
| 1104 | /* We have seen the end of line. Note that we do not |
| 1105 | * necessarily have the \n yet, but at least we know that we |
| 1106 | * have EITHER \r OR \n, otherwise the request would not be |
| 1107 | * complete. We can then record the request length and return |
| 1108 | * to the caller which will be able to register it. |
| 1109 | */ |
| 1110 | msg->sl.rq.l = ptr - msg->sol; |
| 1111 | return ptr; |
| 1112 | } |
| 1113 | |
| 1114 | /* neither an HTTP_VER token nor a CRLF */ |
| 1115 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1116 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1117 | #ifdef DEBUG_FULL |
| 1118 | default: |
| 1119 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1120 | exit(1); |
| 1121 | #endif |
| 1122 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1124 | http_msg_ood: |
| 1125 | /* out of data */ |
| 1126 | if (ret_state) |
| 1127 | *ret_state = state; |
| 1128 | if (ret_ptr) |
| 1129 | *ret_ptr = (char *)ptr; |
| 1130 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1131 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1132 | http_msg_invalid: |
| 1133 | /* invalid message */ |
| 1134 | if (ret_state) |
| 1135 | *ret_state = HTTP_MSG_ERROR; |
| 1136 | return NULL; |
| 1137 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1138 | |
| 1139 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | /* |
| 1141 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1142 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1143 | * when data are missing and recalled at the exact same location with no |
| 1144 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1145 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1146 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1147 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1148 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1149 | { |
| 1150 | __label__ |
| 1151 | http_msg_rqbefore, |
| 1152 | http_msg_rqbefore_cr, |
| 1153 | http_msg_rqmeth, |
| 1154 | http_msg_rqline_end, |
| 1155 | http_msg_hdr_first, |
| 1156 | http_msg_hdr_name, |
| 1157 | http_msg_hdr_l1_sp, |
| 1158 | http_msg_hdr_l1_lf, |
| 1159 | http_msg_hdr_l1_lws, |
| 1160 | http_msg_hdr_val, |
| 1161 | http_msg_hdr_l2_lf, |
| 1162 | http_msg_hdr_l2_lws, |
| 1163 | http_msg_complete_header, |
| 1164 | http_msg_last_lf, |
| 1165 | http_msg_ood, /* out of data */ |
| 1166 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1168 | int state; /* updated only when leaving the FSM */ |
| 1169 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1171 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1172 | ptr = buf->lr; |
| 1173 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1175 | if (unlikely(ptr >= end)) |
| 1176 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | /* |
| 1180 | * First, states that are specific to the response only. |
| 1181 | * We check them first so that request and headers are |
| 1182 | * closer to each other (accessed more often). |
| 1183 | */ |
| 1184 | http_msg_rpbefore: |
| 1185 | case HTTP_MSG_RPBEFORE: |
| 1186 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1187 | if (likely(ptr == buf->data)) { |
| 1188 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1189 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | } else { |
| 1191 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1192 | /* only skip empty leading lines, don't remove them */ |
| 1193 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1194 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1195 | #else |
| 1196 | /* Remove empty leading lines, as recommended by |
| 1197 | * RFC2616. This takes a lot of time because we |
| 1198 | * must move all the buffer backwards, but this |
| 1199 | * is rarely needed. The method above will be |
| 1200 | * cleaner when we'll be able to start sending |
| 1201 | * the request from any place in the buffer. |
| 1202 | */ |
| 1203 | buf->lr = ptr; |
| 1204 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1205 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | msg->sol = buf->data; |
| 1207 | ptr = buf->data; |
| 1208 | end = buf->r; |
| 1209 | #endif |
| 1210 | } |
| 1211 | hdr_idx_init(idx); |
| 1212 | state = HTTP_MSG_RPVER; |
| 1213 | goto http_msg_rpver; |
| 1214 | } |
| 1215 | |
| 1216 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1217 | goto http_msg_invalid; |
| 1218 | |
| 1219 | if (unlikely(*ptr == '\n')) |
| 1220 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1221 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1222 | /* stop here */ |
| 1223 | |
| 1224 | http_msg_rpbefore_cr: |
| 1225 | case HTTP_MSG_RPBEFORE_CR: |
| 1226 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1227 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1228 | /* stop here */ |
| 1229 | |
| 1230 | http_msg_rpver: |
| 1231 | case HTTP_MSG_RPVER: |
| 1232 | case HTTP_MSG_RPVER_SP: |
| 1233 | case HTTP_MSG_RPCODE: |
| 1234 | case HTTP_MSG_RPCODE_SP: |
| 1235 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1236 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1237 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1238 | if (unlikely(!ptr)) |
| 1239 | return; |
| 1240 | |
| 1241 | /* we have a full response and we know that we have either a CR |
| 1242 | * or an LF at <ptr>. |
| 1243 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1244 | //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] | 1245 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1246 | |
| 1247 | msg->sol = ptr; |
| 1248 | if (likely(*ptr == '\r')) |
| 1249 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1250 | goto http_msg_rpline_end; |
| 1251 | |
| 1252 | http_msg_rpline_end: |
| 1253 | case HTTP_MSG_RPLINE_END: |
| 1254 | /* msg->sol must point to the first of CR or LF. */ |
| 1255 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1256 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1257 | /* stop here */ |
| 1258 | |
| 1259 | /* |
| 1260 | * Second, states that are specific to the request only |
| 1261 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1262 | http_msg_rqbefore: |
| 1263 | case HTTP_MSG_RQBEFORE: |
| 1264 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1265 | if (likely(ptr == buf->data)) { |
| 1266 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1267 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1268 | } else { |
| 1269 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1270 | /* only skip empty leading lines, don't remove them */ |
| 1271 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1272 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1273 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1274 | /* Remove empty leading lines, as recommended by |
| 1275 | * RFC2616. This takes a lot of time because we |
| 1276 | * must move all the buffer backwards, but this |
| 1277 | * is rarely needed. The method above will be |
| 1278 | * cleaner when we'll be able to start sending |
| 1279 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1280 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | buf->lr = ptr; |
| 1282 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1283 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | msg->sol = buf->data; |
| 1285 | ptr = buf->data; |
| 1286 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1287 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1288 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1289 | /* we will need this when keep-alive will be supported |
| 1290 | hdr_idx_init(idx); |
| 1291 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | state = HTTP_MSG_RQMETH; |
| 1293 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1294 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1295 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1296 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1297 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1299 | if (unlikely(*ptr == '\n')) |
| 1300 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1301 | 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] | 1302 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1304 | http_msg_rqbefore_cr: |
| 1305 | case HTTP_MSG_RQBEFORE_CR: |
| 1306 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1307 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1309 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | http_msg_rqmeth: |
| 1311 | case HTTP_MSG_RQMETH: |
| 1312 | case HTTP_MSG_RQMETH_SP: |
| 1313 | case HTTP_MSG_RQURI: |
| 1314 | case HTTP_MSG_RQURI_SP: |
| 1315 | case HTTP_MSG_RQVER: |
| 1316 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1317 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | if (unlikely(!ptr)) |
| 1319 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1320 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | /* we have a full request and we know that we have either a CR |
| 1322 | * or an LF at <ptr>. |
| 1323 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1324 | //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] | 1325 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1327 | msg->sol = ptr; |
| 1328 | if (likely(*ptr == '\r')) |
| 1329 | 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] | 1330 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1331 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1332 | http_msg_rqline_end: |
| 1333 | case HTTP_MSG_RQLINE_END: |
| 1334 | /* check for HTTP/0.9 request : no version information available. |
| 1335 | * msg->sol must point to the first of CR or LF. |
| 1336 | */ |
| 1337 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1338 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1339 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1341 | 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] | 1342 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1343 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1344 | /* |
| 1345 | * Common states below |
| 1346 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | http_msg_hdr_first: |
| 1348 | case HTTP_MSG_HDR_FIRST: |
| 1349 | msg->sol = ptr; |
| 1350 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1351 | goto http_msg_hdr_name; |
| 1352 | } |
| 1353 | |
| 1354 | if (likely(*ptr == '\r')) |
| 1355 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1356 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | http_msg_hdr_name: |
| 1359 | case HTTP_MSG_HDR_NAME: |
| 1360 | /* assumes msg->sol points to the first char */ |
| 1361 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1362 | 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] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | if (likely(*ptr == ':')) { |
| 1365 | msg->col = ptr - buf->data; |
| 1366 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1367 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1368 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1369 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1370 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1371 | http_msg_hdr_l1_sp: |
| 1372 | case HTTP_MSG_HDR_L1_SP: |
| 1373 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1374 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1375 | 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] | 1376 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | /* header value can be basically anything except CR/LF */ |
| 1378 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1379 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1381 | goto http_msg_hdr_val; |
| 1382 | } |
| 1383 | |
| 1384 | if (likely(*ptr == '\r')) |
| 1385 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1386 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | http_msg_hdr_l1_lf: |
| 1389 | case HTTP_MSG_HDR_L1_LF: |
| 1390 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1391 | 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] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | http_msg_hdr_l1_lws: |
| 1394 | case HTTP_MSG_HDR_L1_LWS: |
| 1395 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1396 | /* replace HT,CR,LF with spaces */ |
| 1397 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1398 | buf->data[msg->sov] = ' '; |
| 1399 | goto http_msg_hdr_l1_sp; |
| 1400 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1401 | /* we had a header consisting only in spaces ! */ |
| 1402 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | goto http_msg_complete_header; |
| 1404 | |
| 1405 | http_msg_hdr_val: |
| 1406 | case HTTP_MSG_HDR_VAL: |
| 1407 | /* assumes msg->sol points to the first char, msg->col to the |
| 1408 | * colon, and msg->sov points to the first character of the |
| 1409 | * value. |
| 1410 | */ |
| 1411 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1412 | 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] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | msg->eol = ptr; |
| 1415 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1416 | * real header end in case it ends with lots of LWS, but is this |
| 1417 | * really needed ? |
| 1418 | */ |
| 1419 | if (likely(*ptr == '\r')) |
| 1420 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1421 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | http_msg_hdr_l2_lf: |
| 1424 | case HTTP_MSG_HDR_L2_LF: |
| 1425 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1426 | 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] | 1427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | http_msg_hdr_l2_lws: |
| 1429 | case HTTP_MSG_HDR_L2_LWS: |
| 1430 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1431 | /* LWS: replace HT,CR,LF with spaces */ |
| 1432 | for (; msg->eol < ptr; msg->eol++) |
| 1433 | *msg->eol = ' '; |
| 1434 | goto http_msg_hdr_val; |
| 1435 | } |
| 1436 | http_msg_complete_header: |
| 1437 | /* |
| 1438 | * It was a new header, so the last one is finished. |
| 1439 | * Assumes msg->sol points to the first char, msg->col to the |
| 1440 | * colon, msg->sov points to the first character of the value |
| 1441 | * and msg->eol to the first CR or LF so we know how the line |
| 1442 | * ends. We insert last header into the index. |
| 1443 | */ |
| 1444 | /* |
| 1445 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1446 | write(2, msg->sol, msg->eol-msg->sol); |
| 1447 | fprintf(stderr,"\n"); |
| 1448 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1451 | idx, idx->tail) < 0)) |
| 1452 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1453 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | msg->sol = ptr; |
| 1455 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1456 | goto http_msg_hdr_name; |
| 1457 | } |
| 1458 | |
| 1459 | if (likely(*ptr == '\r')) |
| 1460 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1461 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | http_msg_last_lf: |
| 1464 | case HTTP_MSG_LAST_LF: |
| 1465 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1466 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1467 | ptr++; |
| 1468 | buf->lr = ptr; |
| 1469 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1470 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | return; |
| 1472 | #ifdef DEBUG_FULL |
| 1473 | default: |
| 1474 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1475 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1476 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | } |
| 1478 | http_msg_ood: |
| 1479 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1480 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | buf->lr = ptr; |
| 1482 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1483 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1484 | http_msg_invalid: |
| 1485 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1486 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1492 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1493 | * 0 else. |
| 1494 | */ |
| 1495 | int process_cli(struct session *t) |
| 1496 | { |
| 1497 | int s = t->srv_state; |
| 1498 | int c = t->cli_state; |
| 1499 | struct buffer *req = t->req; |
| 1500 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1501 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1503 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1504 | EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | req->rex.tv_sec, req->rex.tv_usec, |
| 1506 | rep->wex.tv_sec, rep->wex.tv_usec); |
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 | if (c == CL_STHEADERS) { |
| 1509 | /* |
| 1510 | * Now parse the partial (or complete) lines. |
| 1511 | * We will check the request syntax, and also join multi-line |
| 1512 | * headers. An index of all the lines will be elaborated while |
| 1513 | * parsing. |
| 1514 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1515 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1518 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | * req->data + req->eoh = end of processed headers / start of current one |
| 1520 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1521 | * req->lr = first non-visited byte |
| 1522 | * req->r = end of data |
| 1523 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1526 | struct http_txn *txn = &t->txn; |
| 1527 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1529 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1531 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1532 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | /* 1: we might have to print this header in debug mode */ |
| 1534 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1535 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1536 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1538 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1539 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | eol = sol + msg->sl.rq.l; |
| 1541 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1543 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1544 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1547 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1549 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1550 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | } |
| 1552 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1554 | |
| 1555 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1557 | * If not so, we check the FD and buffer states before leaving. |
| 1558 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1560 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1561 | * |
| 1562 | */ |
| 1563 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1564 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | /* |
| 1566 | * First, let's catch bad requests. |
| 1567 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1568 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1570 | |
| 1571 | /* 1: Since we are in header mode, if there's no space |
| 1572 | * left for headers, we won't be able to free more |
| 1573 | * later, so the session will never terminate. We |
| 1574 | * must terminate it now. |
| 1575 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1576 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1577 | /* FIXME: check if URI is set and return Status |
| 1578 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1580 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1584 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1585 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1586 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1587 | fd_delete(t->cli_fd); |
| 1588 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1589 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1590 | if (!(t->flags & SN_ERR_MASK)) |
| 1591 | t->flags |= SN_ERR_CLICL; |
| 1592 | if (!(t->flags & SN_FINST_MASK)) |
| 1593 | t->flags |= SN_FINST_R; |
| 1594 | return 1; |
| 1595 | } |
| 1596 | |
| 1597 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1598 | else if (unlikely(tv_isle(&req->rex, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1599 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1600 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1601 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1602 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1603 | if (!(t->flags & SN_ERR_MASK)) |
| 1604 | t->flags |= SN_ERR_CLITO; |
| 1605 | if (!(t->flags & SN_FINST_MASK)) |
| 1606 | t->flags |= SN_FINST_R; |
| 1607 | return 1; |
| 1608 | } |
| 1609 | |
| 1610 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1611 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1612 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1613 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1614 | * req->l == rlim-data |
| 1615 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1616 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1617 | tv_eternity(&req->rex); |
| 1618 | } |
| 1619 | return t->cli_state != CL_STHEADERS; |
| 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | /**************************************************************** |
| 1624 | * More interesting part now : we know that we have a complete * |
| 1625 | * request which at least looks like HTTP. We have an indicator * |
| 1626 | * of each header's length, so we can parse them quickly. * |
| 1627 | ****************************************************************/ |
| 1628 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1629 | /* ensure we keep this pointer to the beginning of the message */ |
| 1630 | msg->sol = req->data + msg->som; |
| 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | /* |
| 1633 | * 1: identify the method |
| 1634 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1635 | 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] | 1636 | |
| 1637 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1639 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1641 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1643 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1644 | !memcmp(&req->data[msg->sl.rq.u], |
| 1645 | t->fe->monitor_uri, |
| 1646 | t->fe->monitor_uri_len))) { |
| 1647 | /* |
| 1648 | * We have found the monitor URI |
| 1649 | */ |
| 1650 | t->flags |= SN_MONITOR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1651 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | client_retnclose(t, &http_200_chunk); |
| 1653 | goto return_prx_cond; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1658 | * Note: we cannot log anymore if the request has been |
| 1659 | * classified as invalid. |
| 1660 | */ |
| 1661 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1662 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1663 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | int urilen = msg->sl.rq.l; |
| 1665 | |
| 1666 | if (urilen >= REQURI_LEN) |
| 1667 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1668 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1669 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | |
| 1671 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1672 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | } else { |
| 1674 | Alert("HTTP logging : out of memory.\n"); |
| 1675 | } |
| 1676 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1680 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1681 | int delta; |
| 1682 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1683 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | cur_end = msg->sol + msg->sl.rq.l; |
| 1685 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | if (msg->sl.rq.u_l == 0) { |
| 1688 | /* if no URI was set, add "/" */ |
| 1689 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1690 | cur_end += delta; |
| 1691 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1692 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | /* add HTTP version */ |
| 1694 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1695 | msg->eoh += delta; |
| 1696 | cur_end += delta; |
| 1697 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1698 | HTTP_MSG_RQMETH, |
| 1699 | msg->sol, cur_end + 1, |
| 1700 | NULL, NULL); |
| 1701 | if (unlikely(!cur_end)) |
| 1702 | goto return_bad_req; |
| 1703 | |
| 1704 | /* we have a full HTTP/1.0 request now and we know that |
| 1705 | * we have either a CR or an LF at <ptr>. |
| 1706 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1707 | 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] | 1708 | } |
| 1709 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | |
| 1711 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1712 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1713 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1714 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1715 | |
| 1716 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1717 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1718 | * As opposed to version 1.2, now they will be evaluated in the |
| 1719 | * filters order and not in the header order. This means that |
| 1720 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1721 | * |
| 1722 | * We can now check whether we want to switch to another |
| 1723 | * backend, in which case we will re-check the backend's |
| 1724 | * filters and various options. In order to support 3-level |
| 1725 | * switching, here's how we should proceed : |
| 1726 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1727 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1728 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1729 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1730 | * There cannot be any switch from there, so ->be cannot be |
| 1731 | * changed anymore. |
| 1732 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1733 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1734 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1735 | * The response path will be able to apply either ->be, or |
| 1736 | * ->be then ->fe filters in order to match the reverse of |
| 1737 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1738 | */ |
| 1739 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1740 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1741 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1742 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1743 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1744 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1745 | /* first check whether we have some ACLs set to block this request */ |
| 1746 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1747 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1748 | if (cond->pol == ACL_COND_UNLESS) |
| 1749 | ret = !ret; |
| 1750 | |
| 1751 | if (ret) { |
| 1752 | txn->status = 403; |
| 1753 | /* let's log the request time */ |
| 1754 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1755 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1756 | goto return_prx_cond; |
| 1757 | } |
| 1758 | } |
| 1759 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1760 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1761 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1763 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1766 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1767 | /* to ensure correct connection accounting on |
| 1768 | * the backend, we count the connection for the |
| 1769 | * one managing the queue. |
| 1770 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1771 | t->be->beconn++; |
| 1772 | if (t->be->beconn > t->be->beconn_max) |
| 1773 | t->be->beconn_max = t->be->beconn; |
| 1774 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1775 | t->flags |= SN_BE_ASSIGNED; |
| 1776 | } |
| 1777 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1778 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1779 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1780 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1781 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1782 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1783 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1784 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1785 | goto return_prx_cond; |
| 1786 | } |
| 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1789 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | !(t->flags & SN_CONN_CLOSED)) { |
| 1791 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1792 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1794 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1795 | 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] | 1796 | old_idx = 0; |
| 1797 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1798 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1799 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1800 | cur_ptr = cur_next; |
| 1801 | cur_end = cur_ptr + cur_hdr->len; |
| 1802 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1803 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1804 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1805 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | /* 3 possibilities : |
| 1807 | * - we have already set Connection: close, |
| 1808 | * so we remove this line. |
| 1809 | * - we have not yet set Connection: close, |
| 1810 | * but this line indicates close. We leave |
| 1811 | * it untouched and set the flag. |
| 1812 | * - we have not yet set Connection: close, |
| 1813 | * and this line indicates non-close. We |
| 1814 | * replace it. |
| 1815 | */ |
| 1816 | if (t->flags & SN_CONN_CLOSED) { |
| 1817 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1818 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1820 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1821 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | cur_hdr->len = 0; |
| 1823 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1824 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1825 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1826 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | cur_next += delta; |
| 1828 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1829 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | } |
| 1831 | t->flags |= SN_CONN_CLOSED; |
| 1832 | } |
| 1833 | } |
| 1834 | old_idx = cur_idx; |
| 1835 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1836 | } |
| 1837 | /* add request headers from the rule sets in the same order */ |
| 1838 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1839 | if (unlikely(http_header_add_tail(req, |
| 1840 | &txn->req, |
| 1841 | &txn->hdr_idx, |
| 1842 | rule_set->req_add[cur_idx])) < 0) |
| 1843 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1844 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1845 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1847 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1848 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1849 | /* we have to check the URI and auth for this request */ |
| 1850 | if (stats_check_uri_auth(t, rule_set)) |
| 1851 | return 1; |
| 1852 | } |
| 1853 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1854 | /* now check whether we have some switching rules for this request */ |
| 1855 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1856 | struct switching_rule *rule; |
| 1857 | |
| 1858 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1859 | int ret; |
| 1860 | |
| 1861 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1862 | if (cond->pol == ACL_COND_UNLESS) |
| 1863 | ret = !ret; |
| 1864 | |
| 1865 | if (ret) { |
| 1866 | t->be = rule->be.backend; |
| 1867 | t->be->beconn++; |
| 1868 | if (t->be->beconn > t->be->beconn_max) |
| 1869 | t->be->beconn_max = t->be->beconn; |
| 1870 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1871 | |
| 1872 | /* assign new parameters to the session from the new backend */ |
| 1873 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1874 | t->req->cto = t->be->contimeout; |
| 1875 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1876 | t->flags |= SN_BE_ASSIGNED; |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1882 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1883 | /* No backend was set, but there was a default |
| 1884 | * backend set in the frontend, so we use it and |
| 1885 | * loop again. |
| 1886 | */ |
| 1887 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1888 | t->be->beconn++; |
| 1889 | if (t->be->beconn > t->be->beconn_max) |
| 1890 | t->be->beconn_max = t->be->beconn; |
| 1891 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1892 | |
| 1893 | /* assign new parameters to the session from the new backend */ |
| 1894 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1895 | t->req->cto = t->be->contimeout; |
| 1896 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1897 | t->flags |= SN_BE_ASSIGNED; |
| 1898 | } |
| 1899 | } 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] | 1900 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1901 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1902 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1903 | /* To ensure correct connection accounting on |
| 1904 | * the backend, we count the connection for the |
| 1905 | * one managing the queue. |
| 1906 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1907 | t->be->beconn++; |
| 1908 | if (t->be->beconn > t->be->beconn_max) |
| 1909 | t->be->beconn_max = t->be->beconn; |
| 1910 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1911 | t->flags |= SN_BE_ASSIGNED; |
| 1912 | } |
| 1913 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1914 | /* |
| 1915 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1916 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1917 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1918 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1919 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1920 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1923 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1924 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1925 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1926 | * so let's do the same now. |
| 1927 | */ |
| 1928 | |
| 1929 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1930 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1931 | 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] | 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1936 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1937 | * Note that doing so might move headers in the request, but |
| 1938 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1939 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1940 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 1941 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 1942 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1943 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1944 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1946 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1947 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1948 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1949 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1950 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1951 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1952 | /* Add an X-Forwarded-For header unless the source IP is |
| 1953 | * in the 'except' network range. |
| 1954 | */ |
| 1955 | if ((!t->fe->except_mask.s_addr || |
| 1956 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1957 | != t->fe->except_net.s_addr) && |
| 1958 | (!t->be->except_mask.s_addr || |
| 1959 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1960 | != t->be->except_net.s_addr)) { |
| 1961 | int len; |
| 1962 | unsigned char *pn; |
| 1963 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1964 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1965 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1966 | pn[0], pn[1], pn[2], pn[3]); |
| 1967 | |
| 1968 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1969 | &txn->hdr_idx, trash, len)) < 0) |
| 1970 | goto return_bad_req; |
| 1971 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1972 | } |
| 1973 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1974 | /* FIXME: for the sake of completeness, we should also support |
| 1975 | * 'except' here, although it is mostly useless in this case. |
| 1976 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1977 | int len; |
| 1978 | char pn[INET6_ADDRSTRLEN]; |
| 1979 | inet_ntop(AF_INET6, |
| 1980 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1981 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1982 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 1983 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1984 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1985 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1986 | } |
| 1987 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1988 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1989 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1990 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1991 | * 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] | 1992 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1993 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1994 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1995 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 1996 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 1997 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1998 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1999 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2000 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2002 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2003 | /************************************************************* |
| 2004 | * OK, that's finished for the headers. We have done what we * |
| 2005 | * could. Let's switch to the DATA state. * |
| 2006 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2007 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2008 | t->cli_state = CL_STDATA; |
| 2009 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2011 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2012 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2013 | if (!tv_isset(&t->fe->clitimeout) || |
| 2014 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2015 | /* If the client has no timeout, or if the server is not ready yet, |
| 2016 | * and we know for sure that it can expire, then it's cleaner to |
| 2017 | * disable the timeout on the client side so that too low values |
| 2018 | * cannot make the sessions abort too early. |
| 2019 | * |
| 2020 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2021 | * when it switches its state, otherwise a client can stay connected |
| 2022 | * indefinitely. This now seems to be OK. |
| 2023 | */ |
| 2024 | tv_eternity(&req->rex); |
| 2025 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2027 | /* When a connection is tarpitted, we use the queue timeout for the |
| 2028 | * tarpit delay, which currently happens to be the server's connect |
| 2029 | * timeout. If unset, then set it to zero because we really want it |
| 2030 | * to expire at one moment. |
| 2031 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2032 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2033 | t->req->l = 0; |
| 2034 | /* flush the request so that we can drop the connection early |
| 2035 | * if the client closes first. |
| 2036 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2037 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2038 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2039 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2040 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2041 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2042 | goto process_data; |
| 2043 | |
| 2044 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2045 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2046 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2047 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2048 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2049 | return_prx_cond: |
| 2050 | if (!(t->flags & SN_ERR_MASK)) |
| 2051 | t->flags |= SN_ERR_PRXCOND; |
| 2052 | if (!(t->flags & SN_FINST_MASK)) |
| 2053 | t->flags |= SN_FINST_R; |
| 2054 | return 1; |
| 2055 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2056 | } |
| 2057 | else if (c == CL_STDATA) { |
| 2058 | process_data: |
| 2059 | /* FIXME: this error handling is partly buggy because we always report |
| 2060 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2061 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2062 | * we're waiting for the server to connect. |
| 2063 | */ |
| 2064 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2065 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2066 | buffer_shutr(req); |
| 2067 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2068 | fd_delete(t->cli_fd); |
| 2069 | t->cli_state = CL_STCLOSE; |
| 2070 | if (!(t->flags & SN_ERR_MASK)) |
| 2071 | t->flags |= SN_ERR_CLICL; |
| 2072 | if (!(t->flags & SN_FINST_MASK)) { |
| 2073 | if (t->pend_pos) |
| 2074 | t->flags |= SN_FINST_Q; |
| 2075 | else if (s == SV_STCONN) |
| 2076 | t->flags |= SN_FINST_C; |
| 2077 | else |
| 2078 | t->flags |= SN_FINST_D; |
| 2079 | } |
| 2080 | return 1; |
| 2081 | } |
| 2082 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2083 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2084 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2085 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2086 | t->cli_state = CL_STSHUTR; |
| 2087 | return 1; |
| 2088 | } |
| 2089 | /* last server read and buffer empty */ |
| 2090 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2091 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2092 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2093 | shutdown(t->cli_fd, SHUT_WR); |
| 2094 | /* We must ensure that the read part is still alive when switching |
| 2095 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2096 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2097 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2098 | t->cli_state = CL_STSHUTW; |
| 2099 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2100 | return 1; |
| 2101 | } |
| 2102 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2103 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2104 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2105 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2106 | t->cli_state = CL_STSHUTR; |
| 2107 | if (!(t->flags & SN_ERR_MASK)) |
| 2108 | t->flags |= SN_ERR_CLITO; |
| 2109 | if (!(t->flags & SN_FINST_MASK)) { |
| 2110 | if (t->pend_pos) |
| 2111 | t->flags |= SN_FINST_Q; |
| 2112 | else if (s == SV_STCONN) |
| 2113 | t->flags |= SN_FINST_C; |
| 2114 | else |
| 2115 | t->flags |= SN_FINST_D; |
| 2116 | } |
| 2117 | return 1; |
| 2118 | } |
| 2119 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2120 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2121 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2122 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2123 | shutdown(t->cli_fd, SHUT_WR); |
| 2124 | /* We must ensure that the read part is still alive when switching |
| 2125 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2126 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2127 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2128 | |
| 2129 | t->cli_state = CL_STSHUTW; |
| 2130 | if (!(t->flags & SN_ERR_MASK)) |
| 2131 | t->flags |= SN_ERR_CLITO; |
| 2132 | if (!(t->flags & SN_FINST_MASK)) { |
| 2133 | if (t->pend_pos) |
| 2134 | t->flags |= SN_FINST_Q; |
| 2135 | else if (s == SV_STCONN) |
| 2136 | t->flags |= SN_FINST_C; |
| 2137 | else |
| 2138 | t->flags |= SN_FINST_D; |
| 2139 | } |
| 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | if (req->l >= req->rlim - req->data) { |
| 2144 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2145 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2146 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2147 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2148 | } |
| 2149 | } else { |
| 2150 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2151 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2152 | if (!tv_isset(&t->fe->clitimeout) || |
| 2153 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2154 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2155 | * know for sure that it can expire, then it's cleaner to disable the |
| 2156 | * timeout on the client side so that too low values cannot make the |
| 2157 | * sessions abort too early. |
| 2158 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2159 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2160 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2161 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | if ((rep->l == 0) || |
| 2166 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2167 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2168 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2169 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2170 | } |
| 2171 | } else { |
| 2172 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2173 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2174 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2175 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2176 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2177 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2178 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2179 | } |
| 2180 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2181 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | return 0; /* other cases change nothing */ |
| 2185 | } |
| 2186 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2187 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2188 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2189 | fd_delete(t->cli_fd); |
| 2190 | t->cli_state = CL_STCLOSE; |
| 2191 | if (!(t->flags & SN_ERR_MASK)) |
| 2192 | t->flags |= SN_ERR_CLICL; |
| 2193 | if (!(t->flags & SN_FINST_MASK)) { |
| 2194 | if (t->pend_pos) |
| 2195 | t->flags |= SN_FINST_Q; |
| 2196 | else if (s == SV_STCONN) |
| 2197 | t->flags |= SN_FINST_C; |
| 2198 | else |
| 2199 | t->flags |= SN_FINST_D; |
| 2200 | } |
| 2201 | return 1; |
| 2202 | } |
| 2203 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2204 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2205 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2206 | fd_delete(t->cli_fd); |
| 2207 | t->cli_state = CL_STCLOSE; |
| 2208 | return 1; |
| 2209 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2210 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2211 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2212 | fd_delete(t->cli_fd); |
| 2213 | t->cli_state = CL_STCLOSE; |
| 2214 | if (!(t->flags & SN_ERR_MASK)) |
| 2215 | t->flags |= SN_ERR_CLITO; |
| 2216 | if (!(t->flags & SN_FINST_MASK)) { |
| 2217 | if (t->pend_pos) |
| 2218 | t->flags |= SN_FINST_Q; |
| 2219 | else if (s == SV_STCONN) |
| 2220 | t->flags |= SN_FINST_C; |
| 2221 | else |
| 2222 | t->flags |= SN_FINST_D; |
| 2223 | } |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
| 2227 | if (t->flags & SN_SELF_GEN) { |
| 2228 | produce_content(t); |
| 2229 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2230 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2231 | fd_delete(t->cli_fd); |
| 2232 | t->cli_state = CL_STCLOSE; |
| 2233 | return 1; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | if ((rep->l == 0) |
| 2238 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2239 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2240 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2241 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2242 | } |
| 2243 | } else { |
| 2244 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2245 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2246 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 2247 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2248 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2254 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2255 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2256 | fd_delete(t->cli_fd); |
| 2257 | t->cli_state = CL_STCLOSE; |
| 2258 | if (!(t->flags & SN_ERR_MASK)) |
| 2259 | t->flags |= SN_ERR_CLICL; |
| 2260 | if (!(t->flags & SN_FINST_MASK)) { |
| 2261 | if (t->pend_pos) |
| 2262 | t->flags |= SN_FINST_Q; |
| 2263 | else if (s == SV_STCONN) |
| 2264 | t->flags |= SN_FINST_C; |
| 2265 | else |
| 2266 | t->flags |= SN_FINST_D; |
| 2267 | } |
| 2268 | return 1; |
| 2269 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2270 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2271 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2272 | fd_delete(t->cli_fd); |
| 2273 | t->cli_state = CL_STCLOSE; |
| 2274 | return 1; |
| 2275 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2276 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2277 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2278 | fd_delete(t->cli_fd); |
| 2279 | t->cli_state = CL_STCLOSE; |
| 2280 | if (!(t->flags & SN_ERR_MASK)) |
| 2281 | t->flags |= SN_ERR_CLITO; |
| 2282 | if (!(t->flags & SN_FINST_MASK)) { |
| 2283 | if (t->pend_pos) |
| 2284 | t->flags |= SN_FINST_Q; |
| 2285 | else if (s == SV_STCONN) |
| 2286 | t->flags |= SN_FINST_C; |
| 2287 | else |
| 2288 | t->flags |= SN_FINST_D; |
| 2289 | } |
| 2290 | return 1; |
| 2291 | } |
| 2292 | else if (req->l >= req->rlim - req->data) { |
| 2293 | /* no room to read more data */ |
| 2294 | |
| 2295 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2296 | * after the timeout by sending more data after it receives a close ? |
| 2297 | */ |
| 2298 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2299 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2300 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2301 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2302 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2303 | } |
| 2304 | } else { |
| 2305 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2306 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2307 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2308 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2309 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2310 | } |
| 2311 | } |
| 2312 | return 0; |
| 2313 | } |
| 2314 | else { /* CL_STCLOSE: nothing to do */ |
| 2315 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2316 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2317 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2318 | write(1, trash, len); |
| 2319 | } |
| 2320 | return 0; |
| 2321 | } |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | /* |
| 2327 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2328 | * (and a resync may be needed), 0 else. |
| 2329 | */ |
| 2330 | int process_srv(struct session *t) |
| 2331 | { |
| 2332 | int s = t->srv_state; |
| 2333 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2334 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2335 | struct buffer *req = t->req; |
| 2336 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2337 | int conn_err; |
| 2338 | |
| 2339 | #ifdef DEBUG_FULL |
| 2340 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2341 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2342 | |
| 2343 | #if 0 |
| 2344 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2345 | __FUNCTION__, __LINE__, |
| 2346 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2347 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2348 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2349 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2350 | __FUNCTION__, __LINE__, |
| 2351 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2352 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2353 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2354 | |
| 2355 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2356 | __FUNCTION__, __LINE__, |
| 2357 | req->cto.tv_sec, req->cto.tv_usec, |
| 2358 | req->rto.tv_sec, req->rto.tv_usec, |
| 2359 | req->wto.tv_sec, req->wto.tv_usec); |
| 2360 | |
| 2361 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2362 | __FUNCTION__, __LINE__, |
| 2363 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2364 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2365 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2366 | #endif |
| 2367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2368 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2369 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2370 | //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2371 | //); |
| 2372 | if (s == SV_STIDLE) { |
| 2373 | if (c == CL_STHEADERS) |
| 2374 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2375 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2376 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2377 | (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2378 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2379 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2380 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2381 | /* note that this must not return any error because it would be able to |
| 2382 | * overwrite the client_retnclose() output. |
| 2383 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2384 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2385 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
Willy Tarreau | 08fa2e3 | 2006-09-03 10:47:37 +0200 | [diff] [blame] | 2386 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2387 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2388 | |
| 2389 | return 1; |
| 2390 | } |
| 2391 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2392 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2393 | /* This connection is being tarpitted. The CLIENT side has |
| 2394 | * already set the connect expiration date to the right |
| 2395 | * timeout. We just have to check that it has not expired. |
| 2396 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2397 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2398 | return 0; |
| 2399 | |
| 2400 | /* We will set the queue timer to the time spent, just for |
| 2401 | * logging purposes. We fake a 500 server error, so that the |
| 2402 | * attacker will not suspect his connection has been tarpitted. |
| 2403 | * It will not cause trouble to the logs because we can exclude |
| 2404 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2405 | */ |
| 2406 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2407 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2408 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2409 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2410 | return 1; |
| 2411 | } |
| 2412 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2413 | /* Right now, we will need to create a connection to the server. |
| 2414 | * We might already have tried, and got a connection pending, in |
| 2415 | * which case we will not do anything till it's pending. It's up |
| 2416 | * to any other session to release it and wake us up again. |
| 2417 | */ |
| 2418 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2419 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2420 | return 0; |
| 2421 | else { |
| 2422 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2423 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2424 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2425 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2426 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2427 | if (t->srv) |
| 2428 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2429 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2430 | return 1; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | do { |
| 2435 | /* first, get a connection */ |
| 2436 | if (srv_redispatch_connect(t)) |
| 2437 | return t->srv_state != SV_STIDLE; |
| 2438 | |
| 2439 | /* try to (re-)connect to the server, and fail if we expire the |
| 2440 | * number of retries. |
| 2441 | */ |
| 2442 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2443 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2444 | return t->srv_state != SV_STIDLE; |
| 2445 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2446 | } while (1); |
| 2447 | } |
| 2448 | } |
| 2449 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2450 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2451 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2452 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2453 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2454 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2455 | fd_delete(t->srv_fd); |
| 2456 | if (t->srv) |
| 2457 | t->srv->cur_sess--; |
| 2458 | |
| 2459 | /* note that this must not return any error because it would be able to |
| 2460 | * overwrite the client_retnclose() output. |
| 2461 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2462 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2463 | return 1; |
| 2464 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2465 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2466 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2467 | return 0; /* nothing changed */ |
| 2468 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2469 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2470 | /* timeout, asynchronous connect error or first write error */ |
| 2471 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2472 | |
| 2473 | fd_delete(t->srv_fd); |
| 2474 | if (t->srv) |
| 2475 | t->srv->cur_sess--; |
| 2476 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2477 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2478 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2479 | else |
| 2480 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2481 | |
| 2482 | /* ensure that we have enough retries left */ |
| 2483 | if (srv_count_retry_down(t, conn_err)) |
| 2484 | return 1; |
| 2485 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2486 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2487 | /* We're on our last chance, and the REDISP option was specified. |
| 2488 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2489 | */ |
| 2490 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2491 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2492 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2493 | |
| 2494 | if (t->srv) |
| 2495 | t->srv->failed_conns++; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 2496 | t->be->redispatches++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2497 | |
| 2498 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2499 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | a5e6575 | 2007-03-18 20:53:22 +0100 | [diff] [blame] | 2500 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2501 | |
| 2502 | /* first, get a connection */ |
| 2503 | if (srv_redispatch_connect(t)) |
| 2504 | return t->srv_state != SV_STIDLE; |
| 2505 | } |
| 2506 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2507 | do { |
| 2508 | /* Now we will try to either reconnect to the same server or |
| 2509 | * connect to another server. If the connection gets queued |
| 2510 | * because all servers are saturated, then we will go back to |
| 2511 | * the SV_STIDLE state. |
| 2512 | */ |
| 2513 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2514 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2515 | return t->srv_state != SV_STCONN; |
| 2516 | } |
| 2517 | |
| 2518 | /* we need to redispatch the connection to another server */ |
| 2519 | if (srv_redispatch_connect(t)) |
| 2520 | return t->srv_state != SV_STCONN; |
| 2521 | } while (1); |
| 2522 | } |
| 2523 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2524 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2525 | |
| 2526 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2527 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2528 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2529 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2530 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2531 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2532 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2533 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2534 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2535 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2536 | } |
| 2537 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2538 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2541 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2542 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2543 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2544 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2545 | |
| 2546 | t->srv_state = SV_STDATA; |
| 2547 | if (t->srv) |
| 2548 | t->srv->cum_sess++; |
| 2549 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2550 | |
| 2551 | /* if the user wants to log as soon as possible, without counting |
| 2552 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2553 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2554 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2555 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2556 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2557 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2558 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2559 | /* TCP splicing supported by both FE and BE */ |
| 2560 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2561 | } |
| 2562 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2563 | } |
| 2564 | else { |
| 2565 | t->srv_state = SV_STHEADERS; |
| 2566 | if (t->srv) |
| 2567 | t->srv->cum_sess++; |
| 2568 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2569 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2570 | /* reset hdr_idx which was already initialized by the request. |
| 2571 | * right now, the http parser does it. |
| 2572 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2573 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2574 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2575 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2576 | return 1; |
| 2577 | } |
| 2578 | } |
| 2579 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2580 | /* |
| 2581 | * Now parse the partial (or complete) lines. |
| 2582 | * We will check the response syntax, and also join multi-line |
| 2583 | * headers. An index of all the lines will be elaborated while |
| 2584 | * parsing. |
| 2585 | * |
| 2586 | * For the parsing, we use a 28 states FSM. |
| 2587 | * |
| 2588 | * Here is the information we currently have : |
| 2589 | * rep->data + req->som = beginning of response |
| 2590 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2591 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2592 | * rep->lr = first non-visited byte |
| 2593 | * rep->r = end of data |
| 2594 | */ |
| 2595 | |
| 2596 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2597 | struct http_msg *msg = &txn->rsp; |
| 2598 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2599 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2600 | if (likely(rep->lr < rep->r)) |
| 2601 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2602 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2603 | /* 1: we might have to print this header in debug mode */ |
| 2604 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2605 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2606 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2607 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2609 | sol = rep->data + msg->som; |
| 2610 | eol = sol + msg->sl.rq.l; |
| 2611 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2612 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2613 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2614 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2615 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2616 | while (cur_idx) { |
| 2617 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2618 | debug_hdr("srvhdr", t, sol, eol); |
| 2619 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2620 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2621 | } |
| 2622 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2624 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2625 | if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2626 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2627 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2628 | * rep->l == rlim-data |
| 2629 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2630 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2631 | tv_eternity(&rep->rex); |
| 2632 | } |
| 2633 | |
| 2634 | |
| 2635 | /* |
| 2636 | * Now we quickly check if we have found a full valid response. |
| 2637 | * If not so, we check the FD and buffer states before leaving. |
| 2638 | * A full response is indicated by the fact that we have seen |
| 2639 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2640 | * responses are checked first. |
| 2641 | * |
| 2642 | * Depending on whether the client is still there or not, we |
| 2643 | * may send an error response back or not. Note that normally |
| 2644 | * we should only check for HTTP status there, and check I/O |
| 2645 | * errors somewhere else. |
| 2646 | */ |
| 2647 | |
| 2648 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2649 | |
| 2650 | /* Invalid response, or read error or write error */ |
| 2651 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2652 | (req->flags & BF_WRITE_ERROR) || |
| 2653 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2654 | buffer_shutr(rep); |
| 2655 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2656 | fd_delete(t->srv_fd); |
| 2657 | if (t->srv) { |
| 2658 | t->srv->cur_sess--; |
| 2659 | t->srv->failed_resp++; |
| 2660 | } |
| 2661 | t->be->failed_resp++; |
| 2662 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2663 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2664 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2665 | if (!(t->flags & SN_ERR_MASK)) |
| 2666 | t->flags |= SN_ERR_SRVCL; |
| 2667 | if (!(t->flags & SN_FINST_MASK)) |
| 2668 | t->flags |= SN_FINST_H; |
| 2669 | /* We used to have a free connection slot. Since we'll never use it, |
| 2670 | * we have to inform the server that it may be used by another session. |
| 2671 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2672 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2673 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2674 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2675 | return 1; |
| 2676 | } |
| 2677 | |
| 2678 | /* end of client write or end of server read. |
| 2679 | * since we are in header mode, if there's no space left for headers, we |
| 2680 | * won't be able to free more later, so the session will never terminate. |
| 2681 | */ |
| 2682 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2683 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2684 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2685 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2686 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2687 | t->srv_state = SV_STSHUTR; |
| 2688 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | /* read timeout : return a 504 to the client. |
| 2693 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2694 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2695 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2696 | buffer_shutr(rep); |
| 2697 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2698 | fd_delete(t->srv_fd); |
| 2699 | if (t->srv) { |
| 2700 | t->srv->cur_sess--; |
| 2701 | t->srv->failed_resp++; |
| 2702 | } |
| 2703 | t->be->failed_resp++; |
| 2704 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2705 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2706 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2707 | if (!(t->flags & SN_ERR_MASK)) |
| 2708 | t->flags |= SN_ERR_SRVTO; |
| 2709 | if (!(t->flags & SN_FINST_MASK)) |
| 2710 | t->flags |= SN_FINST_H; |
| 2711 | /* We used to have a free connection slot. Since we'll never use it, |
| 2712 | * we have to inform the server that it may be used by another session. |
| 2713 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2714 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2715 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2716 | return 1; |
| 2717 | } |
| 2718 | |
| 2719 | /* last client read and buffer empty */ |
| 2720 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2721 | * client shuts read too early, because we may still have |
| 2722 | * some work to do on the headers. |
| 2723 | * The side-effect is that if the client completely closes its |
| 2724 | * connection during SV_STHEADER, the connection to the server |
| 2725 | * is kept until a response comes back or the timeout is reached. |
| 2726 | */ |
| 2727 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2728 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2729 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2730 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2731 | |
| 2732 | /* We must ensure that the read part is still |
| 2733 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2734 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2735 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2736 | |
| 2737 | shutdown(t->srv_fd, SHUT_WR); |
| 2738 | t->srv_state = SV_STSHUTW; |
| 2739 | return 1; |
| 2740 | } |
| 2741 | |
| 2742 | /* write timeout */ |
| 2743 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2744 | * client shuts read too early, because we may still have |
| 2745 | * some work to do on the headers. |
| 2746 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2747 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2748 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2749 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2750 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2751 | shutdown(t->srv_fd, SHUT_WR); |
| 2752 | /* We must ensure that the read part is still alive |
| 2753 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2754 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2755 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2756 | |
| 2757 | t->srv_state = SV_STSHUTW; |
| 2758 | if (!(t->flags & SN_ERR_MASK)) |
| 2759 | t->flags |= SN_ERR_SRVTO; |
| 2760 | if (!(t->flags & SN_FINST_MASK)) |
| 2761 | t->flags |= SN_FINST_H; |
| 2762 | return 1; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * And now the non-error cases. |
| 2767 | */ |
| 2768 | |
| 2769 | /* Data remaining in the request buffer. |
| 2770 | * This happens during the first pass here, and during |
| 2771 | * long posts. |
| 2772 | */ |
| 2773 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2774 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2775 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2776 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2777 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2778 | * we refresh it. */ |
| 2779 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2780 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2781 | else |
| 2782 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2784 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2786 | /* nothing left in the request buffer */ |
| 2787 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2788 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2789 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2790 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | return t->srv_state != SV_STHEADERS; |
| 2795 | } |
| 2796 | |
| 2797 | |
| 2798 | /***************************************************************** |
| 2799 | * More interesting part now : we know that we have a complete * |
| 2800 | * response which at least looks like HTTP. We have an indicator * |
| 2801 | * of each header's length, so we can parse them quickly. * |
| 2802 | ****************************************************************/ |
| 2803 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2804 | /* ensure we keep this pointer to the beginning of the message */ |
| 2805 | msg->sol = rep->data + msg->som; |
| 2806 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2807 | /* |
| 2808 | * 1: get the status code and check for cacheability. |
| 2809 | */ |
| 2810 | |
| 2811 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2812 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2813 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2814 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2815 | case 200: |
| 2816 | case 203: |
| 2817 | case 206: |
| 2818 | case 300: |
| 2819 | case 301: |
| 2820 | case 410: |
| 2821 | /* RFC2616 @13.4: |
| 2822 | * "A response received with a status code of |
| 2823 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2824 | * by a cache (...) unless a cache-control |
| 2825 | * directive prohibits caching." |
| 2826 | * |
| 2827 | * RFC2616 @9.5: POST method : |
| 2828 | * "Responses to this method are not cacheable, |
| 2829 | * unless the response includes appropriate |
| 2830 | * Cache-Control or Expires header fields." |
| 2831 | */ |
| 2832 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2833 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2834 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2835 | break; |
| 2836 | default: |
| 2837 | break; |
| 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * 2: we may need to capture headers |
| 2842 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2843 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2844 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2845 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* |
| 2848 | * 3: we will have to evaluate the filters. |
| 2849 | * As opposed to version 1.2, now they will be evaluated in the |
| 2850 | * filters order and not in the header order. This means that |
| 2851 | * each filter has to be validated among all headers. |
| 2852 | * |
| 2853 | * Filters are tried with ->be first, then with ->fe if it is |
| 2854 | * different from ->be. |
| 2855 | */ |
| 2856 | |
| 2857 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2858 | |
| 2859 | cur_proxy = t->be; |
| 2860 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2861 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2862 | |
| 2863 | /* try headers filters */ |
| 2864 | if (rule_set->rsp_exp != NULL) { |
| 2865 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2866 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2867 | if (t->srv) { |
| 2868 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2869 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2870 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2871 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2872 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2873 | buffer_shutr(rep); |
| 2874 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2875 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2876 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2877 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2878 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2879 | if (!(t->flags & SN_ERR_MASK)) |
| 2880 | t->flags |= SN_ERR_PRXCOND; |
| 2881 | if (!(t->flags & SN_FINST_MASK)) |
| 2882 | t->flags |= SN_FINST_H; |
| 2883 | /* We used to have a free connection slot. Since we'll never use it, |
| 2884 | * we have to inform the server that it may be used by another session. |
| 2885 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2886 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2887 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2888 | return 1; |
| 2889 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2890 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2891 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2892 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2893 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2894 | if (t->srv) { |
| 2895 | t->srv->cur_sess--; |
| 2896 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2897 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2898 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2899 | goto return_srv_prx_502; |
| 2900 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2901 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2902 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2903 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2904 | !(t->flags & SN_CONN_CLOSED)) { |
| 2905 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2906 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2907 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2908 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2909 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2910 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2912 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2913 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2914 | cur_ptr = cur_next; |
| 2915 | cur_end = cur_ptr + cur_hdr->len; |
| 2916 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2918 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2919 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2920 | /* 3 possibilities : |
| 2921 | * - we have already set Connection: close, |
| 2922 | * so we remove this line. |
| 2923 | * - we have not yet set Connection: close, |
| 2924 | * but this line indicates close. We leave |
| 2925 | * it untouched and set the flag. |
| 2926 | * - we have not yet set Connection: close, |
| 2927 | * and this line indicates non-close. We |
| 2928 | * replace it. |
| 2929 | */ |
| 2930 | if (t->flags & SN_CONN_CLOSED) { |
| 2931 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2932 | txn->rsp.eoh += delta; |
| 2933 | cur_next += delta; |
| 2934 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2935 | txn->hdr_idx.used--; |
| 2936 | cur_hdr->len = 0; |
| 2937 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2938 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2939 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2940 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2941 | cur_next += delta; |
| 2942 | cur_hdr->len += delta; |
| 2943 | txn->rsp.eoh += delta; |
| 2944 | } |
| 2945 | t->flags |= SN_CONN_CLOSED; |
| 2946 | } |
| 2947 | } |
| 2948 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2949 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2950 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2952 | /* add response headers from the rule sets in the same order */ |
| 2953 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2954 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2955 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2956 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2959 | /* check whether we're already working on the frontend */ |
| 2960 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2961 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2962 | cur_proxy = t->fe; |
| 2963 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2965 | /* |
| 2966 | * 4: check for server cookie. |
| 2967 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2968 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 2969 | || (t->be->options & PR_O_CHK_CACHE)) |
| 2970 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2971 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2973 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2974 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2975 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2976 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 2977 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2978 | |
| 2979 | /* |
| 2980 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2981 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2982 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2983 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2984 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2985 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2986 | /* the server is known, it's not the one the client requested, we have to |
| 2987 | * insert a set-cookie here, except if we want to insert only on POST |
| 2988 | * requests and this one isn't. Note that servers which don't have cookies |
| 2989 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2990 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2991 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2992 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2993 | 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] | 2994 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2995 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2996 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2997 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2998 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2999 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3000 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3001 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3002 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3003 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3004 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3005 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3006 | |
| 3007 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3008 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3009 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3010 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3011 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3012 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | |
| 3016 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3017 | * 7: check if result will be cacheable with a cookie. |
| 3018 | * We'll block the response if security checks have caught |
| 3019 | * nasty things such as a cacheable cookie. |
| 3020 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3021 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3022 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3023 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3024 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3025 | /* we're in presence of a cacheable response containing |
| 3026 | * a set-cookie header. We'll block it as requested by |
| 3027 | * the 'checkcache' option, and send an alert. |
| 3028 | */ |
| 3029 | if (t->srv) { |
| 3030 | t->srv->cur_sess--; |
| 3031 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3032 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3033 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3034 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3035 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3036 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3037 | send_log(t->be, LOG_ALERT, |
| 3038 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3039 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3040 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3043 | /* |
| 3044 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3045 | * 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] | 3046 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3047 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3048 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3049 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3050 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3051 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3052 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3053 | goto return_bad_resp; |
| 3054 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3055 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3057 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3058 | /************************************************************* |
| 3059 | * OK, that's finished for the headers. We have done what we * |
| 3060 | * could. Let's switch to the DATA state. * |
| 3061 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3062 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3063 | t->srv_state = SV_STDATA; |
| 3064 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3065 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3066 | |
| 3067 | /* client connection already closed or option 'forceclose' required : |
| 3068 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3069 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3070 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3071 | (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3072 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3073 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3074 | |
| 3075 | /* We must ensure that the read part is still alive when switching |
| 3076 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3077 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3078 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3079 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3080 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3081 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3084 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3085 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3086 | /* TCP splicing supported by both FE and BE */ |
| 3087 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3088 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3089 | #endif |
| 3090 | /* if the user wants to log as soon as possible, without counting |
| 3091 | bytes from the server, then this is the right moment. */ |
| 3092 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3093 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | b498717 | 2007-03-18 16:28:03 +0100 | [diff] [blame] | 3094 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3095 | if (t->fe->to_log & LW_REQ) |
| 3096 | http_sess_log(t); |
| 3097 | else |
| 3098 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3099 | } |
| 3100 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3101 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3102 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3103 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3104 | |
| 3105 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3106 | } |
| 3107 | else if (s == SV_STDATA) { |
| 3108 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3109 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3110 | buffer_shutr(rep); |
| 3111 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3112 | fd_delete(t->srv_fd); |
| 3113 | if (t->srv) { |
| 3114 | t->srv->cur_sess--; |
| 3115 | t->srv->failed_resp++; |
| 3116 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3117 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3118 | t->srv_state = SV_STCLOSE; |
| 3119 | if (!(t->flags & SN_ERR_MASK)) |
| 3120 | t->flags |= SN_ERR_SRVCL; |
| 3121 | if (!(t->flags & SN_FINST_MASK)) |
| 3122 | t->flags |= SN_FINST_D; |
| 3123 | /* We used to have a free connection slot. Since we'll never use it, |
| 3124 | * we have to inform the server that it may be used by another session. |
| 3125 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3126 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3127 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3128 | |
| 3129 | return 1; |
| 3130 | } |
| 3131 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3132 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3133 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3134 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3135 | t->srv_state = SV_STSHUTR; |
| 3136 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3137 | return 1; |
| 3138 | } |
| 3139 | /* end of client read and no more data to send */ |
| 3140 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3141 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3142 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3143 | shutdown(t->srv_fd, SHUT_WR); |
| 3144 | /* We must ensure that the read part is still alive when switching |
| 3145 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3146 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3147 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3148 | |
| 3149 | t->srv_state = SV_STSHUTW; |
| 3150 | return 1; |
| 3151 | } |
| 3152 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3153 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3154 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3155 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3156 | t->srv_state = SV_STSHUTR; |
| 3157 | if (!(t->flags & SN_ERR_MASK)) |
| 3158 | t->flags |= SN_ERR_SRVTO; |
| 3159 | if (!(t->flags & SN_FINST_MASK)) |
| 3160 | t->flags |= SN_FINST_D; |
| 3161 | return 1; |
| 3162 | } |
| 3163 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3164 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3165 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3166 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3167 | shutdown(t->srv_fd, SHUT_WR); |
| 3168 | /* We must ensure that the read part is still alive when switching |
| 3169 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3170 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3171 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3172 | t->srv_state = SV_STSHUTW; |
| 3173 | if (!(t->flags & SN_ERR_MASK)) |
| 3174 | t->flags |= SN_ERR_SRVTO; |
| 3175 | if (!(t->flags & SN_FINST_MASK)) |
| 3176 | t->flags |= SN_FINST_D; |
| 3177 | return 1; |
| 3178 | } |
| 3179 | |
| 3180 | /* recompute request time-outs */ |
| 3181 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3182 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3183 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3184 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3188 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3189 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3190 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3191 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3192 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3193 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3194 | } |
| 3195 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3196 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | /* recompute response time-outs */ |
| 3201 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3202 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3203 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3204 | } |
| 3205 | } |
| 3206 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3207 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3208 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3209 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | return 0; /* other cases change nothing */ |
| 3214 | } |
| 3215 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3216 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3217 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3218 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3219 | fd_delete(t->srv_fd); |
| 3220 | if (t->srv) { |
| 3221 | t->srv->cur_sess--; |
| 3222 | t->srv->failed_resp++; |
| 3223 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3224 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3225 | //close(t->srv_fd); |
| 3226 | t->srv_state = SV_STCLOSE; |
| 3227 | if (!(t->flags & SN_ERR_MASK)) |
| 3228 | t->flags |= SN_ERR_SRVCL; |
| 3229 | if (!(t->flags & SN_FINST_MASK)) |
| 3230 | t->flags |= SN_FINST_D; |
| 3231 | /* We used to have a free connection slot. Since we'll never use it, |
| 3232 | * we have to inform the server that it may be used by another session. |
| 3233 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3234 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3235 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3236 | |
| 3237 | return 1; |
| 3238 | } |
| 3239 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3240 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3241 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3242 | fd_delete(t->srv_fd); |
| 3243 | if (t->srv) |
| 3244 | t->srv->cur_sess--; |
| 3245 | //close(t->srv_fd); |
| 3246 | t->srv_state = SV_STCLOSE; |
| 3247 | /* We used to have a free connection slot. Since we'll never use it, |
| 3248 | * we have to inform the server that it may be used by another session. |
| 3249 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3250 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3251 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3252 | |
| 3253 | return 1; |
| 3254 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3255 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3256 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3257 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3258 | fd_delete(t->srv_fd); |
| 3259 | if (t->srv) |
| 3260 | t->srv->cur_sess--; |
| 3261 | //close(t->srv_fd); |
| 3262 | t->srv_state = SV_STCLOSE; |
| 3263 | if (!(t->flags & SN_ERR_MASK)) |
| 3264 | t->flags |= SN_ERR_SRVTO; |
| 3265 | if (!(t->flags & SN_FINST_MASK)) |
| 3266 | t->flags |= SN_FINST_D; |
| 3267 | /* We used to have a free connection slot. Since we'll never use it, |
| 3268 | * we have to inform the server that it may be used by another session. |
| 3269 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3270 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3271 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3272 | |
| 3273 | return 1; |
| 3274 | } |
| 3275 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3276 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3277 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3278 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3279 | } |
| 3280 | } |
| 3281 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3282 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3283 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 3284 | if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3285 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | return 0; |
| 3289 | } |
| 3290 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3291 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3292 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3293 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3294 | fd_delete(t->srv_fd); |
| 3295 | if (t->srv) { |
| 3296 | t->srv->cur_sess--; |
| 3297 | t->srv->failed_resp++; |
| 3298 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3299 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3300 | //close(t->srv_fd); |
| 3301 | t->srv_state = SV_STCLOSE; |
| 3302 | if (!(t->flags & SN_ERR_MASK)) |
| 3303 | t->flags |= SN_ERR_SRVCL; |
| 3304 | if (!(t->flags & SN_FINST_MASK)) |
| 3305 | t->flags |= SN_FINST_D; |
| 3306 | /* We used to have a free connection slot. Since we'll never use it, |
| 3307 | * we have to inform the server that it may be used by another session. |
| 3308 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3309 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3310 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3311 | |
| 3312 | return 1; |
| 3313 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3314 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3315 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3316 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3317 | fd_delete(t->srv_fd); |
| 3318 | if (t->srv) |
| 3319 | t->srv->cur_sess--; |
| 3320 | //close(t->srv_fd); |
| 3321 | t->srv_state = SV_STCLOSE; |
| 3322 | /* We used to have a free connection slot. Since we'll never use it, |
| 3323 | * we have to inform the server that it may be used by another session. |
| 3324 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3325 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3326 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3327 | |
| 3328 | return 1; |
| 3329 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3330 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3331 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3332 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3333 | fd_delete(t->srv_fd); |
| 3334 | if (t->srv) |
| 3335 | t->srv->cur_sess--; |
| 3336 | //close(t->srv_fd); |
| 3337 | t->srv_state = SV_STCLOSE; |
| 3338 | if (!(t->flags & SN_ERR_MASK)) |
| 3339 | t->flags |= SN_ERR_SRVTO; |
| 3340 | if (!(t->flags & SN_FINST_MASK)) |
| 3341 | t->flags |= SN_FINST_D; |
| 3342 | /* We used to have a free connection slot. Since we'll never use it, |
| 3343 | * we have to inform the server that it may be used by another session. |
| 3344 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3345 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3346 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3347 | |
| 3348 | return 1; |
| 3349 | } |
| 3350 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3351 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3352 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3356 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3357 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3358 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | return 0; |
| 3362 | } |
| 3363 | else { /* SV_STCLOSE : nothing to do */ |
| 3364 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3365 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3366 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3367 | t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3368 | write(1, trash, len); |
| 3369 | } |
| 3370 | return 0; |
| 3371 | } |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
| 3375 | |
| 3376 | /* |
| 3377 | * Produces data for the session <s> depending on its source. Expects to be |
| 3378 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3379 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3380 | * session, which it uses to keep on being called when there is free space in |
| 3381 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3382 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3383 | */ |
| 3384 | int produce_content(struct session *s) |
| 3385 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3386 | if (s->data_source == DATA_SRC_NONE) { |
| 3387 | s->flags &= ~SN_SELF_GEN; |
| 3388 | return 1; |
| 3389 | } |
| 3390 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3391 | /* dump server statistics */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 3392 | int ret = stats_dump_http(s, s->be->uri_auth, |
| 3393 | (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3394 | if (ret >= 0) |
| 3395 | return ret; |
| 3396 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3397 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3398 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3399 | /* unknown data source or internal error */ |
| 3400 | s->txn.status = 500; |
| 3401 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3402 | if (!(s->flags & SN_ERR_MASK)) |
| 3403 | s->flags |= SN_ERR_PRXCOND; |
| 3404 | if (!(s->flags & SN_FINST_MASK)) |
| 3405 | s->flags |= SN_FINST_R; |
| 3406 | s->flags &= ~SN_SELF_GEN; |
| 3407 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3411 | /* Iterate the same filter through all request headers. |
| 3412 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3413 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3414 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3415 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3416 | 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] | 3417 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3418 | char term; |
| 3419 | char *cur_ptr, *cur_end, *cur_next; |
| 3420 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3421 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3422 | struct hdr_idx_elem *cur_hdr; |
| 3423 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3425 | last_hdr = 0; |
| 3426 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3427 | 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] | 3428 | old_idx = 0; |
| 3429 | |
| 3430 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3431 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3432 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3433 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3434 | (exp->action == ACT_ALLOW || |
| 3435 | exp->action == ACT_DENY || |
| 3436 | exp->action == ACT_TARPIT)) |
| 3437 | return 0; |
| 3438 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3439 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3440 | if (!cur_idx) |
| 3441 | break; |
| 3442 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3443 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3444 | cur_ptr = cur_next; |
| 3445 | cur_end = cur_ptr + cur_hdr->len; |
| 3446 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3447 | |
| 3448 | /* Now we have one header between cur_ptr and cur_end, |
| 3449 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3450 | */ |
| 3451 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3452 | /* The annoying part is that pattern matching needs |
| 3453 | * that we modify the contents to null-terminate all |
| 3454 | * strings before testing them. |
| 3455 | */ |
| 3456 | |
| 3457 | term = *cur_end; |
| 3458 | *cur_end = '\0'; |
| 3459 | |
| 3460 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3461 | switch (exp->action) { |
| 3462 | case ACT_SETBE: |
| 3463 | /* It is not possible to jump a second time. |
| 3464 | * FIXME: should we return an HTTP/500 here so that |
| 3465 | * the admin knows there's a problem ? |
| 3466 | */ |
| 3467 | if (t->be != t->fe) |
| 3468 | break; |
| 3469 | |
| 3470 | /* Swithing Proxy */ |
| 3471 | t->be = (struct proxy *) exp->replace; |
| 3472 | |
| 3473 | /* right now, the backend switch is not too much complicated |
| 3474 | * because we have associated req_cap and rsp_cap to the |
| 3475 | * frontend, and the beconn will be updated later. |
| 3476 | */ |
| 3477 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3478 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3479 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3480 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3481 | last_hdr = 1; |
| 3482 | break; |
| 3483 | |
| 3484 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3485 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3486 | last_hdr = 1; |
| 3487 | break; |
| 3488 | |
| 3489 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3490 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3491 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3492 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3493 | break; |
| 3494 | |
| 3495 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3496 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3497 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3498 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3499 | break; |
| 3500 | |
| 3501 | case ACT_REPLACE: |
| 3502 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3503 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3504 | /* FIXME: if the user adds a newline in the replacement, the |
| 3505 | * index will not be recalculated for now, and the new line |
| 3506 | * will not be counted as a new header. |
| 3507 | */ |
| 3508 | |
| 3509 | cur_end += delta; |
| 3510 | cur_next += delta; |
| 3511 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3512 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3513 | break; |
| 3514 | |
| 3515 | case ACT_REMOVE: |
| 3516 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3517 | cur_next += delta; |
| 3518 | |
| 3519 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3520 | txn->req.eoh += delta; |
| 3521 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3522 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3523 | cur_hdr->len = 0; |
| 3524 | cur_end = NULL; /* null-term has been rewritten */ |
| 3525 | break; |
| 3526 | |
| 3527 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3528 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3529 | if (cur_end) |
| 3530 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3531 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3532 | /* keep the link from this header to next one in case of later |
| 3533 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3534 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3535 | old_idx = cur_idx; |
| 3536 | } |
| 3537 | return 0; |
| 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | /* Apply the filter to the request line. |
| 3542 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3543 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3544 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3545 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3546 | */ |
| 3547 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3548 | { |
| 3549 | char term; |
| 3550 | char *cur_ptr, *cur_end; |
| 3551 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3552 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3553 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3554 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3555 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3556 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3557 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3558 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3559 | (exp->action == ACT_ALLOW || |
| 3560 | exp->action == ACT_DENY || |
| 3561 | exp->action == ACT_TARPIT)) |
| 3562 | return 0; |
| 3563 | else if (exp->action == ACT_REMOVE) |
| 3564 | return 0; |
| 3565 | |
| 3566 | done = 0; |
| 3567 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3568 | 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] | 3569 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3570 | |
| 3571 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3572 | |
| 3573 | /* The annoying part is that pattern matching needs |
| 3574 | * that we modify the contents to null-terminate all |
| 3575 | * strings before testing them. |
| 3576 | */ |
| 3577 | |
| 3578 | term = *cur_end; |
| 3579 | *cur_end = '\0'; |
| 3580 | |
| 3581 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3582 | switch (exp->action) { |
| 3583 | case ACT_SETBE: |
| 3584 | /* It is not possible to jump a second time. |
| 3585 | * FIXME: should we return an HTTP/500 here so that |
| 3586 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3587 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3588 | if (t->be != t->fe) |
| 3589 | break; |
| 3590 | |
| 3591 | /* Swithing Proxy */ |
| 3592 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3593 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3594 | /* right now, the backend switch is not too much complicated |
| 3595 | * because we have associated req_cap and rsp_cap to the |
| 3596 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3597 | */ |
| 3598 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3599 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3600 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3601 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3602 | done = 1; |
| 3603 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3604 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3605 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3606 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3607 | done = 1; |
| 3608 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3610 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3611 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3612 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3613 | done = 1; |
| 3614 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3615 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3616 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3617 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3618 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3619 | done = 1; |
| 3620 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3621 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3622 | case ACT_REPLACE: |
| 3623 | *cur_end = term; /* restore the string terminator */ |
| 3624 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3625 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3626 | /* FIXME: if the user adds a newline in the replacement, the |
| 3627 | * index will not be recalculated for now, and the new line |
| 3628 | * will not be counted as a new header. |
| 3629 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3630 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3631 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3632 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3633 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3634 | 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] | 3635 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3636 | HTTP_MSG_RQMETH, |
| 3637 | cur_ptr, cur_end + 1, |
| 3638 | NULL, NULL); |
| 3639 | if (unlikely(!cur_end)) |
| 3640 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3641 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3642 | /* we have a full request and we know that we have either a CR |
| 3643 | * or an LF at <ptr>. |
| 3644 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3645 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3646 | 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] | 3647 | /* there is no point trying this regex on headers */ |
| 3648 | return 1; |
| 3649 | } |
| 3650 | } |
| 3651 | *cur_end = term; /* restore the string terminator */ |
| 3652 | return done; |
| 3653 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3654 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3655 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3657 | /* |
| 3658 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3659 | * 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] | 3660 | * unparsable request. Since it can manage the switch to another backend, it |
| 3661 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3662 | */ |
| 3663 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3664 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3665 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3666 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3667 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3668 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3669 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3670 | /* |
| 3671 | * The interleaving of transformations and verdicts |
| 3672 | * makes it difficult to decide to continue or stop |
| 3673 | * the evaluation. |
| 3674 | */ |
| 3675 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3676 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3677 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3678 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3679 | exp = exp->next; |
| 3680 | continue; |
| 3681 | } |
| 3682 | |
| 3683 | /* Apply the filter to the request line. */ |
| 3684 | ret = apply_filter_to_req_line(t, req, exp); |
| 3685 | if (unlikely(ret < 0)) |
| 3686 | return -1; |
| 3687 | |
| 3688 | if (likely(ret == 0)) { |
| 3689 | /* The filter did not match the request, it can be |
| 3690 | * iterated through all headers. |
| 3691 | */ |
| 3692 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3693 | } |
| 3694 | exp = exp->next; |
| 3695 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3696 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3700 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3701 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3702 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3703 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3704 | */ |
| 3705 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3706 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3707 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3708 | char *p1, *p2, *p3, *p4; |
| 3709 | char *del_colon, *del_cookie, *colon; |
| 3710 | int app_cookies; |
| 3711 | |
| 3712 | appsess *asession_temp = NULL; |
| 3713 | appsess local_asession; |
| 3714 | |
| 3715 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3716 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3717 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3718 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3719 | * we start with the start line. |
| 3720 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3721 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3722 | 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] | 3723 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3724 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3725 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3726 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3727 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3728 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3729 | cur_ptr = cur_next; |
| 3730 | cur_end = cur_ptr + cur_hdr->len; |
| 3731 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3732 | |
| 3733 | /* We have one full header between cur_ptr and cur_end, and the |
| 3734 | * next header starts at cur_next. We're only interested in |
| 3735 | * "Cookie:" headers. |
| 3736 | */ |
| 3737 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3738 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3739 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3740 | old_idx = cur_idx; |
| 3741 | continue; |
| 3742 | } |
| 3743 | |
| 3744 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3745 | * attributes whose name begin with a '$', and associate them with |
| 3746 | * the right cookie, if we want to delete this cookie. |
| 3747 | * So there are 3 cases for each cookie read : |
| 3748 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3749 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3750 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3751 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3752 | * "special" cookie. |
| 3753 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3754 | * remove it. If no application cookie persists in the header, we |
| 3755 | * *MUST* delete it |
| 3756 | */ |
| 3757 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3758 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3759 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3760 | /* del_cookie == NULL => nothing to be deleted */ |
| 3761 | del_colon = del_cookie = NULL; |
| 3762 | app_cookies = 0; |
| 3763 | |
| 3764 | while (p1 < cur_end) { |
| 3765 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3766 | while (p1 < cur_end) { |
| 3767 | if (*p1 == ';' || *p1 == ',') |
| 3768 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3769 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3770 | break; |
| 3771 | p1++; |
| 3772 | } |
| 3773 | |
| 3774 | if (p1 == cur_end) |
| 3775 | break; |
| 3776 | |
| 3777 | /* p1 is at the beginning of the cookie name */ |
| 3778 | p2 = p1; |
| 3779 | while (p2 < cur_end && *p2 != '=') |
| 3780 | p2++; |
| 3781 | |
| 3782 | if (p2 == cur_end) |
| 3783 | break; |
| 3784 | |
| 3785 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3786 | if (p3 == cur_end) |
| 3787 | break; |
| 3788 | |
| 3789 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3790 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3791 | p4++; |
| 3792 | |
| 3793 | /* here, we have the cookie name between p1 and p2, |
| 3794 | * and its value between p3 and p4. |
| 3795 | * we can process it : |
| 3796 | * |
| 3797 | * Cookie: NAME=VALUE; |
| 3798 | * | || || | |
| 3799 | * | || || +--> p4 |
| 3800 | * | || |+-------> p3 |
| 3801 | * | || +--------> p2 |
| 3802 | * | |+------------> p1 |
| 3803 | * | +-------------> colon |
| 3804 | * +--------------------> cur_ptr |
| 3805 | */ |
| 3806 | |
| 3807 | if (*p1 == '$') { |
| 3808 | /* skip this one */ |
| 3809 | } |
| 3810 | else { |
| 3811 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3812 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3813 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3814 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3815 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3816 | int log_len = p4 - p1; |
| 3817 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3818 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3819 | Alert("HTTP logging : out of memory.\n"); |
| 3820 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3821 | if (log_len > t->fe->capture_len) |
| 3822 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3823 | memcpy(txn->cli_cookie, p1, log_len); |
| 3824 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3825 | } |
| 3826 | } |
| 3827 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3828 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3829 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3830 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3831 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3832 | char *delim; |
| 3833 | |
| 3834 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3835 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3836 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3837 | * |
| 3838 | * Cookie: NAME=SRV~VALUE; |
| 3839 | * | || || | | |
| 3840 | * | || || | +--> p4 |
| 3841 | * | || || +--------> delim |
| 3842 | * | || |+-----------> p3 |
| 3843 | * | || +------------> p2 |
| 3844 | * | |+----------------> p1 |
| 3845 | * | +-----------------> colon |
| 3846 | * +------------------------> cur_ptr |
| 3847 | */ |
| 3848 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3849 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3850 | for (delim = p3; delim < p4; delim++) |
| 3851 | if (*delim == COOKIE_DELIM) |
| 3852 | break; |
| 3853 | } |
| 3854 | else |
| 3855 | delim = p4; |
| 3856 | |
| 3857 | |
| 3858 | /* Here, we'll look for the first running server which supports the cookie. |
| 3859 | * This allows to share a same cookie between several servers, for example |
| 3860 | * to dedicate backup servers to specific servers only. |
| 3861 | * However, to prevent clients from sticking to cookie-less backup server |
| 3862 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3863 | * empty cookies and mark them as invalid. |
| 3864 | */ |
| 3865 | if (delim == p3) |
| 3866 | srv = NULL; |
| 3867 | |
| 3868 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3869 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3870 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3871 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3872 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3873 | txn->flags &= ~TX_CK_MASK; |
| 3874 | txn->flags |= TX_CK_VALID; |
| 3875 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3876 | t->srv = srv; |
| 3877 | break; |
| 3878 | } else { |
| 3879 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3880 | txn->flags &= ~TX_CK_MASK; |
| 3881 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | srv = srv->next; |
| 3885 | } |
| 3886 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3887 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3888 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3889 | txn->flags &= ~TX_CK_MASK; |
| 3890 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* depending on the cookie mode, we may have to either : |
| 3894 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3895 | * the server never sees it ; |
| 3896 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3897 | * application cookie so that it does not get accidentely removed later, |
| 3898 | * if we're in cookie prefix mode |
| 3899 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3900 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3901 | int delta; /* negative */ |
| 3902 | |
| 3903 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3904 | p4 += delta; |
| 3905 | cur_end += delta; |
| 3906 | cur_next += delta; |
| 3907 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3908 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3909 | |
| 3910 | del_cookie = del_colon = NULL; |
| 3911 | app_cookies++; /* protect the header from deletion */ |
| 3912 | } |
| 3913 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3914 | (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] | 3915 | del_cookie = p1; |
| 3916 | del_colon = colon; |
| 3917 | } |
| 3918 | } else { |
| 3919 | /* now we know that we must keep this cookie since it's |
| 3920 | * not ours. But if we wanted to delete our cookie |
| 3921 | * earlier, we cannot remove the complete header, but we |
| 3922 | * can remove the previous block itself. |
| 3923 | */ |
| 3924 | app_cookies++; |
| 3925 | |
| 3926 | if (del_cookie != NULL) { |
| 3927 | int delta; /* negative */ |
| 3928 | |
| 3929 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3930 | p4 += delta; |
| 3931 | cur_end += delta; |
| 3932 | cur_next += delta; |
| 3933 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3934 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3935 | del_cookie = del_colon = NULL; |
| 3936 | } |
| 3937 | } |
| 3938 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3939 | if ((t->be->appsession_name != NULL) && |
| 3940 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3941 | /* first, let's see if the cookie is our appcookie*/ |
| 3942 | |
| 3943 | /* Cool... it's the right one */ |
| 3944 | |
| 3945 | asession_temp = &local_asession; |
| 3946 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3947 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3948 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3949 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3950 | return; |
| 3951 | } |
| 3952 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3953 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3954 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3955 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3956 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3957 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3958 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3959 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3960 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3961 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3962 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3963 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3964 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3965 | return; |
| 3966 | } |
| 3967 | |
| 3968 | asession_temp->sessid = local_asession.sessid; |
| 3969 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3970 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3971 | } else { |
| 3972 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3973 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3974 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3975 | if (asession_temp->serverid == NULL) { |
| 3976 | Alert("Found Application Session without matching server.\n"); |
| 3977 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3978 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3979 | while (srv) { |
| 3980 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3981 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3982 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3983 | txn->flags &= ~TX_CK_MASK; |
| 3984 | txn->flags |= TX_CK_VALID; |
| 3985 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3986 | t->srv = srv; |
| 3987 | break; |
| 3988 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3989 | txn->flags &= ~TX_CK_MASK; |
| 3990 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | srv = srv->next; |
| 3994 | }/* end while(srv) */ |
| 3995 | }/* end else if server == NULL */ |
| 3996 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 3997 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3998 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 3999 | } |
| 4000 | |
| 4001 | /* we'll have to look for another cookie ... */ |
| 4002 | p1 = p4; |
| 4003 | } /* while (p1 < cur_end) */ |
| 4004 | |
| 4005 | /* There's no more cookie on this line. |
| 4006 | * We may have marked the last one(s) for deletion. |
| 4007 | * We must do this now in two ways : |
| 4008 | * - if there is no app cookie, we simply delete the header ; |
| 4009 | * - if there are app cookies, we must delete the end of the |
| 4010 | * string properly, including the colon/semi-colon before |
| 4011 | * the cookie name. |
| 4012 | */ |
| 4013 | if (del_cookie != NULL) { |
| 4014 | int delta; |
| 4015 | if (app_cookies) { |
| 4016 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4017 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4018 | cur_hdr->len += delta; |
| 4019 | } else { |
| 4020 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4021 | |
| 4022 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4023 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4024 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4025 | cur_hdr->len = 0; |
| 4026 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4027 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4028 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | /* keep the link from this header to next one */ |
| 4032 | old_idx = cur_idx; |
| 4033 | } /* end of cookie processing on this header */ |
| 4034 | } |
| 4035 | |
| 4036 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4037 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4038 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4039 | */ |
| 4040 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4041 | { |
| 4042 | char term; |
| 4043 | char *cur_ptr, *cur_end, *cur_next; |
| 4044 | int cur_idx, old_idx, last_hdr; |
| 4045 | struct http_txn *txn = &t->txn; |
| 4046 | struct hdr_idx_elem *cur_hdr; |
| 4047 | int len, delta; |
| 4048 | |
| 4049 | last_hdr = 0; |
| 4050 | |
| 4051 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4052 | old_idx = 0; |
| 4053 | |
| 4054 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4055 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4056 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4057 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4058 | (exp->action == ACT_ALLOW || |
| 4059 | exp->action == ACT_DENY)) |
| 4060 | return 0; |
| 4061 | |
| 4062 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4063 | if (!cur_idx) |
| 4064 | break; |
| 4065 | |
| 4066 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4067 | cur_ptr = cur_next; |
| 4068 | cur_end = cur_ptr + cur_hdr->len; |
| 4069 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4070 | |
| 4071 | /* Now we have one header between cur_ptr and cur_end, |
| 4072 | * and the next header starts at cur_next. |
| 4073 | */ |
| 4074 | |
| 4075 | /* The annoying part is that pattern matching needs |
| 4076 | * that we modify the contents to null-terminate all |
| 4077 | * strings before testing them. |
| 4078 | */ |
| 4079 | |
| 4080 | term = *cur_end; |
| 4081 | *cur_end = '\0'; |
| 4082 | |
| 4083 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4084 | switch (exp->action) { |
| 4085 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4086 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4087 | last_hdr = 1; |
| 4088 | break; |
| 4089 | |
| 4090 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4091 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4092 | last_hdr = 1; |
| 4093 | break; |
| 4094 | |
| 4095 | case ACT_REPLACE: |
| 4096 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4097 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4098 | /* FIXME: if the user adds a newline in the replacement, the |
| 4099 | * index will not be recalculated for now, and the new line |
| 4100 | * will not be counted as a new header. |
| 4101 | */ |
| 4102 | |
| 4103 | cur_end += delta; |
| 4104 | cur_next += delta; |
| 4105 | cur_hdr->len += delta; |
| 4106 | txn->rsp.eoh += delta; |
| 4107 | break; |
| 4108 | |
| 4109 | case ACT_REMOVE: |
| 4110 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4111 | cur_next += delta; |
| 4112 | |
| 4113 | /* FIXME: this should be a separate function */ |
| 4114 | txn->rsp.eoh += delta; |
| 4115 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4116 | txn->hdr_idx.used--; |
| 4117 | cur_hdr->len = 0; |
| 4118 | cur_end = NULL; /* null-term has been rewritten */ |
| 4119 | break; |
| 4120 | |
| 4121 | } |
| 4122 | } |
| 4123 | if (cur_end) |
| 4124 | *cur_end = term; /* restore the string terminator */ |
| 4125 | |
| 4126 | /* keep the link from this header to next one in case of later |
| 4127 | * removal of next header. |
| 4128 | */ |
| 4129 | old_idx = cur_idx; |
| 4130 | } |
| 4131 | return 0; |
| 4132 | } |
| 4133 | |
| 4134 | |
| 4135 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4136 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4137 | * or -1 if a replacement resulted in an invalid status line. |
| 4138 | */ |
| 4139 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4140 | { |
| 4141 | char term; |
| 4142 | char *cur_ptr, *cur_end; |
| 4143 | int done; |
| 4144 | struct http_txn *txn = &t->txn; |
| 4145 | int len, delta; |
| 4146 | |
| 4147 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4148 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4149 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4150 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4151 | (exp->action == ACT_ALLOW || |
| 4152 | exp->action == ACT_DENY)) |
| 4153 | return 0; |
| 4154 | else if (exp->action == ACT_REMOVE) |
| 4155 | return 0; |
| 4156 | |
| 4157 | done = 0; |
| 4158 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4159 | 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] | 4160 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4161 | |
| 4162 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4163 | |
| 4164 | /* The annoying part is that pattern matching needs |
| 4165 | * that we modify the contents to null-terminate all |
| 4166 | * strings before testing them. |
| 4167 | */ |
| 4168 | |
| 4169 | term = *cur_end; |
| 4170 | *cur_end = '\0'; |
| 4171 | |
| 4172 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4173 | switch (exp->action) { |
| 4174 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4175 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4176 | done = 1; |
| 4177 | break; |
| 4178 | |
| 4179 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4180 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4181 | done = 1; |
| 4182 | break; |
| 4183 | |
| 4184 | case ACT_REPLACE: |
| 4185 | *cur_end = term; /* restore the string terminator */ |
| 4186 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4187 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4188 | /* FIXME: if the user adds a newline in the replacement, the |
| 4189 | * index will not be recalculated for now, and the new line |
| 4190 | * will not be counted as a new header. |
| 4191 | */ |
| 4192 | |
| 4193 | txn->rsp.eoh += delta; |
| 4194 | cur_end += delta; |
| 4195 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4196 | 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] | 4197 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4198 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4199 | cur_ptr, cur_end + 1, |
| 4200 | NULL, NULL); |
| 4201 | if (unlikely(!cur_end)) |
| 4202 | return -1; |
| 4203 | |
| 4204 | /* we have a full respnse and we know that we have either a CR |
| 4205 | * or an LF at <ptr>. |
| 4206 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4207 | 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] | 4208 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4209 | /* there is no point trying this regex on headers */ |
| 4210 | return 1; |
| 4211 | } |
| 4212 | } |
| 4213 | *cur_end = term; /* restore the string terminator */ |
| 4214 | return done; |
| 4215 | } |
| 4216 | |
| 4217 | |
| 4218 | |
| 4219 | /* |
| 4220 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4221 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4222 | * unparsable response. |
| 4223 | */ |
| 4224 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4225 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4226 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4227 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4228 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4229 | int ret; |
| 4230 | |
| 4231 | /* |
| 4232 | * The interleaving of transformations and verdicts |
| 4233 | * makes it difficult to decide to continue or stop |
| 4234 | * the evaluation. |
| 4235 | */ |
| 4236 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4237 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4238 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4239 | exp->action == ACT_PASS)) { |
| 4240 | exp = exp->next; |
| 4241 | continue; |
| 4242 | } |
| 4243 | |
| 4244 | /* Apply the filter to the status line. */ |
| 4245 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4246 | if (unlikely(ret < 0)) |
| 4247 | return -1; |
| 4248 | |
| 4249 | if (likely(ret == 0)) { |
| 4250 | /* The filter did not match the response, it can be |
| 4251 | * iterated through all headers. |
| 4252 | */ |
| 4253 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4254 | } |
| 4255 | exp = exp->next; |
| 4256 | } |
| 4257 | return 0; |
| 4258 | } |
| 4259 | |
| 4260 | |
| 4261 | |
| 4262 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4263 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4264 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4265 | */ |
| 4266 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4267 | { |
| 4268 | struct http_txn *txn = &t->txn; |
| 4269 | char *p1, *p2, *p3, *p4; |
| 4270 | |
| 4271 | appsess *asession_temp = NULL; |
| 4272 | appsess local_asession; |
| 4273 | |
| 4274 | char *cur_ptr, *cur_end, *cur_next; |
| 4275 | int cur_idx, old_idx, delta; |
| 4276 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4277 | /* Iterate through the headers. |
| 4278 | * we start with the start line. |
| 4279 | */ |
| 4280 | old_idx = 0; |
| 4281 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4282 | |
| 4283 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4284 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4285 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4286 | |
| 4287 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4288 | cur_ptr = cur_next; |
| 4289 | cur_end = cur_ptr + cur_hdr->len; |
| 4290 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4291 | |
| 4292 | /* We have one full header between cur_ptr and cur_end, and the |
| 4293 | * next header starts at cur_next. We're only interested in |
| 4294 | * "Cookie:" headers. |
| 4295 | */ |
| 4296 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4297 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4298 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4299 | old_idx = cur_idx; |
| 4300 | continue; |
| 4301 | } |
| 4302 | |
| 4303 | /* 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] | 4304 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4305 | |
| 4306 | |
| 4307 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4308 | if (t->be->cookie_name == NULL && |
| 4309 | t->be->appsession_name == NULL && |
| 4310 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4311 | return; |
| 4312 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4313 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4314 | |
| 4315 | 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] | 4316 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4317 | break; |
| 4318 | |
| 4319 | /* p1 is at the beginning of the cookie name */ |
| 4320 | p2 = p1; |
| 4321 | |
| 4322 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4323 | p2++; |
| 4324 | |
| 4325 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4326 | break; |
| 4327 | |
| 4328 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4329 | if (p3 == cur_end) |
| 4330 | break; |
| 4331 | |
| 4332 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4333 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4334 | p4++; |
| 4335 | |
| 4336 | /* here, we have the cookie name between p1 and p2, |
| 4337 | * and its value between p3 and p4. |
| 4338 | * we can process it. |
| 4339 | */ |
| 4340 | |
| 4341 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4342 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4343 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4344 | (p4 - p1 >= t->be->capture_namelen) && |
| 4345 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4346 | int log_len = p4 - p1; |
| 4347 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4348 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4349 | Alert("HTTP logging : out of memory.\n"); |
| 4350 | } |
| 4351 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4352 | if (log_len > t->be->capture_len) |
| 4353 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4354 | memcpy(txn->srv_cookie, p1, log_len); |
| 4355 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4359 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4360 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4361 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4362 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4365 | * this occurrence because we'll insert another one later. |
| 4366 | * We'll delete it too if the "indirect" option is set and we're in |
| 4367 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4368 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4369 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4370 | /* this header must be deleted */ |
| 4371 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4372 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4373 | txn->hdr_idx.used--; |
| 4374 | cur_hdr->len = 0; |
| 4375 | cur_next += delta; |
| 4376 | txn->rsp.eoh += delta; |
| 4377 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4378 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4379 | } |
| 4380 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4381 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4382 | /* replace bytes p3->p4 with the cookie name associated |
| 4383 | * with this server since we know it. |
| 4384 | */ |
| 4385 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4386 | cur_hdr->len += delta; |
| 4387 | cur_next += delta; |
| 4388 | txn->rsp.eoh += delta; |
| 4389 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4390 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4391 | } |
| 4392 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4393 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4394 | /* insert the cookie name associated with this server |
| 4395 | * before existing cookie, and insert a delimitor between them.. |
| 4396 | */ |
| 4397 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4398 | cur_hdr->len += delta; |
| 4399 | cur_next += delta; |
| 4400 | txn->rsp.eoh += delta; |
| 4401 | |
| 4402 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4403 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4407 | else if ((t->be->appsession_name != NULL) && |
| 4408 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4409 | |
| 4410 | /* Cool... it's the right one */ |
| 4411 | |
| 4412 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4413 | asession_temp = &local_asession; |
| 4414 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4415 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4416 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4417 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4418 | return; |
| 4419 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4420 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4421 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4422 | asession_temp->serverid = NULL; |
| 4423 | |
| 4424 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4425 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4426 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4427 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4428 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4429 | return; |
| 4430 | } |
| 4431 | asession_temp->sessid = local_asession.sessid; |
| 4432 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4433 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4434 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4435 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4436 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4437 | } |
| 4438 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4439 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4440 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4441 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4442 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4443 | return; |
| 4444 | } |
| 4445 | asession_temp->serverid[0] = '\0'; |
| 4446 | } |
| 4447 | |
| 4448 | if (asession_temp->serverid[0] == '\0') |
| 4449 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4450 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4451 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4452 | |
| 4453 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4454 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4455 | #endif |
| 4456 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4457 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4458 | } /* we're now at the end of the cookie value */ |
| 4459 | |
| 4460 | /* keep the link from this header to next one */ |
| 4461 | old_idx = cur_idx; |
| 4462 | } /* end of cookie processing on this header */ |
| 4463 | } |
| 4464 | |
| 4465 | |
| 4466 | |
| 4467 | /* |
| 4468 | * Check if response is cacheable or not. Updates t->flags. |
| 4469 | */ |
| 4470 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4471 | { |
| 4472 | struct http_txn *txn = &t->txn; |
| 4473 | char *p1, *p2; |
| 4474 | |
| 4475 | char *cur_ptr, *cur_end, *cur_next; |
| 4476 | int cur_idx; |
| 4477 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4478 | if (!txn->flags & TX_CACHEABLE) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4479 | return; |
| 4480 | |
| 4481 | /* Iterate through the headers. |
| 4482 | * we start with the start line. |
| 4483 | */ |
| 4484 | cur_idx = 0; |
| 4485 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4486 | |
| 4487 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4488 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4489 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4490 | |
| 4491 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4492 | cur_ptr = cur_next; |
| 4493 | cur_end = cur_ptr + cur_hdr->len; |
| 4494 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4495 | |
| 4496 | /* We have one full header between cur_ptr and cur_end, and the |
| 4497 | * next header starts at cur_next. We're only interested in |
| 4498 | * "Cookie:" headers. |
| 4499 | */ |
| 4500 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4501 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4502 | if (val) { |
| 4503 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4504 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4505 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4506 | return; |
| 4507 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4508 | } |
| 4509 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4510 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4511 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4512 | continue; |
| 4513 | |
| 4514 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4515 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4516 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4517 | |
| 4518 | if (p1 >= cur_end) /* no more info */ |
| 4519 | continue; |
| 4520 | |
| 4521 | /* p1 is at the beginning of the value */ |
| 4522 | p2 = p1; |
| 4523 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4524 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4525 | p2++; |
| 4526 | |
| 4527 | /* we have a complete value between p1 and p2 */ |
| 4528 | if (p2 < cur_end && *p2 == '=') { |
| 4529 | /* we have something of the form no-cache="set-cookie" */ |
| 4530 | if ((cur_end - p1 >= 21) && |
| 4531 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4532 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4533 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4534 | continue; |
| 4535 | } |
| 4536 | |
| 4537 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4538 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4539 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4540 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4541 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4542 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4543 | return; |
| 4544 | } |
| 4545 | |
| 4546 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4547 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4548 | continue; |
| 4549 | } |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4554 | /* |
| 4555 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4556 | * If the server is found, it's assigned to the session. |
| 4557 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4558 | 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] | 4559 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4560 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4561 | appsess *asession_temp = NULL; |
| 4562 | appsess local_asession; |
| 4563 | char *request_line; |
| 4564 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4565 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4566 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4567 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4568 | ((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] | 4569 | return; |
| 4570 | |
| 4571 | /* skip ';' */ |
| 4572 | request_line++; |
| 4573 | |
| 4574 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4575 | 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] | 4576 | return; |
| 4577 | |
| 4578 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4579 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4580 | |
| 4581 | /* First try if we already have an appsession */ |
| 4582 | asession_temp = &local_asession; |
| 4583 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4584 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4585 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4586 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4587 | return; |
| 4588 | } |
| 4589 | |
| 4590 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4591 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4592 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4593 | asession_temp->serverid = NULL; |
| 4594 | |
| 4595 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4596 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4597 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4598 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4599 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4600 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4601 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4602 | return; |
| 4603 | } |
| 4604 | asession_temp->sessid = local_asession.sessid; |
| 4605 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4606 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4607 | } |
| 4608 | else { |
| 4609 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4610 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4611 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4613 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4614 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4615 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4616 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4617 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4618 | #endif |
| 4619 | if (asession_temp->serverid == NULL) { |
| 4620 | Alert("Found Application Session without matching server.\n"); |
| 4621 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4622 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4623 | while (srv) { |
| 4624 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4625 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4626 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4627 | txn->flags &= ~TX_CK_MASK; |
| 4628 | txn->flags |= TX_CK_VALID; |
| 4629 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4630 | t->srv = srv; |
| 4631 | break; |
| 4632 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4633 | txn->flags &= ~TX_CK_MASK; |
| 4634 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | srv = srv->next; |
| 4638 | } |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4643 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4644 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4645 | * 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] | 4646 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4647 | * 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] | 4648 | * 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] | 4649 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4650 | * |
| 4651 | * Returns 1 if the session's state changes, otherwise 0. |
| 4652 | */ |
| 4653 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4654 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4655 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4656 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4657 | struct user_auth *user; |
| 4658 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4659 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4661 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4662 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4663 | return 0; |
| 4664 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4665 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4666 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4667 | /* the URI is in h */ |
| 4668 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4669 | return 0; |
| 4670 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4671 | h += uri_auth->uri_len; |
| 4672 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4673 | if (memcmp(h, ";up", 3) == 0) { |
| 4674 | t->flags |= SN_STAT_HIDEDWN; |
| 4675 | break; |
| 4676 | } |
| 4677 | h++; |
| 4678 | } |
| 4679 | |
| 4680 | if (uri_auth->refresh) { |
| 4681 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4682 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4683 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4684 | t->flags |= SN_STAT_NORFRSH; |
| 4685 | break; |
| 4686 | } |
| 4687 | h++; |
| 4688 | } |
| 4689 | } |
| 4690 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4691 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4692 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4693 | if (memcmp(h, ";csv", 4) == 0) { |
| 4694 | t->flags |= SN_STAT_FMTCSV; |
| 4695 | break; |
| 4696 | } |
| 4697 | h++; |
| 4698 | } |
| 4699 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4700 | /* we are in front of a interceptable URI. Let's check |
| 4701 | * if there's an authentication and if it's valid. |
| 4702 | */ |
| 4703 | user = uri_auth->users; |
| 4704 | if (!user) { |
| 4705 | /* no user auth required, it's OK */ |
| 4706 | authenticated = 1; |
| 4707 | } else { |
| 4708 | authenticated = 0; |
| 4709 | |
| 4710 | /* a user list is defined, we have to check. |
| 4711 | * skip 21 chars for "Authorization: Basic ". |
| 4712 | */ |
| 4713 | |
| 4714 | /* FIXME: this should move to an earlier place */ |
| 4715 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4716 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4717 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4718 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4719 | if (len > 14 && |
| 4720 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4721 | txn->auth_hdr.str = h; |
| 4722 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4723 | break; |
| 4724 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4725 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4728 | if (txn->auth_hdr.len < 21 || |
| 4729 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4730 | user = NULL; |
| 4731 | |
| 4732 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4733 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4734 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4735 | user->user_pwd, user->user_len)) { |
| 4736 | authenticated = 1; |
| 4737 | break; |
| 4738 | } |
| 4739 | user = user->next; |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4744 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4745 | |
| 4746 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4747 | msg.str = trash; |
| 4748 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4749 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4750 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4751 | if (!(t->flags & SN_ERR_MASK)) |
| 4752 | t->flags |= SN_ERR_PRXCOND; |
| 4753 | if (!(t->flags & SN_FINST_MASK)) |
| 4754 | t->flags |= SN_FINST_R; |
| 4755 | return 1; |
| 4756 | } |
| 4757 | |
| 4758 | /* The request is valid, the user is authenticate. Let's start sending |
| 4759 | * data. |
| 4760 | */ |
| 4761 | t->cli_state = CL_STSHUTR; |
| 4762 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4763 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4764 | t->data_source = DATA_SRC_STATS; |
| 4765 | t->data_state = DATA_ST_INIT; |
| 4766 | produce_content(t); |
| 4767 | return 1; |
| 4768 | } |
| 4769 | |
| 4770 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4771 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4772 | * Print a debug line with a header |
| 4773 | */ |
| 4774 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4775 | { |
| 4776 | int len, max; |
| 4777 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4778 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4779 | max = end - start; |
| 4780 | UBOUND(max, sizeof(trash) - len - 1); |
| 4781 | len += strlcpy2(trash + len, start, max + 1); |
| 4782 | trash[len++] = '\n'; |
| 4783 | write(1, trash, len); |
| 4784 | } |
| 4785 | |
| 4786 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4787 | /************************************************************************/ |
| 4788 | /* The code below is dedicated to ACL parsing and matching */ |
| 4789 | /************************************************************************/ |
| 4790 | |
| 4791 | |
| 4792 | |
| 4793 | |
| 4794 | /* 1. Check on METHOD |
| 4795 | * We use the pre-parsed method if it is known, and store its number as an |
| 4796 | * integer. If it is unknown, we use the pointer and the length. |
| 4797 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4798 | 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] | 4799 | { |
| 4800 | int len, meth; |
| 4801 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4802 | len = strlen(*text); |
| 4803 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4804 | |
| 4805 | pattern->val.i = meth; |
| 4806 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4807 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4808 | if (!pattern->ptr.str) |
| 4809 | return 0; |
| 4810 | pattern->len = len; |
| 4811 | } |
| 4812 | return 1; |
| 4813 | } |
| 4814 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4815 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4816 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4817 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4818 | { |
| 4819 | int meth; |
| 4820 | struct http_txn *txn = l7; |
| 4821 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4822 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4823 | return 0; |
| 4824 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4825 | meth = txn->meth; |
| 4826 | test->i = meth; |
| 4827 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4828 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4829 | /* ensure the indexes are not affected */ |
| 4830 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4831 | test->len = txn->req.sl.rq.m_l; |
| 4832 | test->ptr = txn->req.sol; |
| 4833 | } |
| 4834 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4835 | return 1; |
| 4836 | } |
| 4837 | |
| 4838 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4839 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4840 | int icase; |
| 4841 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4842 | if (test->i != pattern->val.i) |
| 4843 | return 0; |
| 4844 | |
| 4845 | if (test->i != HTTP_METH_OTHER) |
| 4846 | return 1; |
| 4847 | |
| 4848 | /* Other method, we must compare the strings */ |
| 4849 | if (pattern->len != test->len) |
| 4850 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4851 | |
| 4852 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4853 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4854 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4855 | return 0; |
| 4856 | return 1; |
| 4857 | } |
| 4858 | |
| 4859 | /* 2. Check on Request/Status Version |
| 4860 | * We simply compare strings here. |
| 4861 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4862 | 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] | 4863 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4864 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4865 | if (!pattern->ptr.str) |
| 4866 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4867 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4868 | return 1; |
| 4869 | } |
| 4870 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4871 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4872 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4873 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4874 | { |
| 4875 | struct http_txn *txn = l7; |
| 4876 | char *ptr; |
| 4877 | int len; |
| 4878 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4879 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4880 | return 0; |
| 4881 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4882 | len = txn->req.sl.rq.v_l; |
| 4883 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4884 | |
| 4885 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4886 | if (len <= 0) |
| 4887 | return 0; |
| 4888 | |
| 4889 | test->ptr = ptr; |
| 4890 | test->len = len; |
| 4891 | |
| 4892 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4893 | return 1; |
| 4894 | } |
| 4895 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4896 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4897 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4898 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4899 | { |
| 4900 | struct http_txn *txn = l7; |
| 4901 | char *ptr; |
| 4902 | int len; |
| 4903 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4904 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4905 | return 0; |
| 4906 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4907 | len = txn->rsp.sl.st.v_l; |
| 4908 | ptr = txn->rsp.sol; |
| 4909 | |
| 4910 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4911 | if (len <= 0) |
| 4912 | return 0; |
| 4913 | |
| 4914 | test->ptr = ptr; |
| 4915 | test->len = len; |
| 4916 | |
| 4917 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4918 | return 1; |
| 4919 | } |
| 4920 | |
| 4921 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4922 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4923 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4924 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4925 | { |
| 4926 | struct http_txn *txn = l7; |
| 4927 | char *ptr; |
| 4928 | int len; |
| 4929 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4930 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4931 | return 0; |
| 4932 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4933 | len = txn->rsp.sl.st.c_l; |
| 4934 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4935 | |
| 4936 | test->i = __strl2ui(ptr, len); |
| 4937 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4938 | return 1; |
| 4939 | } |
| 4940 | |
| 4941 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4942 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4943 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4944 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4945 | { |
| 4946 | struct http_txn *txn = l7; |
| 4947 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4948 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4949 | return 0; |
| 4950 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4951 | /* ensure the indexes are not affected */ |
| 4952 | return 0; |
| 4953 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4954 | test->len = txn->req.sl.rq.u_l; |
| 4955 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4956 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4957 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4958 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4959 | return 1; |
| 4960 | } |
| 4961 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4962 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 4963 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 4964 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4965 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4966 | 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] | 4967 | struct acl_expr *expr, struct acl_test *test) |
| 4968 | { |
| 4969 | struct http_txn *txn = l7; |
| 4970 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4971 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4972 | |
| 4973 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4974 | /* search for header from the beginning */ |
| 4975 | ctx->idx = 0; |
| 4976 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4977 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4978 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4979 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4980 | test->len = ctx->vlen; |
| 4981 | test->ptr = (char *)ctx->line + ctx->val; |
| 4982 | return 1; |
| 4983 | } |
| 4984 | |
| 4985 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4986 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4987 | return 0; |
| 4988 | } |
| 4989 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4990 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4991 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4992 | struct acl_expr *expr, struct acl_test *test) |
| 4993 | { |
| 4994 | struct http_txn *txn = l7; |
| 4995 | |
| 4996 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4997 | return 0; |
| 4998 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4999 | /* ensure the indexes are not affected */ |
| 5000 | return 0; |
| 5001 | |
| 5002 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5003 | } |
| 5004 | |
| 5005 | static int |
| 5006 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5007 | struct acl_expr *expr, struct acl_test *test) |
| 5008 | { |
| 5009 | struct http_txn *txn = l7; |
| 5010 | |
| 5011 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5012 | return 0; |
| 5013 | |
| 5014 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5015 | } |
| 5016 | |
| 5017 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5018 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5019 | */ |
| 5020 | static int |
| 5021 | 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] | 5022 | struct acl_expr *expr, struct acl_test *test) |
| 5023 | { |
| 5024 | struct http_txn *txn = l7; |
| 5025 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5026 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5027 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5028 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5029 | ctx.idx = 0; |
| 5030 | cnt = 0; |
| 5031 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5032 | cnt++; |
| 5033 | |
| 5034 | test->i = cnt; |
| 5035 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5036 | return 1; |
| 5037 | } |
| 5038 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5039 | static int |
| 5040 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5041 | struct acl_expr *expr, struct acl_test *test) |
| 5042 | { |
| 5043 | struct http_txn *txn = l7; |
| 5044 | |
| 5045 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5046 | return 0; |
| 5047 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5048 | /* ensure the indexes are not affected */ |
| 5049 | return 0; |
| 5050 | |
| 5051 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5052 | } |
| 5053 | |
| 5054 | static int |
| 5055 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5056 | struct acl_expr *expr, struct acl_test *test) |
| 5057 | { |
| 5058 | struct http_txn *txn = l7; |
| 5059 | |
| 5060 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5061 | return 0; |
| 5062 | |
| 5063 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5064 | } |
| 5065 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5066 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5067 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5068 | * 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] | 5069 | */ |
| 5070 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5071 | 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] | 5072 | struct acl_expr *expr, struct acl_test *test) |
| 5073 | { |
| 5074 | struct http_txn *txn = l7; |
| 5075 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5076 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5077 | |
| 5078 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5079 | /* search for header from the beginning */ |
| 5080 | ctx->idx = 0; |
| 5081 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5082 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5083 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5084 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5085 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5086 | return 1; |
| 5087 | } |
| 5088 | |
| 5089 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5090 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5094 | static int |
| 5095 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5096 | struct acl_expr *expr, struct acl_test *test) |
| 5097 | { |
| 5098 | struct http_txn *txn = l7; |
| 5099 | |
| 5100 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5101 | return 0; |
| 5102 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5103 | /* ensure the indexes are not affected */ |
| 5104 | return 0; |
| 5105 | |
| 5106 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5107 | } |
| 5108 | |
| 5109 | static int |
| 5110 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5111 | struct acl_expr *expr, struct acl_test *test) |
| 5112 | { |
| 5113 | struct http_txn *txn = l7; |
| 5114 | |
| 5115 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5116 | return 0; |
| 5117 | |
| 5118 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5119 | } |
| 5120 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5121 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5122 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5123 | */ |
| 5124 | static int |
| 5125 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5126 | struct acl_expr *expr, struct acl_test *test) |
| 5127 | { |
| 5128 | struct http_txn *txn = l7; |
| 5129 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5130 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5131 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5132 | return 0; |
| 5133 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5134 | /* ensure the indexes are not affected */ |
| 5135 | return 0; |
| 5136 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5137 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5138 | end = ptr + txn->req.sl.rq.u_l; |
| 5139 | |
| 5140 | if (ptr >= end) |
| 5141 | return 0; |
| 5142 | |
| 5143 | /* RFC2616, par. 5.1.2 : |
| 5144 | * Request-URI = "*" | absuri | abspath | authority |
| 5145 | */ |
| 5146 | |
| 5147 | if (*ptr == '*') |
| 5148 | return 0; |
| 5149 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5150 | if (isalpha((unsigned char)*ptr)) { |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5151 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5152 | ptr++; |
| 5153 | while (ptr < end && |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5154 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5155 | ptr++; |
| 5156 | /* skip '://' */ |
| 5157 | if (ptr == end || *ptr++ != ':') |
| 5158 | return 0; |
| 5159 | if (ptr == end || *ptr++ != '/') |
| 5160 | return 0; |
| 5161 | if (ptr == end || *ptr++ != '/') |
| 5162 | return 0; |
| 5163 | } |
| 5164 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5165 | |
| 5166 | while (ptr < end && *ptr != '/') |
| 5167 | ptr++; |
| 5168 | |
| 5169 | if (ptr == end) |
| 5170 | return 0; |
| 5171 | |
| 5172 | /* OK, we got the '/' ! */ |
| 5173 | test->ptr = ptr; |
| 5174 | |
| 5175 | while (ptr < end && *ptr != '?') |
| 5176 | ptr++; |
| 5177 | |
| 5178 | test->len = ptr - test->ptr; |
| 5179 | |
| 5180 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5181 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5182 | return 1; |
| 5183 | } |
| 5184 | |
| 5185 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5186 | |
| 5187 | /************************************************************************/ |
| 5188 | /* All supported keywords must be declared here. */ |
| 5189 | /************************************************************************/ |
| 5190 | |
| 5191 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5192 | static struct acl_kw_list acl_kws = {{ },{ |
| 5193 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5194 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5195 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5196 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5197 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5198 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5199 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5200 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5201 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5202 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5203 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5204 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5205 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5206 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5207 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5208 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5209 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5210 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5211 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5212 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5213 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5214 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5215 | |
| 5216 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5217 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5218 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5219 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5220 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5221 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5222 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5223 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5224 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5225 | |
| 5226 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5227 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5228 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5229 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5230 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5231 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5232 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5233 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5234 | { NULL, NULL, NULL, NULL }, |
| 5235 | |
| 5236 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5237 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5238 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5239 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5240 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5241 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5242 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5243 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5244 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5245 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5246 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5247 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5248 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5249 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5250 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5251 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5252 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5253 | |
| 5254 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5255 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5256 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5257 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5258 | { NULL, NULL, NULL, NULL }, |
| 5259 | #endif |
| 5260 | }}; |
| 5261 | |
| 5262 | |
| 5263 | __attribute__((constructor)) |
| 5264 | static void __http_protocol_init(void) |
| 5265 | { |
| 5266 | acl_register_keywords(&acl_kws); |
| 5267 | } |
| 5268 | |
| 5269 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5270 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5271 | * Local variables: |
| 5272 | * c-indent-level: 8 |
| 5273 | * c-basic-offset: 8 |
| 5274 | * End: |
| 5275 | */ |