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 | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 595 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 596 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 597 | * It is returned otherwise. |
| 598 | */ |
| 599 | static char * |
| 600 | http_get_path(struct http_txn *txn) |
| 601 | { |
| 602 | char *ptr, *end; |
| 603 | |
| 604 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 605 | end = ptr + txn->req.sl.rq.u_l; |
| 606 | |
| 607 | if (ptr >= end) |
| 608 | return NULL; |
| 609 | |
| 610 | /* RFC2616, par. 5.1.2 : |
| 611 | * Request-URI = "*" | absuri | abspath | authority |
| 612 | */ |
| 613 | |
| 614 | if (*ptr == '*') |
| 615 | return NULL; |
| 616 | |
| 617 | if (isalpha((unsigned char)*ptr)) { |
| 618 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 619 | ptr++; |
| 620 | while (ptr < end && |
| 621 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 622 | ptr++; |
| 623 | /* skip '://' */ |
| 624 | if (ptr == end || *ptr++ != ':') |
| 625 | return NULL; |
| 626 | if (ptr == end || *ptr++ != '/') |
| 627 | return NULL; |
| 628 | if (ptr == end || *ptr++ != '/') |
| 629 | return NULL; |
| 630 | } |
| 631 | /* skip [user[:passwd]@]host[:[port]] */ |
| 632 | |
| 633 | while (ptr < end && *ptr != '/') |
| 634 | ptr++; |
| 635 | |
| 636 | if (ptr == end) |
| 637 | return NULL; |
| 638 | |
| 639 | /* OK, we got the '/' ! */ |
| 640 | return ptr; |
| 641 | } |
| 642 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 643 | /* Processes the client and server jobs of a session task, then |
| 644 | * puts it back to the wait queue in a clean state, or |
| 645 | * cleans up its resources if it must be deleted. Returns |
| 646 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 647 | * infinity. |
| 648 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 649 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | { |
| 651 | struct session *s = t->context; |
| 652 | int fsm_resync = 0; |
| 653 | |
| 654 | do { |
| 655 | fsm_resync = 0; |
| 656 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 657 | fsm_resync |= process_cli(s); |
| 658 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 659 | fsm_resync |= process_srv(s); |
| 660 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 661 | } while (fsm_resync); |
| 662 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 663 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 664 | |
| 665 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 666 | session_process_counters(s); |
| 667 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 668 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 669 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 670 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 671 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 672 | tv_bound(&t->expire, &s->req->cex); |
| 673 | tv_bound(&t->expire, &s->rep->rex); |
| 674 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 675 | if (s->cli_state == CL_STHEADERS) |
| 676 | tv_bound(&t->expire, &s->txn.exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 677 | |
| 678 | /* restore t to its place in the task list */ |
| 679 | task_queue(t); |
| 680 | |
| 681 | #ifdef DEBUG_FULL |
| 682 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 683 | * that a task still has something to do and will provoke a quick loop. |
| 684 | */ |
Willy Tarreau | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 685 | if (tv_ms_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 686 | exit(100); |
| 687 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 688 | *next = t->expire; |
| 689 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 692 | s->fe->feconn--; |
| 693 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 694 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 695 | actconn--; |
| 696 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 697 | if (unlikely((global.mode & MODE_DEBUG) && |
| 698 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 699 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 700 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 701 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 702 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 703 | write(1, trash, len); |
| 704 | } |
| 705 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 706 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 707 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 708 | |
| 709 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 710 | if (s->logs.logwait && |
| 711 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 712 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 713 | if (s->fe->to_log & LW_REQ) |
| 714 | http_sess_log(s); |
| 715 | else |
| 716 | tcp_sess_log(s); |
| 717 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 718 | |
| 719 | /* the task MUST not be in the run queue anymore */ |
| 720 | task_delete(t); |
| 721 | session_free(s); |
| 722 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 723 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 727 | extern const char sess_term_cond[8]; |
| 728 | extern const char sess_fin_state[8]; |
| 729 | extern const char *monthname[12]; |
| 730 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 731 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 732 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 733 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 734 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 735 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 736 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 737 | /* |
| 738 | * send a log for the session when we have enough info about it. |
| 739 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 740 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 741 | static void http_sess_log(struct session *s) |
| 742 | { |
| 743 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 744 | struct proxy *fe = s->fe; |
| 745 | struct proxy *be = s->be; |
| 746 | struct proxy *prx_log; |
| 747 | struct http_txn *txn = &s->txn; |
| 748 | int tolog; |
| 749 | char *uri, *h; |
| 750 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 751 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 752 | static char tmpline[MAX_SYSLOG_LEN]; |
| 753 | int hdr; |
| 754 | |
| 755 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 756 | return; |
| 757 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 758 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 759 | if (s->cli_addr.ss_family == AF_INET) |
| 760 | inet_ntop(AF_INET, |
| 761 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 762 | pn, sizeof(pn)); |
| 763 | else |
| 764 | inet_ntop(AF_INET6, |
| 765 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 766 | pn, sizeof(pn)); |
| 767 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 768 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 769 | |
| 770 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 771 | tolog = fe->to_log; |
| 772 | |
| 773 | h = tmpline; |
| 774 | if (fe->to_log & LW_REQHDR && |
| 775 | txn->req.cap && |
| 776 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 777 | *(h++) = ' '; |
| 778 | *(h++) = '{'; |
| 779 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 780 | if (hdr) |
| 781 | *(h++) = '|'; |
| 782 | if (txn->req.cap[hdr] != NULL) |
| 783 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 784 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 785 | } |
| 786 | *(h++) = '}'; |
| 787 | } |
| 788 | |
| 789 | if (fe->to_log & LW_RSPHDR && |
| 790 | txn->rsp.cap && |
| 791 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 792 | *(h++) = ' '; |
| 793 | *(h++) = '{'; |
| 794 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 795 | if (hdr) |
| 796 | *(h++) = '|'; |
| 797 | if (txn->rsp.cap[hdr] != NULL) |
| 798 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 799 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 800 | } |
| 801 | *(h++) = '}'; |
| 802 | } |
| 803 | |
| 804 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 805 | *(h++) = ' '; |
| 806 | *(h++) = '"'; |
| 807 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 808 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 809 | '#', url_encode_map, uri); |
| 810 | *(h++) = '"'; |
| 811 | } |
| 812 | *h = '\0'; |
| 813 | |
| 814 | svid = (tolog & LW_SVID) ? |
| 815 | (s->data_source != DATA_SRC_STATS) ? |
| 816 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 817 | |
| 818 | send_log(prx_log, LOG_INFO, |
| 819 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 820 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 821 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 822 | pn, |
| 823 | (s->cli_addr.ss_family == AF_INET) ? |
| 824 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 825 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 826 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 827 | 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] | 828 | fe->id, be->id, svid, |
| 829 | s->logs.t_request, |
| 830 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 831 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 832 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 833 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 834 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 835 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 836 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 837 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 838 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 839 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 840 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 841 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 842 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 843 | (s->flags & SN_REDISP)?"+":"", |
| 844 | (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 845 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 846 | |
| 847 | s->logs.logwait = 0; |
| 848 | } |
| 849 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 850 | |
| 851 | /* |
| 852 | * Capture headers from message starting at <som> according to header list |
| 853 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 854 | */ |
| 855 | void capture_headers(char *som, struct hdr_idx *idx, |
| 856 | char **cap, struct cap_hdr *cap_hdr) |
| 857 | { |
| 858 | char *eol, *sol, *col, *sov; |
| 859 | int cur_idx; |
| 860 | struct cap_hdr *h; |
| 861 | int len; |
| 862 | |
| 863 | sol = som + hdr_idx_first_pos(idx); |
| 864 | cur_idx = hdr_idx_first_idx(idx); |
| 865 | |
| 866 | while (cur_idx) { |
| 867 | eol = sol + idx->v[cur_idx].len; |
| 868 | |
| 869 | col = sol; |
| 870 | while (col < eol && *col != ':') |
| 871 | col++; |
| 872 | |
| 873 | sov = col + 1; |
| 874 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 875 | sov++; |
| 876 | |
| 877 | for (h = cap_hdr; h; h = h->next) { |
| 878 | if ((h->namelen == col - sol) && |
| 879 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 880 | if (cap[h->index] == NULL) |
| 881 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 882 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 883 | |
| 884 | if (cap[h->index] == NULL) { |
| 885 | Alert("HTTP capture : out of memory.\n"); |
| 886 | continue; |
| 887 | } |
| 888 | |
| 889 | len = eol - sov; |
| 890 | if (len > h->len) |
| 891 | len = h->len; |
| 892 | |
| 893 | memcpy(cap[h->index], sov, len); |
| 894 | cap[h->index][len]=0; |
| 895 | } |
| 896 | } |
| 897 | sol = eol + idx->v[cur_idx].cr + 1; |
| 898 | cur_idx = idx->v[cur_idx].next; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 903 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 904 | */ |
| 905 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 906 | |
| 907 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 908 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 909 | */ |
| 910 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 911 | ptr++; \ |
| 912 | if (likely(ptr < end)) \ |
| 913 | goto good; \ |
| 914 | else { \ |
| 915 | state = (st); \ |
| 916 | goto http_msg_ood; \ |
| 917 | } \ |
| 918 | } while (0) |
| 919 | |
| 920 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 922 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 923 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 924 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 925 | * will give undefined results. |
| 926 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 927 | * and that msg->sol points to the beginning of the response. |
| 928 | * If a complete line is found (which implies that at least one CR or LF is |
| 929 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 930 | * returned indicating an incomplete line (which does not mean that parts have |
| 931 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 932 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 933 | * upon next call. |
| 934 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 935 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 936 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 937 | * 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] | 938 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 939 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 940 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 941 | unsigned int state, const char *ptr, const char *end, |
| 942 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 943 | { |
| 944 | __label__ |
| 945 | http_msg_rpver, |
| 946 | http_msg_rpver_sp, |
| 947 | http_msg_rpcode, |
| 948 | http_msg_rpcode_sp, |
| 949 | http_msg_rpreason, |
| 950 | http_msg_rpline_eol, |
| 951 | http_msg_ood, /* out of data */ |
| 952 | http_msg_invalid; |
| 953 | |
| 954 | switch (state) { |
| 955 | http_msg_rpver: |
| 956 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 957 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 959 | |
| 960 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 961 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 963 | } |
| 964 | goto http_msg_invalid; |
| 965 | |
| 966 | http_msg_rpver_sp: |
| 967 | case HTTP_MSG_RPVER_SP: |
| 968 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 969 | msg->sl.st.c = ptr - msg_buf; |
| 970 | goto http_msg_rpcode; |
| 971 | } |
| 972 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 973 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 974 | /* so it's a CR/LF, this is invalid */ |
| 975 | goto http_msg_invalid; |
| 976 | |
| 977 | http_msg_rpcode: |
| 978 | case HTTP_MSG_RPCODE: |
| 979 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 980 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 981 | |
| 982 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 983 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 984 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 985 | } |
| 986 | |
| 987 | /* so it's a CR/LF, so there is no reason phrase */ |
| 988 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 989 | http_msg_rsp_reason: |
| 990 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 991 | msg->sl.st.r = ptr - msg_buf; |
| 992 | msg->sl.st.r_l = 0; |
| 993 | goto http_msg_rpline_eol; |
| 994 | |
| 995 | http_msg_rpcode_sp: |
| 996 | case HTTP_MSG_RPCODE_SP: |
| 997 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 998 | msg->sl.st.r = ptr - msg_buf; |
| 999 | goto http_msg_rpreason; |
| 1000 | } |
| 1001 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1002 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1003 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1004 | goto http_msg_rsp_reason; |
| 1005 | |
| 1006 | http_msg_rpreason: |
| 1007 | case HTTP_MSG_RPREASON: |
| 1008 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1009 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1010 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1011 | http_msg_rpline_eol: |
| 1012 | /* We have seen the end of line. Note that we do not |
| 1013 | * necessarily have the \n yet, but at least we know that we |
| 1014 | * have EITHER \r OR \n, otherwise the response would not be |
| 1015 | * complete. We can then record the response length and return |
| 1016 | * to the caller which will be able to register it. |
| 1017 | */ |
| 1018 | msg->sl.st.l = ptr - msg->sol; |
| 1019 | return ptr; |
| 1020 | |
| 1021 | #ifdef DEBUG_FULL |
| 1022 | default: |
| 1023 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1024 | exit(1); |
| 1025 | #endif |
| 1026 | } |
| 1027 | |
| 1028 | http_msg_ood: |
| 1029 | /* out of data */ |
| 1030 | if (ret_state) |
| 1031 | *ret_state = state; |
| 1032 | if (ret_ptr) |
| 1033 | *ret_ptr = (char *)ptr; |
| 1034 | return NULL; |
| 1035 | |
| 1036 | http_msg_invalid: |
| 1037 | /* invalid message */ |
| 1038 | if (ret_state) |
| 1039 | *ret_state = HTTP_MSG_ERROR; |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1045 | * This function parses a request line between <ptr> and <end>, starting with |
| 1046 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1047 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1048 | * will give undefined results. |
| 1049 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1050 | * and that msg->sol points to the beginning of the request. |
| 1051 | * If a complete line is found (which implies that at least one CR or LF is |
| 1052 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1053 | * returned indicating an incomplete line (which does not mean that parts have |
| 1054 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1055 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1056 | * upon next call. |
| 1057 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1058 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1059 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1060 | * 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] | 1061 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1062 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1063 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1064 | unsigned int state, const char *ptr, const char *end, |
| 1065 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1066 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | __label__ |
| 1068 | http_msg_rqmeth, |
| 1069 | http_msg_rqmeth_sp, |
| 1070 | http_msg_rquri, |
| 1071 | http_msg_rquri_sp, |
| 1072 | http_msg_rqver, |
| 1073 | http_msg_rqline_eol, |
| 1074 | http_msg_ood, /* out of data */ |
| 1075 | http_msg_invalid; |
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 | switch (state) { |
| 1078 | http_msg_rqmeth: |
| 1079 | case HTTP_MSG_RQMETH: |
| 1080 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1081 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1084 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1086 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1087 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1089 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1090 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | http_msg_req09_uri: |
| 1092 | msg->sl.rq.u = ptr - msg_buf; |
| 1093 | http_msg_req09_uri_e: |
| 1094 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1095 | http_msg_req09_ver: |
| 1096 | msg->sl.rq.v = ptr - msg_buf; |
| 1097 | msg->sl.rq.v_l = 0; |
| 1098 | goto http_msg_rqline_eol; |
| 1099 | } |
| 1100 | goto http_msg_invalid; |
| 1101 | |
| 1102 | http_msg_rqmeth_sp: |
| 1103 | case HTTP_MSG_RQMETH_SP: |
| 1104 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1105 | msg->sl.rq.u = ptr - msg_buf; |
| 1106 | goto http_msg_rquri; |
| 1107 | } |
| 1108 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1109 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1110 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1111 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | http_msg_rquri: |
| 1114 | case HTTP_MSG_RQURI: |
| 1115 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1116 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1117 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1118 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1119 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1120 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1121 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1122 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1123 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1124 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1125 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1126 | http_msg_rquri_sp: |
| 1127 | case HTTP_MSG_RQURI_SP: |
| 1128 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1129 | msg->sl.rq.v = ptr - msg_buf; |
| 1130 | goto http_msg_rqver; |
| 1131 | } |
| 1132 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1134 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1135 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1136 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1137 | http_msg_rqver: |
| 1138 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1139 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1140 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1141 | |
| 1142 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1143 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1144 | http_msg_rqline_eol: |
| 1145 | /* We have seen the end of line. Note that we do not |
| 1146 | * necessarily have the \n yet, but at least we know that we |
| 1147 | * have EITHER \r OR \n, otherwise the request would not be |
| 1148 | * complete. We can then record the request length and return |
| 1149 | * to the caller which will be able to register it. |
| 1150 | */ |
| 1151 | msg->sl.rq.l = ptr - msg->sol; |
| 1152 | return ptr; |
| 1153 | } |
| 1154 | |
| 1155 | /* neither an HTTP_VER token nor a CRLF */ |
| 1156 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1157 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1158 | #ifdef DEBUG_FULL |
| 1159 | default: |
| 1160 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1161 | exit(1); |
| 1162 | #endif |
| 1163 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1164 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1165 | http_msg_ood: |
| 1166 | /* out of data */ |
| 1167 | if (ret_state) |
| 1168 | *ret_state = state; |
| 1169 | if (ret_ptr) |
| 1170 | *ret_ptr = (char *)ptr; |
| 1171 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1172 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1173 | http_msg_invalid: |
| 1174 | /* invalid message */ |
| 1175 | if (ret_state) |
| 1176 | *ret_state = HTTP_MSG_ERROR; |
| 1177 | return NULL; |
| 1178 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1179 | |
| 1180 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | /* |
| 1182 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1183 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1184 | * when data are missing and recalled at the exact same location with no |
| 1185 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1186 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1187 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1188 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1189 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1190 | { |
| 1191 | __label__ |
| 1192 | http_msg_rqbefore, |
| 1193 | http_msg_rqbefore_cr, |
| 1194 | http_msg_rqmeth, |
| 1195 | http_msg_rqline_end, |
| 1196 | http_msg_hdr_first, |
| 1197 | http_msg_hdr_name, |
| 1198 | http_msg_hdr_l1_sp, |
| 1199 | http_msg_hdr_l1_lf, |
| 1200 | http_msg_hdr_l1_lws, |
| 1201 | http_msg_hdr_val, |
| 1202 | http_msg_hdr_l2_lf, |
| 1203 | http_msg_hdr_l2_lws, |
| 1204 | http_msg_complete_header, |
| 1205 | http_msg_last_lf, |
| 1206 | http_msg_ood, /* out of data */ |
| 1207 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1208 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1209 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1210 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1211 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1212 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1213 | ptr = buf->lr; |
| 1214 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1215 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1216 | if (unlikely(ptr >= end)) |
| 1217 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1218 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1219 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1220 | /* |
| 1221 | * First, states that are specific to the response only. |
| 1222 | * We check them first so that request and headers are |
| 1223 | * closer to each other (accessed more often). |
| 1224 | */ |
| 1225 | http_msg_rpbefore: |
| 1226 | case HTTP_MSG_RPBEFORE: |
| 1227 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1228 | if (likely(ptr == buf->data)) { |
| 1229 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1230 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1231 | } else { |
| 1232 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1233 | /* only skip empty leading lines, don't remove them */ |
| 1234 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1235 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1236 | #else |
| 1237 | /* Remove empty leading lines, as recommended by |
| 1238 | * RFC2616. This takes a lot of time because we |
| 1239 | * must move all the buffer backwards, but this |
| 1240 | * is rarely needed. The method above will be |
| 1241 | * cleaner when we'll be able to start sending |
| 1242 | * the request from any place in the buffer. |
| 1243 | */ |
| 1244 | buf->lr = ptr; |
| 1245 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1246 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1247 | msg->sol = buf->data; |
| 1248 | ptr = buf->data; |
| 1249 | end = buf->r; |
| 1250 | #endif |
| 1251 | } |
| 1252 | hdr_idx_init(idx); |
| 1253 | state = HTTP_MSG_RPVER; |
| 1254 | goto http_msg_rpver; |
| 1255 | } |
| 1256 | |
| 1257 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1258 | goto http_msg_invalid; |
| 1259 | |
| 1260 | if (unlikely(*ptr == '\n')) |
| 1261 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1262 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1263 | /* stop here */ |
| 1264 | |
| 1265 | http_msg_rpbefore_cr: |
| 1266 | case HTTP_MSG_RPBEFORE_CR: |
| 1267 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1268 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1269 | /* stop here */ |
| 1270 | |
| 1271 | http_msg_rpver: |
| 1272 | case HTTP_MSG_RPVER: |
| 1273 | case HTTP_MSG_RPVER_SP: |
| 1274 | case HTTP_MSG_RPCODE: |
| 1275 | case HTTP_MSG_RPCODE_SP: |
| 1276 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1277 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1278 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | if (unlikely(!ptr)) |
| 1280 | return; |
| 1281 | |
| 1282 | /* we have a full response and we know that we have either a CR |
| 1283 | * or an LF at <ptr>. |
| 1284 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1285 | //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] | 1286 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1287 | |
| 1288 | msg->sol = ptr; |
| 1289 | if (likely(*ptr == '\r')) |
| 1290 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1291 | goto http_msg_rpline_end; |
| 1292 | |
| 1293 | http_msg_rpline_end: |
| 1294 | case HTTP_MSG_RPLINE_END: |
| 1295 | /* msg->sol must point to the first of CR or LF. */ |
| 1296 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1297 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1298 | /* stop here */ |
| 1299 | |
| 1300 | /* |
| 1301 | * Second, states that are specific to the request only |
| 1302 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1303 | http_msg_rqbefore: |
| 1304 | case HTTP_MSG_RQBEFORE: |
| 1305 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1306 | if (likely(ptr == buf->data)) { |
| 1307 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1308 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1309 | } else { |
| 1310 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1311 | /* only skip empty leading lines, don't remove them */ |
| 1312 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1313 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1314 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | /* Remove empty leading lines, as recommended by |
| 1316 | * RFC2616. This takes a lot of time because we |
| 1317 | * must move all the buffer backwards, but this |
| 1318 | * is rarely needed. The method above will be |
| 1319 | * cleaner when we'll be able to start sending |
| 1320 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1321 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1322 | buf->lr = ptr; |
| 1323 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1324 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | msg->sol = buf->data; |
| 1326 | ptr = buf->data; |
| 1327 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1328 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1329 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1330 | /* we will need this when keep-alive will be supported |
| 1331 | hdr_idx_init(idx); |
| 1332 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | state = HTTP_MSG_RQMETH; |
| 1334 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1338 | goto http_msg_invalid; |
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 | if (unlikely(*ptr == '\n')) |
| 1341 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1342 | 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] | 1343 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1344 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1345 | http_msg_rqbefore_cr: |
| 1346 | case HTTP_MSG_RQBEFORE_CR: |
| 1347 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1348 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | http_msg_rqmeth: |
| 1352 | case HTTP_MSG_RQMETH: |
| 1353 | case HTTP_MSG_RQMETH_SP: |
| 1354 | case HTTP_MSG_RQURI: |
| 1355 | case HTTP_MSG_RQURI_SP: |
| 1356 | case HTTP_MSG_RQVER: |
| 1357 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1358 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1359 | if (unlikely(!ptr)) |
| 1360 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | /* we have a full request and we know that we have either a CR |
| 1363 | * or an LF at <ptr>. |
| 1364 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1365 | //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] | 1366 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | msg->sol = ptr; |
| 1369 | if (likely(*ptr == '\r')) |
| 1370 | 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] | 1371 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1372 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1373 | http_msg_rqline_end: |
| 1374 | case HTTP_MSG_RQLINE_END: |
| 1375 | /* check for HTTP/0.9 request : no version information available. |
| 1376 | * msg->sol must point to the first of CR or LF. |
| 1377 | */ |
| 1378 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1379 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1382 | 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] | 1383 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1384 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1385 | /* |
| 1386 | * Common states below |
| 1387 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | http_msg_hdr_first: |
| 1389 | case HTTP_MSG_HDR_FIRST: |
| 1390 | msg->sol = ptr; |
| 1391 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1392 | goto http_msg_hdr_name; |
| 1393 | } |
| 1394 | |
| 1395 | if (likely(*ptr == '\r')) |
| 1396 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1397 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1399 | http_msg_hdr_name: |
| 1400 | case HTTP_MSG_HDR_NAME: |
| 1401 | /* assumes msg->sol points to the first char */ |
| 1402 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1403 | 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] | 1404 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | if (likely(*ptr == ':')) { |
| 1406 | msg->col = ptr - buf->data; |
| 1407 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1408 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1409 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | http_msg_hdr_l1_sp: |
| 1413 | case HTTP_MSG_HDR_L1_SP: |
| 1414 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1415 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1416 | 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] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | /* header value can be basically anything except CR/LF */ |
| 1419 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1422 | goto http_msg_hdr_val; |
| 1423 | } |
| 1424 | |
| 1425 | if (likely(*ptr == '\r')) |
| 1426 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1427 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | http_msg_hdr_l1_lf: |
| 1430 | case HTTP_MSG_HDR_L1_LF: |
| 1431 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1432 | 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] | 1433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | http_msg_hdr_l1_lws: |
| 1435 | case HTTP_MSG_HDR_L1_LWS: |
| 1436 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1437 | /* replace HT,CR,LF with spaces */ |
| 1438 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1439 | buf->data[msg->sov] = ' '; |
| 1440 | goto http_msg_hdr_l1_sp; |
| 1441 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1442 | /* we had a header consisting only in spaces ! */ |
| 1443 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | goto http_msg_complete_header; |
| 1445 | |
| 1446 | http_msg_hdr_val: |
| 1447 | case HTTP_MSG_HDR_VAL: |
| 1448 | /* assumes msg->sol points to the first char, msg->col to the |
| 1449 | * colon, and msg->sov points to the first character of the |
| 1450 | * value. |
| 1451 | */ |
| 1452 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1453 | 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] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | msg->eol = ptr; |
| 1456 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1457 | * real header end in case it ends with lots of LWS, but is this |
| 1458 | * really needed ? |
| 1459 | */ |
| 1460 | if (likely(*ptr == '\r')) |
| 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1462 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | http_msg_hdr_l2_lf: |
| 1465 | case HTTP_MSG_HDR_L2_LF: |
| 1466 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1467 | 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] | 1468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | http_msg_hdr_l2_lws: |
| 1470 | case HTTP_MSG_HDR_L2_LWS: |
| 1471 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1472 | /* LWS: replace HT,CR,LF with spaces */ |
| 1473 | for (; msg->eol < ptr; msg->eol++) |
| 1474 | *msg->eol = ' '; |
| 1475 | goto http_msg_hdr_val; |
| 1476 | } |
| 1477 | http_msg_complete_header: |
| 1478 | /* |
| 1479 | * It was a new header, so the last one is finished. |
| 1480 | * Assumes msg->sol points to the first char, msg->col to the |
| 1481 | * colon, msg->sov points to the first character of the value |
| 1482 | * and msg->eol to the first CR or LF so we know how the line |
| 1483 | * ends. We insert last header into the index. |
| 1484 | */ |
| 1485 | /* |
| 1486 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1487 | write(2, msg->sol, msg->eol-msg->sol); |
| 1488 | fprintf(stderr,"\n"); |
| 1489 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1492 | idx, idx->tail) < 0)) |
| 1493 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1494 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | msg->sol = ptr; |
| 1496 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1497 | goto http_msg_hdr_name; |
| 1498 | } |
| 1499 | |
| 1500 | if (likely(*ptr == '\r')) |
| 1501 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1502 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1504 | http_msg_last_lf: |
| 1505 | case HTTP_MSG_LAST_LF: |
| 1506 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1507 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1508 | ptr++; |
| 1509 | buf->lr = ptr; |
| 1510 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1511 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | return; |
| 1513 | #ifdef DEBUG_FULL |
| 1514 | default: |
| 1515 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1516 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1517 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | } |
| 1519 | http_msg_ood: |
| 1520 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1521 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | buf->lr = ptr; |
| 1523 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | http_msg_invalid: |
| 1526 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1527 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | return; |
| 1529 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | /* |
| 1532 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1533 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1534 | * 0 else. |
| 1535 | */ |
| 1536 | int process_cli(struct session *t) |
| 1537 | { |
| 1538 | int s = t->srv_state; |
| 1539 | int c = t->cli_state; |
| 1540 | struct buffer *req = t->req; |
| 1541 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1543 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1544 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1545 | 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] | 1546 | req->rex.tv_sec, req->rex.tv_usec, |
| 1547 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1548 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | if (c == CL_STHEADERS) { |
| 1550 | /* |
| 1551 | * Now parse the partial (or complete) lines. |
| 1552 | * We will check the request syntax, and also join multi-line |
| 1553 | * headers. An index of all the lines will be elaborated while |
| 1554 | * parsing. |
| 1555 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1556 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1559 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | * req->data + req->eoh = end of processed headers / start of current one |
| 1561 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1562 | * req->lr = first non-visited byte |
| 1563 | * req->r = end of data |
| 1564 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1567 | struct http_txn *txn = &t->txn; |
| 1568 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1572 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | /* 1: we might have to print this header in debug mode */ |
| 1575 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1576 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1580 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | eol = sol + msg->sl.rq.l; |
| 1582 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1583 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1584 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1585 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1588 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1590 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1591 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | } |
| 1593 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1595 | |
| 1596 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1598 | * If not so, we check the FD and buffer states before leaving. |
| 1599 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1601 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1602 | * |
| 1603 | */ |
| 1604 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1605 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | /* |
| 1607 | * First, let's catch bad requests. |
| 1608 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1609 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1611 | |
| 1612 | /* 1: Since we are in header mode, if there's no space |
| 1613 | * left for headers, we won't be able to free more |
| 1614 | * later, so the session will never terminate. We |
| 1615 | * must terminate it now. |
| 1616 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1617 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1618 | /* FIXME: check if URI is set and return Status |
| 1619 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1620 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1621 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1626 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1627 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1628 | fd_delete(t->cli_fd); |
| 1629 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1630 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1631 | if (!(t->flags & SN_ERR_MASK)) |
| 1632 | t->flags |= SN_ERR_CLICL; |
| 1633 | if (!(t->flags & SN_FINST_MASK)) |
| 1634 | t->flags |= SN_FINST_R; |
| 1635 | return 1; |
| 1636 | } |
| 1637 | |
| 1638 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 1639 | else if (unlikely(tv_isle(&req->rex, &now) || |
| 1640 | tv_isle(&txn->exp, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1641 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1642 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1643 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1644 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1645 | if (!(t->flags & SN_ERR_MASK)) |
| 1646 | t->flags |= SN_ERR_CLITO; |
| 1647 | if (!(t->flags & SN_FINST_MASK)) |
| 1648 | t->flags |= SN_FINST_R; |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
| 1652 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1653 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1654 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1655 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1656 | * req->l == rlim-data |
| 1657 | */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1658 | if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1659 | tv_eternity(&req->rex); |
| 1660 | } |
| 1661 | return t->cli_state != CL_STHEADERS; |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | /**************************************************************** |
| 1666 | * More interesting part now : we know that we have a complete * |
| 1667 | * request which at least looks like HTTP. We have an indicator * |
| 1668 | * of each header's length, so we can parse them quickly. * |
| 1669 | ****************************************************************/ |
| 1670 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1671 | /* ensure we keep this pointer to the beginning of the message */ |
| 1672 | msg->sol = req->data + msg->som; |
| 1673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | /* |
| 1675 | * 1: identify the method |
| 1676 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1677 | 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] | 1678 | |
| 1679 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1680 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1681 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1683 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1685 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1686 | !memcmp(&req->data[msg->sl.rq.u], |
| 1687 | t->fe->monitor_uri, |
| 1688 | t->fe->monitor_uri_len))) { |
| 1689 | /* |
| 1690 | * We have found the monitor URI |
| 1691 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1692 | struct acl_cond *cond; |
| 1693 | cur_proxy = t->fe; |
| 1694 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1695 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1696 | |
| 1697 | /* Check if we want to fail this monitor request or not */ |
| 1698 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1699 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1700 | if (cond->pol == ACL_COND_UNLESS) |
| 1701 | ret = !ret; |
| 1702 | |
| 1703 | if (ret) { |
| 1704 | /* we fail this request, let's return 503 service unavail */ |
| 1705 | txn->status = 503; |
| 1706 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1707 | goto return_prx_cond; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1712 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | client_retnclose(t, &http_200_chunk); |
| 1714 | goto return_prx_cond; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1719 | * Note: we cannot log anymore if the request has been |
| 1720 | * classified as invalid. |
| 1721 | */ |
| 1722 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1723 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1724 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | int urilen = msg->sl.rq.l; |
| 1726 | |
| 1727 | if (urilen >= REQURI_LEN) |
| 1728 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1729 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1730 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | |
| 1732 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1733 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | } else { |
| 1735 | Alert("HTTP logging : out of memory.\n"); |
| 1736 | } |
| 1737 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1738 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1741 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1742 | int delta; |
| 1743 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1744 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | cur_end = msg->sol + msg->sl.rq.l; |
| 1746 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1747 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | if (msg->sl.rq.u_l == 0) { |
| 1749 | /* if no URI was set, add "/" */ |
| 1750 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1751 | cur_end += delta; |
| 1752 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1753 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | /* add HTTP version */ |
| 1755 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1756 | msg->eoh += delta; |
| 1757 | cur_end += delta; |
| 1758 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1759 | HTTP_MSG_RQMETH, |
| 1760 | msg->sol, cur_end + 1, |
| 1761 | NULL, NULL); |
| 1762 | if (unlikely(!cur_end)) |
| 1763 | goto return_bad_req; |
| 1764 | |
| 1765 | /* we have a full HTTP/1.0 request now and we know that |
| 1766 | * we have either a CR or an LF at <ptr>. |
| 1767 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1768 | 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] | 1769 | } |
| 1770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | |
| 1772 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1773 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1774 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1775 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1776 | |
| 1777 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1779 | * As opposed to version 1.2, now they will be evaluated in the |
| 1780 | * filters order and not in the header order. This means that |
| 1781 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1782 | * |
| 1783 | * We can now check whether we want to switch to another |
| 1784 | * backend, in which case we will re-check the backend's |
| 1785 | * filters and various options. In order to support 3-level |
| 1786 | * switching, here's how we should proceed : |
| 1787 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1788 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1789 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1790 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1791 | * There cannot be any switch from there, so ->be cannot be |
| 1792 | * changed anymore. |
| 1793 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1794 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1795 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1796 | * The response path will be able to apply either ->be, or |
| 1797 | * ->be then ->fe filters in order to match the reverse of |
| 1798 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1799 | */ |
| 1800 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1801 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1802 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1803 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1804 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1805 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1806 | /* first check whether we have some ACLs set to block this request */ |
| 1807 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1808 | 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] | 1809 | if (cond->pol == ACL_COND_UNLESS) |
| 1810 | ret = !ret; |
| 1811 | |
| 1812 | if (ret) { |
| 1813 | txn->status = 403; |
| 1814 | /* let's log the request time */ |
| 1815 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1816 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1817 | goto return_prx_cond; |
| 1818 | } |
| 1819 | } |
| 1820 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1821 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1822 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1824 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1825 | } |
| 1826 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1827 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1828 | /* to ensure correct connection accounting on |
| 1829 | * the backend, we count the connection for the |
| 1830 | * one managing the queue. |
| 1831 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1832 | t->be->beconn++; |
| 1833 | if (t->be->beconn > t->be->beconn_max) |
| 1834 | t->be->beconn_max = t->be->beconn; |
| 1835 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1836 | t->flags |= SN_BE_ASSIGNED; |
| 1837 | } |
| 1838 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1839 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1840 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1841 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1842 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1843 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1844 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1845 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1846 | goto return_prx_cond; |
| 1847 | } |
| 1848 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 1850 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | !(t->flags & SN_CONN_CLOSED)) { |
| 1852 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1853 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1855 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1856 | 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] | 1857 | old_idx = 0; |
| 1858 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1859 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1860 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1861 | cur_ptr = cur_next; |
| 1862 | cur_end = cur_ptr + cur_hdr->len; |
| 1863 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1864 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1865 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1866 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | /* 3 possibilities : |
| 1868 | * - we have already set Connection: close, |
| 1869 | * so we remove this line. |
| 1870 | * - we have not yet set Connection: close, |
| 1871 | * but this line indicates close. We leave |
| 1872 | * it untouched and set the flag. |
| 1873 | * - we have not yet set Connection: close, |
| 1874 | * and this line indicates non-close. We |
| 1875 | * replace it. |
| 1876 | */ |
| 1877 | if (t->flags & SN_CONN_CLOSED) { |
| 1878 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1879 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1880 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1881 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1882 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | cur_hdr->len = 0; |
| 1884 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1885 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1886 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1887 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1888 | cur_next += delta; |
| 1889 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1890 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1891 | } |
| 1892 | t->flags |= SN_CONN_CLOSED; |
| 1893 | } |
| 1894 | } |
| 1895 | old_idx = cur_idx; |
| 1896 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1897 | } |
| 1898 | /* add request headers from the rule sets in the same order */ |
| 1899 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1900 | if (unlikely(http_header_add_tail(req, |
| 1901 | &txn->req, |
| 1902 | &txn->hdr_idx, |
| 1903 | rule_set->req_add[cur_idx])) < 0) |
| 1904 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1905 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1906 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1908 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1909 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1910 | /* we have to check the URI and auth for this request */ |
| 1911 | if (stats_check_uri_auth(t, rule_set)) |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1915 | /* now check whether we have some switching rules for this request */ |
| 1916 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1917 | struct switching_rule *rule; |
| 1918 | |
| 1919 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1920 | int ret; |
| 1921 | |
| 1922 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1923 | if (cond->pol == ACL_COND_UNLESS) |
| 1924 | ret = !ret; |
| 1925 | |
| 1926 | if (ret) { |
| 1927 | t->be = rule->be.backend; |
| 1928 | t->be->beconn++; |
| 1929 | if (t->be->beconn > t->be->beconn_max) |
| 1930 | t->be->beconn_max = t->be->beconn; |
| 1931 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1932 | |
| 1933 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1934 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 1935 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1936 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1937 | t->flags |= SN_BE_ASSIGNED; |
| 1938 | break; |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1943 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1944 | /* No backend was set, but there was a default |
| 1945 | * backend set in the frontend, so we use it and |
| 1946 | * loop again. |
| 1947 | */ |
| 1948 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1949 | t->be->beconn++; |
| 1950 | if (t->be->beconn > t->be->beconn_max) |
| 1951 | t->be->beconn_max = t->be->beconn; |
| 1952 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1955 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 1956 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1957 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1958 | t->flags |= SN_BE_ASSIGNED; |
| 1959 | } |
| 1960 | } 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] | 1961 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1962 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1963 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1964 | /* To ensure correct connection accounting on |
| 1965 | * the backend, we count the connection for the |
| 1966 | * one managing the queue. |
| 1967 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1968 | t->be->beconn++; |
| 1969 | if (t->be->beconn > t->be->beconn_max) |
| 1970 | t->be->beconn_max = t->be->beconn; |
| 1971 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1972 | t->flags |= SN_BE_ASSIGNED; |
| 1973 | } |
| 1974 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1975 | /* |
| 1976 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1977 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1978 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1979 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1980 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1981 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1982 | /* |
| 1983 | * If HTTP PROXY is set we simply get remote server address |
| 1984 | * parsing incoming request. |
| 1985 | */ |
| 1986 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 1987 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 1988 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1989 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1990 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1991 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1992 | * so let's do the same now. |
| 1993 | */ |
| 1994 | |
| 1995 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1996 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1997 | 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] | 1998 | } |
| 1999 | |
| 2000 | |
| 2001 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2002 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2003 | * Note that doing so might move headers in the request, but |
| 2004 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2005 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2006 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2007 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2008 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2009 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2010 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2011 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2012 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2013 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2014 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2015 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2016 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2017 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2018 | /* Add an X-Forwarded-For header unless the source IP is |
| 2019 | * in the 'except' network range. |
| 2020 | */ |
| 2021 | if ((!t->fe->except_mask.s_addr || |
| 2022 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2023 | != t->fe->except_net.s_addr) && |
| 2024 | (!t->be->except_mask.s_addr || |
| 2025 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2026 | != t->be->except_net.s_addr)) { |
| 2027 | int len; |
| 2028 | unsigned char *pn; |
| 2029 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2030 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2031 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 2032 | pn[0], pn[1], pn[2], pn[3]); |
| 2033 | |
| 2034 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2035 | &txn->hdr_idx, trash, len)) < 0) |
| 2036 | goto return_bad_req; |
| 2037 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2038 | } |
| 2039 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2040 | /* FIXME: for the sake of completeness, we should also support |
| 2041 | * 'except' here, although it is mostly useless in this case. |
| 2042 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2043 | int len; |
| 2044 | char pn[INET6_ADDRSTRLEN]; |
| 2045 | inet_ntop(AF_INET6, |
| 2046 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2047 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2048 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 2049 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2050 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2051 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2052 | } |
| 2053 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2054 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2055 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2056 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2057 | * 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] | 2058 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2059 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2060 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2061 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2062 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2063 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2064 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2065 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2066 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2067 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2068 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2069 | /************************************************************* |
| 2070 | * OK, that's finished for the headers. We have done what we * |
| 2071 | * could. Let's switch to the DATA state. * |
| 2072 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2073 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2074 | t->cli_state = CL_STDATA; |
| 2075 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2076 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2077 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2078 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2079 | if (!tv_isset(&t->fe->timeout.client) || |
| 2080 | (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2081 | /* If the client has no timeout, or if the server is not ready yet, |
| 2082 | * and we know for sure that it can expire, then it's cleaner to |
| 2083 | * disable the timeout on the client side so that too low values |
| 2084 | * cannot make the sessions abort too early. |
| 2085 | * |
| 2086 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2087 | * when it switches its state, otherwise a client can stay connected |
| 2088 | * indefinitely. This now seems to be OK. |
| 2089 | */ |
| 2090 | tv_eternity(&req->rex); |
| 2091 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2092 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2093 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2094 | * which may be the same as the connect timeout if unspecified. |
| 2095 | * If unset, then set it to zero because we really want it to |
| 2096 | * eventually expire. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2097 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2098 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2099 | t->req->l = 0; |
| 2100 | /* flush the request so that we can drop the connection early |
| 2101 | * if the client closes first. |
| 2102 | */ |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2103 | if (!tv_add_ifset(&req->cex, &now, &t->be->timeout.tarpit)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2104 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2105 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2106 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2107 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2108 | goto process_data; |
| 2109 | |
| 2110 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2111 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2112 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2113 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2114 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2115 | return_prx_cond: |
| 2116 | if (!(t->flags & SN_ERR_MASK)) |
| 2117 | t->flags |= SN_ERR_PRXCOND; |
| 2118 | if (!(t->flags & SN_FINST_MASK)) |
| 2119 | t->flags |= SN_FINST_R; |
| 2120 | return 1; |
| 2121 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2122 | } |
| 2123 | else if (c == CL_STDATA) { |
| 2124 | process_data: |
| 2125 | /* FIXME: this error handling is partly buggy because we always report |
| 2126 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2127 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2128 | * we're waiting for the server to connect. |
| 2129 | */ |
| 2130 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2131 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2132 | buffer_shutr(req); |
| 2133 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2134 | fd_delete(t->cli_fd); |
| 2135 | t->cli_state = CL_STCLOSE; |
| 2136 | if (!(t->flags & SN_ERR_MASK)) |
| 2137 | t->flags |= SN_ERR_CLICL; |
| 2138 | if (!(t->flags & SN_FINST_MASK)) { |
| 2139 | if (t->pend_pos) |
| 2140 | t->flags |= SN_FINST_Q; |
| 2141 | else if (s == SV_STCONN) |
| 2142 | t->flags |= SN_FINST_C; |
| 2143 | else |
| 2144 | t->flags |= SN_FINST_D; |
| 2145 | } |
| 2146 | return 1; |
| 2147 | } |
| 2148 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2149 | 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] | 2150 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2151 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2152 | t->cli_state = CL_STSHUTR; |
| 2153 | return 1; |
| 2154 | } |
| 2155 | /* last server read and buffer empty */ |
| 2156 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2157 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2158 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2159 | shutdown(t->cli_fd, SHUT_WR); |
| 2160 | /* We must ensure that the read part is still alive when switching |
| 2161 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2162 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2163 | tv_add_ifset(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2164 | t->cli_state = CL_STSHUTW; |
| 2165 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2166 | return 1; |
| 2167 | } |
| 2168 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2169 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2170 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2171 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2172 | t->cli_state = CL_STSHUTR; |
| 2173 | if (!(t->flags & SN_ERR_MASK)) |
| 2174 | t->flags |= SN_ERR_CLITO; |
| 2175 | if (!(t->flags & SN_FINST_MASK)) { |
| 2176 | if (t->pend_pos) |
| 2177 | t->flags |= SN_FINST_Q; |
| 2178 | else if (s == SV_STCONN) |
| 2179 | t->flags |= SN_FINST_C; |
| 2180 | else |
| 2181 | t->flags |= SN_FINST_D; |
| 2182 | } |
| 2183 | return 1; |
| 2184 | } |
| 2185 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2186 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2187 | EV_FD_CLR(t->cli_fd, DIR_WR); |
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 | shutdown(t->cli_fd, SHUT_WR); |
| 2190 | /* We must ensure that the read part is still alive when switching |
| 2191 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2192 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2193 | tv_add_ifset(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2194 | |
| 2195 | t->cli_state = CL_STSHUTW; |
| 2196 | if (!(t->flags & SN_ERR_MASK)) |
| 2197 | t->flags |= SN_ERR_CLITO; |
| 2198 | if (!(t->flags & SN_FINST_MASK)) { |
| 2199 | if (t->pend_pos) |
| 2200 | t->flags |= SN_FINST_Q; |
| 2201 | else if (s == SV_STCONN) |
| 2202 | t->flags |= SN_FINST_C; |
| 2203 | else |
| 2204 | t->flags |= SN_FINST_D; |
| 2205 | } |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | if (req->l >= req->rlim - req->data) { |
| 2210 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2211 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2212 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2213 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2214 | } |
| 2215 | } else { |
| 2216 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2217 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2218 | if (!tv_isset(&t->fe->timeout.client) || |
| 2219 | (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2220 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2221 | * know for sure that it can expire, then it's cleaner to disable the |
| 2222 | * timeout on the client side so that too low values cannot make the |
| 2223 | * sessions abort too early. |
| 2224 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2225 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2226 | else |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2227 | tv_add(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | if ((rep->l == 0) || |
| 2232 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2233 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2234 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2235 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2236 | } |
| 2237 | } else { |
| 2238 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2239 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2240 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2241 | if (tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2242 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2243 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2244 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2245 | } |
| 2246 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2247 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | return 0; /* other cases change nothing */ |
| 2251 | } |
| 2252 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2253 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2254 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2255 | fd_delete(t->cli_fd); |
| 2256 | t->cli_state = CL_STCLOSE; |
| 2257 | if (!(t->flags & SN_ERR_MASK)) |
| 2258 | t->flags |= SN_ERR_CLICL; |
| 2259 | if (!(t->flags & SN_FINST_MASK)) { |
| 2260 | if (t->pend_pos) |
| 2261 | t->flags |= SN_FINST_Q; |
| 2262 | else if (s == SV_STCONN) |
| 2263 | t->flags |= SN_FINST_C; |
| 2264 | else |
| 2265 | t->flags |= SN_FINST_D; |
| 2266 | } |
| 2267 | return 1; |
| 2268 | } |
| 2269 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2270 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2271 | buffer_shutw(rep); |
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(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2277 | buffer_shutw(rep); |
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 | |
| 2293 | if (t->flags & SN_SELF_GEN) { |
| 2294 | produce_content(t); |
| 2295 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2296 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2297 | fd_delete(t->cli_fd); |
| 2298 | t->cli_state = CL_STCLOSE; |
| 2299 | return 1; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | if ((rep->l == 0) |
| 2304 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2305 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2306 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2307 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2308 | } |
| 2309 | } else { |
| 2310 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2311 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2312 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2313 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2314 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2315 | } |
| 2316 | } |
| 2317 | return 0; |
| 2318 | } |
| 2319 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2320 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2321 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2322 | fd_delete(t->cli_fd); |
| 2323 | t->cli_state = CL_STCLOSE; |
| 2324 | if (!(t->flags & SN_ERR_MASK)) |
| 2325 | t->flags |= SN_ERR_CLICL; |
| 2326 | if (!(t->flags & SN_FINST_MASK)) { |
| 2327 | if (t->pend_pos) |
| 2328 | t->flags |= SN_FINST_Q; |
| 2329 | else if (s == SV_STCONN) |
| 2330 | t->flags |= SN_FINST_C; |
| 2331 | else |
| 2332 | t->flags |= SN_FINST_D; |
| 2333 | } |
| 2334 | return 1; |
| 2335 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2336 | 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] | 2337 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2338 | fd_delete(t->cli_fd); |
| 2339 | t->cli_state = CL_STCLOSE; |
| 2340 | return 1; |
| 2341 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2342 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2343 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2344 | fd_delete(t->cli_fd); |
| 2345 | t->cli_state = CL_STCLOSE; |
| 2346 | if (!(t->flags & SN_ERR_MASK)) |
| 2347 | t->flags |= SN_ERR_CLITO; |
| 2348 | if (!(t->flags & SN_FINST_MASK)) { |
| 2349 | if (t->pend_pos) |
| 2350 | t->flags |= SN_FINST_Q; |
| 2351 | else if (s == SV_STCONN) |
| 2352 | t->flags |= SN_FINST_C; |
| 2353 | else |
| 2354 | t->flags |= SN_FINST_D; |
| 2355 | } |
| 2356 | return 1; |
| 2357 | } |
| 2358 | else if (req->l >= req->rlim - req->data) { |
| 2359 | /* no room to read more data */ |
| 2360 | |
| 2361 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2362 | * after the timeout by sending more data after it receives a close ? |
| 2363 | */ |
| 2364 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2365 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2366 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2367 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2368 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2369 | } |
| 2370 | } else { |
| 2371 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2372 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2373 | if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2374 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2375 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2376 | } |
| 2377 | } |
| 2378 | return 0; |
| 2379 | } |
| 2380 | else { /* CL_STCLOSE: nothing to do */ |
| 2381 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2382 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2383 | 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] | 2384 | write(1, trash, len); |
| 2385 | } |
| 2386 | return 0; |
| 2387 | } |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
| 2391 | |
| 2392 | /* |
| 2393 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2394 | * (and a resync may be needed), 0 else. |
| 2395 | */ |
| 2396 | int process_srv(struct session *t) |
| 2397 | { |
| 2398 | int s = t->srv_state; |
| 2399 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2400 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2401 | struct buffer *req = t->req; |
| 2402 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2403 | int conn_err; |
| 2404 | |
| 2405 | #ifdef DEBUG_FULL |
| 2406 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2407 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2408 | |
| 2409 | #if 0 |
| 2410 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2411 | __FUNCTION__, __LINE__, |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2412 | t->fe->timeout.client.tv_sec, t->fe->timeout.client.tv_usec, |
| 2413 | t->fe->timeout.connect.tv_sec, t->fe->timeout.connect.tv_usec, |
| 2414 | t->fe->timeout.server.tv_sec, t->fe->timeout.server.tv_usec); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2415 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2416 | __FUNCTION__, __LINE__, |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2417 | t->be->timeout.client.tv_sec, t->be->timeout.client.tv_usec, |
| 2418 | t->be->timeout.connect.tv_sec, t->be->timeout.connect.tv_usec, |
| 2419 | t->be->timeout.server.tv_sec, t->be->timeout.server.tv_usec); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2420 | |
| 2421 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2422 | __FUNCTION__, __LINE__, |
| 2423 | req->cto.tv_sec, req->cto.tv_usec, |
| 2424 | req->rto.tv_sec, req->rto.tv_usec, |
| 2425 | req->wto.tv_sec, req->wto.tv_usec); |
| 2426 | |
| 2427 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2428 | __FUNCTION__, __LINE__, |
| 2429 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2430 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2431 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2432 | #endif |
| 2433 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2434 | //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] | 2435 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2436 | //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] | 2437 | //); |
| 2438 | if (s == SV_STIDLE) { |
| 2439 | if (c == CL_STHEADERS) |
| 2440 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2441 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2442 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2443 | (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] | 2444 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2445 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2446 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2447 | /* note that this must not return any error because it would be able to |
| 2448 | * overwrite the client_retnclose() output. |
| 2449 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2450 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2451 | 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] | 2452 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2453 | 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] | 2454 | |
| 2455 | return 1; |
| 2456 | } |
| 2457 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2458 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2459 | /* This connection is being tarpitted. The CLIENT side has |
| 2460 | * already set the connect expiration date to the right |
| 2461 | * timeout. We just have to check that it has not expired. |
| 2462 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2463 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2464 | return 0; |
| 2465 | |
| 2466 | /* We will set the queue timer to the time spent, just for |
| 2467 | * logging purposes. We fake a 500 server error, so that the |
| 2468 | * attacker will not suspect his connection has been tarpitted. |
| 2469 | * It will not cause trouble to the logs because we can exclude |
| 2470 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2471 | */ |
| 2472 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2473 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2474 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2475 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2476 | return 1; |
| 2477 | } |
| 2478 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2479 | /* Right now, we will need to create a connection to the server. |
| 2480 | * We might already have tried, and got a connection pending, in |
| 2481 | * which case we will not do anything till it's pending. It's up |
| 2482 | * to any other session to release it and wake us up again. |
| 2483 | */ |
| 2484 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2485 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2486 | return 0; |
| 2487 | else { |
| 2488 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2489 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2490 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2491 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2492 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2493 | if (t->srv) |
| 2494 | t->srv->failed_conns++; |
Willy Tarreau | 50fd1e1 | 2007-12-10 15:25:35 +0100 | [diff] [blame] | 2495 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2496 | return 1; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | do { |
| 2501 | /* first, get a connection */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2502 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2503 | t->flags |= SN_REDIRECTABLE; |
| 2504 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2505 | if (srv_redispatch_connect(t)) |
| 2506 | return t->srv_state != SV_STIDLE; |
| 2507 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2508 | if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) { |
| 2509 | /* Server supporting redirection and it is possible. |
| 2510 | * Invalid requests are reported as such. It concerns all |
| 2511 | * the largest ones. |
| 2512 | */ |
| 2513 | struct chunk rdr; |
| 2514 | char *path; |
| 2515 | int len; |
| 2516 | |
| 2517 | /* 1: create the response header */ |
| 2518 | rdr.len = strlen(HTTP_302); |
| 2519 | rdr.str = trash; |
| 2520 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 2521 | |
| 2522 | /* 2: add the server's prefix */ |
| 2523 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 2524 | goto cancel_redir; |
| 2525 | |
| 2526 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 2527 | rdr.len += t->srv->rdr_len; |
| 2528 | |
| 2529 | /* 3: add the request URI */ |
| 2530 | path = http_get_path(txn); |
| 2531 | if (!path) |
| 2532 | goto cancel_redir; |
| 2533 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2534 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 2535 | goto cancel_redir; |
| 2536 | |
| 2537 | memcpy(rdr.str + rdr.len, path, len); |
| 2538 | rdr.len += len; |
| 2539 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2540 | rdr.len += 4; |
| 2541 | |
| 2542 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 2543 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 2544 | if (t->srv) |
| 2545 | t->srv->cum_sess++; |
| 2546 | return 1; |
| 2547 | cancel_redir: |
| 2548 | txn->status = 400; |
| 2549 | t->fe->failed_req++; |
| 2550 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 2551 | 400, error_message(t, HTTP_ERR_400)); |
| 2552 | return 1; |
| 2553 | } |
| 2554 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2555 | /* try to (re-)connect to the server, and fail if we expire the |
| 2556 | * number of retries. |
| 2557 | */ |
| 2558 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2559 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2560 | return t->srv_state != SV_STIDLE; |
| 2561 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2562 | } while (1); |
| 2563 | } |
| 2564 | } |
| 2565 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2566 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2567 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2568 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2569 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2570 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2571 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2572 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2573 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2574 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 2575 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 2576 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2577 | |
| 2578 | /* note that this must not return any error because it would be able to |
| 2579 | * overwrite the client_retnclose() output. |
| 2580 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2581 | 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] | 2582 | return 1; |
| 2583 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2584 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2585 | //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] | 2586 | return 0; /* nothing changed */ |
| 2587 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2588 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2589 | /* timeout, asynchronous connect error or first write error */ |
| 2590 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2591 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2592 | if (t->flags & SN_CONN_TAR) { |
| 2593 | /* We are doing a turn-around waiting for a new connection attempt. */ |
| 2594 | if (!tv_isle(&req->cex, &now)) |
| 2595 | return 0; |
| 2596 | t->flags &= ~SN_CONN_TAR; |
| 2597 | } |
| 2598 | else { |
| 2599 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2600 | if (t->srv) { |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2601 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2602 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 2603 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 2604 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2605 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2606 | if (!(req->flags & BF_WRITE_STATUS)) |
| 2607 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2608 | else |
| 2609 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2610 | |
| 2611 | /* ensure that we have enough retries left */ |
| 2612 | if (srv_count_retry_down(t, conn_err)) |
| 2613 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2614 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2615 | if (req->flags & BF_WRITE_ERROR) { |
| 2616 | /* we encountered an immediate connection error, and we |
| 2617 | * will have to retry connecting to the same server, most |
| 2618 | * likely leading to the same result. To avoid this, we |
| 2619 | * fake a connection timeout to retry after a turn-around |
| 2620 | * time of 1 second. We will wait in the previous if block. |
| 2621 | */ |
| 2622 | t->flags |= SN_CONN_TAR; |
| 2623 | tv_ms_add(&req->cex, &now, 1000); |
| 2624 | return 0; |
| 2625 | } |
| 2626 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2628 | 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] | 2629 | /* We're on our last chance, and the REDISP option was specified. |
| 2630 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2631 | */ |
| 2632 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2633 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2634 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2635 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 2636 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2637 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2638 | |
| 2639 | /* first, get a connection */ |
| 2640 | if (srv_redispatch_connect(t)) |
Willy Tarreau | 00559e7 | 2008-01-06 23:46:19 +0100 | [diff] [blame] | 2641 | return t->srv_state != SV_STCONN; |
Krzysztof Piotr Oledzki | 626a19b | 2008-02-04 02:10:09 +0100 | [diff] [blame] | 2642 | } else { |
| 2643 | if (t->srv) |
| 2644 | t->srv->retries++; |
| 2645 | t->be->retries++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2646 | } |
| 2647 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2648 | do { |
| 2649 | /* Now we will try to either reconnect to the same server or |
| 2650 | * connect to another server. If the connection gets queued |
| 2651 | * because all servers are saturated, then we will go back to |
| 2652 | * the SV_STIDLE state. |
| 2653 | */ |
| 2654 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2655 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2656 | return t->srv_state != SV_STCONN; |
| 2657 | } |
| 2658 | |
| 2659 | /* we need to redispatch the connection to another server */ |
| 2660 | if (srv_redispatch_connect(t)) |
| 2661 | return t->srv_state != SV_STCONN; |
| 2662 | } while (1); |
| 2663 | } |
| 2664 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2665 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2666 | |
| 2667 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2668 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2669 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2670 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2671 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2672 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2673 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2674 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2675 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2676 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2677 | } |
| 2678 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2679 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2682 | 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] | 2683 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2684 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2685 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2686 | |
| 2687 | t->srv_state = SV_STDATA; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2688 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2689 | |
| 2690 | /* if the user wants to log as soon as possible, without counting |
| 2691 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2692 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2693 | 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] | 2694 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2695 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2696 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2697 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2698 | /* TCP splicing supported by both FE and BE */ |
| 2699 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2700 | } |
| 2701 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2702 | } |
| 2703 | else { |
| 2704 | t->srv_state = SV_STHEADERS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2705 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2706 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2707 | /* reset hdr_idx which was already initialized by the request. |
| 2708 | * right now, the http parser does it. |
| 2709 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2710 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2711 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2712 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2713 | return 1; |
| 2714 | } |
| 2715 | } |
| 2716 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2717 | /* |
| 2718 | * Now parse the partial (or complete) lines. |
| 2719 | * We will check the response syntax, and also join multi-line |
| 2720 | * headers. An index of all the lines will be elaborated while |
| 2721 | * parsing. |
| 2722 | * |
| 2723 | * For the parsing, we use a 28 states FSM. |
| 2724 | * |
| 2725 | * Here is the information we currently have : |
| 2726 | * rep->data + req->som = beginning of response |
| 2727 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2728 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2729 | * rep->lr = first non-visited byte |
| 2730 | * rep->r = end of data |
| 2731 | */ |
| 2732 | |
| 2733 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2734 | struct http_msg *msg = &txn->rsp; |
| 2735 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2737 | if (likely(rep->lr < rep->r)) |
| 2738 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2739 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2740 | /* 1: we might have to print this header in debug mode */ |
| 2741 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2742 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2743 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2744 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2746 | sol = rep->data + msg->som; |
| 2747 | eol = sol + msg->sl.rq.l; |
| 2748 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2749 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2750 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2751 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2752 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2753 | while (cur_idx) { |
| 2754 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2755 | debug_hdr("srvhdr", t, sol, eol); |
| 2756 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2757 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2758 | } |
| 2759 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2760 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2761 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2762 | 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] | 2763 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2764 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2765 | * rep->l == rlim-data |
| 2766 | */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2767 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2768 | tv_eternity(&rep->rex); |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | /* |
| 2773 | * Now we quickly check if we have found a full valid response. |
| 2774 | * If not so, we check the FD and buffer states before leaving. |
| 2775 | * A full response is indicated by the fact that we have seen |
| 2776 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2777 | * responses are checked first. |
| 2778 | * |
| 2779 | * Depending on whether the client is still there or not, we |
| 2780 | * may send an error response back or not. Note that normally |
| 2781 | * we should only check for HTTP status there, and check I/O |
| 2782 | * errors somewhere else. |
| 2783 | */ |
| 2784 | |
| 2785 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2786 | |
| 2787 | /* Invalid response, or read error or write error */ |
| 2788 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2789 | (req->flags & BF_WRITE_ERROR) || |
| 2790 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2791 | buffer_shutr(rep); |
| 2792 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2793 | fd_delete(t->srv_fd); |
| 2794 | if (t->srv) { |
| 2795 | t->srv->cur_sess--; |
| 2796 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2797 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 2798 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2799 | } |
| 2800 | t->be->failed_resp++; |
| 2801 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2802 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2803 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2804 | if (!(t->flags & SN_ERR_MASK)) |
| 2805 | t->flags |= SN_ERR_SRVCL; |
| 2806 | if (!(t->flags & SN_FINST_MASK)) |
| 2807 | t->flags |= SN_FINST_H; |
| 2808 | /* We used to have a free connection slot. Since we'll never use it, |
| 2809 | * we have to inform the server that it may be used by another session. |
| 2810 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2811 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2812 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2813 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2814 | return 1; |
| 2815 | } |
| 2816 | |
| 2817 | /* end of client write or end of server read. |
| 2818 | * since we are in header mode, if there's no space left for headers, we |
| 2819 | * won't be able to free more later, so the session will never terminate. |
| 2820 | */ |
| 2821 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2822 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2823 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2824 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2825 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2826 | t->srv_state = SV_STSHUTR; |
| 2827 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2828 | return 1; |
| 2829 | } |
| 2830 | |
| 2831 | /* read timeout : return a 504 to the client. |
| 2832 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2833 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2834 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2835 | buffer_shutr(rep); |
| 2836 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2837 | fd_delete(t->srv_fd); |
| 2838 | if (t->srv) { |
| 2839 | t->srv->cur_sess--; |
| 2840 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2841 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 2842 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2843 | } |
| 2844 | t->be->failed_resp++; |
| 2845 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2846 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2847 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2848 | if (!(t->flags & SN_ERR_MASK)) |
| 2849 | t->flags |= SN_ERR_SRVTO; |
| 2850 | if (!(t->flags & SN_FINST_MASK)) |
| 2851 | t->flags |= SN_FINST_H; |
| 2852 | /* We used to have a free connection slot. Since we'll never use it, |
| 2853 | * we have to inform the server that it may be used by another session. |
| 2854 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2855 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2856 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2857 | return 1; |
| 2858 | } |
| 2859 | |
| 2860 | /* last client read and buffer empty */ |
| 2861 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2862 | * client shuts read too early, because we may still have |
| 2863 | * some work to do on the headers. |
| 2864 | * The side-effect is that if the client completely closes its |
| 2865 | * connection during SV_STHEADER, the connection to the server |
| 2866 | * is kept until a response comes back or the timeout is reached. |
| 2867 | */ |
| 2868 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2869 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2870 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2871 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2872 | |
| 2873 | /* We must ensure that the read part is still |
| 2874 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2875 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2876 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2877 | |
| 2878 | shutdown(t->srv_fd, SHUT_WR); |
| 2879 | t->srv_state = SV_STSHUTW; |
| 2880 | return 1; |
| 2881 | } |
| 2882 | |
| 2883 | /* write timeout */ |
| 2884 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2885 | * client shuts read too early, because we may still have |
| 2886 | * some work to do on the headers. |
| 2887 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2888 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2889 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2890 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2891 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2892 | shutdown(t->srv_fd, SHUT_WR); |
| 2893 | /* We must ensure that the read part is still alive |
| 2894 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2895 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2896 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2897 | |
| 2898 | t->srv_state = SV_STSHUTW; |
| 2899 | if (!(t->flags & SN_ERR_MASK)) |
| 2900 | t->flags |= SN_ERR_SRVTO; |
| 2901 | if (!(t->flags & SN_FINST_MASK)) |
| 2902 | t->flags |= SN_FINST_H; |
| 2903 | return 1; |
| 2904 | } |
| 2905 | |
| 2906 | /* |
| 2907 | * And now the non-error cases. |
| 2908 | */ |
| 2909 | |
| 2910 | /* Data remaining in the request buffer. |
| 2911 | * This happens during the first pass here, and during |
| 2912 | * long posts. |
| 2913 | */ |
| 2914 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2915 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2916 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2917 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2918 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2919 | * we refresh it. */ |
| 2920 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2921 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2922 | else |
| 2923 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2924 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2925 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2926 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2927 | /* nothing left in the request buffer */ |
| 2928 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2929 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2930 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2931 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | return t->srv_state != SV_STHEADERS; |
| 2936 | } |
| 2937 | |
| 2938 | |
| 2939 | /***************************************************************** |
| 2940 | * More interesting part now : we know that we have a complete * |
| 2941 | * response which at least looks like HTTP. We have an indicator * |
| 2942 | * of each header's length, so we can parse them quickly. * |
| 2943 | ****************************************************************/ |
| 2944 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2945 | /* ensure we keep this pointer to the beginning of the message */ |
| 2946 | msg->sol = rep->data + msg->som; |
| 2947 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2948 | /* |
| 2949 | * 1: get the status code and check for cacheability. |
| 2950 | */ |
| 2951 | |
| 2952 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2953 | 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] | 2954 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2955 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2956 | case 200: |
| 2957 | case 203: |
| 2958 | case 206: |
| 2959 | case 300: |
| 2960 | case 301: |
| 2961 | case 410: |
| 2962 | /* RFC2616 @13.4: |
| 2963 | * "A response received with a status code of |
| 2964 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2965 | * by a cache (...) unless a cache-control |
| 2966 | * directive prohibits caching." |
| 2967 | * |
| 2968 | * RFC2616 @9.5: POST method : |
| 2969 | * "Responses to this method are not cacheable, |
| 2970 | * unless the response includes appropriate |
| 2971 | * Cache-Control or Expires header fields." |
| 2972 | */ |
| 2973 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2974 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2975 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2976 | break; |
| 2977 | default: |
| 2978 | break; |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | * 2: we may need to capture headers |
| 2983 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2984 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2985 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2986 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2987 | |
| 2988 | /* |
| 2989 | * 3: we will have to evaluate the filters. |
| 2990 | * As opposed to version 1.2, now they will be evaluated in the |
| 2991 | * filters order and not in the header order. This means that |
| 2992 | * each filter has to be validated among all headers. |
| 2993 | * |
| 2994 | * Filters are tried with ->be first, then with ->fe if it is |
| 2995 | * different from ->be. |
| 2996 | */ |
| 2997 | |
| 2998 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2999 | |
| 3000 | cur_proxy = t->be; |
| 3001 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3002 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3003 | |
| 3004 | /* try headers filters */ |
| 3005 | if (rule_set->rsp_exp != NULL) { |
| 3006 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3007 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3008 | if (t->srv) { |
| 3009 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3010 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3011 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3012 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3013 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3014 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3015 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3016 | buffer_shutr(rep); |
| 3017 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3018 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3019 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3020 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 3021 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3022 | if (!(t->flags & SN_ERR_MASK)) |
| 3023 | t->flags |= SN_ERR_PRXCOND; |
| 3024 | if (!(t->flags & SN_FINST_MASK)) |
| 3025 | t->flags |= SN_FINST_H; |
| 3026 | /* We used to have a free connection slot. Since we'll never use it, |
| 3027 | * we have to inform the server that it may be used by another session. |
| 3028 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3029 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3030 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3031 | return 1; |
| 3032 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3033 | } |
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 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3036 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3037 | if (t->srv) { |
| 3038 | t->srv->cur_sess--; |
| 3039 | t->srv->failed_secu++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3040 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3041 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3042 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3043 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3044 | goto return_srv_prx_502; |
| 3045 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3046 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3047 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3048 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3049 | !(t->flags & SN_CONN_CLOSED)) { |
| 3050 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3051 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3052 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3053 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3054 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3055 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3057 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3058 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3059 | cur_ptr = cur_next; |
| 3060 | cur_end = cur_ptr + cur_hdr->len; |
| 3061 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3062 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3063 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3064 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3065 | /* 3 possibilities : |
| 3066 | * - we have already set Connection: close, |
| 3067 | * so we remove this line. |
| 3068 | * - we have not yet set Connection: close, |
| 3069 | * but this line indicates close. We leave |
| 3070 | * it untouched and set the flag. |
| 3071 | * - we have not yet set Connection: close, |
| 3072 | * and this line indicates non-close. We |
| 3073 | * replace it. |
| 3074 | */ |
| 3075 | if (t->flags & SN_CONN_CLOSED) { |
| 3076 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3077 | txn->rsp.eoh += delta; |
| 3078 | cur_next += delta; |
| 3079 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3080 | txn->hdr_idx.used--; |
| 3081 | cur_hdr->len = 0; |
| 3082 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3083 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3084 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3085 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3086 | cur_next += delta; |
| 3087 | cur_hdr->len += delta; |
| 3088 | txn->rsp.eoh += delta; |
| 3089 | } |
| 3090 | t->flags |= SN_CONN_CLOSED; |
| 3091 | } |
| 3092 | } |
| 3093 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3094 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3095 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3097 | /* add response headers from the rule sets in the same order */ |
| 3098 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3099 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3100 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3101 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3104 | /* check whether we're already working on the frontend */ |
| 3105 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3106 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3107 | cur_proxy = t->fe; |
| 3108 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3109 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3110 | /* |
| 3111 | * 4: check for server cookie. |
| 3112 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3113 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3114 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3115 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3116 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3118 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3119 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3120 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3121 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3122 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3123 | |
| 3124 | /* |
| 3125 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3126 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3127 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3128 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3129 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3130 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3131 | /* the server is known, it's not the one the client requested, we have to |
| 3132 | * insert a set-cookie here, except if we want to insert only on POST |
| 3133 | * requests and this one isn't. Note that servers which don't have cookies |
| 3134 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3135 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3136 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3137 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3138 | 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] | 3139 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3140 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3141 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3142 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3143 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3144 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3145 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3146 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3147 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3148 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3149 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3150 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3151 | |
| 3152 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3153 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3154 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3155 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3156 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3157 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | |
| 3161 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3162 | * 7: check if result will be cacheable with a cookie. |
| 3163 | * We'll block the response if security checks have caught |
| 3164 | * nasty things such as a cacheable cookie. |
| 3165 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3166 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3167 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3168 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3169 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3170 | /* we're in presence of a cacheable response containing |
| 3171 | * a set-cookie header. We'll block it as requested by |
| 3172 | * the 'checkcache' option, and send an alert. |
| 3173 | */ |
| 3174 | if (t->srv) { |
| 3175 | t->srv->cur_sess--; |
| 3176 | t->srv->failed_secu++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3177 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3178 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3179 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3180 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3182 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3183 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3184 | send_log(t->be, LOG_ALERT, |
| 3185 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3186 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3187 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3190 | /* |
| 3191 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3192 | * 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] | 3193 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3194 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3195 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3196 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3197 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3198 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3199 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3200 | goto return_bad_resp; |
| 3201 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3202 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3203 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3204 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3205 | /************************************************************* |
| 3206 | * OK, that's finished for the headers. We have done what we * |
| 3207 | * could. Let's switch to the DATA state. * |
| 3208 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3210 | t->srv_state = SV_STDATA; |
| 3211 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3212 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* client connection already closed or option 'forceclose' required : |
| 3215 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3216 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3217 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3218 | (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] | 3219 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3220 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3221 | |
| 3222 | /* We must ensure that the read part is still alive when switching |
| 3223 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3224 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3225 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3226 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3227 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3228 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3231 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3232 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3233 | /* TCP splicing supported by both FE and BE */ |
| 3234 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3235 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3236 | #endif |
| 3237 | /* if the user wants to log as soon as possible, without counting |
Krzysztof Piotr Oledzki | f1e1cb4 | 2008-01-20 23:27:02 +0100 | [diff] [blame] | 3238 | * bytes from the server, then this is the right moment. We have |
| 3239 | * to temporarily assign bytes_out to log what we currently have. |
| 3240 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3241 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3242 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 3243 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3244 | if (t->fe->to_log & LW_REQ) |
| 3245 | http_sess_log(t); |
| 3246 | else |
| 3247 | tcp_sess_log(t); |
Krzysztof Piotr Oledzki | f1e1cb4 | 2008-01-20 23:27:02 +0100 | [diff] [blame] | 3248 | t->logs.bytes_out = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3251 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3252 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3253 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3254 | |
| 3255 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3256 | } |
| 3257 | else if (s == SV_STDATA) { |
| 3258 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3259 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3260 | buffer_shutr(rep); |
| 3261 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3262 | fd_delete(t->srv_fd); |
| 3263 | if (t->srv) { |
| 3264 | t->srv->cur_sess--; |
| 3265 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3266 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3267 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3268 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3269 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3270 | t->srv_state = SV_STCLOSE; |
| 3271 | if (!(t->flags & SN_ERR_MASK)) |
| 3272 | t->flags |= SN_ERR_SRVCL; |
| 3273 | if (!(t->flags & SN_FINST_MASK)) |
| 3274 | t->flags |= SN_FINST_D; |
| 3275 | /* We used to have a free connection slot. Since we'll never use it, |
| 3276 | * we have to inform the server that it may be used by another session. |
| 3277 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3278 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3279 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3280 | |
| 3281 | return 1; |
| 3282 | } |
| 3283 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3284 | 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] | 3285 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3286 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3287 | t->srv_state = SV_STSHUTR; |
| 3288 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3289 | return 1; |
| 3290 | } |
| 3291 | /* end of client read and no more data to send */ |
| 3292 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3293 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3294 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3295 | shutdown(t->srv_fd, SHUT_WR); |
| 3296 | /* We must ensure that the read part is still alive when switching |
| 3297 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3298 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3299 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3300 | |
| 3301 | t->srv_state = SV_STSHUTW; |
| 3302 | return 1; |
| 3303 | } |
| 3304 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3305 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3306 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3307 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3308 | t->srv_state = SV_STSHUTR; |
| 3309 | if (!(t->flags & SN_ERR_MASK)) |
| 3310 | t->flags |= SN_ERR_SRVTO; |
| 3311 | if (!(t->flags & SN_FINST_MASK)) |
| 3312 | t->flags |= SN_FINST_D; |
| 3313 | return 1; |
| 3314 | } |
| 3315 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3316 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3317 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3318 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3319 | shutdown(t->srv_fd, SHUT_WR); |
| 3320 | /* We must ensure that the read part is still alive when switching |
| 3321 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3322 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3323 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3324 | t->srv_state = SV_STSHUTW; |
| 3325 | if (!(t->flags & SN_ERR_MASK)) |
| 3326 | t->flags |= SN_ERR_SRVTO; |
| 3327 | if (!(t->flags & SN_FINST_MASK)) |
| 3328 | t->flags |= SN_FINST_D; |
| 3329 | return 1; |
| 3330 | } |
| 3331 | |
| 3332 | /* recompute request time-outs */ |
| 3333 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3334 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3335 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3336 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3340 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3341 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3342 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3343 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3344 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3345 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3346 | } |
| 3347 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3348 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | /* recompute response time-outs */ |
| 3353 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3354 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3355 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3359 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3360 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3361 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | return 0; /* other cases change nothing */ |
| 3366 | } |
| 3367 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3368 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3369 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3370 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3371 | fd_delete(t->srv_fd); |
| 3372 | if (t->srv) { |
| 3373 | t->srv->cur_sess--; |
| 3374 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3375 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3376 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3377 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3378 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3379 | //close(t->srv_fd); |
| 3380 | t->srv_state = SV_STCLOSE; |
| 3381 | if (!(t->flags & SN_ERR_MASK)) |
| 3382 | t->flags |= SN_ERR_SRVCL; |
| 3383 | if (!(t->flags & SN_FINST_MASK)) |
| 3384 | t->flags |= SN_FINST_D; |
| 3385 | /* We used to have a free connection slot. Since we'll never use it, |
| 3386 | * we have to inform the server that it may be used by another session. |
| 3387 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3388 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3389 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3390 | |
| 3391 | return 1; |
| 3392 | } |
| 3393 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3394 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3395 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3396 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3397 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3398 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3399 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3400 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 3401 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3402 | //close(t->srv_fd); |
| 3403 | t->srv_state = SV_STCLOSE; |
| 3404 | /* We used to have a free connection slot. Since we'll never use it, |
| 3405 | * we have to inform the server that it may be used by another session. |
| 3406 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3407 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3408 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3409 | |
| 3410 | return 1; |
| 3411 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3412 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3413 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3414 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3415 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3416 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3417 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3418 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3419 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 3420 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3421 | //close(t->srv_fd); |
| 3422 | t->srv_state = SV_STCLOSE; |
| 3423 | if (!(t->flags & SN_ERR_MASK)) |
| 3424 | t->flags |= SN_ERR_SRVTO; |
| 3425 | if (!(t->flags & SN_FINST_MASK)) |
| 3426 | t->flags |= SN_FINST_D; |
| 3427 | /* We used to have a free connection slot. Since we'll never use it, |
| 3428 | * we have to inform the server that it may be used by another session. |
| 3429 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3430 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3431 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3432 | |
| 3433 | return 1; |
| 3434 | } |
| 3435 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3436 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3437 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3438 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3439 | } |
| 3440 | } |
| 3441 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3442 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3443 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3444 | if (!tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3445 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | return 0; |
| 3449 | } |
| 3450 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3451 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3452 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3453 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3454 | fd_delete(t->srv_fd); |
| 3455 | if (t->srv) { |
| 3456 | t->srv->cur_sess--; |
| 3457 | t->srv->failed_resp++; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3458 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3459 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3460 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3461 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3462 | //close(t->srv_fd); |
| 3463 | t->srv_state = SV_STCLOSE; |
| 3464 | if (!(t->flags & SN_ERR_MASK)) |
| 3465 | t->flags |= SN_ERR_SRVCL; |
| 3466 | if (!(t->flags & SN_FINST_MASK)) |
| 3467 | t->flags |= SN_FINST_D; |
| 3468 | /* We used to have a free connection slot. Since we'll never use it, |
| 3469 | * we have to inform the server that it may be used by another session. |
| 3470 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3471 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3472 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3473 | |
| 3474 | return 1; |
| 3475 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3476 | 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] | 3477 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3478 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3479 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3480 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3481 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3482 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3483 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 3484 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3485 | //close(t->srv_fd); |
| 3486 | t->srv_state = SV_STCLOSE; |
| 3487 | /* We used to have a free connection slot. Since we'll never use it, |
| 3488 | * we have to inform the server that it may be used by another session. |
| 3489 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3490 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3491 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3492 | |
| 3493 | return 1; |
| 3494 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3495 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3496 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3497 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3498 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3499 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3500 | t->srv->cur_sess--; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3501 | if (t->srv->proxy->lbprm.server_drop_conn) |
| 3502 | t->srv->proxy->lbprm.server_drop_conn(t->srv); |
| 3503 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3504 | //close(t->srv_fd); |
| 3505 | t->srv_state = SV_STCLOSE; |
| 3506 | if (!(t->flags & SN_ERR_MASK)) |
| 3507 | t->flags |= SN_ERR_SRVTO; |
| 3508 | if (!(t->flags & SN_FINST_MASK)) |
| 3509 | t->flags |= SN_FINST_D; |
| 3510 | /* We used to have a free connection slot. Since we'll never use it, |
| 3511 | * we have to inform the server that it may be used by another session. |
| 3512 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3513 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3514 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3515 | |
| 3516 | return 1; |
| 3517 | } |
| 3518 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3519 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3520 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3524 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3525 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3526 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | return 0; |
| 3530 | } |
| 3531 | else { /* SV_STCLOSE : nothing to do */ |
| 3532 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3533 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3534 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3535 | 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] | 3536 | write(1, trash, len); |
| 3537 | } |
| 3538 | return 0; |
| 3539 | } |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
| 3543 | |
| 3544 | /* |
| 3545 | * Produces data for the session <s> depending on its source. Expects to be |
| 3546 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3547 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3548 | * session, which it uses to keep on being called when there is free space in |
| 3549 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3550 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3551 | */ |
| 3552 | int produce_content(struct session *s) |
| 3553 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3554 | if (s->data_source == DATA_SRC_NONE) { |
| 3555 | s->flags &= ~SN_SELF_GEN; |
| 3556 | return 1; |
| 3557 | } |
| 3558 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3559 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 3560 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3561 | if (ret >= 0) |
| 3562 | return ret; |
| 3563 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3565 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3566 | /* unknown data source or internal error */ |
| 3567 | s->txn.status = 500; |
| 3568 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3569 | if (!(s->flags & SN_ERR_MASK)) |
| 3570 | s->flags |= SN_ERR_PRXCOND; |
| 3571 | if (!(s->flags & SN_FINST_MASK)) |
| 3572 | s->flags |= SN_FINST_R; |
| 3573 | s->flags &= ~SN_SELF_GEN; |
| 3574 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3578 | /* Iterate the same filter through all request headers. |
| 3579 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3580 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3581 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3582 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3583 | 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] | 3584 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3585 | char term; |
| 3586 | char *cur_ptr, *cur_end, *cur_next; |
| 3587 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3588 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3589 | struct hdr_idx_elem *cur_hdr; |
| 3590 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3592 | last_hdr = 0; |
| 3593 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3594 | 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] | 3595 | old_idx = 0; |
| 3596 | |
| 3597 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3598 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3599 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3600 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3601 | (exp->action == ACT_ALLOW || |
| 3602 | exp->action == ACT_DENY || |
| 3603 | exp->action == ACT_TARPIT)) |
| 3604 | return 0; |
| 3605 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3606 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3607 | if (!cur_idx) |
| 3608 | break; |
| 3609 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3610 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3611 | cur_ptr = cur_next; |
| 3612 | cur_end = cur_ptr + cur_hdr->len; |
| 3613 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3614 | |
| 3615 | /* Now we have one header between cur_ptr and cur_end, |
| 3616 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3617 | */ |
| 3618 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3619 | /* The annoying part is that pattern matching needs |
| 3620 | * that we modify the contents to null-terminate all |
| 3621 | * strings before testing them. |
| 3622 | */ |
| 3623 | |
| 3624 | term = *cur_end; |
| 3625 | *cur_end = '\0'; |
| 3626 | |
| 3627 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3628 | switch (exp->action) { |
| 3629 | case ACT_SETBE: |
| 3630 | /* It is not possible to jump a second time. |
| 3631 | * FIXME: should we return an HTTP/500 here so that |
| 3632 | * the admin knows there's a problem ? |
| 3633 | */ |
| 3634 | if (t->be != t->fe) |
| 3635 | break; |
| 3636 | |
| 3637 | /* Swithing Proxy */ |
| 3638 | t->be = (struct proxy *) exp->replace; |
| 3639 | |
| 3640 | /* right now, the backend switch is not too much complicated |
| 3641 | * because we have associated req_cap and rsp_cap to the |
| 3642 | * frontend, and the beconn will be updated later. |
| 3643 | */ |
| 3644 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3645 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3646 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3647 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3648 | last_hdr = 1; |
| 3649 | break; |
| 3650 | |
| 3651 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3652 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3653 | last_hdr = 1; |
| 3654 | break; |
| 3655 | |
| 3656 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3657 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3658 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3659 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3660 | break; |
| 3661 | |
| 3662 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3663 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3664 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3665 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3666 | break; |
| 3667 | |
| 3668 | case ACT_REPLACE: |
| 3669 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3670 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3671 | /* FIXME: if the user adds a newline in the replacement, the |
| 3672 | * index will not be recalculated for now, and the new line |
| 3673 | * will not be counted as a new header. |
| 3674 | */ |
| 3675 | |
| 3676 | cur_end += delta; |
| 3677 | cur_next += delta; |
| 3678 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3679 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3680 | break; |
| 3681 | |
| 3682 | case ACT_REMOVE: |
| 3683 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3684 | cur_next += delta; |
| 3685 | |
| 3686 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3687 | txn->req.eoh += delta; |
| 3688 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3689 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3690 | cur_hdr->len = 0; |
| 3691 | cur_end = NULL; /* null-term has been rewritten */ |
| 3692 | break; |
| 3693 | |
| 3694 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3695 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3696 | if (cur_end) |
| 3697 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3698 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3699 | /* keep the link from this header to next one in case of later |
| 3700 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3701 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3702 | old_idx = cur_idx; |
| 3703 | } |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
| 3707 | |
| 3708 | /* Apply the filter to the request line. |
| 3709 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3710 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3711 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3712 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3713 | */ |
| 3714 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3715 | { |
| 3716 | char term; |
| 3717 | char *cur_ptr, *cur_end; |
| 3718 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3719 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3720 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3721 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3722 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3723 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3724 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3725 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3726 | (exp->action == ACT_ALLOW || |
| 3727 | exp->action == ACT_DENY || |
| 3728 | exp->action == ACT_TARPIT)) |
| 3729 | return 0; |
| 3730 | else if (exp->action == ACT_REMOVE) |
| 3731 | return 0; |
| 3732 | |
| 3733 | done = 0; |
| 3734 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3735 | 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] | 3736 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3737 | |
| 3738 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3739 | |
| 3740 | /* The annoying part is that pattern matching needs |
| 3741 | * that we modify the contents to null-terminate all |
| 3742 | * strings before testing them. |
| 3743 | */ |
| 3744 | |
| 3745 | term = *cur_end; |
| 3746 | *cur_end = '\0'; |
| 3747 | |
| 3748 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3749 | switch (exp->action) { |
| 3750 | case ACT_SETBE: |
| 3751 | /* It is not possible to jump a second time. |
| 3752 | * FIXME: should we return an HTTP/500 here so that |
| 3753 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3754 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3755 | if (t->be != t->fe) |
| 3756 | break; |
| 3757 | |
| 3758 | /* Swithing Proxy */ |
| 3759 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3761 | /* right now, the backend switch is not too much complicated |
| 3762 | * because we have associated req_cap and rsp_cap to the |
| 3763 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3764 | */ |
| 3765 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3766 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3767 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3768 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3769 | done = 1; |
| 3770 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3771 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3772 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3773 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3774 | done = 1; |
| 3775 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3777 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3778 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3779 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3780 | done = 1; |
| 3781 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3783 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3784 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3785 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3786 | done = 1; |
| 3787 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3789 | case ACT_REPLACE: |
| 3790 | *cur_end = term; /* restore the string terminator */ |
| 3791 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3792 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3793 | /* FIXME: if the user adds a newline in the replacement, the |
| 3794 | * index will not be recalculated for now, and the new line |
| 3795 | * will not be counted as a new header. |
| 3796 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3797 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3798 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3799 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3800 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3801 | 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] | 3802 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3803 | HTTP_MSG_RQMETH, |
| 3804 | cur_ptr, cur_end + 1, |
| 3805 | NULL, NULL); |
| 3806 | if (unlikely(!cur_end)) |
| 3807 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3808 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3809 | /* we have a full request and we know that we have either a CR |
| 3810 | * or an LF at <ptr>. |
| 3811 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3812 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3813 | 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] | 3814 | /* there is no point trying this regex on headers */ |
| 3815 | return 1; |
| 3816 | } |
| 3817 | } |
| 3818 | *cur_end = term; /* restore the string terminator */ |
| 3819 | return done; |
| 3820 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3821 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3822 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3823 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3824 | /* |
| 3825 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3826 | * 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] | 3827 | * unparsable request. Since it can manage the switch to another backend, it |
| 3828 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3829 | */ |
| 3830 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3831 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3832 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3833 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3834 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3835 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3836 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3837 | /* |
| 3838 | * The interleaving of transformations and verdicts |
| 3839 | * makes it difficult to decide to continue or stop |
| 3840 | * the evaluation. |
| 3841 | */ |
| 3842 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3843 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3844 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3845 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3846 | exp = exp->next; |
| 3847 | continue; |
| 3848 | } |
| 3849 | |
| 3850 | /* Apply the filter to the request line. */ |
| 3851 | ret = apply_filter_to_req_line(t, req, exp); |
| 3852 | if (unlikely(ret < 0)) |
| 3853 | return -1; |
| 3854 | |
| 3855 | if (likely(ret == 0)) { |
| 3856 | /* The filter did not match the request, it can be |
| 3857 | * iterated through all headers. |
| 3858 | */ |
| 3859 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3860 | } |
| 3861 | exp = exp->next; |
| 3862 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3863 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3867 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3868 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3869 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3870 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3871 | */ |
| 3872 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3873 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3874 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3875 | char *p1, *p2, *p3, *p4; |
| 3876 | char *del_colon, *del_cookie, *colon; |
| 3877 | int app_cookies; |
| 3878 | |
| 3879 | appsess *asession_temp = NULL; |
| 3880 | appsess local_asession; |
| 3881 | |
| 3882 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3883 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3884 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3885 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3886 | * we start with the start line. |
| 3887 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3888 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3889 | 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] | 3890 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3891 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3892 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3893 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3894 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3895 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3896 | cur_ptr = cur_next; |
| 3897 | cur_end = cur_ptr + cur_hdr->len; |
| 3898 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3899 | |
| 3900 | /* We have one full header between cur_ptr and cur_end, and the |
| 3901 | * next header starts at cur_next. We're only interested in |
| 3902 | * "Cookie:" headers. |
| 3903 | */ |
| 3904 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3905 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3906 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3907 | old_idx = cur_idx; |
| 3908 | continue; |
| 3909 | } |
| 3910 | |
| 3911 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3912 | * attributes whose name begin with a '$', and associate them with |
| 3913 | * the right cookie, if we want to delete this cookie. |
| 3914 | * So there are 3 cases for each cookie read : |
| 3915 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3916 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3917 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3918 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3919 | * "special" cookie. |
| 3920 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3921 | * remove it. If no application cookie persists in the header, we |
| 3922 | * *MUST* delete it |
| 3923 | */ |
| 3924 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3925 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3926 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3927 | /* del_cookie == NULL => nothing to be deleted */ |
| 3928 | del_colon = del_cookie = NULL; |
| 3929 | app_cookies = 0; |
| 3930 | |
| 3931 | while (p1 < cur_end) { |
| 3932 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3933 | while (p1 < cur_end) { |
| 3934 | if (*p1 == ';' || *p1 == ',') |
| 3935 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3936 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3937 | break; |
| 3938 | p1++; |
| 3939 | } |
| 3940 | |
| 3941 | if (p1 == cur_end) |
| 3942 | break; |
| 3943 | |
| 3944 | /* p1 is at the beginning of the cookie name */ |
| 3945 | p2 = p1; |
| 3946 | while (p2 < cur_end && *p2 != '=') |
| 3947 | p2++; |
| 3948 | |
| 3949 | if (p2 == cur_end) |
| 3950 | break; |
| 3951 | |
| 3952 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3953 | if (p3 == cur_end) |
| 3954 | break; |
| 3955 | |
| 3956 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3957 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3958 | p4++; |
| 3959 | |
| 3960 | /* here, we have the cookie name between p1 and p2, |
| 3961 | * and its value between p3 and p4. |
| 3962 | * we can process it : |
| 3963 | * |
| 3964 | * Cookie: NAME=VALUE; |
| 3965 | * | || || | |
| 3966 | * | || || +--> p4 |
| 3967 | * | || |+-------> p3 |
| 3968 | * | || +--------> p2 |
| 3969 | * | |+------------> p1 |
| 3970 | * | +-------------> colon |
| 3971 | * +--------------------> cur_ptr |
| 3972 | */ |
| 3973 | |
| 3974 | if (*p1 == '$') { |
| 3975 | /* skip this one */ |
| 3976 | } |
| 3977 | else { |
| 3978 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3979 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3980 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3981 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3982 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3983 | int log_len = p4 - p1; |
| 3984 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3985 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3986 | Alert("HTTP logging : out of memory.\n"); |
| 3987 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3988 | if (log_len > t->fe->capture_len) |
| 3989 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3990 | memcpy(txn->cli_cookie, p1, log_len); |
| 3991 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3995 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3996 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3997 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3998 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3999 | char *delim; |
| 4000 | |
| 4001 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4002 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4003 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4004 | * |
| 4005 | * Cookie: NAME=SRV~VALUE; |
| 4006 | * | || || | | |
| 4007 | * | || || | +--> p4 |
| 4008 | * | || || +--------> delim |
| 4009 | * | || |+-----------> p3 |
| 4010 | * | || +------------> p2 |
| 4011 | * | |+----------------> p1 |
| 4012 | * | +-----------------> colon |
| 4013 | * +------------------------> cur_ptr |
| 4014 | */ |
| 4015 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4016 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4017 | for (delim = p3; delim < p4; delim++) |
| 4018 | if (*delim == COOKIE_DELIM) |
| 4019 | break; |
| 4020 | } |
| 4021 | else |
| 4022 | delim = p4; |
| 4023 | |
| 4024 | |
| 4025 | /* Here, we'll look for the first running server which supports the cookie. |
| 4026 | * This allows to share a same cookie between several servers, for example |
| 4027 | * to dedicate backup servers to specific servers only. |
| 4028 | * However, to prevent clients from sticking to cookie-less backup server |
| 4029 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4030 | * empty cookies and mark them as invalid. |
| 4031 | */ |
| 4032 | if (delim == p3) |
| 4033 | srv = NULL; |
| 4034 | |
| 4035 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4036 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4037 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4038 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4039 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4040 | txn->flags &= ~TX_CK_MASK; |
| 4041 | txn->flags |= TX_CK_VALID; |
| 4042 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4043 | t->srv = srv; |
| 4044 | break; |
| 4045 | } else { |
| 4046 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4047 | txn->flags &= ~TX_CK_MASK; |
| 4048 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4049 | } |
| 4050 | } |
| 4051 | srv = srv->next; |
| 4052 | } |
| 4053 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4054 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4055 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4056 | txn->flags &= ~TX_CK_MASK; |
| 4057 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | /* depending on the cookie mode, we may have to either : |
| 4061 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4062 | * the server never sees it ; |
| 4063 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4064 | * application cookie so that it does not get accidentely removed later, |
| 4065 | * if we're in cookie prefix mode |
| 4066 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4067 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4068 | int delta; /* negative */ |
| 4069 | |
| 4070 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4071 | p4 += delta; |
| 4072 | cur_end += delta; |
| 4073 | cur_next += delta; |
| 4074 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4075 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4076 | |
| 4077 | del_cookie = del_colon = NULL; |
| 4078 | app_cookies++; /* protect the header from deletion */ |
| 4079 | } |
| 4080 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4081 | (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] | 4082 | del_cookie = p1; |
| 4083 | del_colon = colon; |
| 4084 | } |
| 4085 | } else { |
| 4086 | /* now we know that we must keep this cookie since it's |
| 4087 | * not ours. But if we wanted to delete our cookie |
| 4088 | * earlier, we cannot remove the complete header, but we |
| 4089 | * can remove the previous block itself. |
| 4090 | */ |
| 4091 | app_cookies++; |
| 4092 | |
| 4093 | if (del_cookie != NULL) { |
| 4094 | int delta; /* negative */ |
| 4095 | |
| 4096 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4097 | p4 += delta; |
| 4098 | cur_end += delta; |
| 4099 | cur_next += delta; |
| 4100 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4101 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4102 | del_cookie = del_colon = NULL; |
| 4103 | } |
| 4104 | } |
| 4105 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4106 | if ((t->be->appsession_name != NULL) && |
| 4107 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4108 | /* first, let's see if the cookie is our appcookie*/ |
| 4109 | |
| 4110 | /* Cool... it's the right one */ |
| 4111 | |
| 4112 | asession_temp = &local_asession; |
| 4113 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4114 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4115 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4116 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4117 | return; |
| 4118 | } |
| 4119 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4120 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4121 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4122 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4123 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4124 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4125 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4126 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4127 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4128 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4129 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4130 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4131 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4132 | return; |
| 4133 | } |
| 4134 | |
| 4135 | asession_temp->sessid = local_asession.sessid; |
| 4136 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4137 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4138 | } else { |
| 4139 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4140 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4141 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4142 | if (asession_temp->serverid == NULL) { |
| 4143 | Alert("Found Application Session without matching server.\n"); |
| 4144 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4145 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4146 | while (srv) { |
| 4147 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4148 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4149 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4150 | txn->flags &= ~TX_CK_MASK; |
| 4151 | txn->flags |= TX_CK_VALID; |
| 4152 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4153 | t->srv = srv; |
| 4154 | break; |
| 4155 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4156 | txn->flags &= ~TX_CK_MASK; |
| 4157 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | srv = srv->next; |
| 4161 | }/* end while(srv) */ |
| 4162 | }/* end else if server == NULL */ |
| 4163 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4164 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4165 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4166 | } |
| 4167 | |
| 4168 | /* we'll have to look for another cookie ... */ |
| 4169 | p1 = p4; |
| 4170 | } /* while (p1 < cur_end) */ |
| 4171 | |
| 4172 | /* There's no more cookie on this line. |
| 4173 | * We may have marked the last one(s) for deletion. |
| 4174 | * We must do this now in two ways : |
| 4175 | * - if there is no app cookie, we simply delete the header ; |
| 4176 | * - if there are app cookies, we must delete the end of the |
| 4177 | * string properly, including the colon/semi-colon before |
| 4178 | * the cookie name. |
| 4179 | */ |
| 4180 | if (del_cookie != NULL) { |
| 4181 | int delta; |
| 4182 | if (app_cookies) { |
| 4183 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4184 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4185 | cur_hdr->len += delta; |
| 4186 | } else { |
| 4187 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4190 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4191 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4192 | cur_hdr->len = 0; |
| 4193 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4194 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4195 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | /* keep the link from this header to next one */ |
| 4199 | old_idx = cur_idx; |
| 4200 | } /* end of cookie processing on this header */ |
| 4201 | } |
| 4202 | |
| 4203 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4204 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4205 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4206 | */ |
| 4207 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4208 | { |
| 4209 | char term; |
| 4210 | char *cur_ptr, *cur_end, *cur_next; |
| 4211 | int cur_idx, old_idx, last_hdr; |
| 4212 | struct http_txn *txn = &t->txn; |
| 4213 | struct hdr_idx_elem *cur_hdr; |
| 4214 | int len, delta; |
| 4215 | |
| 4216 | last_hdr = 0; |
| 4217 | |
| 4218 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4219 | old_idx = 0; |
| 4220 | |
| 4221 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4222 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4223 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4224 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4225 | (exp->action == ACT_ALLOW || |
| 4226 | exp->action == ACT_DENY)) |
| 4227 | return 0; |
| 4228 | |
| 4229 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4230 | if (!cur_idx) |
| 4231 | break; |
| 4232 | |
| 4233 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4234 | cur_ptr = cur_next; |
| 4235 | cur_end = cur_ptr + cur_hdr->len; |
| 4236 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4237 | |
| 4238 | /* Now we have one header between cur_ptr and cur_end, |
| 4239 | * and the next header starts at cur_next. |
| 4240 | */ |
| 4241 | |
| 4242 | /* The annoying part is that pattern matching needs |
| 4243 | * that we modify the contents to null-terminate all |
| 4244 | * strings before testing them. |
| 4245 | */ |
| 4246 | |
| 4247 | term = *cur_end; |
| 4248 | *cur_end = '\0'; |
| 4249 | |
| 4250 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4251 | switch (exp->action) { |
| 4252 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4253 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4254 | last_hdr = 1; |
| 4255 | break; |
| 4256 | |
| 4257 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4258 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4259 | last_hdr = 1; |
| 4260 | break; |
| 4261 | |
| 4262 | case ACT_REPLACE: |
| 4263 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4264 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4265 | /* FIXME: if the user adds a newline in the replacement, the |
| 4266 | * index will not be recalculated for now, and the new line |
| 4267 | * will not be counted as a new header. |
| 4268 | */ |
| 4269 | |
| 4270 | cur_end += delta; |
| 4271 | cur_next += delta; |
| 4272 | cur_hdr->len += delta; |
| 4273 | txn->rsp.eoh += delta; |
| 4274 | break; |
| 4275 | |
| 4276 | case ACT_REMOVE: |
| 4277 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4278 | cur_next += delta; |
| 4279 | |
| 4280 | /* FIXME: this should be a separate function */ |
| 4281 | txn->rsp.eoh += delta; |
| 4282 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4283 | txn->hdr_idx.used--; |
| 4284 | cur_hdr->len = 0; |
| 4285 | cur_end = NULL; /* null-term has been rewritten */ |
| 4286 | break; |
| 4287 | |
| 4288 | } |
| 4289 | } |
| 4290 | if (cur_end) |
| 4291 | *cur_end = term; /* restore the string terminator */ |
| 4292 | |
| 4293 | /* keep the link from this header to next one in case of later |
| 4294 | * removal of next header. |
| 4295 | */ |
| 4296 | old_idx = cur_idx; |
| 4297 | } |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | |
| 4302 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4303 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4304 | * or -1 if a replacement resulted in an invalid status line. |
| 4305 | */ |
| 4306 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4307 | { |
| 4308 | char term; |
| 4309 | char *cur_ptr, *cur_end; |
| 4310 | int done; |
| 4311 | struct http_txn *txn = &t->txn; |
| 4312 | int len, delta; |
| 4313 | |
| 4314 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4315 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4316 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4317 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4318 | (exp->action == ACT_ALLOW || |
| 4319 | exp->action == ACT_DENY)) |
| 4320 | return 0; |
| 4321 | else if (exp->action == ACT_REMOVE) |
| 4322 | return 0; |
| 4323 | |
| 4324 | done = 0; |
| 4325 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4326 | 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] | 4327 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4328 | |
| 4329 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4330 | |
| 4331 | /* The annoying part is that pattern matching needs |
| 4332 | * that we modify the contents to null-terminate all |
| 4333 | * strings before testing them. |
| 4334 | */ |
| 4335 | |
| 4336 | term = *cur_end; |
| 4337 | *cur_end = '\0'; |
| 4338 | |
| 4339 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4340 | switch (exp->action) { |
| 4341 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4342 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4343 | done = 1; |
| 4344 | break; |
| 4345 | |
| 4346 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4347 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4348 | done = 1; |
| 4349 | break; |
| 4350 | |
| 4351 | case ACT_REPLACE: |
| 4352 | *cur_end = term; /* restore the string terminator */ |
| 4353 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4354 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4355 | /* FIXME: if the user adds a newline in the replacement, the |
| 4356 | * index will not be recalculated for now, and the new line |
| 4357 | * will not be counted as a new header. |
| 4358 | */ |
| 4359 | |
| 4360 | txn->rsp.eoh += delta; |
| 4361 | cur_end += delta; |
| 4362 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4363 | 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] | 4364 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4365 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4366 | cur_ptr, cur_end + 1, |
| 4367 | NULL, NULL); |
| 4368 | if (unlikely(!cur_end)) |
| 4369 | return -1; |
| 4370 | |
| 4371 | /* we have a full respnse and we know that we have either a CR |
| 4372 | * or an LF at <ptr>. |
| 4373 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4374 | 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] | 4375 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4376 | /* there is no point trying this regex on headers */ |
| 4377 | return 1; |
| 4378 | } |
| 4379 | } |
| 4380 | *cur_end = term; /* restore the string terminator */ |
| 4381 | return done; |
| 4382 | } |
| 4383 | |
| 4384 | |
| 4385 | |
| 4386 | /* |
| 4387 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4388 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4389 | * unparsable response. |
| 4390 | */ |
| 4391 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4392 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4393 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4394 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4395 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4396 | int ret; |
| 4397 | |
| 4398 | /* |
| 4399 | * The interleaving of transformations and verdicts |
| 4400 | * makes it difficult to decide to continue or stop |
| 4401 | * the evaluation. |
| 4402 | */ |
| 4403 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4404 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4405 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4406 | exp->action == ACT_PASS)) { |
| 4407 | exp = exp->next; |
| 4408 | continue; |
| 4409 | } |
| 4410 | |
| 4411 | /* Apply the filter to the status line. */ |
| 4412 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4413 | if (unlikely(ret < 0)) |
| 4414 | return -1; |
| 4415 | |
| 4416 | if (likely(ret == 0)) { |
| 4417 | /* The filter did not match the response, it can be |
| 4418 | * iterated through all headers. |
| 4419 | */ |
| 4420 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4421 | } |
| 4422 | exp = exp->next; |
| 4423 | } |
| 4424 | return 0; |
| 4425 | } |
| 4426 | |
| 4427 | |
| 4428 | |
| 4429 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4430 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4431 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4432 | */ |
| 4433 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4434 | { |
| 4435 | struct http_txn *txn = &t->txn; |
| 4436 | char *p1, *p2, *p3, *p4; |
| 4437 | |
| 4438 | appsess *asession_temp = NULL; |
| 4439 | appsess local_asession; |
| 4440 | |
| 4441 | char *cur_ptr, *cur_end, *cur_next; |
| 4442 | int cur_idx, old_idx, delta; |
| 4443 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4444 | /* Iterate through the headers. |
| 4445 | * we start with the start line. |
| 4446 | */ |
| 4447 | old_idx = 0; |
| 4448 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4449 | |
| 4450 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4451 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4452 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4453 | |
| 4454 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4455 | cur_ptr = cur_next; |
| 4456 | cur_end = cur_ptr + cur_hdr->len; |
| 4457 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4458 | |
| 4459 | /* We have one full header between cur_ptr and cur_end, and the |
| 4460 | * next header starts at cur_next. We're only interested in |
| 4461 | * "Cookie:" headers. |
| 4462 | */ |
| 4463 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4464 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4465 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4466 | old_idx = cur_idx; |
| 4467 | continue; |
| 4468 | } |
| 4469 | |
| 4470 | /* 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] | 4471 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4472 | |
| 4473 | |
| 4474 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4475 | if (t->be->cookie_name == NULL && |
| 4476 | t->be->appsession_name == NULL && |
| 4477 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4478 | return; |
| 4479 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4480 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4481 | |
| 4482 | 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] | 4483 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4484 | break; |
| 4485 | |
| 4486 | /* p1 is at the beginning of the cookie name */ |
| 4487 | p2 = p1; |
| 4488 | |
| 4489 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4490 | p2++; |
| 4491 | |
| 4492 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4493 | break; |
| 4494 | |
| 4495 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4496 | if (p3 == cur_end) |
| 4497 | break; |
| 4498 | |
| 4499 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4500 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4501 | p4++; |
| 4502 | |
| 4503 | /* here, we have the cookie name between p1 and p2, |
| 4504 | * and its value between p3 and p4. |
| 4505 | * we can process it. |
| 4506 | */ |
| 4507 | |
| 4508 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4509 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4510 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4511 | (p4 - p1 >= t->be->capture_namelen) && |
| 4512 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4513 | int log_len = p4 - p1; |
| 4514 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4515 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4516 | Alert("HTTP logging : out of memory.\n"); |
| 4517 | } |
| 4518 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4519 | if (log_len > t->be->capture_len) |
| 4520 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4521 | memcpy(txn->srv_cookie, p1, log_len); |
| 4522 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4526 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4527 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4528 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4529 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4530 | |
| 4531 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4532 | * this occurrence because we'll insert another one later. |
| 4533 | * We'll delete it too if the "indirect" option is set and we're in |
| 4534 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4535 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4536 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4537 | /* this header must be deleted */ |
| 4538 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4539 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4540 | txn->hdr_idx.used--; |
| 4541 | cur_hdr->len = 0; |
| 4542 | cur_next += delta; |
| 4543 | txn->rsp.eoh += delta; |
| 4544 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4545 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4546 | } |
| 4547 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4548 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4549 | /* replace bytes p3->p4 with the cookie name associated |
| 4550 | * with this server since we know it. |
| 4551 | */ |
| 4552 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4553 | cur_hdr->len += delta; |
| 4554 | cur_next += delta; |
| 4555 | txn->rsp.eoh += delta; |
| 4556 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4557 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4558 | } |
| 4559 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4560 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4561 | /* insert the cookie name associated with this server |
| 4562 | * before existing cookie, and insert a delimitor between them.. |
| 4563 | */ |
| 4564 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4565 | cur_hdr->len += delta; |
| 4566 | cur_next += delta; |
| 4567 | txn->rsp.eoh += delta; |
| 4568 | |
| 4569 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4570 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4571 | } |
| 4572 | } |
| 4573 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4574 | else if ((t->be->appsession_name != NULL) && |
| 4575 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4576 | |
| 4577 | /* Cool... it's the right one */ |
| 4578 | |
| 4579 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4580 | asession_temp = &local_asession; |
| 4581 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4582 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4583 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4584 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4585 | return; |
| 4586 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4587 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4588 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4589 | asession_temp->serverid = NULL; |
| 4590 | |
| 4591 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4592 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4593 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4594 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4595 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4596 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4597 | return; |
| 4598 | } |
| 4599 | asession_temp->sessid = local_asession.sessid; |
| 4600 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4601 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4602 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4603 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4604 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4607 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4608 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4609 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4610 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4611 | return; |
| 4612 | } |
| 4613 | asession_temp->serverid[0] = '\0'; |
| 4614 | } |
| 4615 | |
| 4616 | if (asession_temp->serverid[0] == '\0') |
| 4617 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4618 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4619 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4620 | |
| 4621 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4622 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4623 | #endif |
| 4624 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4625 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4626 | } /* we're now at the end of the cookie value */ |
| 4627 | |
| 4628 | /* keep the link from this header to next one */ |
| 4629 | old_idx = cur_idx; |
| 4630 | } /* end of cookie processing on this header */ |
| 4631 | } |
| 4632 | |
| 4633 | |
| 4634 | |
| 4635 | /* |
| 4636 | * Check if response is cacheable or not. Updates t->flags. |
| 4637 | */ |
| 4638 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4639 | { |
| 4640 | struct http_txn *txn = &t->txn; |
| 4641 | char *p1, *p2; |
| 4642 | |
| 4643 | char *cur_ptr, *cur_end, *cur_next; |
| 4644 | int cur_idx; |
| 4645 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4646 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4647 | return; |
| 4648 | |
| 4649 | /* Iterate through the headers. |
| 4650 | * we start with the start line. |
| 4651 | */ |
| 4652 | cur_idx = 0; |
| 4653 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4654 | |
| 4655 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4656 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4657 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4658 | |
| 4659 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4660 | cur_ptr = cur_next; |
| 4661 | cur_end = cur_ptr + cur_hdr->len; |
| 4662 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4663 | |
| 4664 | /* We have one full header between cur_ptr and cur_end, and the |
| 4665 | * next header starts at cur_next. We're only interested in |
| 4666 | * "Cookie:" headers. |
| 4667 | */ |
| 4668 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4669 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4670 | if (val) { |
| 4671 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4672 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4673 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4674 | return; |
| 4675 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4676 | } |
| 4677 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4678 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4679 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4680 | continue; |
| 4681 | |
| 4682 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4683 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4684 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4685 | |
| 4686 | if (p1 >= cur_end) /* no more info */ |
| 4687 | continue; |
| 4688 | |
| 4689 | /* p1 is at the beginning of the value */ |
| 4690 | p2 = p1; |
| 4691 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4692 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4693 | p2++; |
| 4694 | |
| 4695 | /* we have a complete value between p1 and p2 */ |
| 4696 | if (p2 < cur_end && *p2 == '=') { |
| 4697 | /* we have something of the form no-cache="set-cookie" */ |
| 4698 | if ((cur_end - p1 >= 21) && |
| 4699 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4700 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4701 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4702 | continue; |
| 4703 | } |
| 4704 | |
| 4705 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4706 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4707 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4708 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4709 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4710 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4711 | return; |
| 4712 | } |
| 4713 | |
| 4714 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4715 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4716 | continue; |
| 4717 | } |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4722 | /* |
| 4723 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4724 | * If the server is found, it's assigned to the session. |
| 4725 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4726 | 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] | 4727 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4728 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4729 | appsess *asession_temp = NULL; |
| 4730 | appsess local_asession; |
| 4731 | char *request_line; |
| 4732 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4733 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4734 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4735 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4736 | ((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] | 4737 | return; |
| 4738 | |
| 4739 | /* skip ';' */ |
| 4740 | request_line++; |
| 4741 | |
| 4742 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4743 | 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] | 4744 | return; |
| 4745 | |
| 4746 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4747 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4748 | |
| 4749 | /* First try if we already have an appsession */ |
| 4750 | asession_temp = &local_asession; |
| 4751 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4752 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4753 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4754 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4755 | return; |
| 4756 | } |
| 4757 | |
| 4758 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4759 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4760 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4761 | asession_temp->serverid = NULL; |
| 4762 | |
| 4763 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4764 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4765 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4766 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4767 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4768 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4769 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4770 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4771 | return; |
| 4772 | } |
| 4773 | asession_temp->sessid = local_asession.sessid; |
| 4774 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4775 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4776 | } |
| 4777 | else { |
| 4778 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4779 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4780 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4782 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4783 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4784 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4785 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4786 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4787 | #endif |
| 4788 | if (asession_temp->serverid == NULL) { |
| 4789 | Alert("Found Application Session without matching server.\n"); |
| 4790 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4791 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4792 | while (srv) { |
| 4793 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4794 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4795 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4796 | txn->flags &= ~TX_CK_MASK; |
| 4797 | txn->flags |= TX_CK_VALID; |
| 4798 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4799 | t->srv = srv; |
| 4800 | break; |
| 4801 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4802 | txn->flags &= ~TX_CK_MASK; |
| 4803 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | srv = srv->next; |
| 4807 | } |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4812 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4813 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4814 | * 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] | 4815 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4816 | * 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] | 4817 | * 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] | 4818 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4819 | * |
| 4820 | * Returns 1 if the session's state changes, otherwise 0. |
| 4821 | */ |
| 4822 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4823 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4824 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4825 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4826 | struct user_auth *user; |
| 4827 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4828 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4829 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4830 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 4831 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4832 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4833 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4834 | return 0; |
| 4835 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4836 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4837 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4838 | /* the URI is in h */ |
| 4839 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4840 | return 0; |
| 4841 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4842 | h += uri_auth->uri_len; |
| 4843 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4844 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4845 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4846 | break; |
| 4847 | } |
| 4848 | h++; |
| 4849 | } |
| 4850 | |
| 4851 | if (uri_auth->refresh) { |
| 4852 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4853 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4854 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4855 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4856 | break; |
| 4857 | } |
| 4858 | h++; |
| 4859 | } |
| 4860 | } |
| 4861 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4862 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4863 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4864 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4865 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4866 | break; |
| 4867 | } |
| 4868 | h++; |
| 4869 | } |
| 4870 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4871 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 4872 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4873 | /* we are in front of a interceptable URI. Let's check |
| 4874 | * if there's an authentication and if it's valid. |
| 4875 | */ |
| 4876 | user = uri_auth->users; |
| 4877 | if (!user) { |
| 4878 | /* no user auth required, it's OK */ |
| 4879 | authenticated = 1; |
| 4880 | } else { |
| 4881 | authenticated = 0; |
| 4882 | |
| 4883 | /* a user list is defined, we have to check. |
| 4884 | * skip 21 chars for "Authorization: Basic ". |
| 4885 | */ |
| 4886 | |
| 4887 | /* FIXME: this should move to an earlier place */ |
| 4888 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4889 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4890 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4891 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4892 | if (len > 14 && |
| 4893 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4894 | txn->auth_hdr.str = h; |
| 4895 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4896 | break; |
| 4897 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4898 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4899 | } |
| 4900 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4901 | if (txn->auth_hdr.len < 21 || |
| 4902 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4903 | user = NULL; |
| 4904 | |
| 4905 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4906 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4907 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4908 | user->user_pwd, user->user_len)) { |
| 4909 | authenticated = 1; |
| 4910 | break; |
| 4911 | } |
| 4912 | user = user->next; |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4917 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4918 | |
| 4919 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4920 | msg.str = trash; |
| 4921 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4922 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4923 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4924 | if (!(t->flags & SN_ERR_MASK)) |
| 4925 | t->flags |= SN_ERR_PRXCOND; |
| 4926 | if (!(t->flags & SN_FINST_MASK)) |
| 4927 | t->flags |= SN_FINST_R; |
| 4928 | return 1; |
| 4929 | } |
| 4930 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame^] | 4931 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4932 | * data. |
| 4933 | */ |
| 4934 | t->cli_state = CL_STSHUTR; |
| 4935 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4936 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4937 | t->data_source = DATA_SRC_STATS; |
| 4938 | t->data_state = DATA_ST_INIT; |
| 4939 | produce_content(t); |
| 4940 | return 1; |
| 4941 | } |
| 4942 | |
| 4943 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4944 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4945 | * Print a debug line with a header |
| 4946 | */ |
| 4947 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4948 | { |
| 4949 | int len, max; |
| 4950 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4951 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4952 | max = end - start; |
| 4953 | UBOUND(max, sizeof(trash) - len - 1); |
| 4954 | len += strlcpy2(trash + len, start, max + 1); |
| 4955 | trash[len++] = '\n'; |
| 4956 | write(1, trash, len); |
| 4957 | } |
| 4958 | |
| 4959 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4960 | /************************************************************************/ |
| 4961 | /* The code below is dedicated to ACL parsing and matching */ |
| 4962 | /************************************************************************/ |
| 4963 | |
| 4964 | |
| 4965 | |
| 4966 | |
| 4967 | /* 1. Check on METHOD |
| 4968 | * We use the pre-parsed method if it is known, and store its number as an |
| 4969 | * integer. If it is unknown, we use the pointer and the length. |
| 4970 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4971 | 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] | 4972 | { |
| 4973 | int len, meth; |
| 4974 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4975 | len = strlen(*text); |
| 4976 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4977 | |
| 4978 | pattern->val.i = meth; |
| 4979 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4980 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4981 | if (!pattern->ptr.str) |
| 4982 | return 0; |
| 4983 | pattern->len = len; |
| 4984 | } |
| 4985 | return 1; |
| 4986 | } |
| 4987 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4988 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4989 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4990 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4991 | { |
| 4992 | int meth; |
| 4993 | struct http_txn *txn = l7; |
| 4994 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4995 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4996 | return 0; |
| 4997 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4998 | meth = txn->meth; |
| 4999 | test->i = meth; |
| 5000 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5001 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5002 | /* ensure the indexes are not affected */ |
| 5003 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5004 | test->len = txn->req.sl.rq.m_l; |
| 5005 | test->ptr = txn->req.sol; |
| 5006 | } |
| 5007 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5008 | return 1; |
| 5009 | } |
| 5010 | |
| 5011 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5012 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5013 | int icase; |
| 5014 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5015 | if (test->i != pattern->val.i) |
| 5016 | return 0; |
| 5017 | |
| 5018 | if (test->i != HTTP_METH_OTHER) |
| 5019 | return 1; |
| 5020 | |
| 5021 | /* Other method, we must compare the strings */ |
| 5022 | if (pattern->len != test->len) |
| 5023 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5024 | |
| 5025 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5026 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5027 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5028 | return 0; |
| 5029 | return 1; |
| 5030 | } |
| 5031 | |
| 5032 | /* 2. Check on Request/Status Version |
| 5033 | * We simply compare strings here. |
| 5034 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5035 | 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] | 5036 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5037 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5038 | if (!pattern->ptr.str) |
| 5039 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5040 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5041 | return 1; |
| 5042 | } |
| 5043 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5044 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5045 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5046 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5047 | { |
| 5048 | struct http_txn *txn = l7; |
| 5049 | char *ptr; |
| 5050 | int len; |
| 5051 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5052 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5053 | return 0; |
| 5054 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5055 | len = txn->req.sl.rq.v_l; |
| 5056 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5057 | |
| 5058 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5059 | if (len <= 0) |
| 5060 | return 0; |
| 5061 | |
| 5062 | test->ptr = ptr; |
| 5063 | test->len = len; |
| 5064 | |
| 5065 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5066 | return 1; |
| 5067 | } |
| 5068 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5069 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5070 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5071 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5072 | { |
| 5073 | struct http_txn *txn = l7; |
| 5074 | char *ptr; |
| 5075 | int len; |
| 5076 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5077 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5078 | return 0; |
| 5079 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5080 | len = txn->rsp.sl.st.v_l; |
| 5081 | ptr = txn->rsp.sol; |
| 5082 | |
| 5083 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5084 | if (len <= 0) |
| 5085 | return 0; |
| 5086 | |
| 5087 | test->ptr = ptr; |
| 5088 | test->len = len; |
| 5089 | |
| 5090 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5091 | return 1; |
| 5092 | } |
| 5093 | |
| 5094 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5095 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5096 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5097 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5098 | { |
| 5099 | struct http_txn *txn = l7; |
| 5100 | char *ptr; |
| 5101 | int len; |
| 5102 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5103 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5104 | return 0; |
| 5105 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5106 | len = txn->rsp.sl.st.c_l; |
| 5107 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5108 | |
| 5109 | test->i = __strl2ui(ptr, len); |
| 5110 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5111 | return 1; |
| 5112 | } |
| 5113 | |
| 5114 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5115 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5116 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5117 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5118 | { |
| 5119 | struct http_txn *txn = l7; |
| 5120 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5121 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5122 | return 0; |
| 5123 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5124 | /* ensure the indexes are not affected */ |
| 5125 | return 0; |
| 5126 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5127 | test->len = txn->req.sl.rq.u_l; |
| 5128 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5129 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5130 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5131 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5132 | return 1; |
| 5133 | } |
| 5134 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5135 | static int |
| 5136 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5137 | struct acl_expr *expr, struct acl_test *test) |
| 5138 | { |
| 5139 | struct http_txn *txn = l7; |
| 5140 | |
| 5141 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5142 | return 0; |
| 5143 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5144 | /* ensure the indexes are not affected */ |
| 5145 | return 0; |
| 5146 | |
| 5147 | /* Parse HTTP request */ |
| 5148 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5149 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5150 | test->i = AF_INET; |
| 5151 | |
| 5152 | /* |
| 5153 | * If we are parsing url in frontend space, we prepare backend stage |
| 5154 | * to not parse again the same url ! optimization lazyness... |
| 5155 | */ |
| 5156 | if (px->options & PR_O_HTTP_PROXY) |
| 5157 | l4->flags |= SN_ADDR_SET; |
| 5158 | |
| 5159 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5160 | return 1; |
| 5161 | } |
| 5162 | |
| 5163 | static int |
| 5164 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5165 | struct acl_expr *expr, struct acl_test *test) |
| 5166 | { |
| 5167 | struct http_txn *txn = l7; |
| 5168 | |
| 5169 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5170 | return 0; |
| 5171 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5172 | /* ensure the indexes are not affected */ |
| 5173 | return 0; |
| 5174 | |
| 5175 | /* Same optimization as url_ip */ |
| 5176 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5177 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5178 | |
| 5179 | if (px->options & PR_O_HTTP_PROXY) |
| 5180 | l4->flags |= SN_ADDR_SET; |
| 5181 | |
| 5182 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5183 | return 1; |
| 5184 | } |
| 5185 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5186 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5187 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5188 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5189 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5190 | 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] | 5191 | struct acl_expr *expr, struct acl_test *test) |
| 5192 | { |
| 5193 | struct http_txn *txn = l7; |
| 5194 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5195 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5196 | |
| 5197 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5198 | /* search for header from the beginning */ |
| 5199 | ctx->idx = 0; |
| 5200 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5201 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5202 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5203 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5204 | test->len = ctx->vlen; |
| 5205 | test->ptr = (char *)ctx->line + ctx->val; |
| 5206 | return 1; |
| 5207 | } |
| 5208 | |
| 5209 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5210 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5214 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5215 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5216 | struct acl_expr *expr, struct acl_test *test) |
| 5217 | { |
| 5218 | struct http_txn *txn = l7; |
| 5219 | |
| 5220 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5221 | return 0; |
| 5222 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5223 | /* ensure the indexes are not affected */ |
| 5224 | return 0; |
| 5225 | |
| 5226 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5227 | } |
| 5228 | |
| 5229 | static int |
| 5230 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5231 | struct acl_expr *expr, struct acl_test *test) |
| 5232 | { |
| 5233 | struct http_txn *txn = l7; |
| 5234 | |
| 5235 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5236 | return 0; |
| 5237 | |
| 5238 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5239 | } |
| 5240 | |
| 5241 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5242 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5243 | */ |
| 5244 | static int |
| 5245 | 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] | 5246 | struct acl_expr *expr, struct acl_test *test) |
| 5247 | { |
| 5248 | struct http_txn *txn = l7; |
| 5249 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5250 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5251 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5252 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5253 | ctx.idx = 0; |
| 5254 | cnt = 0; |
| 5255 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5256 | cnt++; |
| 5257 | |
| 5258 | test->i = cnt; |
| 5259 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5260 | return 1; |
| 5261 | } |
| 5262 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5263 | static int |
| 5264 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5265 | struct acl_expr *expr, struct acl_test *test) |
| 5266 | { |
| 5267 | struct http_txn *txn = l7; |
| 5268 | |
| 5269 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5270 | return 0; |
| 5271 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5272 | /* ensure the indexes are not affected */ |
| 5273 | return 0; |
| 5274 | |
| 5275 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5276 | } |
| 5277 | |
| 5278 | static int |
| 5279 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5280 | struct acl_expr *expr, struct acl_test *test) |
| 5281 | { |
| 5282 | struct http_txn *txn = l7; |
| 5283 | |
| 5284 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5285 | return 0; |
| 5286 | |
| 5287 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5288 | } |
| 5289 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5290 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5291 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5292 | * 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] | 5293 | */ |
| 5294 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5295 | 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] | 5296 | struct acl_expr *expr, struct acl_test *test) |
| 5297 | { |
| 5298 | struct http_txn *txn = l7; |
| 5299 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5300 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5301 | |
| 5302 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5303 | /* search for header from the beginning */ |
| 5304 | ctx->idx = 0; |
| 5305 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5306 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5307 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5308 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5309 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5310 | return 1; |
| 5311 | } |
| 5312 | |
| 5313 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5314 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5318 | static int |
| 5319 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5320 | struct acl_expr *expr, struct acl_test *test) |
| 5321 | { |
| 5322 | struct http_txn *txn = l7; |
| 5323 | |
| 5324 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5325 | return 0; |
| 5326 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5327 | /* ensure the indexes are not affected */ |
| 5328 | return 0; |
| 5329 | |
| 5330 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5331 | } |
| 5332 | |
| 5333 | static int |
| 5334 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5335 | struct acl_expr *expr, struct acl_test *test) |
| 5336 | { |
| 5337 | struct http_txn *txn = l7; |
| 5338 | |
| 5339 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5340 | return 0; |
| 5341 | |
| 5342 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5343 | } |
| 5344 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5345 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5346 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5347 | */ |
| 5348 | static int |
| 5349 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5350 | struct acl_expr *expr, struct acl_test *test) |
| 5351 | { |
| 5352 | struct http_txn *txn = l7; |
| 5353 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5354 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5355 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5356 | return 0; |
| 5357 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5358 | /* ensure the indexes are not affected */ |
| 5359 | return 0; |
| 5360 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5361 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5362 | ptr = http_get_path(txn); |
| 5363 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5364 | return 0; |
| 5365 | |
| 5366 | /* OK, we got the '/' ! */ |
| 5367 | test->ptr = ptr; |
| 5368 | |
| 5369 | while (ptr < end && *ptr != '?') |
| 5370 | ptr++; |
| 5371 | |
| 5372 | test->len = ptr - test->ptr; |
| 5373 | |
| 5374 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5375 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5376 | return 1; |
| 5377 | } |
| 5378 | |
| 5379 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5380 | |
| 5381 | /************************************************************************/ |
| 5382 | /* All supported keywords must be declared here. */ |
| 5383 | /************************************************************************/ |
| 5384 | |
| 5385 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5386 | static struct acl_kw_list acl_kws = {{ },{ |
| 5387 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5388 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5389 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5390 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5391 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5392 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5393 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5394 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5395 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5396 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5397 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5398 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
| 5399 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip }, |
| 5400 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5401 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5402 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5403 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5404 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5405 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5406 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5407 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5408 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5409 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5410 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5411 | |
| 5412 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5413 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5414 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5415 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5416 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5417 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5418 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5419 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5420 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5421 | |
| 5422 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5423 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5424 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5425 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5426 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5427 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5428 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5429 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5430 | { NULL, NULL, NULL, NULL }, |
| 5431 | |
| 5432 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5433 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5434 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5435 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5436 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5437 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5438 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5439 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5440 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5441 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5442 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5443 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5444 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5445 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5446 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5447 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5448 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5449 | |
| 5450 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5451 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5452 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5453 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5454 | { NULL, NULL, NULL, NULL }, |
| 5455 | #endif |
| 5456 | }}; |
| 5457 | |
| 5458 | |
| 5459 | __attribute__((constructor)) |
| 5460 | static void __http_protocol_init(void) |
| 5461 | { |
| 5462 | acl_register_keywords(&acl_kws); |
| 5463 | } |
| 5464 | |
| 5465 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5466 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5467 | * Local variables: |
| 5468 | * c-indent-level: 8 |
| 5469 | * c-basic-offset: 8 |
| 5470 | * End: |
| 5471 | */ |