Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
| 33 | #include <common/time.h> |
| 34 | #include <common/uri_auth.h> |
| 35 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 37 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <types/capture.h> |
| 39 | #include <types/client.h> |
| 40 | #include <types/global.h> |
| 41 | #include <types/httperr.h> |
| 42 | #include <types/polling.h> |
| 43 | #include <types/proxy.h> |
| 44 | #include <types/server.h> |
| 45 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 46 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
| 51 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 52 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
| 54 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 55 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/session.h> |
| 57 | #include <proto/task.h> |
| 58 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_HAP_TCPSPLICE |
| 60 | #include <libtcpsplice.h> |
| 61 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 63 | #define DEBUG_PARSE_NO_SPEEDUP |
| 64 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 65 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 66 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 67 | * instruction. The first argument is the label for normal operation, and the |
| 68 | * second one is the break/continue instruction in the no_speedup mode. |
| 69 | */ |
| 70 | |
| 71 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 72 | #define QUICK_JUMP(x,y) y |
| 73 | #else |
| 74 | #define QUICK_JUMP(x,y) goto x |
| 75 | #endif |
| 76 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 79 | "HTTP/1.0 200 OK\r\n" |
| 80 | "Cache-Control: no-cache\r\n" |
| 81 | "Connection: close\r\n" |
| 82 | "Content-Type: text/html\r\n" |
| 83 | "\r\n" |
| 84 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 85 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | const struct chunk http_200_chunk = { |
| 87 | .str = (char *)&HTTP_200, |
| 88 | .len = sizeof(HTTP_200)-1 |
| 89 | }; |
| 90 | |
| 91 | const char *HTTP_302 = |
| 92 | "HTTP/1.0 302 Found\r\n" |
| 93 | "Cache-Control: no-cache\r\n" |
| 94 | "Connection: close\r\n" |
| 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
| 99 | "HTTP/1.0 303 See Other\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Connection: close\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 114 | |
| 115 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 133 | |
| 134 | [HTTP_ERR_403] = |
| 135 | "HTTP/1.0 403 Forbidden\r\n" |
| 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_408] = |
| 143 | "HTTP/1.0 408 Request Time-out\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_500] = |
| 151 | "HTTP/1.0 500 Server Error\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_502] = |
| 159 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_503] = |
| 167 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_504] = |
| 175 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 181 | |
| 182 | }; |
| 183 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 184 | /* We must put the messages here since GCC cannot initialize consts depending |
| 185 | * on strlen(). |
| 186 | */ |
| 187 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 188 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 189 | #define FD_SETS_ARE_BITFIELDS |
| 190 | #ifdef FD_SETS_ARE_BITFIELDS |
| 191 | /* |
| 192 | * This map is used with all the FD_* macros to check whether a particular bit |
| 193 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 194 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 195 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 196 | * exclusively to the macros. |
| 197 | */ |
| 198 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 199 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 200 | |
| 201 | #else |
| 202 | #error "Check if your OS uses bitfields for fd_sets" |
| 203 | #endif |
| 204 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 205 | void init_proto_http() |
| 206 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 207 | int i; |
| 208 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 212 | if (!http_err_msgs[msg]) { |
| 213 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 214 | abort(); |
| 215 | } |
| 216 | |
| 217 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 218 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 219 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
| 221 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 222 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 223 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 224 | * printable characters above. |
| 225 | */ |
| 226 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 227 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 228 | for (i = 0; i < 32; i++) { |
| 229 | FD_SET(i, hdr_encode_map); |
| 230 | FD_SET(i, url_encode_map); |
| 231 | } |
| 232 | for (i = 127; i < 256; i++) { |
| 233 | FD_SET(i, hdr_encode_map); |
| 234 | FD_SET(i, url_encode_map); |
| 235 | } |
| 236 | |
| 237 | tmp = "\"#{|}"; |
| 238 | while (*tmp) { |
| 239 | FD_SET(*tmp, hdr_encode_map); |
| 240 | tmp++; |
| 241 | } |
| 242 | |
| 243 | tmp = "\"#"; |
| 244 | while (*tmp) { |
| 245 | FD_SET(*tmp, url_encode_map); |
| 246 | tmp++; |
| 247 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 248 | |
| 249 | /* memory allocations */ |
| 250 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 251 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 252 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 254 | /* |
| 255 | * We have 26 list of methods (1 per first letter), each of which can have |
| 256 | * up to 3 entries (2 valid, 1 null). |
| 257 | */ |
| 258 | struct http_method_desc { |
| 259 | http_meth_t meth; |
| 260 | int len; |
| 261 | const char text[8]; |
| 262 | }; |
| 263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 264 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 265 | ['C' - 'A'] = { |
| 266 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 267 | }, |
| 268 | ['D' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 270 | }, |
| 271 | ['G' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 273 | }, |
| 274 | ['H' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 276 | }, |
| 277 | ['P' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 279 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 280 | }, |
| 281 | ['T' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 283 | }, |
| 284 | /* rest is empty like this : |
| 285 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 286 | */ |
| 287 | }; |
| 288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 289 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 290 | * table than two perform a boolean AND or OR between two tests. Refer to |
| 291 | * RFC2616 for those chars. |
| 292 | */ |
| 293 | |
| 294 | const char http_is_spht[256] = { |
| 295 | [' '] = 1, ['\t'] = 1, |
| 296 | }; |
| 297 | |
| 298 | const char http_is_crlf[256] = { |
| 299 | ['\r'] = 1, ['\n'] = 1, |
| 300 | }; |
| 301 | |
| 302 | const char http_is_lws[256] = { |
| 303 | [' '] = 1, ['\t'] = 1, |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_sep[256] = { |
| 308 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 309 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 310 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 311 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 312 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 313 | }; |
| 314 | |
| 315 | const char http_is_ctl[256] = { |
| 316 | [0 ... 31] = 1, |
| 317 | [127] = 1, |
| 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 322 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 323 | * them correctly. Instead, define every non-CTL char's status. |
| 324 | */ |
| 325 | const char http_is_token[256] = { |
| 326 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 327 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 328 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 329 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 330 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 331 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 332 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 333 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 334 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 335 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 336 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 337 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 338 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 339 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 340 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 341 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 342 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 343 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 344 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 345 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 346 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 347 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 348 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 349 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 350 | }; |
| 351 | |
| 352 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 353 | /* |
| 354 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 355 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 356 | */ |
| 357 | const char http_is_ver_token[256] = { |
| 358 | ['.'] = 1, ['/'] = 1, |
| 359 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 360 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 361 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 362 | }; |
| 363 | |
| 364 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | #ifdef DEBUG_FULL |
| 366 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 367 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 368 | #endif |
| 369 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 370 | static void http_sess_log(struct session *s); |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 538 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 539 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 540 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 541 | */ |
| 542 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 543 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | { |
| 545 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 546 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 547 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 548 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 549 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 550 | } |
| 551 | if (!(t->flags & SN_ERR_MASK)) |
| 552 | t->flags |= err; |
| 553 | if (!(t->flags & SN_FINST_MASK)) |
| 554 | t->flags |= finst; |
| 555 | } |
| 556 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 557 | /* This function returns the appropriate error location for the given session |
| 558 | * and message. |
| 559 | */ |
| 560 | |
| 561 | struct chunk *error_message(struct session *s, int msgnum) |
| 562 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 563 | if (s->be->errmsg[msgnum].str) |
| 564 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 565 | else if (s->fe->errmsg[msgnum].str) |
| 566 | return &s->fe->errmsg[msgnum]; |
| 567 | else |
| 568 | return &http_err_chunks[msgnum]; |
| 569 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 571 | /* |
| 572 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 573 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 574 | */ |
| 575 | static http_meth_t find_http_meth(const char *str, const int len) |
| 576 | { |
| 577 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 578 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 579 | |
| 580 | m = ((unsigned)*str - 'A'); |
| 581 | |
| 582 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 583 | for (h = http_methods[m]; h->len > 0; h++) { |
| 584 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 585 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 587 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | }; |
| 589 | return HTTP_METH_OTHER; |
| 590 | } |
| 591 | return HTTP_METH_NONE; |
| 592 | |
| 593 | } |
| 594 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | /* Processes the client and server jobs of a session task, then |
| 596 | * puts it back to the wait queue in a clean state, or |
| 597 | * cleans up its resources if it must be deleted. Returns |
| 598 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 599 | * infinity. |
| 600 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 601 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 602 | { |
| 603 | struct session *s = t->context; |
| 604 | int fsm_resync = 0; |
| 605 | |
| 606 | do { |
| 607 | fsm_resync = 0; |
| 608 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 609 | fsm_resync |= process_cli(s); |
| 610 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 611 | fsm_resync |= process_srv(s); |
| 612 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 613 | } while (fsm_resync); |
| 614 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 615 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 616 | |
| 617 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 618 | session_process_counters(s); |
| 619 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 620 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 621 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 622 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 623 | t->expire = s->req->rex; |
| 624 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 625 | tv_bound(&t->expire, &s->req->cex); |
| 626 | tv_bound(&t->expire, &s->rep->rex); |
| 627 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 628 | |
| 629 | /* restore t to its place in the task list */ |
| 630 | task_queue(t); |
| 631 | |
| 632 | #ifdef DEBUG_FULL |
| 633 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 634 | * that a task still has something to do and will provoke a quick loop. |
| 635 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 636 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | exit(100); |
| 638 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 639 | *next = t->expire; |
| 640 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 643 | s->fe->feconn--; |
| 644 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 645 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 646 | actconn--; |
| 647 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 648 | if (unlikely((global.mode & MODE_DEBUG) && |
| 649 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 651 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 652 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 653 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | write(1, trash, len); |
| 655 | } |
| 656 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 657 | 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] | 658 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 659 | |
| 660 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 661 | if (s->logs.logwait && |
| 662 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 663 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 664 | if (s->fe->to_log & LW_REQ) |
| 665 | http_sess_log(s); |
| 666 | else |
| 667 | tcp_sess_log(s); |
| 668 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | |
| 670 | /* the task MUST not be in the run queue anymore */ |
| 671 | task_delete(t); |
| 672 | session_free(s); |
| 673 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 674 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 678 | extern const char sess_term_cond[8]; |
| 679 | extern const char sess_fin_state[8]; |
| 680 | extern const char *monthname[12]; |
| 681 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 682 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 683 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 684 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 685 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 686 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 687 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 688 | /* |
| 689 | * send a log for the session when we have enough info about it. |
| 690 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 691 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 692 | static void http_sess_log(struct session *s) |
| 693 | { |
| 694 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 695 | struct proxy *fe = s->fe; |
| 696 | struct proxy *be = s->be; |
| 697 | struct proxy *prx_log; |
| 698 | struct http_txn *txn = &s->txn; |
| 699 | int tolog; |
| 700 | char *uri, *h; |
| 701 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 702 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 703 | static char tmpline[MAX_SYSLOG_LEN]; |
| 704 | int hdr; |
| 705 | |
| 706 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 707 | return; |
| 708 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 709 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 710 | if (s->cli_addr.ss_family == AF_INET) |
| 711 | inet_ntop(AF_INET, |
| 712 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 713 | pn, sizeof(pn)); |
| 714 | else |
| 715 | inet_ntop(AF_INET6, |
| 716 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 717 | pn, sizeof(pn)); |
| 718 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 719 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 720 | |
| 721 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 722 | tolog = fe->to_log; |
| 723 | |
| 724 | h = tmpline; |
| 725 | if (fe->to_log & LW_REQHDR && |
| 726 | txn->req.cap && |
| 727 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 728 | *(h++) = ' '; |
| 729 | *(h++) = '{'; |
| 730 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 731 | if (hdr) |
| 732 | *(h++) = '|'; |
| 733 | if (txn->req.cap[hdr] != NULL) |
| 734 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 735 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 736 | } |
| 737 | *(h++) = '}'; |
| 738 | } |
| 739 | |
| 740 | if (fe->to_log & LW_RSPHDR && |
| 741 | txn->rsp.cap && |
| 742 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 743 | *(h++) = ' '; |
| 744 | *(h++) = '{'; |
| 745 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 746 | if (hdr) |
| 747 | *(h++) = '|'; |
| 748 | if (txn->rsp.cap[hdr] != NULL) |
| 749 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 750 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 751 | } |
| 752 | *(h++) = '}'; |
| 753 | } |
| 754 | |
| 755 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 756 | *(h++) = ' '; |
| 757 | *(h++) = '"'; |
| 758 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 759 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 760 | '#', url_encode_map, uri); |
| 761 | *(h++) = '"'; |
| 762 | } |
| 763 | *h = '\0'; |
| 764 | |
| 765 | svid = (tolog & LW_SVID) ? |
| 766 | (s->data_source != DATA_SRC_STATS) ? |
| 767 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 768 | |
| 769 | send_log(prx_log, LOG_INFO, |
| 770 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 771 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
| 772 | " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n", |
| 773 | pn, |
| 774 | (s->cli_addr.ss_family == AF_INET) ? |
| 775 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 776 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 777 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 778 | 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] | 779 | fe->id, be->id, svid, |
| 780 | s->logs.t_request, |
| 781 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 782 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 783 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 784 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 785 | txn->status, |
| 786 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in, |
| 787 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 788 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 789 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 790 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 791 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 792 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 793 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 794 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 795 | |
| 796 | s->logs.logwait = 0; |
| 797 | } |
| 798 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * Capture headers from message starting at <som> according to header list |
| 802 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 803 | */ |
| 804 | void capture_headers(char *som, struct hdr_idx *idx, |
| 805 | char **cap, struct cap_hdr *cap_hdr) |
| 806 | { |
| 807 | char *eol, *sol, *col, *sov; |
| 808 | int cur_idx; |
| 809 | struct cap_hdr *h; |
| 810 | int len; |
| 811 | |
| 812 | sol = som + hdr_idx_first_pos(idx); |
| 813 | cur_idx = hdr_idx_first_idx(idx); |
| 814 | |
| 815 | while (cur_idx) { |
| 816 | eol = sol + idx->v[cur_idx].len; |
| 817 | |
| 818 | col = sol; |
| 819 | while (col < eol && *col != ':') |
| 820 | col++; |
| 821 | |
| 822 | sov = col + 1; |
| 823 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 824 | sov++; |
| 825 | |
| 826 | for (h = cap_hdr; h; h = h->next) { |
| 827 | if ((h->namelen == col - sol) && |
| 828 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 829 | if (cap[h->index] == NULL) |
| 830 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 831 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 832 | |
| 833 | if (cap[h->index] == NULL) { |
| 834 | Alert("HTTP capture : out of memory.\n"); |
| 835 | continue; |
| 836 | } |
| 837 | |
| 838 | len = eol - sov; |
| 839 | if (len > h->len) |
| 840 | len = h->len; |
| 841 | |
| 842 | memcpy(cap[h->index], sov, len); |
| 843 | cap[h->index][len]=0; |
| 844 | } |
| 845 | } |
| 846 | sol = eol + idx->v[cur_idx].cr + 1; |
| 847 | cur_idx = idx->v[cur_idx].next; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 852 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 853 | */ |
| 854 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 855 | |
| 856 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 857 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 858 | */ |
| 859 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 860 | ptr++; \ |
| 861 | if (likely(ptr < end)) \ |
| 862 | goto good; \ |
| 863 | else { \ |
| 864 | state = (st); \ |
| 865 | goto http_msg_ood; \ |
| 866 | } \ |
| 867 | } while (0) |
| 868 | |
| 869 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 870 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 871 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 872 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 873 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 874 | * will give undefined results. |
| 875 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 876 | * and that msg->sol points to the beginning of the response. |
| 877 | * If a complete line is found (which implies that at least one CR or LF is |
| 878 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 879 | * returned indicating an incomplete line (which does not mean that parts have |
| 880 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 881 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 882 | * upon next call. |
| 883 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 884 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 885 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 886 | * 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] | 887 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 888 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 889 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 890 | const char *ptr, const char *end, |
| 891 | char **ret_ptr, int *ret_state) |
| 892 | { |
| 893 | __label__ |
| 894 | http_msg_rpver, |
| 895 | http_msg_rpver_sp, |
| 896 | http_msg_rpcode, |
| 897 | http_msg_rpcode_sp, |
| 898 | http_msg_rpreason, |
| 899 | http_msg_rpline_eol, |
| 900 | http_msg_ood, /* out of data */ |
| 901 | http_msg_invalid; |
| 902 | |
| 903 | switch (state) { |
| 904 | http_msg_rpver: |
| 905 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 906 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 907 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 908 | |
| 909 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 910 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 911 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 912 | } |
| 913 | goto http_msg_invalid; |
| 914 | |
| 915 | http_msg_rpver_sp: |
| 916 | case HTTP_MSG_RPVER_SP: |
| 917 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 918 | msg->sl.st.c = ptr - msg_buf; |
| 919 | goto http_msg_rpcode; |
| 920 | } |
| 921 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 922 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 923 | /* so it's a CR/LF, this is invalid */ |
| 924 | goto http_msg_invalid; |
| 925 | |
| 926 | http_msg_rpcode: |
| 927 | case HTTP_MSG_RPCODE: |
| 928 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 929 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 930 | |
| 931 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 932 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 933 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 934 | } |
| 935 | |
| 936 | /* so it's a CR/LF, so there is no reason phrase */ |
| 937 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 938 | http_msg_rsp_reason: |
| 939 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 940 | msg->sl.st.r = ptr - msg_buf; |
| 941 | msg->sl.st.r_l = 0; |
| 942 | goto http_msg_rpline_eol; |
| 943 | |
| 944 | http_msg_rpcode_sp: |
| 945 | case HTTP_MSG_RPCODE_SP: |
| 946 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 947 | msg->sl.st.r = ptr - msg_buf; |
| 948 | goto http_msg_rpreason; |
| 949 | } |
| 950 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 951 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 952 | /* so it's a CR/LF, so there is no reason phrase */ |
| 953 | goto http_msg_rsp_reason; |
| 954 | |
| 955 | http_msg_rpreason: |
| 956 | case HTTP_MSG_RPREASON: |
| 957 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 958 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 959 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 960 | http_msg_rpline_eol: |
| 961 | /* We have seen the end of line. Note that we do not |
| 962 | * necessarily have the \n yet, but at least we know that we |
| 963 | * have EITHER \r OR \n, otherwise the response would not be |
| 964 | * complete. We can then record the response length and return |
| 965 | * to the caller which will be able to register it. |
| 966 | */ |
| 967 | msg->sl.st.l = ptr - msg->sol; |
| 968 | return ptr; |
| 969 | |
| 970 | #ifdef DEBUG_FULL |
| 971 | default: |
| 972 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 973 | exit(1); |
| 974 | #endif |
| 975 | } |
| 976 | |
| 977 | http_msg_ood: |
| 978 | /* out of data */ |
| 979 | if (ret_state) |
| 980 | *ret_state = state; |
| 981 | if (ret_ptr) |
| 982 | *ret_ptr = (char *)ptr; |
| 983 | return NULL; |
| 984 | |
| 985 | http_msg_invalid: |
| 986 | /* invalid message */ |
| 987 | if (ret_state) |
| 988 | *ret_state = HTTP_MSG_ERROR; |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | |
| 993 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 994 | * This function parses a request line between <ptr> and <end>, starting with |
| 995 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 996 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 997 | * will give undefined results. |
| 998 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 999 | * and that msg->sol points to the beginning of the request. |
| 1000 | * If a complete line is found (which implies that at least one CR or LF is |
| 1001 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1002 | * returned indicating an incomplete line (which does not mean that parts have |
| 1003 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1004 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1005 | * upon next call. |
| 1006 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1007 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1008 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1009 | * 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] | 1010 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1011 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1012 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1013 | const char *ptr, const char *end, |
| 1014 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1015 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1016 | __label__ |
| 1017 | http_msg_rqmeth, |
| 1018 | http_msg_rqmeth_sp, |
| 1019 | http_msg_rquri, |
| 1020 | http_msg_rquri_sp, |
| 1021 | http_msg_rqver, |
| 1022 | http_msg_rqline_eol, |
| 1023 | http_msg_ood, /* out of data */ |
| 1024 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1025 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1026 | switch (state) { |
| 1027 | http_msg_rqmeth: |
| 1028 | case HTTP_MSG_RQMETH: |
| 1029 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1030 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1032 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1033 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1034 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1035 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1036 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1037 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1038 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1039 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1040 | http_msg_req09_uri: |
| 1041 | msg->sl.rq.u = ptr - msg_buf; |
| 1042 | http_msg_req09_uri_e: |
| 1043 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1044 | http_msg_req09_ver: |
| 1045 | msg->sl.rq.v = ptr - msg_buf; |
| 1046 | msg->sl.rq.v_l = 0; |
| 1047 | goto http_msg_rqline_eol; |
| 1048 | } |
| 1049 | goto http_msg_invalid; |
| 1050 | |
| 1051 | http_msg_rqmeth_sp: |
| 1052 | case HTTP_MSG_RQMETH_SP: |
| 1053 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1054 | msg->sl.rq.u = ptr - msg_buf; |
| 1055 | goto http_msg_rquri; |
| 1056 | } |
| 1057 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1058 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1059 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1060 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1061 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1062 | http_msg_rquri: |
| 1063 | case HTTP_MSG_RQURI: |
| 1064 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1065 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1066 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1068 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1069 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1070 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1071 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1073 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1074 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1075 | http_msg_rquri_sp: |
| 1076 | case HTTP_MSG_RQURI_SP: |
| 1077 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1078 | msg->sl.rq.v = ptr - msg_buf; |
| 1079 | goto http_msg_rqver; |
| 1080 | } |
| 1081 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1082 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1083 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1084 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1086 | http_msg_rqver: |
| 1087 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1088 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1090 | |
| 1091 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1092 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1093 | http_msg_rqline_eol: |
| 1094 | /* We have seen the end of line. Note that we do not |
| 1095 | * necessarily have the \n yet, but at least we know that we |
| 1096 | * have EITHER \r OR \n, otherwise the request would not be |
| 1097 | * complete. We can then record the request length and return |
| 1098 | * to the caller which will be able to register it. |
| 1099 | */ |
| 1100 | msg->sl.rq.l = ptr - msg->sol; |
| 1101 | return ptr; |
| 1102 | } |
| 1103 | |
| 1104 | /* neither an HTTP_VER token nor a CRLF */ |
| 1105 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1106 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | #ifdef DEBUG_FULL |
| 1108 | default: |
| 1109 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1110 | exit(1); |
| 1111 | #endif |
| 1112 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1114 | http_msg_ood: |
| 1115 | /* out of data */ |
| 1116 | if (ret_state) |
| 1117 | *ret_state = state; |
| 1118 | if (ret_ptr) |
| 1119 | *ret_ptr = (char *)ptr; |
| 1120 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1122 | http_msg_invalid: |
| 1123 | /* invalid message */ |
| 1124 | if (ret_state) |
| 1125 | *ret_state = HTTP_MSG_ERROR; |
| 1126 | return NULL; |
| 1127 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1128 | |
| 1129 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1130 | /* |
| 1131 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1132 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | * when data are missing and recalled at the exact same location with no |
| 1134 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1135 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1136 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1137 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1138 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1139 | { |
| 1140 | __label__ |
| 1141 | http_msg_rqbefore, |
| 1142 | http_msg_rqbefore_cr, |
| 1143 | http_msg_rqmeth, |
| 1144 | http_msg_rqline_end, |
| 1145 | http_msg_hdr_first, |
| 1146 | http_msg_hdr_name, |
| 1147 | http_msg_hdr_l1_sp, |
| 1148 | http_msg_hdr_l1_lf, |
| 1149 | http_msg_hdr_l1_lws, |
| 1150 | http_msg_hdr_val, |
| 1151 | http_msg_hdr_l2_lf, |
| 1152 | http_msg_hdr_l2_lws, |
| 1153 | http_msg_complete_header, |
| 1154 | http_msg_last_lf, |
| 1155 | http_msg_ood, /* out of data */ |
| 1156 | 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 | int state; /* updated only when leaving the FSM */ |
| 1159 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1160 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1161 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1162 | ptr = buf->lr; |
| 1163 | end = buf->r; |
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 | if (unlikely(ptr >= end)) |
| 1166 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1168 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1169 | /* |
| 1170 | * First, states that are specific to the response only. |
| 1171 | * We check them first so that request and headers are |
| 1172 | * closer to each other (accessed more often). |
| 1173 | */ |
| 1174 | http_msg_rpbefore: |
| 1175 | case HTTP_MSG_RPBEFORE: |
| 1176 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1177 | if (likely(ptr == buf->data)) { |
| 1178 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1179 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1180 | } else { |
| 1181 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1182 | /* only skip empty leading lines, don't remove them */ |
| 1183 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1184 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1185 | #else |
| 1186 | /* Remove empty leading lines, as recommended by |
| 1187 | * RFC2616. This takes a lot of time because we |
| 1188 | * must move all the buffer backwards, but this |
| 1189 | * is rarely needed. The method above will be |
| 1190 | * cleaner when we'll be able to start sending |
| 1191 | * the request from any place in the buffer. |
| 1192 | */ |
| 1193 | buf->lr = ptr; |
| 1194 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1195 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | msg->sol = buf->data; |
| 1197 | ptr = buf->data; |
| 1198 | end = buf->r; |
| 1199 | #endif |
| 1200 | } |
| 1201 | hdr_idx_init(idx); |
| 1202 | state = HTTP_MSG_RPVER; |
| 1203 | goto http_msg_rpver; |
| 1204 | } |
| 1205 | |
| 1206 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1207 | goto http_msg_invalid; |
| 1208 | |
| 1209 | if (unlikely(*ptr == '\n')) |
| 1210 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1211 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1212 | /* stop here */ |
| 1213 | |
| 1214 | http_msg_rpbefore_cr: |
| 1215 | case HTTP_MSG_RPBEFORE_CR: |
| 1216 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1217 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1218 | /* stop here */ |
| 1219 | |
| 1220 | http_msg_rpver: |
| 1221 | case HTTP_MSG_RPVER: |
| 1222 | case HTTP_MSG_RPVER_SP: |
| 1223 | case HTTP_MSG_RPCODE: |
| 1224 | case HTTP_MSG_RPCODE_SP: |
| 1225 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1226 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1227 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1228 | if (unlikely(!ptr)) |
| 1229 | return; |
| 1230 | |
| 1231 | /* we have a full response and we know that we have either a CR |
| 1232 | * or an LF at <ptr>. |
| 1233 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1234 | //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] | 1235 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1236 | |
| 1237 | msg->sol = ptr; |
| 1238 | if (likely(*ptr == '\r')) |
| 1239 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1240 | goto http_msg_rpline_end; |
| 1241 | |
| 1242 | http_msg_rpline_end: |
| 1243 | case HTTP_MSG_RPLINE_END: |
| 1244 | /* msg->sol must point to the first of CR or LF. */ |
| 1245 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1246 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1247 | /* stop here */ |
| 1248 | |
| 1249 | /* |
| 1250 | * Second, states that are specific to the request only |
| 1251 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1252 | http_msg_rqbefore: |
| 1253 | case HTTP_MSG_RQBEFORE: |
| 1254 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1255 | if (likely(ptr == buf->data)) { |
| 1256 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1257 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1258 | } else { |
| 1259 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1260 | /* only skip empty leading lines, don't remove them */ |
| 1261 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1262 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1263 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | /* Remove empty leading lines, as recommended by |
| 1265 | * RFC2616. This takes a lot of time because we |
| 1266 | * must move all the buffer backwards, but this |
| 1267 | * is rarely needed. The method above will be |
| 1268 | * cleaner when we'll be able to start sending |
| 1269 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1270 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1271 | buf->lr = ptr; |
| 1272 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1273 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1274 | msg->sol = buf->data; |
| 1275 | ptr = buf->data; |
| 1276 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1277 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1279 | /* we will need this when keep-alive will be supported |
| 1280 | hdr_idx_init(idx); |
| 1281 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1282 | state = HTTP_MSG_RQMETH; |
| 1283 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1285 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1286 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1287 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | if (unlikely(*ptr == '\n')) |
| 1290 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1291 | 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] | 1292 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1294 | http_msg_rqbefore_cr: |
| 1295 | case HTTP_MSG_RQBEFORE_CR: |
| 1296 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1297 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1299 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1300 | http_msg_rqmeth: |
| 1301 | case HTTP_MSG_RQMETH: |
| 1302 | case HTTP_MSG_RQMETH_SP: |
| 1303 | case HTTP_MSG_RQURI: |
| 1304 | case HTTP_MSG_RQURI_SP: |
| 1305 | case HTTP_MSG_RQVER: |
| 1306 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1307 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | if (unlikely(!ptr)) |
| 1309 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | /* we have a full request and we know that we have either a CR |
| 1312 | * or an LF at <ptr>. |
| 1313 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1314 | //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] | 1315 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | msg->sol = ptr; |
| 1318 | if (likely(*ptr == '\r')) |
| 1319 | 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] | 1320 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1321 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1322 | http_msg_rqline_end: |
| 1323 | case HTTP_MSG_RQLINE_END: |
| 1324 | /* check for HTTP/0.9 request : no version information available. |
| 1325 | * msg->sol must point to the first of CR or LF. |
| 1326 | */ |
| 1327 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1328 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1330 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1331 | 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] | 1332 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1333 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | /* |
| 1335 | * Common states below |
| 1336 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | http_msg_hdr_first: |
| 1338 | case HTTP_MSG_HDR_FIRST: |
| 1339 | msg->sol = ptr; |
| 1340 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1341 | goto http_msg_hdr_name; |
| 1342 | } |
| 1343 | |
| 1344 | if (likely(*ptr == '\r')) |
| 1345 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1346 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | http_msg_hdr_name: |
| 1349 | case HTTP_MSG_HDR_NAME: |
| 1350 | /* assumes msg->sol points to the first char */ |
| 1351 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1352 | 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] | 1353 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1354 | if (likely(*ptr == ':')) { |
| 1355 | msg->col = ptr - buf->data; |
| 1356 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1357 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1358 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1359 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1360 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1361 | http_msg_hdr_l1_sp: |
| 1362 | case HTTP_MSG_HDR_L1_SP: |
| 1363 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1364 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1365 | 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] | 1366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1367 | /* header value can be basically anything except CR/LF */ |
| 1368 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1371 | goto http_msg_hdr_val; |
| 1372 | } |
| 1373 | |
| 1374 | if (likely(*ptr == '\r')) |
| 1375 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1376 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | http_msg_hdr_l1_lf: |
| 1379 | case HTTP_MSG_HDR_L1_LF: |
| 1380 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1381 | 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] | 1382 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1383 | http_msg_hdr_l1_lws: |
| 1384 | case HTTP_MSG_HDR_L1_LWS: |
| 1385 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1386 | /* replace HT,CR,LF with spaces */ |
| 1387 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1388 | buf->data[msg->sov] = ' '; |
| 1389 | goto http_msg_hdr_l1_sp; |
| 1390 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1391 | /* we had a header consisting only in spaces ! */ |
| 1392 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | goto http_msg_complete_header; |
| 1394 | |
| 1395 | http_msg_hdr_val: |
| 1396 | case HTTP_MSG_HDR_VAL: |
| 1397 | /* assumes msg->sol points to the first char, msg->col to the |
| 1398 | * colon, and msg->sov points to the first character of the |
| 1399 | * value. |
| 1400 | */ |
| 1401 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1402 | 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] | 1403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | msg->eol = ptr; |
| 1405 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1406 | * real header end in case it ends with lots of LWS, but is this |
| 1407 | * really needed ? |
| 1408 | */ |
| 1409 | if (likely(*ptr == '\r')) |
| 1410 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1411 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | http_msg_hdr_l2_lf: |
| 1414 | case HTTP_MSG_HDR_L2_LF: |
| 1415 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1416 | 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] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | http_msg_hdr_l2_lws: |
| 1419 | case HTTP_MSG_HDR_L2_LWS: |
| 1420 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1421 | /* LWS: replace HT,CR,LF with spaces */ |
| 1422 | for (; msg->eol < ptr; msg->eol++) |
| 1423 | *msg->eol = ' '; |
| 1424 | goto http_msg_hdr_val; |
| 1425 | } |
| 1426 | http_msg_complete_header: |
| 1427 | /* |
| 1428 | * It was a new header, so the last one is finished. |
| 1429 | * Assumes msg->sol points to the first char, msg->col to the |
| 1430 | * colon, msg->sov points to the first character of the value |
| 1431 | * and msg->eol to the first CR or LF so we know how the line |
| 1432 | * ends. We insert last header into the index. |
| 1433 | */ |
| 1434 | /* |
| 1435 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1436 | write(2, msg->sol, msg->eol-msg->sol); |
| 1437 | fprintf(stderr,"\n"); |
| 1438 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1441 | idx, idx->tail) < 0)) |
| 1442 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1443 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | msg->sol = ptr; |
| 1445 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1446 | goto http_msg_hdr_name; |
| 1447 | } |
| 1448 | |
| 1449 | if (likely(*ptr == '\r')) |
| 1450 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1451 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | http_msg_last_lf: |
| 1454 | case HTTP_MSG_LAST_LF: |
| 1455 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1456 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1457 | ptr++; |
| 1458 | buf->lr = ptr; |
| 1459 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1460 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | return; |
| 1462 | #ifdef DEBUG_FULL |
| 1463 | default: |
| 1464 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1465 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1466 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1467 | } |
| 1468 | http_msg_ood: |
| 1469 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1470 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | buf->lr = ptr; |
| 1472 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1473 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | http_msg_invalid: |
| 1475 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1476 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | return; |
| 1478 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1479 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | /* |
| 1481 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1482 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1483 | * 0 else. |
| 1484 | */ |
| 1485 | int process_cli(struct session *t) |
| 1486 | { |
| 1487 | int s = t->srv_state; |
| 1488 | int c = t->cli_state; |
| 1489 | struct buffer *req = t->req; |
| 1490 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1491 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1493 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1494 | 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] | 1495 | req->rex.tv_sec, req->rex.tv_usec, |
| 1496 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1497 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1498 | if (c == CL_STHEADERS) { |
| 1499 | /* |
| 1500 | * Now parse the partial (or complete) lines. |
| 1501 | * We will check the request syntax, and also join multi-line |
| 1502 | * headers. An index of all the lines will be elaborated while |
| 1503 | * parsing. |
| 1504 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1508 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | * req->data + req->eoh = end of processed headers / start of current one |
| 1510 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1511 | * req->lr = first non-visited byte |
| 1512 | * req->r = end of data |
| 1513 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1514 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1515 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1516 | struct http_txn *txn = &t->txn; |
| 1517 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1521 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | /* 1: we might have to print this header in debug mode */ |
| 1524 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1525 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1526 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1529 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | eol = sol + msg->sl.rq.l; |
| 1531 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1532 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1533 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1534 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1535 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1537 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1539 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1540 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | } |
| 1542 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1547 | * If not so, we check the FD and buffer states before leaving. |
| 1548 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1550 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1551 | * |
| 1552 | */ |
| 1553 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1554 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | /* |
| 1556 | * First, let's catch bad requests. |
| 1557 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1558 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1560 | |
| 1561 | /* 1: Since we are in header mode, if there's no space |
| 1562 | * left for headers, we won't be able to free more |
| 1563 | * later, so the session will never terminate. We |
| 1564 | * must terminate it now. |
| 1565 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1567 | /* FIXME: check if URI is set and return Status |
| 1568 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1569 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1570 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1575 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1576 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1577 | fd_delete(t->cli_fd); |
| 1578 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1579 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | if (!(t->flags & SN_ERR_MASK)) |
| 1581 | t->flags |= SN_ERR_CLICL; |
| 1582 | if (!(t->flags & SN_FINST_MASK)) |
| 1583 | t->flags |= SN_FINST_R; |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
| 1587 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1588 | else if (unlikely(tv_isle(&req->rex, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1589 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1590 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1591 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1592 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1593 | if (!(t->flags & SN_ERR_MASK)) |
| 1594 | t->flags |= SN_ERR_CLITO; |
| 1595 | if (!(t->flags & SN_FINST_MASK)) |
| 1596 | t->flags |= SN_FINST_R; |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
| 1600 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1601 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1602 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1603 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1604 | * req->l == rlim-data |
| 1605 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1606 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | tv_eternity(&req->rex); |
| 1608 | } |
| 1609 | return t->cli_state != CL_STHEADERS; |
| 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | /**************************************************************** |
| 1614 | * More interesting part now : we know that we have a complete * |
| 1615 | * request which at least looks like HTTP. We have an indicator * |
| 1616 | * of each header's length, so we can parse them quickly. * |
| 1617 | ****************************************************************/ |
| 1618 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1619 | /* ensure we keep this pointer to the beginning of the message */ |
| 1620 | msg->sol = req->data + msg->som; |
| 1621 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | /* |
| 1623 | * 1: identify the method |
| 1624 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1625 | 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] | 1626 | |
| 1627 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1629 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1631 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1633 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1634 | !memcmp(&req->data[msg->sl.rq.u], |
| 1635 | t->fe->monitor_uri, |
| 1636 | t->fe->monitor_uri_len))) { |
| 1637 | /* |
| 1638 | * We have found the monitor URI |
| 1639 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1640 | struct acl_cond *cond; |
| 1641 | cur_proxy = t->fe; |
| 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1644 | |
| 1645 | /* Check if we want to fail this monitor request or not */ |
| 1646 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1647 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1648 | if (cond->pol == ACL_COND_UNLESS) |
| 1649 | ret = !ret; |
| 1650 | |
| 1651 | if (ret) { |
| 1652 | /* we fail this request, let's return 503 service unavail */ |
| 1653 | txn->status = 503; |
| 1654 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1655 | goto return_prx_cond; |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1660 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | client_retnclose(t, &http_200_chunk); |
| 1662 | goto return_prx_cond; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1667 | * Note: we cannot log anymore if the request has been |
| 1668 | * classified as invalid. |
| 1669 | */ |
| 1670 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1671 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1672 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | int urilen = msg->sl.rq.l; |
| 1674 | |
| 1675 | if (urilen >= REQURI_LEN) |
| 1676 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1677 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1678 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | |
| 1680 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1681 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | } else { |
| 1683 | Alert("HTTP logging : out of memory.\n"); |
| 1684 | } |
| 1685 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1686 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1689 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1690 | int delta; |
| 1691 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1692 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | cur_end = msg->sol + msg->sl.rq.l; |
| 1694 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | if (msg->sl.rq.u_l == 0) { |
| 1697 | /* if no URI was set, add "/" */ |
| 1698 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1699 | cur_end += delta; |
| 1700 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1701 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | /* add HTTP version */ |
| 1703 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1704 | msg->eoh += delta; |
| 1705 | cur_end += delta; |
| 1706 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1707 | HTTP_MSG_RQMETH, |
| 1708 | msg->sol, cur_end + 1, |
| 1709 | NULL, NULL); |
| 1710 | if (unlikely(!cur_end)) |
| 1711 | goto return_bad_req; |
| 1712 | |
| 1713 | /* we have a full HTTP/1.0 request now and we know that |
| 1714 | * we have either a CR or an LF at <ptr>. |
| 1715 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1716 | 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] | 1717 | } |
| 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | |
| 1720 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1721 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1722 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1723 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1724 | |
| 1725 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1727 | * As opposed to version 1.2, now they will be evaluated in the |
| 1728 | * filters order and not in the header order. This means that |
| 1729 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1730 | * |
| 1731 | * We can now check whether we want to switch to another |
| 1732 | * backend, in which case we will re-check the backend's |
| 1733 | * filters and various options. In order to support 3-level |
| 1734 | * switching, here's how we should proceed : |
| 1735 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1736 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1737 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1738 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1739 | * There cannot be any switch from there, so ->be cannot be |
| 1740 | * changed anymore. |
| 1741 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1742 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1743 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1744 | * The response path will be able to apply either ->be, or |
| 1745 | * ->be then ->fe filters in order to match the reverse of |
| 1746 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1747 | */ |
| 1748 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1749 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1750 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1751 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1752 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1753 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1754 | /* first check whether we have some ACLs set to block this request */ |
| 1755 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1756 | 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] | 1757 | if (cond->pol == ACL_COND_UNLESS) |
| 1758 | ret = !ret; |
| 1759 | |
| 1760 | if (ret) { |
| 1761 | txn->status = 403; |
| 1762 | /* let's log the request time */ |
| 1763 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1764 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1765 | goto return_prx_cond; |
| 1766 | } |
| 1767 | } |
| 1768 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1769 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1770 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1772 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1773 | } |
| 1774 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1775 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1776 | /* to ensure correct connection accounting on |
| 1777 | * the backend, we count the connection for the |
| 1778 | * one managing the queue. |
| 1779 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1780 | t->be->beconn++; |
| 1781 | if (t->be->beconn > t->be->beconn_max) |
| 1782 | t->be->beconn_max = t->be->beconn; |
| 1783 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1784 | t->flags |= SN_BE_ASSIGNED; |
| 1785 | } |
| 1786 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1787 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1788 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1789 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1790 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1791 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1792 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1793 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1794 | goto return_prx_cond; |
| 1795 | } |
| 1796 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1798 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | !(t->flags & SN_CONN_CLOSED)) { |
| 1800 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1801 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1802 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1804 | 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] | 1805 | old_idx = 0; |
| 1806 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1807 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1808 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1809 | cur_ptr = cur_next; |
| 1810 | cur_end = cur_ptr + cur_hdr->len; |
| 1811 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1812 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1813 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1814 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | /* 3 possibilities : |
| 1816 | * - we have already set Connection: close, |
| 1817 | * so we remove this line. |
| 1818 | * - we have not yet set Connection: close, |
| 1819 | * but this line indicates close. We leave |
| 1820 | * it untouched and set the flag. |
| 1821 | * - we have not yet set Connection: close, |
| 1822 | * and this line indicates non-close. We |
| 1823 | * replace it. |
| 1824 | */ |
| 1825 | if (t->flags & SN_CONN_CLOSED) { |
| 1826 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1827 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1829 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1830 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | cur_hdr->len = 0; |
| 1832 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1833 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1834 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1835 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | cur_next += delta; |
| 1837 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1838 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1839 | } |
| 1840 | t->flags |= SN_CONN_CLOSED; |
| 1841 | } |
| 1842 | } |
| 1843 | old_idx = cur_idx; |
| 1844 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1845 | } |
| 1846 | /* add request headers from the rule sets in the same order */ |
| 1847 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1848 | if (unlikely(http_header_add_tail(req, |
| 1849 | &txn->req, |
| 1850 | &txn->hdr_idx, |
| 1851 | rule_set->req_add[cur_idx])) < 0) |
| 1852 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1853 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1854 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1855 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1856 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1857 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1858 | /* we have to check the URI and auth for this request */ |
| 1859 | if (stats_check_uri_auth(t, rule_set)) |
| 1860 | return 1; |
| 1861 | } |
| 1862 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1863 | /* now check whether we have some switching rules for this request */ |
| 1864 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1865 | struct switching_rule *rule; |
| 1866 | |
| 1867 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1868 | int ret; |
| 1869 | |
| 1870 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1871 | if (cond->pol == ACL_COND_UNLESS) |
| 1872 | ret = !ret; |
| 1873 | |
| 1874 | if (ret) { |
| 1875 | t->be = rule->be.backend; |
| 1876 | t->be->beconn++; |
| 1877 | if (t->be->beconn > t->be->beconn_max) |
| 1878 | t->be->beconn_max = t->be->beconn; |
| 1879 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1880 | |
| 1881 | /* assign new parameters to the session from the new backend */ |
| 1882 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1883 | t->req->cto = t->be->contimeout; |
| 1884 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1885 | t->flags |= SN_BE_ASSIGNED; |
| 1886 | break; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1891 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1892 | /* No backend was set, but there was a default |
| 1893 | * backend set in the frontend, so we use it and |
| 1894 | * loop again. |
| 1895 | */ |
| 1896 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1897 | t->be->beconn++; |
| 1898 | if (t->be->beconn > t->be->beconn_max) |
| 1899 | t->be->beconn_max = t->be->beconn; |
| 1900 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1901 | |
| 1902 | /* assign new parameters to the session from the new backend */ |
| 1903 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1904 | t->req->cto = t->be->contimeout; |
| 1905 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1906 | t->flags |= SN_BE_ASSIGNED; |
| 1907 | } |
| 1908 | } 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] | 1909 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1910 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1911 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1912 | /* To ensure correct connection accounting on |
| 1913 | * the backend, we count the connection for the |
| 1914 | * one managing the queue. |
| 1915 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1916 | t->be->beconn++; |
| 1917 | if (t->be->beconn > t->be->beconn_max) |
| 1918 | t->be->beconn_max = t->be->beconn; |
| 1919 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1920 | t->flags |= SN_BE_ASSIGNED; |
| 1921 | } |
| 1922 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1923 | /* |
| 1924 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1925 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1926 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1927 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1928 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1929 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1930 | /* |
| 1931 | * If HTTP PROXY is set we simply get remote server address |
| 1932 | * parsing incoming request. |
| 1933 | */ |
| 1934 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 1935 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 1936 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1937 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1938 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1939 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1940 | * so let's do the same now. |
| 1941 | */ |
| 1942 | |
| 1943 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1944 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1945 | 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] | 1946 | } |
| 1947 | |
| 1948 | |
| 1949 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1950 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1951 | * Note that doing so might move headers in the request, but |
| 1952 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1953 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1954 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 1955 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 1956 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1957 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1958 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1959 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1960 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1961 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1962 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1963 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1964 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1965 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1966 | /* Add an X-Forwarded-For header unless the source IP is |
| 1967 | * in the 'except' network range. |
| 1968 | */ |
| 1969 | if ((!t->fe->except_mask.s_addr || |
| 1970 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1971 | != t->fe->except_net.s_addr) && |
| 1972 | (!t->be->except_mask.s_addr || |
| 1973 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1974 | != t->be->except_net.s_addr)) { |
| 1975 | int len; |
| 1976 | unsigned char *pn; |
| 1977 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1978 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1979 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1980 | pn[0], pn[1], pn[2], pn[3]); |
| 1981 | |
| 1982 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1983 | &txn->hdr_idx, trash, len)) < 0) |
| 1984 | goto return_bad_req; |
| 1985 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1986 | } |
| 1987 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1988 | /* FIXME: for the sake of completeness, we should also support |
| 1989 | * 'except' here, although it is mostly useless in this case. |
| 1990 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1991 | int len; |
| 1992 | char pn[INET6_ADDRSTRLEN]; |
| 1993 | inet_ntop(AF_INET6, |
| 1994 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1995 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1996 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 1997 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1998 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1999 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2000 | } |
| 2001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2002 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2003 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2004 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2005 | * 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] | 2006 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2007 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2008 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2009 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2010 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2011 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2012 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2013 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2014 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2015 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2016 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2017 | /************************************************************* |
| 2018 | * OK, that's finished for the headers. We have done what we * |
| 2019 | * could. Let's switch to the DATA state. * |
| 2020 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2021 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2022 | t->cli_state = CL_STDATA; |
| 2023 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2024 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2025 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2027 | if (!tv_isset(&t->fe->clitimeout) || |
| 2028 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2029 | /* If the client has no timeout, or if the server is not ready yet, |
| 2030 | * and we know for sure that it can expire, then it's cleaner to |
| 2031 | * disable the timeout on the client side so that too low values |
| 2032 | * cannot make the sessions abort too early. |
| 2033 | * |
| 2034 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2035 | * when it switches its state, otherwise a client can stay connected |
| 2036 | * indefinitely. This now seems to be OK. |
| 2037 | */ |
| 2038 | tv_eternity(&req->rex); |
| 2039 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2040 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2041 | /* When a connection is tarpitted, we use the queue timeout for the |
| 2042 | * tarpit delay, which currently happens to be the server's connect |
| 2043 | * timeout. If unset, then set it to zero because we really want it |
| 2044 | * to expire at one moment. |
| 2045 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2046 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2047 | t->req->l = 0; |
| 2048 | /* flush the request so that we can drop the connection early |
| 2049 | * if the client closes first. |
| 2050 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2051 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2052 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2053 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2054 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2055 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2056 | goto process_data; |
| 2057 | |
| 2058 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2059 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2060 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2061 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2062 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2063 | return_prx_cond: |
| 2064 | if (!(t->flags & SN_ERR_MASK)) |
| 2065 | t->flags |= SN_ERR_PRXCOND; |
| 2066 | if (!(t->flags & SN_FINST_MASK)) |
| 2067 | t->flags |= SN_FINST_R; |
| 2068 | return 1; |
| 2069 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2070 | } |
| 2071 | else if (c == CL_STDATA) { |
| 2072 | process_data: |
| 2073 | /* FIXME: this error handling is partly buggy because we always report |
| 2074 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2075 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2076 | * we're waiting for the server to connect. |
| 2077 | */ |
| 2078 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2079 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2080 | buffer_shutr(req); |
| 2081 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2082 | fd_delete(t->cli_fd); |
| 2083 | t->cli_state = CL_STCLOSE; |
| 2084 | if (!(t->flags & SN_ERR_MASK)) |
| 2085 | t->flags |= SN_ERR_CLICL; |
| 2086 | if (!(t->flags & SN_FINST_MASK)) { |
| 2087 | if (t->pend_pos) |
| 2088 | t->flags |= SN_FINST_Q; |
| 2089 | else if (s == SV_STCONN) |
| 2090 | t->flags |= SN_FINST_C; |
| 2091 | else |
| 2092 | t->flags |= SN_FINST_D; |
| 2093 | } |
| 2094 | return 1; |
| 2095 | } |
| 2096 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2097 | 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] | 2098 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2099 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2100 | t->cli_state = CL_STSHUTR; |
| 2101 | return 1; |
| 2102 | } |
| 2103 | /* last server read and buffer empty */ |
| 2104 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2105 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2106 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2107 | shutdown(t->cli_fd, SHUT_WR); |
| 2108 | /* We must ensure that the read part is still alive when switching |
| 2109 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2110 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2111 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2112 | t->cli_state = CL_STSHUTW; |
| 2113 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2114 | return 1; |
| 2115 | } |
| 2116 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2117 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2118 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2119 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2120 | t->cli_state = CL_STSHUTR; |
| 2121 | if (!(t->flags & SN_ERR_MASK)) |
| 2122 | t->flags |= SN_ERR_CLITO; |
| 2123 | if (!(t->flags & SN_FINST_MASK)) { |
| 2124 | if (t->pend_pos) |
| 2125 | t->flags |= SN_FINST_Q; |
| 2126 | else if (s == SV_STCONN) |
| 2127 | t->flags |= SN_FINST_C; |
| 2128 | else |
| 2129 | t->flags |= SN_FINST_D; |
| 2130 | } |
| 2131 | return 1; |
| 2132 | } |
| 2133 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2134 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2135 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2136 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2137 | shutdown(t->cli_fd, SHUT_WR); |
| 2138 | /* We must ensure that the read part is still alive when switching |
| 2139 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2140 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2141 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2142 | |
| 2143 | t->cli_state = CL_STSHUTW; |
| 2144 | if (!(t->flags & SN_ERR_MASK)) |
| 2145 | t->flags |= SN_ERR_CLITO; |
| 2146 | if (!(t->flags & SN_FINST_MASK)) { |
| 2147 | if (t->pend_pos) |
| 2148 | t->flags |= SN_FINST_Q; |
| 2149 | else if (s == SV_STCONN) |
| 2150 | t->flags |= SN_FINST_C; |
| 2151 | else |
| 2152 | t->flags |= SN_FINST_D; |
| 2153 | } |
| 2154 | return 1; |
| 2155 | } |
| 2156 | |
| 2157 | if (req->l >= req->rlim - req->data) { |
| 2158 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2159 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2160 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2161 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2162 | } |
| 2163 | } else { |
| 2164 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2165 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2166 | if (!tv_isset(&t->fe->clitimeout) || |
| 2167 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2168 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2169 | * know for sure that it can expire, then it's cleaner to disable the |
| 2170 | * timeout on the client side so that too low values cannot make the |
| 2171 | * sessions abort too early. |
| 2172 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2173 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2174 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2175 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | if ((rep->l == 0) || |
| 2180 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2181 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2182 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2183 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2184 | } |
| 2185 | } else { |
| 2186 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2187 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2188 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2189 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2190 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2191 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2192 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2193 | } |
| 2194 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2195 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | return 0; /* other cases change nothing */ |
| 2199 | } |
| 2200 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2201 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2202 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2203 | fd_delete(t->cli_fd); |
| 2204 | t->cli_state = CL_STCLOSE; |
| 2205 | if (!(t->flags & SN_ERR_MASK)) |
| 2206 | t->flags |= SN_ERR_CLICL; |
| 2207 | if (!(t->flags & SN_FINST_MASK)) { |
| 2208 | if (t->pend_pos) |
| 2209 | t->flags |= SN_FINST_Q; |
| 2210 | else if (s == SV_STCONN) |
| 2211 | t->flags |= SN_FINST_C; |
| 2212 | else |
| 2213 | t->flags |= SN_FINST_D; |
| 2214 | } |
| 2215 | return 1; |
| 2216 | } |
| 2217 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2218 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2219 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2220 | fd_delete(t->cli_fd); |
| 2221 | t->cli_state = CL_STCLOSE; |
| 2222 | return 1; |
| 2223 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2224 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2225 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2226 | fd_delete(t->cli_fd); |
| 2227 | t->cli_state = CL_STCLOSE; |
| 2228 | if (!(t->flags & SN_ERR_MASK)) |
| 2229 | t->flags |= SN_ERR_CLITO; |
| 2230 | if (!(t->flags & SN_FINST_MASK)) { |
| 2231 | if (t->pend_pos) |
| 2232 | t->flags |= SN_FINST_Q; |
| 2233 | else if (s == SV_STCONN) |
| 2234 | t->flags |= SN_FINST_C; |
| 2235 | else |
| 2236 | t->flags |= SN_FINST_D; |
| 2237 | } |
| 2238 | return 1; |
| 2239 | } |
| 2240 | |
| 2241 | if (t->flags & SN_SELF_GEN) { |
| 2242 | produce_content(t); |
| 2243 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2244 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2245 | fd_delete(t->cli_fd); |
| 2246 | t->cli_state = CL_STCLOSE; |
| 2247 | return 1; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | if ((rep->l == 0) |
| 2252 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2253 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2254 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2255 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2256 | } |
| 2257 | } else { |
| 2258 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2259 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2260 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 2261 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2262 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | return 0; |
| 2266 | } |
| 2267 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2268 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2269 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2270 | fd_delete(t->cli_fd); |
| 2271 | t->cli_state = CL_STCLOSE; |
| 2272 | if (!(t->flags & SN_ERR_MASK)) |
| 2273 | t->flags |= SN_ERR_CLICL; |
| 2274 | if (!(t->flags & SN_FINST_MASK)) { |
| 2275 | if (t->pend_pos) |
| 2276 | t->flags |= SN_FINST_Q; |
| 2277 | else if (s == SV_STCONN) |
| 2278 | t->flags |= SN_FINST_C; |
| 2279 | else |
| 2280 | t->flags |= SN_FINST_D; |
| 2281 | } |
| 2282 | return 1; |
| 2283 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2284 | 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] | 2285 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2286 | fd_delete(t->cli_fd); |
| 2287 | t->cli_state = CL_STCLOSE; |
| 2288 | return 1; |
| 2289 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2290 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2291 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2292 | fd_delete(t->cli_fd); |
| 2293 | t->cli_state = CL_STCLOSE; |
| 2294 | if (!(t->flags & SN_ERR_MASK)) |
| 2295 | t->flags |= SN_ERR_CLITO; |
| 2296 | if (!(t->flags & SN_FINST_MASK)) { |
| 2297 | if (t->pend_pos) |
| 2298 | t->flags |= SN_FINST_Q; |
| 2299 | else if (s == SV_STCONN) |
| 2300 | t->flags |= SN_FINST_C; |
| 2301 | else |
| 2302 | t->flags |= SN_FINST_D; |
| 2303 | } |
| 2304 | return 1; |
| 2305 | } |
| 2306 | else if (req->l >= req->rlim - req->data) { |
| 2307 | /* no room to read more data */ |
| 2308 | |
| 2309 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2310 | * after the timeout by sending more data after it receives a close ? |
| 2311 | */ |
| 2312 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2313 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2314 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2315 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2316 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2317 | } |
| 2318 | } else { |
| 2319 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2320 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2321 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2322 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2323 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2324 | } |
| 2325 | } |
| 2326 | return 0; |
| 2327 | } |
| 2328 | else { /* CL_STCLOSE: nothing to do */ |
| 2329 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2330 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2331 | 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] | 2332 | write(1, trash, len); |
| 2333 | } |
| 2334 | return 0; |
| 2335 | } |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | /* |
| 2341 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2342 | * (and a resync may be needed), 0 else. |
| 2343 | */ |
| 2344 | int process_srv(struct session *t) |
| 2345 | { |
| 2346 | int s = t->srv_state; |
| 2347 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2348 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2349 | struct buffer *req = t->req; |
| 2350 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2351 | int conn_err; |
| 2352 | |
| 2353 | #ifdef DEBUG_FULL |
| 2354 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2355 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2356 | |
| 2357 | #if 0 |
| 2358 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2359 | __FUNCTION__, __LINE__, |
| 2360 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2361 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2362 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2363 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2364 | __FUNCTION__, __LINE__, |
| 2365 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2366 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2367 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2368 | |
| 2369 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2370 | __FUNCTION__, __LINE__, |
| 2371 | req->cto.tv_sec, req->cto.tv_usec, |
| 2372 | req->rto.tv_sec, req->rto.tv_usec, |
| 2373 | req->wto.tv_sec, req->wto.tv_usec); |
| 2374 | |
| 2375 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2376 | __FUNCTION__, __LINE__, |
| 2377 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2378 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2379 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2380 | #endif |
| 2381 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2382 | //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] | 2383 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2384 | //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] | 2385 | //); |
| 2386 | if (s == SV_STIDLE) { |
| 2387 | if (c == CL_STHEADERS) |
| 2388 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2389 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2390 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2391 | (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] | 2392 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2393 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2394 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2395 | /* note that this must not return any error because it would be able to |
| 2396 | * overwrite the client_retnclose() output. |
| 2397 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2398 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2399 | 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] | 2400 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2401 | 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] | 2402 | |
| 2403 | return 1; |
| 2404 | } |
| 2405 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2406 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2407 | /* This connection is being tarpitted. The CLIENT side has |
| 2408 | * already set the connect expiration date to the right |
| 2409 | * timeout. We just have to check that it has not expired. |
| 2410 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2411 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2412 | return 0; |
| 2413 | |
| 2414 | /* We will set the queue timer to the time spent, just for |
| 2415 | * logging purposes. We fake a 500 server error, so that the |
| 2416 | * attacker will not suspect his connection has been tarpitted. |
| 2417 | * It will not cause trouble to the logs because we can exclude |
| 2418 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2419 | */ |
| 2420 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2421 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2422 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2423 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2424 | return 1; |
| 2425 | } |
| 2426 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2427 | /* Right now, we will need to create a connection to the server. |
| 2428 | * We might already have tried, and got a connection pending, in |
| 2429 | * which case we will not do anything till it's pending. It's up |
| 2430 | * to any other session to release it and wake us up again. |
| 2431 | */ |
| 2432 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2433 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2434 | return 0; |
| 2435 | else { |
| 2436 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2437 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2438 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2439 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2440 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2441 | if (t->srv) |
| 2442 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2443 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2444 | return 1; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | do { |
| 2449 | /* first, get a connection */ |
| 2450 | if (srv_redispatch_connect(t)) |
| 2451 | return t->srv_state != SV_STIDLE; |
| 2452 | |
| 2453 | /* try to (re-)connect to the server, and fail if we expire the |
| 2454 | * number of retries. |
| 2455 | */ |
| 2456 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2457 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2458 | return t->srv_state != SV_STIDLE; |
| 2459 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2460 | } while (1); |
| 2461 | } |
| 2462 | } |
| 2463 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2464 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2465 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2466 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2467 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2468 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2469 | fd_delete(t->srv_fd); |
| 2470 | if (t->srv) |
| 2471 | t->srv->cur_sess--; |
| 2472 | |
| 2473 | /* note that this must not return any error because it would be able to |
| 2474 | * overwrite the client_retnclose() output. |
| 2475 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2476 | 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] | 2477 | return 1; |
| 2478 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2479 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2480 | //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] | 2481 | return 0; /* nothing changed */ |
| 2482 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2483 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2484 | /* timeout, asynchronous connect error or first write error */ |
| 2485 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2486 | |
| 2487 | fd_delete(t->srv_fd); |
| 2488 | if (t->srv) |
| 2489 | t->srv->cur_sess--; |
| 2490 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2491 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2492 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2493 | else |
| 2494 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2495 | |
| 2496 | /* ensure that we have enough retries left */ |
| 2497 | if (srv_count_retry_down(t, conn_err)) |
| 2498 | return 1; |
| 2499 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2500 | 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] | 2501 | /* We're on our last chance, and the REDISP option was specified. |
| 2502 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2503 | */ |
| 2504 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2505 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2506 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2507 | |
| 2508 | if (t->srv) |
| 2509 | t->srv->failed_conns++; |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 2510 | t->be->redispatches++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2511 | |
| 2512 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2513 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | a5e6575 | 2007-03-18 20:53:22 +0100 | [diff] [blame] | 2514 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2515 | |
| 2516 | /* first, get a connection */ |
| 2517 | if (srv_redispatch_connect(t)) |
| 2518 | return t->srv_state != SV_STIDLE; |
| 2519 | } |
| 2520 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2521 | do { |
| 2522 | /* Now we will try to either reconnect to the same server or |
| 2523 | * connect to another server. If the connection gets queued |
| 2524 | * because all servers are saturated, then we will go back to |
| 2525 | * the SV_STIDLE state. |
| 2526 | */ |
| 2527 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2528 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2529 | return t->srv_state != SV_STCONN; |
| 2530 | } |
| 2531 | |
| 2532 | /* we need to redispatch the connection to another server */ |
| 2533 | if (srv_redispatch_connect(t)) |
| 2534 | return t->srv_state != SV_STCONN; |
| 2535 | } while (1); |
| 2536 | } |
| 2537 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2538 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | |
| 2540 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2541 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2542 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2543 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2544 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2545 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2546 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2547 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2548 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2549 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2550 | } |
| 2551 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2552 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2555 | 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] | 2556 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2557 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2558 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2559 | |
| 2560 | t->srv_state = SV_STDATA; |
| 2561 | if (t->srv) |
| 2562 | t->srv->cum_sess++; |
| 2563 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2564 | |
| 2565 | /* if the user wants to log as soon as possible, without counting |
| 2566 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2567 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2568 | 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] | 2569 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2570 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2571 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2572 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2573 | /* TCP splicing supported by both FE and BE */ |
| 2574 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2575 | } |
| 2576 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2577 | } |
| 2578 | else { |
| 2579 | t->srv_state = SV_STHEADERS; |
| 2580 | if (t->srv) |
| 2581 | t->srv->cum_sess++; |
| 2582 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2583 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2584 | /* reset hdr_idx which was already initialized by the request. |
| 2585 | * right now, the http parser does it. |
| 2586 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2587 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2588 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2589 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2590 | return 1; |
| 2591 | } |
| 2592 | } |
| 2593 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2594 | /* |
| 2595 | * Now parse the partial (or complete) lines. |
| 2596 | * We will check the response syntax, and also join multi-line |
| 2597 | * headers. An index of all the lines will be elaborated while |
| 2598 | * parsing. |
| 2599 | * |
| 2600 | * For the parsing, we use a 28 states FSM. |
| 2601 | * |
| 2602 | * Here is the information we currently have : |
| 2603 | * rep->data + req->som = beginning of response |
| 2604 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2605 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2606 | * rep->lr = first non-visited byte |
| 2607 | * rep->r = end of data |
| 2608 | */ |
| 2609 | |
| 2610 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2611 | struct http_msg *msg = &txn->rsp; |
| 2612 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2613 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2614 | if (likely(rep->lr < rep->r)) |
| 2615 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2616 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2617 | /* 1: we might have to print this header in debug mode */ |
| 2618 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2619 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2620 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2621 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2622 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2623 | sol = rep->data + msg->som; |
| 2624 | eol = sol + msg->sl.rq.l; |
| 2625 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2627 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2628 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2630 | while (cur_idx) { |
| 2631 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2632 | debug_hdr("srvhdr", t, sol, eol); |
| 2633 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2634 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2635 | } |
| 2636 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2638 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2639 | 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] | 2640 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2641 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2642 | * rep->l == rlim-data |
| 2643 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2644 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2645 | tv_eternity(&rep->rex); |
| 2646 | } |
| 2647 | |
| 2648 | |
| 2649 | /* |
| 2650 | * Now we quickly check if we have found a full valid response. |
| 2651 | * If not so, we check the FD and buffer states before leaving. |
| 2652 | * A full response is indicated by the fact that we have seen |
| 2653 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2654 | * responses are checked first. |
| 2655 | * |
| 2656 | * Depending on whether the client is still there or not, we |
| 2657 | * may send an error response back or not. Note that normally |
| 2658 | * we should only check for HTTP status there, and check I/O |
| 2659 | * errors somewhere else. |
| 2660 | */ |
| 2661 | |
| 2662 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2663 | |
| 2664 | /* Invalid response, or read error or write error */ |
| 2665 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2666 | (req->flags & BF_WRITE_ERROR) || |
| 2667 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2668 | buffer_shutr(rep); |
| 2669 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2670 | fd_delete(t->srv_fd); |
| 2671 | if (t->srv) { |
| 2672 | t->srv->cur_sess--; |
| 2673 | t->srv->failed_resp++; |
| 2674 | } |
| 2675 | t->be->failed_resp++; |
| 2676 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2677 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2678 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2679 | if (!(t->flags & SN_ERR_MASK)) |
| 2680 | t->flags |= SN_ERR_SRVCL; |
| 2681 | if (!(t->flags & SN_FINST_MASK)) |
| 2682 | t->flags |= SN_FINST_H; |
| 2683 | /* We used to have a free connection slot. Since we'll never use it, |
| 2684 | * we have to inform the server that it may be used by another session. |
| 2685 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2686 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2687 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2688 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | /* end of client write or end of server read. |
| 2693 | * since we are in header mode, if there's no space left for headers, we |
| 2694 | * won't be able to free more later, so the session will never terminate. |
| 2695 | */ |
| 2696 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2697 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2698 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2699 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2700 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2701 | t->srv_state = SV_STSHUTR; |
| 2702 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2703 | return 1; |
| 2704 | } |
| 2705 | |
| 2706 | /* read timeout : return a 504 to the client. |
| 2707 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2708 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2709 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2710 | buffer_shutr(rep); |
| 2711 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2712 | fd_delete(t->srv_fd); |
| 2713 | if (t->srv) { |
| 2714 | t->srv->cur_sess--; |
| 2715 | t->srv->failed_resp++; |
| 2716 | } |
| 2717 | t->be->failed_resp++; |
| 2718 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2719 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2720 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2721 | if (!(t->flags & SN_ERR_MASK)) |
| 2722 | t->flags |= SN_ERR_SRVTO; |
| 2723 | if (!(t->flags & SN_FINST_MASK)) |
| 2724 | t->flags |= SN_FINST_H; |
| 2725 | /* We used to have a free connection slot. Since we'll never use it, |
| 2726 | * we have to inform the server that it may be used by another session. |
| 2727 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2728 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2729 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2730 | return 1; |
| 2731 | } |
| 2732 | |
| 2733 | /* last client read and buffer empty */ |
| 2734 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2735 | * client shuts read too early, because we may still have |
| 2736 | * some work to do on the headers. |
| 2737 | * The side-effect is that if the client completely closes its |
| 2738 | * connection during SV_STHEADER, the connection to the server |
| 2739 | * is kept until a response comes back or the timeout is reached. |
| 2740 | */ |
| 2741 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2742 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2743 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2744 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2745 | |
| 2746 | /* We must ensure that the read part is still |
| 2747 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2748 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2749 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2750 | |
| 2751 | shutdown(t->srv_fd, SHUT_WR); |
| 2752 | t->srv_state = SV_STSHUTW; |
| 2753 | return 1; |
| 2754 | } |
| 2755 | |
| 2756 | /* write timeout */ |
| 2757 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2758 | * client shuts read too early, because we may still have |
| 2759 | * some work to do on the headers. |
| 2760 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2761 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2762 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2763 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2764 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2765 | shutdown(t->srv_fd, SHUT_WR); |
| 2766 | /* We must ensure that the read part is still alive |
| 2767 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2768 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2769 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2770 | |
| 2771 | t->srv_state = SV_STSHUTW; |
| 2772 | if (!(t->flags & SN_ERR_MASK)) |
| 2773 | t->flags |= SN_ERR_SRVTO; |
| 2774 | if (!(t->flags & SN_FINST_MASK)) |
| 2775 | t->flags |= SN_FINST_H; |
| 2776 | return 1; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * And now the non-error cases. |
| 2781 | */ |
| 2782 | |
| 2783 | /* Data remaining in the request buffer. |
| 2784 | * This happens during the first pass here, and during |
| 2785 | * long posts. |
| 2786 | */ |
| 2787 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2788 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2789 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2790 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2791 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2792 | * we refresh it. */ |
| 2793 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2794 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2795 | else |
| 2796 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2797 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2798 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2799 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2800 | /* nothing left in the request buffer */ |
| 2801 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2802 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2803 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2804 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | return t->srv_state != SV_STHEADERS; |
| 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | /***************************************************************** |
| 2813 | * More interesting part now : we know that we have a complete * |
| 2814 | * response which at least looks like HTTP. We have an indicator * |
| 2815 | * of each header's length, so we can parse them quickly. * |
| 2816 | ****************************************************************/ |
| 2817 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2818 | /* ensure we keep this pointer to the beginning of the message */ |
| 2819 | msg->sol = rep->data + msg->som; |
| 2820 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2821 | /* |
| 2822 | * 1: get the status code and check for cacheability. |
| 2823 | */ |
| 2824 | |
| 2825 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2826 | 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] | 2827 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2828 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2829 | case 200: |
| 2830 | case 203: |
| 2831 | case 206: |
| 2832 | case 300: |
| 2833 | case 301: |
| 2834 | case 410: |
| 2835 | /* RFC2616 @13.4: |
| 2836 | * "A response received with a status code of |
| 2837 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2838 | * by a cache (...) unless a cache-control |
| 2839 | * directive prohibits caching." |
| 2840 | * |
| 2841 | * RFC2616 @9.5: POST method : |
| 2842 | * "Responses to this method are not cacheable, |
| 2843 | * unless the response includes appropriate |
| 2844 | * Cache-Control or Expires header fields." |
| 2845 | */ |
| 2846 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2847 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2848 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2849 | break; |
| 2850 | default: |
| 2851 | break; |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * 2: we may need to capture headers |
| 2856 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2857 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2858 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2859 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2860 | |
| 2861 | /* |
| 2862 | * 3: we will have to evaluate the filters. |
| 2863 | * As opposed to version 1.2, now they will be evaluated in the |
| 2864 | * filters order and not in the header order. This means that |
| 2865 | * each filter has to be validated among all headers. |
| 2866 | * |
| 2867 | * Filters are tried with ->be first, then with ->fe if it is |
| 2868 | * different from ->be. |
| 2869 | */ |
| 2870 | |
| 2871 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2872 | |
| 2873 | cur_proxy = t->be; |
| 2874 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2875 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2876 | |
| 2877 | /* try headers filters */ |
| 2878 | if (rule_set->rsp_exp != NULL) { |
| 2879 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2880 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2881 | if (t->srv) { |
| 2882 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2883 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2884 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2885 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2886 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2887 | buffer_shutr(rep); |
| 2888 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2889 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2890 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2891 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2892 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2893 | if (!(t->flags & SN_ERR_MASK)) |
| 2894 | t->flags |= SN_ERR_PRXCOND; |
| 2895 | if (!(t->flags & SN_FINST_MASK)) |
| 2896 | t->flags |= SN_FINST_H; |
| 2897 | /* We used to have a free connection slot. Since we'll never use it, |
| 2898 | * we have to inform the server that it may be used by another session. |
| 2899 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2900 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2901 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2904 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2905 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2906 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2907 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2908 | if (t->srv) { |
| 2909 | t->srv->cur_sess--; |
| 2910 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2911 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2912 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2913 | goto return_srv_prx_502; |
| 2914 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2916 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2917 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2918 | !(t->flags & SN_CONN_CLOSED)) { |
| 2919 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2920 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2921 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2922 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2923 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2924 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2926 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2927 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2928 | cur_ptr = cur_next; |
| 2929 | cur_end = cur_ptr + cur_hdr->len; |
| 2930 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2931 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2932 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2933 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2934 | /* 3 possibilities : |
| 2935 | * - we have already set Connection: close, |
| 2936 | * so we remove this line. |
| 2937 | * - we have not yet set Connection: close, |
| 2938 | * but this line indicates close. We leave |
| 2939 | * it untouched and set the flag. |
| 2940 | * - we have not yet set Connection: close, |
| 2941 | * and this line indicates non-close. We |
| 2942 | * replace it. |
| 2943 | */ |
| 2944 | if (t->flags & SN_CONN_CLOSED) { |
| 2945 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2946 | txn->rsp.eoh += delta; |
| 2947 | cur_next += delta; |
| 2948 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2949 | txn->hdr_idx.used--; |
| 2950 | cur_hdr->len = 0; |
| 2951 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2952 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2953 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2954 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2955 | cur_next += delta; |
| 2956 | cur_hdr->len += delta; |
| 2957 | txn->rsp.eoh += delta; |
| 2958 | } |
| 2959 | t->flags |= SN_CONN_CLOSED; |
| 2960 | } |
| 2961 | } |
| 2962 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2963 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2964 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2965 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2966 | /* add response headers from the rule sets in the same order */ |
| 2967 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2968 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2969 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2970 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2971 | } |
| 2972 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2973 | /* check whether we're already working on the frontend */ |
| 2974 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2975 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2976 | cur_proxy = t->fe; |
| 2977 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2978 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2979 | /* |
| 2980 | * 4: check for server cookie. |
| 2981 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2982 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 2983 | || (t->be->options & PR_O_CHK_CACHE)) |
| 2984 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2985 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2986 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2987 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2988 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2989 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2990 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 2991 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2992 | |
| 2993 | /* |
| 2994 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2995 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2996 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2997 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2998 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2999 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3000 | /* the server is known, it's not the one the client requested, we have to |
| 3001 | * insert a set-cookie here, except if we want to insert only on POST |
| 3002 | * requests and this one isn't. Note that servers which don't have cookies |
| 3003 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3004 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3005 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3006 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3007 | 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] | 3008 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3009 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3010 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3011 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3012 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3013 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3014 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3015 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3016 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3017 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3018 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3019 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3020 | |
| 3021 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3022 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3023 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3024 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3025 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3026 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | |
| 3030 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3031 | * 7: check if result will be cacheable with a cookie. |
| 3032 | * We'll block the response if security checks have caught |
| 3033 | * nasty things such as a cacheable cookie. |
| 3034 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3035 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3036 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3037 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3038 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3039 | /* we're in presence of a cacheable response containing |
| 3040 | * a set-cookie header. We'll block it as requested by |
| 3041 | * the 'checkcache' option, and send an alert. |
| 3042 | */ |
| 3043 | if (t->srv) { |
| 3044 | t->srv->cur_sess--; |
| 3045 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3046 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3047 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3048 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3049 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3050 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3051 | send_log(t->be, LOG_ALERT, |
| 3052 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3053 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3054 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3055 | } |
| 3056 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3057 | /* |
| 3058 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3059 | * 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] | 3060 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3061 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3062 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3063 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3064 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3065 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3066 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3067 | goto return_bad_resp; |
| 3068 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3069 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3070 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3071 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3072 | /************************************************************* |
| 3073 | * OK, that's finished for the headers. We have done what we * |
| 3074 | * could. Let's switch to the DATA state. * |
| 3075 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3076 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3077 | t->srv_state = SV_STDATA; |
| 3078 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3079 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3080 | |
| 3081 | /* client connection already closed or option 'forceclose' required : |
| 3082 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3083 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3084 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3085 | (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] | 3086 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3087 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3088 | |
| 3089 | /* We must ensure that the read part is still alive when switching |
| 3090 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3091 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3092 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3094 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3095 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3098 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3099 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3100 | /* TCP splicing supported by both FE and BE */ |
| 3101 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3102 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3103 | #endif |
| 3104 | /* if the user wants to log as soon as possible, without counting |
| 3105 | bytes from the server, then this is the right moment. */ |
| 3106 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3107 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | b498717 | 2007-03-18 16:28:03 +0100 | [diff] [blame] | 3108 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3109 | if (t->fe->to_log & LW_REQ) |
| 3110 | http_sess_log(t); |
| 3111 | else |
| 3112 | tcp_sess_log(t); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 3113 | t->logs.bytes_in = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3116 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3117 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3118 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3119 | |
| 3120 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3121 | } |
| 3122 | else if (s == SV_STDATA) { |
| 3123 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3124 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3125 | buffer_shutr(rep); |
| 3126 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3127 | fd_delete(t->srv_fd); |
| 3128 | if (t->srv) { |
| 3129 | t->srv->cur_sess--; |
| 3130 | t->srv->failed_resp++; |
| 3131 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3132 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3133 | t->srv_state = SV_STCLOSE; |
| 3134 | if (!(t->flags & SN_ERR_MASK)) |
| 3135 | t->flags |= SN_ERR_SRVCL; |
| 3136 | if (!(t->flags & SN_FINST_MASK)) |
| 3137 | t->flags |= SN_FINST_D; |
| 3138 | /* We used to have a free connection slot. Since we'll never use it, |
| 3139 | * we have to inform the server that it may be used by another session. |
| 3140 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3141 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3142 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3143 | |
| 3144 | return 1; |
| 3145 | } |
| 3146 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3147 | 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] | 3148 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3149 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3150 | t->srv_state = SV_STSHUTR; |
| 3151 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3152 | return 1; |
| 3153 | } |
| 3154 | /* end of client read and no more data to send */ |
| 3155 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3156 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3157 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3158 | shutdown(t->srv_fd, SHUT_WR); |
| 3159 | /* We must ensure that the read part is still alive when switching |
| 3160 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3161 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3162 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3163 | |
| 3164 | t->srv_state = SV_STSHUTW; |
| 3165 | return 1; |
| 3166 | } |
| 3167 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3168 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3169 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3170 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3171 | t->srv_state = SV_STSHUTR; |
| 3172 | if (!(t->flags & SN_ERR_MASK)) |
| 3173 | t->flags |= SN_ERR_SRVTO; |
| 3174 | if (!(t->flags & SN_FINST_MASK)) |
| 3175 | t->flags |= SN_FINST_D; |
| 3176 | return 1; |
| 3177 | } |
| 3178 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3179 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3180 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3181 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3182 | shutdown(t->srv_fd, SHUT_WR); |
| 3183 | /* We must ensure that the read part is still alive when switching |
| 3184 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3185 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3186 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3187 | t->srv_state = SV_STSHUTW; |
| 3188 | if (!(t->flags & SN_ERR_MASK)) |
| 3189 | t->flags |= SN_ERR_SRVTO; |
| 3190 | if (!(t->flags & SN_FINST_MASK)) |
| 3191 | t->flags |= SN_FINST_D; |
| 3192 | return 1; |
| 3193 | } |
| 3194 | |
| 3195 | /* recompute request time-outs */ |
| 3196 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3197 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3198 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3199 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3203 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3204 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3205 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3206 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3207 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3208 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3209 | } |
| 3210 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3211 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | /* recompute response time-outs */ |
| 3216 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3217 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3218 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3222 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3223 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3224 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | return 0; /* other cases change nothing */ |
| 3229 | } |
| 3230 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3231 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3232 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3233 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3234 | fd_delete(t->srv_fd); |
| 3235 | if (t->srv) { |
| 3236 | t->srv->cur_sess--; |
| 3237 | t->srv->failed_resp++; |
| 3238 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3239 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3240 | //close(t->srv_fd); |
| 3241 | t->srv_state = SV_STCLOSE; |
| 3242 | if (!(t->flags & SN_ERR_MASK)) |
| 3243 | t->flags |= SN_ERR_SRVCL; |
| 3244 | if (!(t->flags & SN_FINST_MASK)) |
| 3245 | t->flags |= SN_FINST_D; |
| 3246 | /* We used to have a free connection slot. Since we'll never use it, |
| 3247 | * we have to inform the server that it may be used by another session. |
| 3248 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3249 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3250 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3251 | |
| 3252 | return 1; |
| 3253 | } |
| 3254 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3255 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3256 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3257 | fd_delete(t->srv_fd); |
| 3258 | if (t->srv) |
| 3259 | t->srv->cur_sess--; |
| 3260 | //close(t->srv_fd); |
| 3261 | t->srv_state = SV_STCLOSE; |
| 3262 | /* We used to have a free connection slot. Since we'll never use it, |
| 3263 | * we have to inform the server that it may be used by another session. |
| 3264 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3265 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3266 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3267 | |
| 3268 | return 1; |
| 3269 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3270 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3271 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3272 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3273 | fd_delete(t->srv_fd); |
| 3274 | if (t->srv) |
| 3275 | t->srv->cur_sess--; |
| 3276 | //close(t->srv_fd); |
| 3277 | t->srv_state = SV_STCLOSE; |
| 3278 | if (!(t->flags & SN_ERR_MASK)) |
| 3279 | t->flags |= SN_ERR_SRVTO; |
| 3280 | if (!(t->flags & SN_FINST_MASK)) |
| 3281 | t->flags |= SN_FINST_D; |
| 3282 | /* We used to have a free connection slot. Since we'll never use it, |
| 3283 | * we have to inform the server that it may be used by another session. |
| 3284 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3285 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3286 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3287 | |
| 3288 | return 1; |
| 3289 | } |
| 3290 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3291 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3292 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3293 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3294 | } |
| 3295 | } |
| 3296 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3297 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3298 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 3299 | if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3300 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | return 0; |
| 3304 | } |
| 3305 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3306 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3307 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3308 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3309 | fd_delete(t->srv_fd); |
| 3310 | if (t->srv) { |
| 3311 | t->srv->cur_sess--; |
| 3312 | t->srv->failed_resp++; |
| 3313 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3314 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3315 | //close(t->srv_fd); |
| 3316 | t->srv_state = SV_STCLOSE; |
| 3317 | if (!(t->flags & SN_ERR_MASK)) |
| 3318 | t->flags |= SN_ERR_SRVCL; |
| 3319 | if (!(t->flags & SN_FINST_MASK)) |
| 3320 | t->flags |= SN_FINST_D; |
| 3321 | /* We used to have a free connection slot. Since we'll never use it, |
| 3322 | * we have to inform the server that it may be used by another session. |
| 3323 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3324 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3325 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3326 | |
| 3327 | return 1; |
| 3328 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3329 | 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] | 3330 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3331 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3332 | fd_delete(t->srv_fd); |
| 3333 | if (t->srv) |
| 3334 | t->srv->cur_sess--; |
| 3335 | //close(t->srv_fd); |
| 3336 | t->srv_state = SV_STCLOSE; |
| 3337 | /* We used to have a free connection slot. Since we'll never use it, |
| 3338 | * we have to inform the server that it may be used by another session. |
| 3339 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3340 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3341 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3342 | |
| 3343 | return 1; |
| 3344 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3345 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3346 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3347 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3348 | fd_delete(t->srv_fd); |
| 3349 | if (t->srv) |
| 3350 | t->srv->cur_sess--; |
| 3351 | //close(t->srv_fd); |
| 3352 | t->srv_state = SV_STCLOSE; |
| 3353 | if (!(t->flags & SN_ERR_MASK)) |
| 3354 | t->flags |= SN_ERR_SRVTO; |
| 3355 | if (!(t->flags & SN_FINST_MASK)) |
| 3356 | t->flags |= SN_FINST_D; |
| 3357 | /* We used to have a free connection slot. Since we'll never use it, |
| 3358 | * we have to inform the server that it may be used by another session. |
| 3359 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3360 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3361 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3362 | |
| 3363 | return 1; |
| 3364 | } |
| 3365 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3366 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3367 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3371 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3372 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3373 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3374 | } |
| 3375 | } |
| 3376 | return 0; |
| 3377 | } |
| 3378 | else { /* SV_STCLOSE : nothing to do */ |
| 3379 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3380 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3381 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3382 | 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] | 3383 | write(1, trash, len); |
| 3384 | } |
| 3385 | return 0; |
| 3386 | } |
| 3387 | return 0; |
| 3388 | } |
| 3389 | |
| 3390 | |
| 3391 | /* |
| 3392 | * Produces data for the session <s> depending on its source. Expects to be |
| 3393 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3394 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3395 | * session, which it uses to keep on being called when there is free space in |
| 3396 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3397 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3398 | */ |
| 3399 | int produce_content(struct session *s) |
| 3400 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3401 | if (s->data_source == DATA_SRC_NONE) { |
| 3402 | s->flags &= ~SN_SELF_GEN; |
| 3403 | return 1; |
| 3404 | } |
| 3405 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3406 | /* dump server statistics */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 3407 | int ret = stats_dump_http(s, s->be->uri_auth, |
| 3408 | (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3409 | if (ret >= 0) |
| 3410 | return ret; |
| 3411 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3413 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3414 | /* unknown data source or internal error */ |
| 3415 | s->txn.status = 500; |
| 3416 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3417 | if (!(s->flags & SN_ERR_MASK)) |
| 3418 | s->flags |= SN_ERR_PRXCOND; |
| 3419 | if (!(s->flags & SN_FINST_MASK)) |
| 3420 | s->flags |= SN_FINST_R; |
| 3421 | s->flags &= ~SN_SELF_GEN; |
| 3422 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3426 | /* Iterate the same filter through all request headers. |
| 3427 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3428 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3429 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3430 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3431 | 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] | 3432 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3433 | char term; |
| 3434 | char *cur_ptr, *cur_end, *cur_next; |
| 3435 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3436 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3437 | struct hdr_idx_elem *cur_hdr; |
| 3438 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3440 | last_hdr = 0; |
| 3441 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3442 | 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] | 3443 | old_idx = 0; |
| 3444 | |
| 3445 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3446 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3447 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3448 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3449 | (exp->action == ACT_ALLOW || |
| 3450 | exp->action == ACT_DENY || |
| 3451 | exp->action == ACT_TARPIT)) |
| 3452 | return 0; |
| 3453 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3454 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3455 | if (!cur_idx) |
| 3456 | break; |
| 3457 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3458 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3459 | cur_ptr = cur_next; |
| 3460 | cur_end = cur_ptr + cur_hdr->len; |
| 3461 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3462 | |
| 3463 | /* Now we have one header between cur_ptr and cur_end, |
| 3464 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3465 | */ |
| 3466 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3467 | /* The annoying part is that pattern matching needs |
| 3468 | * that we modify the contents to null-terminate all |
| 3469 | * strings before testing them. |
| 3470 | */ |
| 3471 | |
| 3472 | term = *cur_end; |
| 3473 | *cur_end = '\0'; |
| 3474 | |
| 3475 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3476 | switch (exp->action) { |
| 3477 | case ACT_SETBE: |
| 3478 | /* It is not possible to jump a second time. |
| 3479 | * FIXME: should we return an HTTP/500 here so that |
| 3480 | * the admin knows there's a problem ? |
| 3481 | */ |
| 3482 | if (t->be != t->fe) |
| 3483 | break; |
| 3484 | |
| 3485 | /* Swithing Proxy */ |
| 3486 | t->be = (struct proxy *) exp->replace; |
| 3487 | |
| 3488 | /* right now, the backend switch is not too much complicated |
| 3489 | * because we have associated req_cap and rsp_cap to the |
| 3490 | * frontend, and the beconn will be updated later. |
| 3491 | */ |
| 3492 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3493 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3494 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3495 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3496 | last_hdr = 1; |
| 3497 | break; |
| 3498 | |
| 3499 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3500 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3501 | last_hdr = 1; |
| 3502 | break; |
| 3503 | |
| 3504 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3505 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3506 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3507 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3508 | break; |
| 3509 | |
| 3510 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3511 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3512 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3513 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3514 | break; |
| 3515 | |
| 3516 | case ACT_REPLACE: |
| 3517 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3518 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3519 | /* FIXME: if the user adds a newline in the replacement, the |
| 3520 | * index will not be recalculated for now, and the new line |
| 3521 | * will not be counted as a new header. |
| 3522 | */ |
| 3523 | |
| 3524 | cur_end += delta; |
| 3525 | cur_next += delta; |
| 3526 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3527 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3528 | break; |
| 3529 | |
| 3530 | case ACT_REMOVE: |
| 3531 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3532 | cur_next += delta; |
| 3533 | |
| 3534 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3535 | txn->req.eoh += delta; |
| 3536 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3537 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3538 | cur_hdr->len = 0; |
| 3539 | cur_end = NULL; /* null-term has been rewritten */ |
| 3540 | break; |
| 3541 | |
| 3542 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3543 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3544 | if (cur_end) |
| 3545 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3546 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3547 | /* keep the link from this header to next one in case of later |
| 3548 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3549 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3550 | old_idx = cur_idx; |
| 3551 | } |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | |
| 3556 | /* Apply the filter to the request line. |
| 3557 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3558 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3559 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3560 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3561 | */ |
| 3562 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3563 | { |
| 3564 | char term; |
| 3565 | char *cur_ptr, *cur_end; |
| 3566 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3567 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3568 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3570 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3571 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3572 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3573 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3574 | (exp->action == ACT_ALLOW || |
| 3575 | exp->action == ACT_DENY || |
| 3576 | exp->action == ACT_TARPIT)) |
| 3577 | return 0; |
| 3578 | else if (exp->action == ACT_REMOVE) |
| 3579 | return 0; |
| 3580 | |
| 3581 | done = 0; |
| 3582 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3583 | 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] | 3584 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3585 | |
| 3586 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3587 | |
| 3588 | /* The annoying part is that pattern matching needs |
| 3589 | * that we modify the contents to null-terminate all |
| 3590 | * strings before testing them. |
| 3591 | */ |
| 3592 | |
| 3593 | term = *cur_end; |
| 3594 | *cur_end = '\0'; |
| 3595 | |
| 3596 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3597 | switch (exp->action) { |
| 3598 | case ACT_SETBE: |
| 3599 | /* It is not possible to jump a second time. |
| 3600 | * FIXME: should we return an HTTP/500 here so that |
| 3601 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3602 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3603 | if (t->be != t->fe) |
| 3604 | break; |
| 3605 | |
| 3606 | /* Swithing Proxy */ |
| 3607 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3609 | /* right now, the backend switch is not too much complicated |
| 3610 | * because we have associated req_cap and rsp_cap to the |
| 3611 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3612 | */ |
| 3613 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3614 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3615 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3616 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3617 | done = 1; |
| 3618 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3620 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3621 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3622 | done = 1; |
| 3623 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3625 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3626 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3627 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3628 | done = 1; |
| 3629 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3631 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3632 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3633 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3634 | done = 1; |
| 3635 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3637 | case ACT_REPLACE: |
| 3638 | *cur_end = term; /* restore the string terminator */ |
| 3639 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3640 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3641 | /* FIXME: if the user adds a newline in the replacement, the |
| 3642 | * index will not be recalculated for now, and the new line |
| 3643 | * will not be counted as a new header. |
| 3644 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3645 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3646 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3647 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3648 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3649 | 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] | 3650 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3651 | HTTP_MSG_RQMETH, |
| 3652 | cur_ptr, cur_end + 1, |
| 3653 | NULL, NULL); |
| 3654 | if (unlikely(!cur_end)) |
| 3655 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3657 | /* we have a full request and we know that we have either a CR |
| 3658 | * or an LF at <ptr>. |
| 3659 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3660 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3661 | 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] | 3662 | /* there is no point trying this regex on headers */ |
| 3663 | return 1; |
| 3664 | } |
| 3665 | } |
| 3666 | *cur_end = term; /* restore the string terminator */ |
| 3667 | return done; |
| 3668 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3669 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3670 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3672 | /* |
| 3673 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3674 | * 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] | 3675 | * unparsable request. Since it can manage the switch to another backend, it |
| 3676 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3677 | */ |
| 3678 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3679 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3680 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3681 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3682 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3683 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3685 | /* |
| 3686 | * The interleaving of transformations and verdicts |
| 3687 | * makes it difficult to decide to continue or stop |
| 3688 | * the evaluation. |
| 3689 | */ |
| 3690 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3691 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3692 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3693 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3694 | exp = exp->next; |
| 3695 | continue; |
| 3696 | } |
| 3697 | |
| 3698 | /* Apply the filter to the request line. */ |
| 3699 | ret = apply_filter_to_req_line(t, req, exp); |
| 3700 | if (unlikely(ret < 0)) |
| 3701 | return -1; |
| 3702 | |
| 3703 | if (likely(ret == 0)) { |
| 3704 | /* The filter did not match the request, it can be |
| 3705 | * iterated through all headers. |
| 3706 | */ |
| 3707 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3708 | } |
| 3709 | exp = exp->next; |
| 3710 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3711 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3715 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3716 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3717 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3718 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3719 | */ |
| 3720 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3721 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3722 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3723 | char *p1, *p2, *p3, *p4; |
| 3724 | char *del_colon, *del_cookie, *colon; |
| 3725 | int app_cookies; |
| 3726 | |
| 3727 | appsess *asession_temp = NULL; |
| 3728 | appsess local_asession; |
| 3729 | |
| 3730 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3731 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3732 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3733 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3734 | * we start with the start line. |
| 3735 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3736 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3737 | 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] | 3738 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3739 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3740 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3741 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3742 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3743 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3744 | cur_ptr = cur_next; |
| 3745 | cur_end = cur_ptr + cur_hdr->len; |
| 3746 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3747 | |
| 3748 | /* We have one full header between cur_ptr and cur_end, and the |
| 3749 | * next header starts at cur_next. We're only interested in |
| 3750 | * "Cookie:" headers. |
| 3751 | */ |
| 3752 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3753 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3754 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3755 | old_idx = cur_idx; |
| 3756 | continue; |
| 3757 | } |
| 3758 | |
| 3759 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3760 | * attributes whose name begin with a '$', and associate them with |
| 3761 | * the right cookie, if we want to delete this cookie. |
| 3762 | * So there are 3 cases for each cookie read : |
| 3763 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3764 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3765 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3766 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3767 | * "special" cookie. |
| 3768 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3769 | * remove it. If no application cookie persists in the header, we |
| 3770 | * *MUST* delete it |
| 3771 | */ |
| 3772 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3773 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3774 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3775 | /* del_cookie == NULL => nothing to be deleted */ |
| 3776 | del_colon = del_cookie = NULL; |
| 3777 | app_cookies = 0; |
| 3778 | |
| 3779 | while (p1 < cur_end) { |
| 3780 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3781 | while (p1 < cur_end) { |
| 3782 | if (*p1 == ';' || *p1 == ',') |
| 3783 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3784 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3785 | break; |
| 3786 | p1++; |
| 3787 | } |
| 3788 | |
| 3789 | if (p1 == cur_end) |
| 3790 | break; |
| 3791 | |
| 3792 | /* p1 is at the beginning of the cookie name */ |
| 3793 | p2 = p1; |
| 3794 | while (p2 < cur_end && *p2 != '=') |
| 3795 | p2++; |
| 3796 | |
| 3797 | if (p2 == cur_end) |
| 3798 | break; |
| 3799 | |
| 3800 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3801 | if (p3 == cur_end) |
| 3802 | break; |
| 3803 | |
| 3804 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3805 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3806 | p4++; |
| 3807 | |
| 3808 | /* here, we have the cookie name between p1 and p2, |
| 3809 | * and its value between p3 and p4. |
| 3810 | * we can process it : |
| 3811 | * |
| 3812 | * Cookie: NAME=VALUE; |
| 3813 | * | || || | |
| 3814 | * | || || +--> p4 |
| 3815 | * | || |+-------> p3 |
| 3816 | * | || +--------> p2 |
| 3817 | * | |+------------> p1 |
| 3818 | * | +-------------> colon |
| 3819 | * +--------------------> cur_ptr |
| 3820 | */ |
| 3821 | |
| 3822 | if (*p1 == '$') { |
| 3823 | /* skip this one */ |
| 3824 | } |
| 3825 | else { |
| 3826 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3827 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3828 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3829 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3830 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3831 | int log_len = p4 - p1; |
| 3832 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3833 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3834 | Alert("HTTP logging : out of memory.\n"); |
| 3835 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3836 | if (log_len > t->fe->capture_len) |
| 3837 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3838 | memcpy(txn->cli_cookie, p1, log_len); |
| 3839 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3840 | } |
| 3841 | } |
| 3842 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3843 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3844 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3845 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3846 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3847 | char *delim; |
| 3848 | |
| 3849 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3850 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3851 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3852 | * |
| 3853 | * Cookie: NAME=SRV~VALUE; |
| 3854 | * | || || | | |
| 3855 | * | || || | +--> p4 |
| 3856 | * | || || +--------> delim |
| 3857 | * | || |+-----------> p3 |
| 3858 | * | || +------------> p2 |
| 3859 | * | |+----------------> p1 |
| 3860 | * | +-----------------> colon |
| 3861 | * +------------------------> cur_ptr |
| 3862 | */ |
| 3863 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3864 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3865 | for (delim = p3; delim < p4; delim++) |
| 3866 | if (*delim == COOKIE_DELIM) |
| 3867 | break; |
| 3868 | } |
| 3869 | else |
| 3870 | delim = p4; |
| 3871 | |
| 3872 | |
| 3873 | /* Here, we'll look for the first running server which supports the cookie. |
| 3874 | * This allows to share a same cookie between several servers, for example |
| 3875 | * to dedicate backup servers to specific servers only. |
| 3876 | * However, to prevent clients from sticking to cookie-less backup server |
| 3877 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3878 | * empty cookies and mark them as invalid. |
| 3879 | */ |
| 3880 | if (delim == p3) |
| 3881 | srv = NULL; |
| 3882 | |
| 3883 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3884 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3885 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3886 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3887 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3888 | txn->flags &= ~TX_CK_MASK; |
| 3889 | txn->flags |= TX_CK_VALID; |
| 3890 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3891 | t->srv = srv; |
| 3892 | break; |
| 3893 | } else { |
| 3894 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3895 | txn->flags &= ~TX_CK_MASK; |
| 3896 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | srv = srv->next; |
| 3900 | } |
| 3901 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3902 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3903 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3904 | txn->flags &= ~TX_CK_MASK; |
| 3905 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | /* depending on the cookie mode, we may have to either : |
| 3909 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3910 | * the server never sees it ; |
| 3911 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3912 | * application cookie so that it does not get accidentely removed later, |
| 3913 | * if we're in cookie prefix mode |
| 3914 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3915 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3916 | int delta; /* negative */ |
| 3917 | |
| 3918 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3919 | p4 += delta; |
| 3920 | cur_end += delta; |
| 3921 | cur_next += delta; |
| 3922 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3923 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3924 | |
| 3925 | del_cookie = del_colon = NULL; |
| 3926 | app_cookies++; /* protect the header from deletion */ |
| 3927 | } |
| 3928 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3929 | (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] | 3930 | del_cookie = p1; |
| 3931 | del_colon = colon; |
| 3932 | } |
| 3933 | } else { |
| 3934 | /* now we know that we must keep this cookie since it's |
| 3935 | * not ours. But if we wanted to delete our cookie |
| 3936 | * earlier, we cannot remove the complete header, but we |
| 3937 | * can remove the previous block itself. |
| 3938 | */ |
| 3939 | app_cookies++; |
| 3940 | |
| 3941 | if (del_cookie != NULL) { |
| 3942 | int delta; /* negative */ |
| 3943 | |
| 3944 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3945 | p4 += delta; |
| 3946 | cur_end += delta; |
| 3947 | cur_next += delta; |
| 3948 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3949 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3950 | del_cookie = del_colon = NULL; |
| 3951 | } |
| 3952 | } |
| 3953 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3954 | if ((t->be->appsession_name != NULL) && |
| 3955 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3956 | /* first, let's see if the cookie is our appcookie*/ |
| 3957 | |
| 3958 | /* Cool... it's the right one */ |
| 3959 | |
| 3960 | asession_temp = &local_asession; |
| 3961 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3962 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3963 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3964 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3965 | return; |
| 3966 | } |
| 3967 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3968 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3969 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3970 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3971 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3972 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3973 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3974 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3975 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3976 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3977 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3978 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3979 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3980 | return; |
| 3981 | } |
| 3982 | |
| 3983 | asession_temp->sessid = local_asession.sessid; |
| 3984 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3985 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3986 | } else { |
| 3987 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3988 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3989 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3990 | if (asession_temp->serverid == NULL) { |
| 3991 | Alert("Found Application Session without matching server.\n"); |
| 3992 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3993 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3994 | while (srv) { |
| 3995 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3996 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3997 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3998 | txn->flags &= ~TX_CK_MASK; |
| 3999 | txn->flags |= TX_CK_VALID; |
| 4000 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4001 | t->srv = srv; |
| 4002 | break; |
| 4003 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4004 | txn->flags &= ~TX_CK_MASK; |
| 4005 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | srv = srv->next; |
| 4009 | }/* end while(srv) */ |
| 4010 | }/* end else if server == NULL */ |
| 4011 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4012 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4013 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4014 | } |
| 4015 | |
| 4016 | /* we'll have to look for another cookie ... */ |
| 4017 | p1 = p4; |
| 4018 | } /* while (p1 < cur_end) */ |
| 4019 | |
| 4020 | /* There's no more cookie on this line. |
| 4021 | * We may have marked the last one(s) for deletion. |
| 4022 | * We must do this now in two ways : |
| 4023 | * - if there is no app cookie, we simply delete the header ; |
| 4024 | * - if there are app cookies, we must delete the end of the |
| 4025 | * string properly, including the colon/semi-colon before |
| 4026 | * the cookie name. |
| 4027 | */ |
| 4028 | if (del_cookie != NULL) { |
| 4029 | int delta; |
| 4030 | if (app_cookies) { |
| 4031 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4032 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4033 | cur_hdr->len += delta; |
| 4034 | } else { |
| 4035 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4036 | |
| 4037 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4038 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4039 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4040 | cur_hdr->len = 0; |
| 4041 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4042 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4043 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | /* keep the link from this header to next one */ |
| 4047 | old_idx = cur_idx; |
| 4048 | } /* end of cookie processing on this header */ |
| 4049 | } |
| 4050 | |
| 4051 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4052 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4053 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4054 | */ |
| 4055 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4056 | { |
| 4057 | char term; |
| 4058 | char *cur_ptr, *cur_end, *cur_next; |
| 4059 | int cur_idx, old_idx, last_hdr; |
| 4060 | struct http_txn *txn = &t->txn; |
| 4061 | struct hdr_idx_elem *cur_hdr; |
| 4062 | int len, delta; |
| 4063 | |
| 4064 | last_hdr = 0; |
| 4065 | |
| 4066 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4067 | old_idx = 0; |
| 4068 | |
| 4069 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4070 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4071 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4072 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4073 | (exp->action == ACT_ALLOW || |
| 4074 | exp->action == ACT_DENY)) |
| 4075 | return 0; |
| 4076 | |
| 4077 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4078 | if (!cur_idx) |
| 4079 | break; |
| 4080 | |
| 4081 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4082 | cur_ptr = cur_next; |
| 4083 | cur_end = cur_ptr + cur_hdr->len; |
| 4084 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4085 | |
| 4086 | /* Now we have one header between cur_ptr and cur_end, |
| 4087 | * and the next header starts at cur_next. |
| 4088 | */ |
| 4089 | |
| 4090 | /* The annoying part is that pattern matching needs |
| 4091 | * that we modify the contents to null-terminate all |
| 4092 | * strings before testing them. |
| 4093 | */ |
| 4094 | |
| 4095 | term = *cur_end; |
| 4096 | *cur_end = '\0'; |
| 4097 | |
| 4098 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4099 | switch (exp->action) { |
| 4100 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4101 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4102 | last_hdr = 1; |
| 4103 | break; |
| 4104 | |
| 4105 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4106 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4107 | last_hdr = 1; |
| 4108 | break; |
| 4109 | |
| 4110 | case ACT_REPLACE: |
| 4111 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4112 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4113 | /* FIXME: if the user adds a newline in the replacement, the |
| 4114 | * index will not be recalculated for now, and the new line |
| 4115 | * will not be counted as a new header. |
| 4116 | */ |
| 4117 | |
| 4118 | cur_end += delta; |
| 4119 | cur_next += delta; |
| 4120 | cur_hdr->len += delta; |
| 4121 | txn->rsp.eoh += delta; |
| 4122 | break; |
| 4123 | |
| 4124 | case ACT_REMOVE: |
| 4125 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4126 | cur_next += delta; |
| 4127 | |
| 4128 | /* FIXME: this should be a separate function */ |
| 4129 | txn->rsp.eoh += delta; |
| 4130 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4131 | txn->hdr_idx.used--; |
| 4132 | cur_hdr->len = 0; |
| 4133 | cur_end = NULL; /* null-term has been rewritten */ |
| 4134 | break; |
| 4135 | |
| 4136 | } |
| 4137 | } |
| 4138 | if (cur_end) |
| 4139 | *cur_end = term; /* restore the string terminator */ |
| 4140 | |
| 4141 | /* keep the link from this header to next one in case of later |
| 4142 | * removal of next header. |
| 4143 | */ |
| 4144 | old_idx = cur_idx; |
| 4145 | } |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
| 4149 | |
| 4150 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4151 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4152 | * or -1 if a replacement resulted in an invalid status line. |
| 4153 | */ |
| 4154 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4155 | { |
| 4156 | char term; |
| 4157 | char *cur_ptr, *cur_end; |
| 4158 | int done; |
| 4159 | struct http_txn *txn = &t->txn; |
| 4160 | int len, delta; |
| 4161 | |
| 4162 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4163 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4164 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4165 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4166 | (exp->action == ACT_ALLOW || |
| 4167 | exp->action == ACT_DENY)) |
| 4168 | return 0; |
| 4169 | else if (exp->action == ACT_REMOVE) |
| 4170 | return 0; |
| 4171 | |
| 4172 | done = 0; |
| 4173 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4174 | 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] | 4175 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4176 | |
| 4177 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4178 | |
| 4179 | /* The annoying part is that pattern matching needs |
| 4180 | * that we modify the contents to null-terminate all |
| 4181 | * strings before testing them. |
| 4182 | */ |
| 4183 | |
| 4184 | term = *cur_end; |
| 4185 | *cur_end = '\0'; |
| 4186 | |
| 4187 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4188 | switch (exp->action) { |
| 4189 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4190 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4191 | done = 1; |
| 4192 | break; |
| 4193 | |
| 4194 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4195 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4196 | done = 1; |
| 4197 | break; |
| 4198 | |
| 4199 | case ACT_REPLACE: |
| 4200 | *cur_end = term; /* restore the string terminator */ |
| 4201 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4202 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4203 | /* FIXME: if the user adds a newline in the replacement, the |
| 4204 | * index will not be recalculated for now, and the new line |
| 4205 | * will not be counted as a new header. |
| 4206 | */ |
| 4207 | |
| 4208 | txn->rsp.eoh += delta; |
| 4209 | cur_end += delta; |
| 4210 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4211 | 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] | 4212 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4213 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4214 | cur_ptr, cur_end + 1, |
| 4215 | NULL, NULL); |
| 4216 | if (unlikely(!cur_end)) |
| 4217 | return -1; |
| 4218 | |
| 4219 | /* we have a full respnse and we know that we have either a CR |
| 4220 | * or an LF at <ptr>. |
| 4221 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4222 | 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] | 4223 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4224 | /* there is no point trying this regex on headers */ |
| 4225 | return 1; |
| 4226 | } |
| 4227 | } |
| 4228 | *cur_end = term; /* restore the string terminator */ |
| 4229 | return done; |
| 4230 | } |
| 4231 | |
| 4232 | |
| 4233 | |
| 4234 | /* |
| 4235 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4236 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4237 | * unparsable response. |
| 4238 | */ |
| 4239 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4240 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4241 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4242 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4243 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4244 | int ret; |
| 4245 | |
| 4246 | /* |
| 4247 | * The interleaving of transformations and verdicts |
| 4248 | * makes it difficult to decide to continue or stop |
| 4249 | * the evaluation. |
| 4250 | */ |
| 4251 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4252 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4253 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4254 | exp->action == ACT_PASS)) { |
| 4255 | exp = exp->next; |
| 4256 | continue; |
| 4257 | } |
| 4258 | |
| 4259 | /* Apply the filter to the status line. */ |
| 4260 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4261 | if (unlikely(ret < 0)) |
| 4262 | return -1; |
| 4263 | |
| 4264 | if (likely(ret == 0)) { |
| 4265 | /* The filter did not match the response, it can be |
| 4266 | * iterated through all headers. |
| 4267 | */ |
| 4268 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4269 | } |
| 4270 | exp = exp->next; |
| 4271 | } |
| 4272 | return 0; |
| 4273 | } |
| 4274 | |
| 4275 | |
| 4276 | |
| 4277 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4278 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4279 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4280 | */ |
| 4281 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4282 | { |
| 4283 | struct http_txn *txn = &t->txn; |
| 4284 | char *p1, *p2, *p3, *p4; |
| 4285 | |
| 4286 | appsess *asession_temp = NULL; |
| 4287 | appsess local_asession; |
| 4288 | |
| 4289 | char *cur_ptr, *cur_end, *cur_next; |
| 4290 | int cur_idx, old_idx, delta; |
| 4291 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4292 | /* Iterate through the headers. |
| 4293 | * we start with the start line. |
| 4294 | */ |
| 4295 | old_idx = 0; |
| 4296 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4297 | |
| 4298 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4299 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4300 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4301 | |
| 4302 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4303 | cur_ptr = cur_next; |
| 4304 | cur_end = cur_ptr + cur_hdr->len; |
| 4305 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4306 | |
| 4307 | /* We have one full header between cur_ptr and cur_end, and the |
| 4308 | * next header starts at cur_next. We're only interested in |
| 4309 | * "Cookie:" headers. |
| 4310 | */ |
| 4311 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4312 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4313 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4314 | old_idx = cur_idx; |
| 4315 | continue; |
| 4316 | } |
| 4317 | |
| 4318 | /* 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] | 4319 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4320 | |
| 4321 | |
| 4322 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4323 | if (t->be->cookie_name == NULL && |
| 4324 | t->be->appsession_name == NULL && |
| 4325 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4326 | return; |
| 4327 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4328 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4329 | |
| 4330 | 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] | 4331 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4332 | break; |
| 4333 | |
| 4334 | /* p1 is at the beginning of the cookie name */ |
| 4335 | p2 = p1; |
| 4336 | |
| 4337 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4338 | p2++; |
| 4339 | |
| 4340 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4341 | break; |
| 4342 | |
| 4343 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4344 | if (p3 == cur_end) |
| 4345 | break; |
| 4346 | |
| 4347 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4348 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4349 | p4++; |
| 4350 | |
| 4351 | /* here, we have the cookie name between p1 and p2, |
| 4352 | * and its value between p3 and p4. |
| 4353 | * we can process it. |
| 4354 | */ |
| 4355 | |
| 4356 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4357 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4358 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4359 | (p4 - p1 >= t->be->capture_namelen) && |
| 4360 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4361 | int log_len = p4 - p1; |
| 4362 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4363 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4364 | Alert("HTTP logging : out of memory.\n"); |
| 4365 | } |
| 4366 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4367 | if (log_len > t->be->capture_len) |
| 4368 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4369 | memcpy(txn->srv_cookie, p1, log_len); |
| 4370 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4374 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4375 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4376 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4377 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4378 | |
| 4379 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4380 | * this occurrence because we'll insert another one later. |
| 4381 | * We'll delete it too if the "indirect" option is set and we're in |
| 4382 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4383 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4384 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4385 | /* this header must be deleted */ |
| 4386 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4387 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4388 | txn->hdr_idx.used--; |
| 4389 | cur_hdr->len = 0; |
| 4390 | cur_next += delta; |
| 4391 | txn->rsp.eoh += delta; |
| 4392 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4393 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4394 | } |
| 4395 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4396 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4397 | /* replace bytes p3->p4 with the cookie name associated |
| 4398 | * with this server since we know it. |
| 4399 | */ |
| 4400 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4401 | cur_hdr->len += delta; |
| 4402 | cur_next += delta; |
| 4403 | txn->rsp.eoh += delta; |
| 4404 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4405 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4406 | } |
| 4407 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4408 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4409 | /* insert the cookie name associated with this server |
| 4410 | * before existing cookie, and insert a delimitor between them.. |
| 4411 | */ |
| 4412 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4413 | cur_hdr->len += delta; |
| 4414 | cur_next += delta; |
| 4415 | txn->rsp.eoh += delta; |
| 4416 | |
| 4417 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4418 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4422 | else if ((t->be->appsession_name != NULL) && |
| 4423 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4424 | |
| 4425 | /* Cool... it's the right one */ |
| 4426 | |
| 4427 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4428 | asession_temp = &local_asession; |
| 4429 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4430 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4431 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4432 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4433 | return; |
| 4434 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4435 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4436 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4437 | asession_temp->serverid = NULL; |
| 4438 | |
| 4439 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4440 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4441 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4442 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4443 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4444 | return; |
| 4445 | } |
| 4446 | asession_temp->sessid = local_asession.sessid; |
| 4447 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4448 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4449 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4450 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4451 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4452 | } |
| 4453 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4454 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4455 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4456 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4457 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4458 | return; |
| 4459 | } |
| 4460 | asession_temp->serverid[0] = '\0'; |
| 4461 | } |
| 4462 | |
| 4463 | if (asession_temp->serverid[0] == '\0') |
| 4464 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4465 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4466 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4467 | |
| 4468 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4469 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4470 | #endif |
| 4471 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4472 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4473 | } /* we're now at the end of the cookie value */ |
| 4474 | |
| 4475 | /* keep the link from this header to next one */ |
| 4476 | old_idx = cur_idx; |
| 4477 | } /* end of cookie processing on this header */ |
| 4478 | } |
| 4479 | |
| 4480 | |
| 4481 | |
| 4482 | /* |
| 4483 | * Check if response is cacheable or not. Updates t->flags. |
| 4484 | */ |
| 4485 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4486 | { |
| 4487 | struct http_txn *txn = &t->txn; |
| 4488 | char *p1, *p2; |
| 4489 | |
| 4490 | char *cur_ptr, *cur_end, *cur_next; |
| 4491 | int cur_idx; |
| 4492 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4493 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4494 | return; |
| 4495 | |
| 4496 | /* Iterate through the headers. |
| 4497 | * we start with the start line. |
| 4498 | */ |
| 4499 | cur_idx = 0; |
| 4500 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4501 | |
| 4502 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4503 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4504 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4505 | |
| 4506 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4507 | cur_ptr = cur_next; |
| 4508 | cur_end = cur_ptr + cur_hdr->len; |
| 4509 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4510 | |
| 4511 | /* We have one full header between cur_ptr and cur_end, and the |
| 4512 | * next header starts at cur_next. We're only interested in |
| 4513 | * "Cookie:" headers. |
| 4514 | */ |
| 4515 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4516 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4517 | if (val) { |
| 4518 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4519 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4520 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4521 | return; |
| 4522 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4523 | } |
| 4524 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4525 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4526 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4527 | continue; |
| 4528 | |
| 4529 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4530 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4531 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4532 | |
| 4533 | if (p1 >= cur_end) /* no more info */ |
| 4534 | continue; |
| 4535 | |
| 4536 | /* p1 is at the beginning of the value */ |
| 4537 | p2 = p1; |
| 4538 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4539 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4540 | p2++; |
| 4541 | |
| 4542 | /* we have a complete value between p1 and p2 */ |
| 4543 | if (p2 < cur_end && *p2 == '=') { |
| 4544 | /* we have something of the form no-cache="set-cookie" */ |
| 4545 | if ((cur_end - p1 >= 21) && |
| 4546 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4547 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4548 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4549 | continue; |
| 4550 | } |
| 4551 | |
| 4552 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4553 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4554 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4555 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4556 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4557 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4558 | return; |
| 4559 | } |
| 4560 | |
| 4561 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4562 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4563 | continue; |
| 4564 | } |
| 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4569 | /* |
| 4570 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4571 | * If the server is found, it's assigned to the session. |
| 4572 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4573 | 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] | 4574 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4575 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4576 | appsess *asession_temp = NULL; |
| 4577 | appsess local_asession; |
| 4578 | char *request_line; |
| 4579 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4580 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4581 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4582 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4583 | ((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] | 4584 | return; |
| 4585 | |
| 4586 | /* skip ';' */ |
| 4587 | request_line++; |
| 4588 | |
| 4589 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4590 | 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] | 4591 | return; |
| 4592 | |
| 4593 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4594 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4595 | |
| 4596 | /* First try if we already have an appsession */ |
| 4597 | asession_temp = &local_asession; |
| 4598 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4599 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4600 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4601 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4602 | return; |
| 4603 | } |
| 4604 | |
| 4605 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4606 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4607 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4608 | asession_temp->serverid = NULL; |
| 4609 | |
| 4610 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4611 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4612 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4613 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4614 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4615 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4616 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4617 | return; |
| 4618 | } |
| 4619 | asession_temp->sessid = local_asession.sessid; |
| 4620 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4621 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4622 | } |
| 4623 | else { |
| 4624 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4625 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4626 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4627 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4628 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4629 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4630 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4631 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4632 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4633 | #endif |
| 4634 | if (asession_temp->serverid == NULL) { |
| 4635 | Alert("Found Application Session without matching server.\n"); |
| 4636 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4637 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4638 | while (srv) { |
| 4639 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4640 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4641 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4642 | txn->flags &= ~TX_CK_MASK; |
| 4643 | txn->flags |= TX_CK_VALID; |
| 4644 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4645 | t->srv = srv; |
| 4646 | break; |
| 4647 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4648 | txn->flags &= ~TX_CK_MASK; |
| 4649 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | srv = srv->next; |
| 4653 | } |
| 4654 | } |
| 4655 | } |
| 4656 | |
| 4657 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4658 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4659 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4660 | * 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] | 4661 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4662 | * 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] | 4663 | * 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] | 4664 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4665 | * |
| 4666 | * Returns 1 if the session's state changes, otherwise 0. |
| 4667 | */ |
| 4668 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4669 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4670 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4671 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4672 | struct user_auth *user; |
| 4673 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4674 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4676 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4677 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4678 | return 0; |
| 4679 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4680 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4681 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4682 | /* the URI is in h */ |
| 4683 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4684 | return 0; |
| 4685 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4686 | h += uri_auth->uri_len; |
| 4687 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4688 | if (memcmp(h, ";up", 3) == 0) { |
| 4689 | t->flags |= SN_STAT_HIDEDWN; |
| 4690 | break; |
| 4691 | } |
| 4692 | h++; |
| 4693 | } |
| 4694 | |
| 4695 | if (uri_auth->refresh) { |
| 4696 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4697 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4698 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4699 | t->flags |= SN_STAT_NORFRSH; |
| 4700 | break; |
| 4701 | } |
| 4702 | h++; |
| 4703 | } |
| 4704 | } |
| 4705 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4706 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4707 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4708 | if (memcmp(h, ";csv", 4) == 0) { |
| 4709 | t->flags |= SN_STAT_FMTCSV; |
| 4710 | break; |
| 4711 | } |
| 4712 | h++; |
| 4713 | } |
| 4714 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4715 | /* we are in front of a interceptable URI. Let's check |
| 4716 | * if there's an authentication and if it's valid. |
| 4717 | */ |
| 4718 | user = uri_auth->users; |
| 4719 | if (!user) { |
| 4720 | /* no user auth required, it's OK */ |
| 4721 | authenticated = 1; |
| 4722 | } else { |
| 4723 | authenticated = 0; |
| 4724 | |
| 4725 | /* a user list is defined, we have to check. |
| 4726 | * skip 21 chars for "Authorization: Basic ". |
| 4727 | */ |
| 4728 | |
| 4729 | /* FIXME: this should move to an earlier place */ |
| 4730 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4731 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4732 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4733 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4734 | if (len > 14 && |
| 4735 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4736 | txn->auth_hdr.str = h; |
| 4737 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4738 | break; |
| 4739 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4740 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4743 | if (txn->auth_hdr.len < 21 || |
| 4744 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4745 | user = NULL; |
| 4746 | |
| 4747 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4748 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4749 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4750 | user->user_pwd, user->user_len)) { |
| 4751 | authenticated = 1; |
| 4752 | break; |
| 4753 | } |
| 4754 | user = user->next; |
| 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4759 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4760 | |
| 4761 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4762 | msg.str = trash; |
| 4763 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4764 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4765 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4766 | if (!(t->flags & SN_ERR_MASK)) |
| 4767 | t->flags |= SN_ERR_PRXCOND; |
| 4768 | if (!(t->flags & SN_FINST_MASK)) |
| 4769 | t->flags |= SN_FINST_R; |
| 4770 | return 1; |
| 4771 | } |
| 4772 | |
| 4773 | /* The request is valid, the user is authenticate. Let's start sending |
| 4774 | * data. |
| 4775 | */ |
| 4776 | t->cli_state = CL_STSHUTR; |
| 4777 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4778 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4779 | t->data_source = DATA_SRC_STATS; |
| 4780 | t->data_state = DATA_ST_INIT; |
| 4781 | produce_content(t); |
| 4782 | return 1; |
| 4783 | } |
| 4784 | |
| 4785 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4786 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4787 | * Print a debug line with a header |
| 4788 | */ |
| 4789 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4790 | { |
| 4791 | int len, max; |
| 4792 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4793 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4794 | max = end - start; |
| 4795 | UBOUND(max, sizeof(trash) - len - 1); |
| 4796 | len += strlcpy2(trash + len, start, max + 1); |
| 4797 | trash[len++] = '\n'; |
| 4798 | write(1, trash, len); |
| 4799 | } |
| 4800 | |
| 4801 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4802 | /************************************************************************/ |
| 4803 | /* The code below is dedicated to ACL parsing and matching */ |
| 4804 | /************************************************************************/ |
| 4805 | |
| 4806 | |
| 4807 | |
| 4808 | |
| 4809 | /* 1. Check on METHOD |
| 4810 | * We use the pre-parsed method if it is known, and store its number as an |
| 4811 | * integer. If it is unknown, we use the pointer and the length. |
| 4812 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4813 | 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] | 4814 | { |
| 4815 | int len, meth; |
| 4816 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4817 | len = strlen(*text); |
| 4818 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4819 | |
| 4820 | pattern->val.i = meth; |
| 4821 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4822 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4823 | if (!pattern->ptr.str) |
| 4824 | return 0; |
| 4825 | pattern->len = len; |
| 4826 | } |
| 4827 | return 1; |
| 4828 | } |
| 4829 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4830 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4831 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4832 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4833 | { |
| 4834 | int meth; |
| 4835 | struct http_txn *txn = l7; |
| 4836 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4837 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4838 | return 0; |
| 4839 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4840 | meth = txn->meth; |
| 4841 | test->i = meth; |
| 4842 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4843 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4844 | /* ensure the indexes are not affected */ |
| 4845 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4846 | test->len = txn->req.sl.rq.m_l; |
| 4847 | test->ptr = txn->req.sol; |
| 4848 | } |
| 4849 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4850 | return 1; |
| 4851 | } |
| 4852 | |
| 4853 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4854 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4855 | int icase; |
| 4856 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4857 | if (test->i != pattern->val.i) |
| 4858 | return 0; |
| 4859 | |
| 4860 | if (test->i != HTTP_METH_OTHER) |
| 4861 | return 1; |
| 4862 | |
| 4863 | /* Other method, we must compare the strings */ |
| 4864 | if (pattern->len != test->len) |
| 4865 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4866 | |
| 4867 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4868 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4869 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4870 | return 0; |
| 4871 | return 1; |
| 4872 | } |
| 4873 | |
| 4874 | /* 2. Check on Request/Status Version |
| 4875 | * We simply compare strings here. |
| 4876 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4877 | 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] | 4878 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4879 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4880 | if (!pattern->ptr.str) |
| 4881 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4882 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4883 | return 1; |
| 4884 | } |
| 4885 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4886 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4887 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4888 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4889 | { |
| 4890 | struct http_txn *txn = l7; |
| 4891 | char *ptr; |
| 4892 | int len; |
| 4893 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4894 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4895 | return 0; |
| 4896 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4897 | len = txn->req.sl.rq.v_l; |
| 4898 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4899 | |
| 4900 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4901 | if (len <= 0) |
| 4902 | return 0; |
| 4903 | |
| 4904 | test->ptr = ptr; |
| 4905 | test->len = len; |
| 4906 | |
| 4907 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4908 | return 1; |
| 4909 | } |
| 4910 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4911 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4912 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4913 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4914 | { |
| 4915 | struct http_txn *txn = l7; |
| 4916 | char *ptr; |
| 4917 | int len; |
| 4918 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4919 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4920 | return 0; |
| 4921 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4922 | len = txn->rsp.sl.st.v_l; |
| 4923 | ptr = txn->rsp.sol; |
| 4924 | |
| 4925 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4926 | if (len <= 0) |
| 4927 | return 0; |
| 4928 | |
| 4929 | test->ptr = ptr; |
| 4930 | test->len = len; |
| 4931 | |
| 4932 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4933 | return 1; |
| 4934 | } |
| 4935 | |
| 4936 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4937 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4938 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4939 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4940 | { |
| 4941 | struct http_txn *txn = l7; |
| 4942 | char *ptr; |
| 4943 | int len; |
| 4944 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4945 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4946 | return 0; |
| 4947 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4948 | len = txn->rsp.sl.st.c_l; |
| 4949 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4950 | |
| 4951 | test->i = __strl2ui(ptr, len); |
| 4952 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4953 | return 1; |
| 4954 | } |
| 4955 | |
| 4956 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4957 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4958 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4959 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4960 | { |
| 4961 | struct http_txn *txn = l7; |
| 4962 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4963 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4964 | return 0; |
| 4965 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4966 | /* ensure the indexes are not affected */ |
| 4967 | return 0; |
| 4968 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4969 | test->len = txn->req.sl.rq.u_l; |
| 4970 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4971 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4972 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4973 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4974 | return 1; |
| 4975 | } |
| 4976 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4977 | static int |
| 4978 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4979 | struct acl_expr *expr, struct acl_test *test) |
| 4980 | { |
| 4981 | struct http_txn *txn = l7; |
| 4982 | |
| 4983 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4984 | return 0; |
| 4985 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4986 | /* ensure the indexes are not affected */ |
| 4987 | return 0; |
| 4988 | |
| 4989 | /* Parse HTTP request */ |
| 4990 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4991 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 4992 | test->i = AF_INET; |
| 4993 | |
| 4994 | /* |
| 4995 | * If we are parsing url in frontend space, we prepare backend stage |
| 4996 | * to not parse again the same url ! optimization lazyness... |
| 4997 | */ |
| 4998 | if (px->options & PR_O_HTTP_PROXY) |
| 4999 | l4->flags |= SN_ADDR_SET; |
| 5000 | |
| 5001 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5002 | return 1; |
| 5003 | } |
| 5004 | |
| 5005 | static int |
| 5006 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5007 | struct acl_expr *expr, struct acl_test *test) |
| 5008 | { |
| 5009 | struct http_txn *txn = l7; |
| 5010 | |
| 5011 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5012 | return 0; |
| 5013 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5014 | /* ensure the indexes are not affected */ |
| 5015 | return 0; |
| 5016 | |
| 5017 | /* Same optimization as url_ip */ |
| 5018 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5019 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5020 | |
| 5021 | if (px->options & PR_O_HTTP_PROXY) |
| 5022 | l4->flags |= SN_ADDR_SET; |
| 5023 | |
| 5024 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5025 | return 1; |
| 5026 | } |
| 5027 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5028 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5029 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5030 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5031 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5032 | 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] | 5033 | struct acl_expr *expr, struct acl_test *test) |
| 5034 | { |
| 5035 | struct http_txn *txn = l7; |
| 5036 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5037 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5038 | |
| 5039 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5040 | /* search for header from the beginning */ |
| 5041 | ctx->idx = 0; |
| 5042 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5043 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5044 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5045 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5046 | test->len = ctx->vlen; |
| 5047 | test->ptr = (char *)ctx->line + ctx->val; |
| 5048 | return 1; |
| 5049 | } |
| 5050 | |
| 5051 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5052 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5053 | return 0; |
| 5054 | } |
| 5055 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5056 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5057 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5058 | struct acl_expr *expr, struct acl_test *test) |
| 5059 | { |
| 5060 | struct http_txn *txn = l7; |
| 5061 | |
| 5062 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5063 | return 0; |
| 5064 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5065 | /* ensure the indexes are not affected */ |
| 5066 | return 0; |
| 5067 | |
| 5068 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5069 | } |
| 5070 | |
| 5071 | static int |
| 5072 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5073 | struct acl_expr *expr, struct acl_test *test) |
| 5074 | { |
| 5075 | struct http_txn *txn = l7; |
| 5076 | |
| 5077 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5078 | return 0; |
| 5079 | |
| 5080 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5081 | } |
| 5082 | |
| 5083 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5084 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5085 | */ |
| 5086 | static int |
| 5087 | 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] | 5088 | struct acl_expr *expr, struct acl_test *test) |
| 5089 | { |
| 5090 | struct http_txn *txn = l7; |
| 5091 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5092 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5093 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5094 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5095 | ctx.idx = 0; |
| 5096 | cnt = 0; |
| 5097 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5098 | cnt++; |
| 5099 | |
| 5100 | test->i = cnt; |
| 5101 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5102 | return 1; |
| 5103 | } |
| 5104 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5105 | static int |
| 5106 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5107 | struct acl_expr *expr, struct acl_test *test) |
| 5108 | { |
| 5109 | struct http_txn *txn = l7; |
| 5110 | |
| 5111 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5112 | return 0; |
| 5113 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5114 | /* ensure the indexes are not affected */ |
| 5115 | return 0; |
| 5116 | |
| 5117 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5118 | } |
| 5119 | |
| 5120 | static int |
| 5121 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5122 | struct acl_expr *expr, struct acl_test *test) |
| 5123 | { |
| 5124 | struct http_txn *txn = l7; |
| 5125 | |
| 5126 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5127 | return 0; |
| 5128 | |
| 5129 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5130 | } |
| 5131 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5132 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5133 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5134 | * 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] | 5135 | */ |
| 5136 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5137 | 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] | 5138 | struct acl_expr *expr, struct acl_test *test) |
| 5139 | { |
| 5140 | struct http_txn *txn = l7; |
| 5141 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5142 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5143 | |
| 5144 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5145 | /* search for header from the beginning */ |
| 5146 | ctx->idx = 0; |
| 5147 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5148 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5149 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5150 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5151 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5152 | return 1; |
| 5153 | } |
| 5154 | |
| 5155 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5156 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5157 | return 0; |
| 5158 | } |
| 5159 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5160 | static int |
| 5161 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5162 | struct acl_expr *expr, struct acl_test *test) |
| 5163 | { |
| 5164 | struct http_txn *txn = l7; |
| 5165 | |
| 5166 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5167 | return 0; |
| 5168 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5169 | /* ensure the indexes are not affected */ |
| 5170 | return 0; |
| 5171 | |
| 5172 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5173 | } |
| 5174 | |
| 5175 | static int |
| 5176 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5177 | struct acl_expr *expr, struct acl_test *test) |
| 5178 | { |
| 5179 | struct http_txn *txn = l7; |
| 5180 | |
| 5181 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5182 | return 0; |
| 5183 | |
| 5184 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5185 | } |
| 5186 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5187 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5188 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5189 | */ |
| 5190 | static int |
| 5191 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5192 | struct acl_expr *expr, struct acl_test *test) |
| 5193 | { |
| 5194 | struct http_txn *txn = l7; |
| 5195 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5196 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5197 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5198 | return 0; |
| 5199 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5200 | /* ensure the indexes are not affected */ |
| 5201 | return 0; |
| 5202 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5203 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5204 | end = ptr + txn->req.sl.rq.u_l; |
| 5205 | |
| 5206 | if (ptr >= end) |
| 5207 | return 0; |
| 5208 | |
| 5209 | /* RFC2616, par. 5.1.2 : |
| 5210 | * Request-URI = "*" | absuri | abspath | authority |
| 5211 | */ |
| 5212 | |
| 5213 | if (*ptr == '*') |
| 5214 | return 0; |
| 5215 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5216 | if (isalpha((unsigned char)*ptr)) { |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5217 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5218 | ptr++; |
| 5219 | while (ptr < end && |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5220 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5221 | ptr++; |
| 5222 | /* skip '://' */ |
| 5223 | if (ptr == end || *ptr++ != ':') |
| 5224 | return 0; |
| 5225 | if (ptr == end || *ptr++ != '/') |
| 5226 | return 0; |
| 5227 | if (ptr == end || *ptr++ != '/') |
| 5228 | return 0; |
| 5229 | } |
| 5230 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5231 | |
| 5232 | while (ptr < end && *ptr != '/') |
| 5233 | ptr++; |
| 5234 | |
| 5235 | if (ptr == end) |
| 5236 | return 0; |
| 5237 | |
| 5238 | /* OK, we got the '/' ! */ |
| 5239 | test->ptr = ptr; |
| 5240 | |
| 5241 | while (ptr < end && *ptr != '?') |
| 5242 | ptr++; |
| 5243 | |
| 5244 | test->len = ptr - test->ptr; |
| 5245 | |
| 5246 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5247 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5248 | return 1; |
| 5249 | } |
| 5250 | |
| 5251 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5252 | |
| 5253 | /************************************************************************/ |
| 5254 | /* All supported keywords must be declared here. */ |
| 5255 | /************************************************************************/ |
| 5256 | |
| 5257 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5258 | static struct acl_kw_list acl_kws = {{ },{ |
| 5259 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5260 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5261 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5262 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5263 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5264 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5265 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5266 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5267 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5268 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5269 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5270 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
| 5271 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip }, |
| 5272 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5273 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5274 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5275 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5276 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5277 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5278 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5279 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5280 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5281 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5282 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5283 | |
| 5284 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5285 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5286 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5287 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5288 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5289 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5290 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5291 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5292 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5293 | |
| 5294 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5295 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5296 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5297 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5298 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5299 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5300 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5301 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5302 | { NULL, NULL, NULL, NULL }, |
| 5303 | |
| 5304 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5305 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5306 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5307 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5308 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5309 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5310 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5311 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5312 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5313 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5314 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5315 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5316 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5317 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5318 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5319 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5320 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5321 | |
| 5322 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5323 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5324 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5325 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5326 | { NULL, NULL, NULL, NULL }, |
| 5327 | #endif |
| 5328 | }}; |
| 5329 | |
| 5330 | |
| 5331 | __attribute__((constructor)) |
| 5332 | static void __http_protocol_init(void) |
| 5333 | { |
| 5334 | acl_register_keywords(&acl_kws); |
| 5335 | } |
| 5336 | |
| 5337 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5338 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5339 | * Local variables: |
| 5340 | * c-indent-level: 8 |
| 5341 | * c-basic-offset: 8 |
| 5342 | * End: |
| 5343 | */ |