Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
| 33 | #include <common/time.h> |
| 34 | #include <common/uri_auth.h> |
| 35 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 37 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <types/capture.h> |
| 39 | #include <types/client.h> |
| 40 | #include <types/global.h> |
| 41 | #include <types/httperr.h> |
| 42 | #include <types/polling.h> |
| 43 | #include <types/proxy.h> |
| 44 | #include <types/server.h> |
| 45 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 46 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
| 51 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 52 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
| 54 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 55 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/session.h> |
| 57 | #include <proto/task.h> |
| 58 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_HAP_TCPSPLICE |
| 60 | #include <libtcpsplice.h> |
| 61 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 63 | #define DEBUG_PARSE_NO_SPEEDUP |
| 64 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 65 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 66 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 67 | * instruction. The first argument is the label for normal operation, and the |
| 68 | * second one is the break/continue instruction in the no_speedup mode. |
| 69 | */ |
| 70 | |
| 71 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 72 | #define QUICK_JUMP(x,y) y |
| 73 | #else |
| 74 | #define QUICK_JUMP(x,y) goto x |
| 75 | #endif |
| 76 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 79 | "HTTP/1.0 200 OK\r\n" |
| 80 | "Cache-Control: no-cache\r\n" |
| 81 | "Connection: close\r\n" |
| 82 | "Content-Type: text/html\r\n" |
| 83 | "\r\n" |
| 84 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 85 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | const struct chunk http_200_chunk = { |
| 87 | .str = (char *)&HTTP_200, |
| 88 | .len = sizeof(HTTP_200)-1 |
| 89 | }; |
| 90 | |
| 91 | const char *HTTP_302 = |
| 92 | "HTTP/1.0 302 Found\r\n" |
| 93 | "Cache-Control: no-cache\r\n" |
| 94 | "Connection: close\r\n" |
| 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
| 99 | "HTTP/1.0 303 See Other\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Connection: close\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 114 | |
| 115 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 133 | |
| 134 | [HTTP_ERR_403] = |
| 135 | "HTTP/1.0 403 Forbidden\r\n" |
| 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_408] = |
| 143 | "HTTP/1.0 408 Request Time-out\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_500] = |
| 151 | "HTTP/1.0 500 Server Error\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_502] = |
| 159 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_503] = |
| 167 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_504] = |
| 175 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 181 | |
| 182 | }; |
| 183 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 184 | /* We must put the messages here since GCC cannot initialize consts depending |
| 185 | * on strlen(). |
| 186 | */ |
| 187 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 188 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 189 | #define FD_SETS_ARE_BITFIELDS |
| 190 | #ifdef FD_SETS_ARE_BITFIELDS |
| 191 | /* |
| 192 | * This map is used with all the FD_* macros to check whether a particular bit |
| 193 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 194 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 195 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 196 | * exclusively to the macros. |
| 197 | */ |
| 198 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 199 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 200 | |
| 201 | #else |
| 202 | #error "Check if your OS uses bitfields for fd_sets" |
| 203 | #endif |
| 204 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 205 | void init_proto_http() |
| 206 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 207 | int i; |
| 208 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 212 | if (!http_err_msgs[msg]) { |
| 213 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 214 | abort(); |
| 215 | } |
| 216 | |
| 217 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 218 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 219 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
| 221 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 222 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 223 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 224 | * printable characters above. |
| 225 | */ |
| 226 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 227 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 228 | for (i = 0; i < 32; i++) { |
| 229 | FD_SET(i, hdr_encode_map); |
| 230 | FD_SET(i, url_encode_map); |
| 231 | } |
| 232 | for (i = 127; i < 256; i++) { |
| 233 | FD_SET(i, hdr_encode_map); |
| 234 | FD_SET(i, url_encode_map); |
| 235 | } |
| 236 | |
| 237 | tmp = "\"#{|}"; |
| 238 | while (*tmp) { |
| 239 | FD_SET(*tmp, hdr_encode_map); |
| 240 | tmp++; |
| 241 | } |
| 242 | |
| 243 | tmp = "\"#"; |
| 244 | while (*tmp) { |
| 245 | FD_SET(*tmp, url_encode_map); |
| 246 | tmp++; |
| 247 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 248 | |
| 249 | /* memory allocations */ |
| 250 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 251 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 252 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 254 | /* |
| 255 | * We have 26 list of methods (1 per first letter), each of which can have |
| 256 | * up to 3 entries (2 valid, 1 null). |
| 257 | */ |
| 258 | struct http_method_desc { |
| 259 | http_meth_t meth; |
| 260 | int len; |
| 261 | const char text[8]; |
| 262 | }; |
| 263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 264 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 265 | ['C' - 'A'] = { |
| 266 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 267 | }, |
| 268 | ['D' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 270 | }, |
| 271 | ['G' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 273 | }, |
| 274 | ['H' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 276 | }, |
| 277 | ['P' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 279 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 280 | }, |
| 281 | ['T' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 283 | }, |
| 284 | /* rest is empty like this : |
| 285 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 286 | */ |
| 287 | }; |
| 288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 289 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 290 | * table than two perform a boolean AND or OR between two tests. Refer to |
| 291 | * RFC2616 for those chars. |
| 292 | */ |
| 293 | |
| 294 | const char http_is_spht[256] = { |
| 295 | [' '] = 1, ['\t'] = 1, |
| 296 | }; |
| 297 | |
| 298 | const char http_is_crlf[256] = { |
| 299 | ['\r'] = 1, ['\n'] = 1, |
| 300 | }; |
| 301 | |
| 302 | const char http_is_lws[256] = { |
| 303 | [' '] = 1, ['\t'] = 1, |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_sep[256] = { |
| 308 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 309 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 310 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 311 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 312 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 313 | }; |
| 314 | |
| 315 | const char http_is_ctl[256] = { |
| 316 | [0 ... 31] = 1, |
| 317 | [127] = 1, |
| 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 322 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 323 | * them correctly. Instead, define every non-CTL char's status. |
| 324 | */ |
| 325 | const char http_is_token[256] = { |
| 326 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 327 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 328 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 329 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 330 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 331 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 332 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 333 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 334 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 335 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 336 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 337 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 338 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 339 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 340 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 341 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 342 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 343 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 344 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 345 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 346 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 347 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 348 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 349 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 350 | }; |
| 351 | |
| 352 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 353 | /* |
| 354 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 355 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 356 | */ |
| 357 | const char http_is_ver_token[256] = { |
| 358 | ['.'] = 1, ['/'] = 1, |
| 359 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 360 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 361 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 362 | }; |
| 363 | |
| 364 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | #ifdef DEBUG_FULL |
| 366 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 367 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 368 | #endif |
| 369 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 370 | static void http_sess_log(struct session *s); |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 538 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 539 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 540 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 541 | */ |
| 542 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 543 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | { |
| 545 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 546 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 547 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 548 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 549 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 550 | } |
| 551 | if (!(t->flags & SN_ERR_MASK)) |
| 552 | t->flags |= err; |
| 553 | if (!(t->flags & SN_FINST_MASK)) |
| 554 | t->flags |= finst; |
| 555 | } |
| 556 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 557 | /* This function returns the appropriate error location for the given session |
| 558 | * and message. |
| 559 | */ |
| 560 | |
| 561 | struct chunk *error_message(struct session *s, int msgnum) |
| 562 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 563 | if (s->be->errmsg[msgnum].str) |
| 564 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 565 | else if (s->fe->errmsg[msgnum].str) |
| 566 | return &s->fe->errmsg[msgnum]; |
| 567 | else |
| 568 | return &http_err_chunks[msgnum]; |
| 569 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 571 | /* |
| 572 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 573 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 574 | */ |
| 575 | static http_meth_t find_http_meth(const char *str, const int len) |
| 576 | { |
| 577 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 578 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 579 | |
| 580 | m = ((unsigned)*str - 'A'); |
| 581 | |
| 582 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 583 | for (h = http_methods[m]; h->len > 0; h++) { |
| 584 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 585 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 587 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | }; |
| 589 | return HTTP_METH_OTHER; |
| 590 | } |
| 591 | return HTTP_METH_NONE; |
| 592 | |
| 593 | } |
| 594 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | /* Processes the client and server jobs of a session task, then |
| 596 | * puts it back to the wait queue in a clean state, or |
| 597 | * cleans up its resources if it must be deleted. Returns |
| 598 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 599 | * infinity. |
| 600 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 601 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 602 | { |
| 603 | struct session *s = t->context; |
| 604 | int fsm_resync = 0; |
| 605 | |
| 606 | do { |
| 607 | fsm_resync = 0; |
| 608 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 609 | fsm_resync |= process_cli(s); |
| 610 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 611 | fsm_resync |= process_srv(s); |
| 612 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 613 | } while (fsm_resync); |
| 614 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 615 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 616 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 617 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 618 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 619 | t->expire = s->req->rex; |
| 620 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 621 | tv_bound(&t->expire, &s->req->cex); |
| 622 | tv_bound(&t->expire, &s->rep->rex); |
| 623 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 624 | |
| 625 | /* restore t to its place in the task list */ |
| 626 | task_queue(t); |
| 627 | |
| 628 | #ifdef DEBUG_FULL |
| 629 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 630 | * that a task still has something to do and will provoke a quick loop. |
| 631 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 632 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | exit(100); |
| 634 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 635 | *next = t->expire; |
| 636 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 639 | s->fe->feconn--; |
| 640 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 641 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | actconn--; |
| 643 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 644 | if (unlikely((global.mode & MODE_DEBUG) && |
| 645 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 646 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 647 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 648 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 649 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | write(1, trash, len); |
| 651 | } |
| 652 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 653 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 654 | if (s->req != NULL) |
| 655 | s->logs.bytes_in = s->req->total; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | if (s->rep != NULL) |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 657 | s->logs.bytes_out = s->rep->total; |
| 658 | |
| 659 | s->fe->bytes_in += s->logs.bytes_in; |
| 660 | s->fe->bytes_out += s->logs.bytes_out; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 661 | if (s->be != s->fe) { |
| 662 | s->be->bytes_in += s->logs.bytes_in; |
| 663 | s->be->bytes_out += s->logs.bytes_out; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 664 | } |
| 665 | if (s->srv) { |
| 666 | s->srv->bytes_in += s->logs.bytes_in; |
| 667 | s->srv->bytes_out += s->logs.bytes_out; |
| 668 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | |
| 670 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 671 | if (s->logs.logwait && |
| 672 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 673 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 674 | if (s->fe->to_log & LW_REQ) |
| 675 | http_sess_log(s); |
| 676 | else |
| 677 | tcp_sess_log(s); |
| 678 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | |
| 680 | /* the task MUST not be in the run queue anymore */ |
| 681 | task_delete(t); |
| 682 | session_free(s); |
| 683 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 684 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 688 | extern const char sess_term_cond[8]; |
| 689 | extern const char sess_fin_state[8]; |
| 690 | extern const char *monthname[12]; |
| 691 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 692 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 693 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 694 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 695 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 696 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 698 | /* |
| 699 | * send a log for the session when we have enough info about it. |
| 700 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 701 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 702 | static void http_sess_log(struct session *s) |
| 703 | { |
| 704 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 705 | struct proxy *fe = s->fe; |
| 706 | struct proxy *be = s->be; |
| 707 | struct proxy *prx_log; |
| 708 | struct http_txn *txn = &s->txn; |
| 709 | int tolog; |
| 710 | char *uri, *h; |
| 711 | char *svid; |
| 712 | struct tm *tm; |
| 713 | static char tmpline[MAX_SYSLOG_LEN]; |
| 714 | int hdr; |
| 715 | |
| 716 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 717 | return; |
| 718 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 719 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 720 | if (s->cli_addr.ss_family == AF_INET) |
| 721 | inet_ntop(AF_INET, |
| 722 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 723 | pn, sizeof(pn)); |
| 724 | else |
| 725 | inet_ntop(AF_INET6, |
| 726 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 727 | pn, sizeof(pn)); |
| 728 | |
| 729 | tm = localtime((time_t *)&s->logs.tv_accept.tv_sec); |
| 730 | |
| 731 | |
| 732 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 733 | tolog = fe->to_log; |
| 734 | |
| 735 | h = tmpline; |
| 736 | if (fe->to_log & LW_REQHDR && |
| 737 | txn->req.cap && |
| 738 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 739 | *(h++) = ' '; |
| 740 | *(h++) = '{'; |
| 741 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 742 | if (hdr) |
| 743 | *(h++) = '|'; |
| 744 | if (txn->req.cap[hdr] != NULL) |
| 745 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 746 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 747 | } |
| 748 | *(h++) = '}'; |
| 749 | } |
| 750 | |
| 751 | if (fe->to_log & LW_RSPHDR && |
| 752 | txn->rsp.cap && |
| 753 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 754 | *(h++) = ' '; |
| 755 | *(h++) = '{'; |
| 756 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 757 | if (hdr) |
| 758 | *(h++) = '|'; |
| 759 | if (txn->rsp.cap[hdr] != NULL) |
| 760 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 761 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 762 | } |
| 763 | *(h++) = '}'; |
| 764 | } |
| 765 | |
| 766 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 767 | *(h++) = ' '; |
| 768 | *(h++) = '"'; |
| 769 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 770 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 771 | '#', url_encode_map, uri); |
| 772 | *(h++) = '"'; |
| 773 | } |
| 774 | *h = '\0'; |
| 775 | |
| 776 | svid = (tolog & LW_SVID) ? |
| 777 | (s->data_source != DATA_SRC_STATS) ? |
| 778 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 779 | |
| 780 | send_log(prx_log, LOG_INFO, |
| 781 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 782 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
| 783 | " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n", |
| 784 | pn, |
| 785 | (s->cli_addr.ss_family == AF_INET) ? |
| 786 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 787 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 788 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 789 | tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000, |
| 790 | fe->id, be->id, svid, |
| 791 | s->logs.t_request, |
| 792 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 793 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 794 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 795 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 796 | txn->status, |
| 797 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in, |
| 798 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 799 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 800 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 801 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 802 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 803 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 804 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 805 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 806 | |
| 807 | s->logs.logwait = 0; |
| 808 | } |
| 809 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 810 | |
| 811 | /* |
| 812 | * Capture headers from message starting at <som> according to header list |
| 813 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 814 | */ |
| 815 | void capture_headers(char *som, struct hdr_idx *idx, |
| 816 | char **cap, struct cap_hdr *cap_hdr) |
| 817 | { |
| 818 | char *eol, *sol, *col, *sov; |
| 819 | int cur_idx; |
| 820 | struct cap_hdr *h; |
| 821 | int len; |
| 822 | |
| 823 | sol = som + hdr_idx_first_pos(idx); |
| 824 | cur_idx = hdr_idx_first_idx(idx); |
| 825 | |
| 826 | while (cur_idx) { |
| 827 | eol = sol + idx->v[cur_idx].len; |
| 828 | |
| 829 | col = sol; |
| 830 | while (col < eol && *col != ':') |
| 831 | col++; |
| 832 | |
| 833 | sov = col + 1; |
| 834 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 835 | sov++; |
| 836 | |
| 837 | for (h = cap_hdr; h; h = h->next) { |
| 838 | if ((h->namelen == col - sol) && |
| 839 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 840 | if (cap[h->index] == NULL) |
| 841 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 842 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 843 | |
| 844 | if (cap[h->index] == NULL) { |
| 845 | Alert("HTTP capture : out of memory.\n"); |
| 846 | continue; |
| 847 | } |
| 848 | |
| 849 | len = eol - sov; |
| 850 | if (len > h->len) |
| 851 | len = h->len; |
| 852 | |
| 853 | memcpy(cap[h->index], sov, len); |
| 854 | cap[h->index][len]=0; |
| 855 | } |
| 856 | } |
| 857 | sol = eol + idx->v[cur_idx].cr + 1; |
| 858 | cur_idx = idx->v[cur_idx].next; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 863 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 864 | */ |
| 865 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 866 | |
| 867 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 868 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 869 | */ |
| 870 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 871 | ptr++; \ |
| 872 | if (likely(ptr < end)) \ |
| 873 | goto good; \ |
| 874 | else { \ |
| 875 | state = (st); \ |
| 876 | goto http_msg_ood; \ |
| 877 | } \ |
| 878 | } while (0) |
| 879 | |
| 880 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 881 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 882 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 883 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 884 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 885 | * will give undefined results. |
| 886 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 887 | * and that msg->sol points to the beginning of the response. |
| 888 | * If a complete line is found (which implies that at least one CR or LF is |
| 889 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 890 | * returned indicating an incomplete line (which does not mean that parts have |
| 891 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 892 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 893 | * upon next call. |
| 894 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 895 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 896 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 897 | * 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] | 898 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 899 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 900 | 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] | 901 | const char *ptr, const char *end, |
| 902 | char **ret_ptr, int *ret_state) |
| 903 | { |
| 904 | __label__ |
| 905 | http_msg_rpver, |
| 906 | http_msg_rpver_sp, |
| 907 | http_msg_rpcode, |
| 908 | http_msg_rpcode_sp, |
| 909 | http_msg_rpreason, |
| 910 | http_msg_rpline_eol, |
| 911 | http_msg_ood, /* out of data */ |
| 912 | http_msg_invalid; |
| 913 | |
| 914 | switch (state) { |
| 915 | http_msg_rpver: |
| 916 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 917 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 918 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 919 | |
| 920 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 921 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 922 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 923 | } |
| 924 | goto http_msg_invalid; |
| 925 | |
| 926 | http_msg_rpver_sp: |
| 927 | case HTTP_MSG_RPVER_SP: |
| 928 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 929 | msg->sl.st.c = ptr - msg_buf; |
| 930 | goto http_msg_rpcode; |
| 931 | } |
| 932 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 933 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 934 | /* so it's a CR/LF, this is invalid */ |
| 935 | goto http_msg_invalid; |
| 936 | |
| 937 | http_msg_rpcode: |
| 938 | case HTTP_MSG_RPCODE: |
| 939 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 940 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 941 | |
| 942 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 943 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 944 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 945 | } |
| 946 | |
| 947 | /* so it's a CR/LF, so there is no reason phrase */ |
| 948 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 949 | http_msg_rsp_reason: |
| 950 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 951 | msg->sl.st.r = ptr - msg_buf; |
| 952 | msg->sl.st.r_l = 0; |
| 953 | goto http_msg_rpline_eol; |
| 954 | |
| 955 | http_msg_rpcode_sp: |
| 956 | case HTTP_MSG_RPCODE_SP: |
| 957 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 958 | msg->sl.st.r = ptr - msg_buf; |
| 959 | goto http_msg_rpreason; |
| 960 | } |
| 961 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 962 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 963 | /* so it's a CR/LF, so there is no reason phrase */ |
| 964 | goto http_msg_rsp_reason; |
| 965 | |
| 966 | http_msg_rpreason: |
| 967 | case HTTP_MSG_RPREASON: |
| 968 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 969 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 970 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 971 | http_msg_rpline_eol: |
| 972 | /* We have seen the end of line. Note that we do not |
| 973 | * necessarily have the \n yet, but at least we know that we |
| 974 | * have EITHER \r OR \n, otherwise the response would not be |
| 975 | * complete. We can then record the response length and return |
| 976 | * to the caller which will be able to register it. |
| 977 | */ |
| 978 | msg->sl.st.l = ptr - msg->sol; |
| 979 | return ptr; |
| 980 | |
| 981 | #ifdef DEBUG_FULL |
| 982 | default: |
| 983 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 984 | exit(1); |
| 985 | #endif |
| 986 | } |
| 987 | |
| 988 | http_msg_ood: |
| 989 | /* out of data */ |
| 990 | if (ret_state) |
| 991 | *ret_state = state; |
| 992 | if (ret_ptr) |
| 993 | *ret_ptr = (char *)ptr; |
| 994 | return NULL; |
| 995 | |
| 996 | http_msg_invalid: |
| 997 | /* invalid message */ |
| 998 | if (ret_state) |
| 999 | *ret_state = HTTP_MSG_ERROR; |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1005 | * This function parses a request line between <ptr> and <end>, starting with |
| 1006 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1007 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1008 | * will give undefined results. |
| 1009 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1010 | * and that msg->sol points to the beginning of the request. |
| 1011 | * If a complete line is found (which implies that at least one CR or LF is |
| 1012 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1013 | * returned indicating an incomplete line (which does not mean that parts have |
| 1014 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1015 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1016 | * upon next call. |
| 1017 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1018 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1019 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1020 | * 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] | 1021 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1022 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1023 | 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] | 1024 | const char *ptr, const char *end, |
| 1025 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1026 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1027 | __label__ |
| 1028 | http_msg_rqmeth, |
| 1029 | http_msg_rqmeth_sp, |
| 1030 | http_msg_rquri, |
| 1031 | http_msg_rquri_sp, |
| 1032 | http_msg_rqver, |
| 1033 | http_msg_rqline_eol, |
| 1034 | http_msg_ood, /* out of data */ |
| 1035 | http_msg_invalid; |
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 | switch (state) { |
| 1038 | http_msg_rqmeth: |
| 1039 | case HTTP_MSG_RQMETH: |
| 1040 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1041 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1042 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1043 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1044 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1045 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1046 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1047 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1048 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1049 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1050 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1051 | http_msg_req09_uri: |
| 1052 | msg->sl.rq.u = ptr - msg_buf; |
| 1053 | http_msg_req09_uri_e: |
| 1054 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1055 | http_msg_req09_ver: |
| 1056 | msg->sl.rq.v = ptr - msg_buf; |
| 1057 | msg->sl.rq.v_l = 0; |
| 1058 | goto http_msg_rqline_eol; |
| 1059 | } |
| 1060 | goto http_msg_invalid; |
| 1061 | |
| 1062 | http_msg_rqmeth_sp: |
| 1063 | case HTTP_MSG_RQMETH_SP: |
| 1064 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1065 | msg->sl.rq.u = ptr - msg_buf; |
| 1066 | goto http_msg_rquri; |
| 1067 | } |
| 1068 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1069 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1070 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1071 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | http_msg_rquri: |
| 1074 | case HTTP_MSG_RQURI: |
| 1075 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1076 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1079 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1080 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1081 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1084 | goto http_msg_req09_uri_e; |
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_rquri_sp: |
| 1087 | case HTTP_MSG_RQURI_SP: |
| 1088 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1089 | msg->sl.rq.v = ptr - msg_buf; |
| 1090 | goto http_msg_rqver; |
| 1091 | } |
| 1092 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1093 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1094 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1095 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1096 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | http_msg_rqver: |
| 1098 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1099 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1100 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1101 | |
| 1102 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1103 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1104 | http_msg_rqline_eol: |
| 1105 | /* We have seen the end of line. Note that we do not |
| 1106 | * necessarily have the \n yet, but at least we know that we |
| 1107 | * have EITHER \r OR \n, otherwise the request would not be |
| 1108 | * complete. We can then record the request length and return |
| 1109 | * to the caller which will be able to register it. |
| 1110 | */ |
| 1111 | msg->sl.rq.l = ptr - msg->sol; |
| 1112 | return ptr; |
| 1113 | } |
| 1114 | |
| 1115 | /* neither an HTTP_VER token nor a CRLF */ |
| 1116 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1117 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1118 | #ifdef DEBUG_FULL |
| 1119 | default: |
| 1120 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1121 | exit(1); |
| 1122 | #endif |
| 1123 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1124 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1125 | http_msg_ood: |
| 1126 | /* out of data */ |
| 1127 | if (ret_state) |
| 1128 | *ret_state = state; |
| 1129 | if (ret_ptr) |
| 1130 | *ret_ptr = (char *)ptr; |
| 1131 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1132 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1133 | http_msg_invalid: |
| 1134 | /* invalid message */ |
| 1135 | if (ret_state) |
| 1136 | *ret_state = HTTP_MSG_ERROR; |
| 1137 | return NULL; |
| 1138 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1139 | |
| 1140 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1141 | /* |
| 1142 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1143 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1144 | * when data are missing and recalled at the exact same location with no |
| 1145 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1146 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1147 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1148 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1149 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1150 | { |
| 1151 | __label__ |
| 1152 | http_msg_rqbefore, |
| 1153 | http_msg_rqbefore_cr, |
| 1154 | http_msg_rqmeth, |
| 1155 | http_msg_rqline_end, |
| 1156 | http_msg_hdr_first, |
| 1157 | http_msg_hdr_name, |
| 1158 | http_msg_hdr_l1_sp, |
| 1159 | http_msg_hdr_l1_lf, |
| 1160 | http_msg_hdr_l1_lws, |
| 1161 | http_msg_hdr_val, |
| 1162 | http_msg_hdr_l2_lf, |
| 1163 | http_msg_hdr_l2_lws, |
| 1164 | http_msg_complete_header, |
| 1165 | http_msg_last_lf, |
| 1166 | http_msg_ood, /* out of data */ |
| 1167 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1168 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1169 | int state; /* updated only when leaving the FSM */ |
| 1170 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1171 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1172 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1173 | ptr = buf->lr; |
| 1174 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1175 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1176 | if (unlikely(ptr >= end)) |
| 1177 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1178 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1179 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1180 | /* |
| 1181 | * First, states that are specific to the response only. |
| 1182 | * We check them first so that request and headers are |
| 1183 | * closer to each other (accessed more often). |
| 1184 | */ |
| 1185 | http_msg_rpbefore: |
| 1186 | case HTTP_MSG_RPBEFORE: |
| 1187 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1188 | if (likely(ptr == buf->data)) { |
| 1189 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1190 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1191 | } else { |
| 1192 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1193 | /* only skip empty leading lines, don't remove them */ |
| 1194 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1195 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | #else |
| 1197 | /* Remove empty leading lines, as recommended by |
| 1198 | * RFC2616. This takes a lot of time because we |
| 1199 | * must move all the buffer backwards, but this |
| 1200 | * is rarely needed. The method above will be |
| 1201 | * cleaner when we'll be able to start sending |
| 1202 | * the request from any place in the buffer. |
| 1203 | */ |
| 1204 | buf->lr = ptr; |
| 1205 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1206 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1207 | msg->sol = buf->data; |
| 1208 | ptr = buf->data; |
| 1209 | end = buf->r; |
| 1210 | #endif |
| 1211 | } |
| 1212 | hdr_idx_init(idx); |
| 1213 | state = HTTP_MSG_RPVER; |
| 1214 | goto http_msg_rpver; |
| 1215 | } |
| 1216 | |
| 1217 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1218 | goto http_msg_invalid; |
| 1219 | |
| 1220 | if (unlikely(*ptr == '\n')) |
| 1221 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1222 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1223 | /* stop here */ |
| 1224 | |
| 1225 | http_msg_rpbefore_cr: |
| 1226 | case HTTP_MSG_RPBEFORE_CR: |
| 1227 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1228 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1229 | /* stop here */ |
| 1230 | |
| 1231 | http_msg_rpver: |
| 1232 | case HTTP_MSG_RPVER: |
| 1233 | case HTTP_MSG_RPVER_SP: |
| 1234 | case HTTP_MSG_RPCODE: |
| 1235 | case HTTP_MSG_RPCODE_SP: |
| 1236 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1237 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1238 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1239 | if (unlikely(!ptr)) |
| 1240 | return; |
| 1241 | |
| 1242 | /* we have a full response and we know that we have either a CR |
| 1243 | * or an LF at <ptr>. |
| 1244 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1245 | //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] | 1246 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1247 | |
| 1248 | msg->sol = ptr; |
| 1249 | if (likely(*ptr == '\r')) |
| 1250 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1251 | goto http_msg_rpline_end; |
| 1252 | |
| 1253 | http_msg_rpline_end: |
| 1254 | case HTTP_MSG_RPLINE_END: |
| 1255 | /* msg->sol must point to the first of CR or LF. */ |
| 1256 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1257 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1258 | /* stop here */ |
| 1259 | |
| 1260 | /* |
| 1261 | * Second, states that are specific to the request only |
| 1262 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1263 | http_msg_rqbefore: |
| 1264 | case HTTP_MSG_RQBEFORE: |
| 1265 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1266 | if (likely(ptr == buf->data)) { |
| 1267 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1268 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1269 | } else { |
| 1270 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1271 | /* only skip empty leading lines, don't remove them */ |
| 1272 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1273 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1274 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | /* Remove empty leading lines, as recommended by |
| 1276 | * RFC2616. This takes a lot of time because we |
| 1277 | * must move all the buffer backwards, but this |
| 1278 | * is rarely needed. The method above will be |
| 1279 | * cleaner when we'll be able to start sending |
| 1280 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1281 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1282 | buf->lr = ptr; |
| 1283 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1284 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1285 | msg->sol = buf->data; |
| 1286 | ptr = buf->data; |
| 1287 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1288 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1290 | /* we will need this when keep-alive will be supported |
| 1291 | hdr_idx_init(idx); |
| 1292 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | state = HTTP_MSG_RQMETH; |
| 1294 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1295 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1296 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1298 | goto http_msg_invalid; |
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 | if (unlikely(*ptr == '\n')) |
| 1301 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1302 | 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] | 1303 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1304 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1305 | http_msg_rqbefore_cr: |
| 1306 | case HTTP_MSG_RQBEFORE_CR: |
| 1307 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1308 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | /* stop here */ |
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 | http_msg_rqmeth: |
| 1312 | case HTTP_MSG_RQMETH: |
| 1313 | case HTTP_MSG_RQMETH_SP: |
| 1314 | case HTTP_MSG_RQURI: |
| 1315 | case HTTP_MSG_RQURI_SP: |
| 1316 | case HTTP_MSG_RQVER: |
| 1317 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1318 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | if (unlikely(!ptr)) |
| 1320 | return; |
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 | /* we have a full request and we know that we have either a CR |
| 1323 | * or an LF at <ptr>. |
| 1324 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1325 | //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] | 1326 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | msg->sol = ptr; |
| 1329 | if (likely(*ptr == '\r')) |
| 1330 | 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] | 1331 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1332 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1333 | http_msg_rqline_end: |
| 1334 | case HTTP_MSG_RQLINE_END: |
| 1335 | /* check for HTTP/0.9 request : no version information available. |
| 1336 | * msg->sol must point to the first of CR or LF. |
| 1337 | */ |
| 1338 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1339 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1342 | 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] | 1343 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1344 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | /* |
| 1346 | * Common states below |
| 1347 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | http_msg_hdr_first: |
| 1349 | case HTTP_MSG_HDR_FIRST: |
| 1350 | msg->sol = ptr; |
| 1351 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1352 | goto http_msg_hdr_name; |
| 1353 | } |
| 1354 | |
| 1355 | if (likely(*ptr == '\r')) |
| 1356 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1357 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1358 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1359 | http_msg_hdr_name: |
| 1360 | case HTTP_MSG_HDR_NAME: |
| 1361 | /* assumes msg->sol points to the first char */ |
| 1362 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1363 | 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] | 1364 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | if (likely(*ptr == ':')) { |
| 1366 | msg->col = ptr - buf->data; |
| 1367 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1368 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1371 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | http_msg_hdr_l1_sp: |
| 1373 | case HTTP_MSG_HDR_L1_SP: |
| 1374 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1375 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1376 | 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] | 1377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | /* header value can be basically anything except CR/LF */ |
| 1379 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1382 | goto http_msg_hdr_val; |
| 1383 | } |
| 1384 | |
| 1385 | if (likely(*ptr == '\r')) |
| 1386 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1387 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | http_msg_hdr_l1_lf: |
| 1390 | case HTTP_MSG_HDR_L1_LF: |
| 1391 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1392 | 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] | 1393 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | http_msg_hdr_l1_lws: |
| 1395 | case HTTP_MSG_HDR_L1_LWS: |
| 1396 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1397 | /* replace HT,CR,LF with spaces */ |
| 1398 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1399 | buf->data[msg->sov] = ' '; |
| 1400 | goto http_msg_hdr_l1_sp; |
| 1401 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1402 | /* we had a header consisting only in spaces ! */ |
| 1403 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | goto http_msg_complete_header; |
| 1405 | |
| 1406 | http_msg_hdr_val: |
| 1407 | case HTTP_MSG_HDR_VAL: |
| 1408 | /* assumes msg->sol points to the first char, msg->col to the |
| 1409 | * colon, and msg->sov points to the first character of the |
| 1410 | * value. |
| 1411 | */ |
| 1412 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1413 | 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] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | msg->eol = ptr; |
| 1416 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1417 | * real header end in case it ends with lots of LWS, but is this |
| 1418 | * really needed ? |
| 1419 | */ |
| 1420 | if (likely(*ptr == '\r')) |
| 1421 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1422 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | http_msg_hdr_l2_lf: |
| 1425 | case HTTP_MSG_HDR_L2_LF: |
| 1426 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1427 | 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] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | http_msg_hdr_l2_lws: |
| 1430 | case HTTP_MSG_HDR_L2_LWS: |
| 1431 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1432 | /* LWS: replace HT,CR,LF with spaces */ |
| 1433 | for (; msg->eol < ptr; msg->eol++) |
| 1434 | *msg->eol = ' '; |
| 1435 | goto http_msg_hdr_val; |
| 1436 | } |
| 1437 | http_msg_complete_header: |
| 1438 | /* |
| 1439 | * It was a new header, so the last one is finished. |
| 1440 | * Assumes msg->sol points to the first char, msg->col to the |
| 1441 | * colon, msg->sov points to the first character of the value |
| 1442 | * and msg->eol to the first CR or LF so we know how the line |
| 1443 | * ends. We insert last header into the index. |
| 1444 | */ |
| 1445 | /* |
| 1446 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1447 | write(2, msg->sol, msg->eol-msg->sol); |
| 1448 | fprintf(stderr,"\n"); |
| 1449 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1450 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1452 | idx, idx->tail) < 0)) |
| 1453 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | msg->sol = ptr; |
| 1456 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1457 | goto http_msg_hdr_name; |
| 1458 | } |
| 1459 | |
| 1460 | if (likely(*ptr == '\r')) |
| 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1462 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | http_msg_last_lf: |
| 1465 | case HTTP_MSG_LAST_LF: |
| 1466 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1467 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1468 | ptr++; |
| 1469 | buf->lr = ptr; |
| 1470 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1471 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | return; |
| 1473 | #ifdef DEBUG_FULL |
| 1474 | default: |
| 1475 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1476 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1477 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | } |
| 1479 | http_msg_ood: |
| 1480 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1481 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1482 | buf->lr = ptr; |
| 1483 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | http_msg_invalid: |
| 1486 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1487 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | return; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1493 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1494 | * 0 else. |
| 1495 | */ |
| 1496 | int process_cli(struct session *t) |
| 1497 | { |
| 1498 | int s = t->srv_state; |
| 1499 | int c = t->cli_state; |
| 1500 | struct buffer *req = t->req; |
| 1501 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1504 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1505 | 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] | 1506 | req->rex.tv_sec, req->rex.tv_usec, |
| 1507 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1508 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | if (c == CL_STHEADERS) { |
| 1510 | /* |
| 1511 | * Now parse the partial (or complete) lines. |
| 1512 | * We will check the request syntax, and also join multi-line |
| 1513 | * headers. An index of all the lines will be elaborated while |
| 1514 | * parsing. |
| 1515 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1516 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1519 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | * req->data + req->eoh = end of processed headers / start of current one |
| 1521 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1522 | * req->lr = first non-visited byte |
| 1523 | * req->r = end of data |
| 1524 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1525 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1527 | struct http_txn *txn = &t->txn; |
| 1528 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1532 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1533 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | /* 1: we might have to print this header in debug mode */ |
| 1535 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1536 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1537 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1539 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1540 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | eol = sol + msg->sl.rq.l; |
| 1542 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1544 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1545 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1546 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1547 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1548 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1550 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1551 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | } |
| 1553 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1554 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1555 | |
| 1556 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1558 | * If not so, we check the FD and buffer states before leaving. |
| 1559 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1561 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1562 | * |
| 1563 | */ |
| 1564 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1565 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | /* |
| 1567 | * First, let's catch bad requests. |
| 1568 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1569 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1571 | |
| 1572 | /* 1: Since we are in header mode, if there's no space |
| 1573 | * left for headers, we won't be able to free more |
| 1574 | * later, so the session will never terminate. We |
| 1575 | * must terminate it now. |
| 1576 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1578 | /* FIXME: check if URI is set and return Status |
| 1579 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1581 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1586 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1587 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1588 | fd_delete(t->cli_fd); |
| 1589 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1590 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1591 | if (!(t->flags & SN_ERR_MASK)) |
| 1592 | t->flags |= SN_ERR_CLICL; |
| 1593 | if (!(t->flags & SN_FINST_MASK)) |
| 1594 | t->flags |= SN_FINST_R; |
| 1595 | return 1; |
| 1596 | } |
| 1597 | |
| 1598 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1599 | else if (unlikely(tv_isle(&req->rex, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1600 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1601 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1602 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1603 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1604 | if (!(t->flags & SN_ERR_MASK)) |
| 1605 | t->flags |= SN_ERR_CLITO; |
| 1606 | if (!(t->flags & SN_FINST_MASK)) |
| 1607 | t->flags |= SN_FINST_R; |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
| 1611 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1612 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1613 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1614 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1615 | * req->l == rlim-data |
| 1616 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1617 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1618 | tv_eternity(&req->rex); |
| 1619 | } |
| 1620 | return t->cli_state != CL_STHEADERS; |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | /**************************************************************** |
| 1625 | * More interesting part now : we know that we have a complete * |
| 1626 | * request which at least looks like HTTP. We have an indicator * |
| 1627 | * of each header's length, so we can parse them quickly. * |
| 1628 | ****************************************************************/ |
| 1629 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1630 | /* ensure we keep this pointer to the beginning of the message */ |
| 1631 | msg->sol = req->data + msg->som; |
| 1632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | /* |
| 1634 | * 1: identify the method |
| 1635 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1636 | 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] | 1637 | |
| 1638 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1640 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1642 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1644 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1645 | !memcmp(&req->data[msg->sl.rq.u], |
| 1646 | t->fe->monitor_uri, |
| 1647 | t->fe->monitor_uri_len))) { |
| 1648 | /* |
| 1649 | * We have found the monitor URI |
| 1650 | */ |
| 1651 | t->flags |= SN_MONITOR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1652 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | client_retnclose(t, &http_200_chunk); |
| 1654 | goto return_prx_cond; |
| 1655 | } |
| 1656 | |
| 1657 | /* |
| 1658 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1659 | * Note: we cannot log anymore if the request has been |
| 1660 | * classified as invalid. |
| 1661 | */ |
| 1662 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1663 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1664 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | int urilen = msg->sl.rq.l; |
| 1666 | |
| 1667 | if (urilen >= REQURI_LEN) |
| 1668 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1669 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1670 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | |
| 1672 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1673 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | } else { |
| 1675 | Alert("HTTP logging : out of memory.\n"); |
| 1676 | } |
| 1677 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1680 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1681 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1682 | int delta; |
| 1683 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1684 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | cur_end = msg->sol + msg->sl.rq.l; |
| 1686 | delta = 0; |
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 | if (msg->sl.rq.u_l == 0) { |
| 1689 | /* if no URI was set, add "/" */ |
| 1690 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1691 | cur_end += delta; |
| 1692 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1693 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | /* add HTTP version */ |
| 1695 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1696 | msg->eoh += delta; |
| 1697 | cur_end += delta; |
| 1698 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1699 | HTTP_MSG_RQMETH, |
| 1700 | msg->sol, cur_end + 1, |
| 1701 | NULL, NULL); |
| 1702 | if (unlikely(!cur_end)) |
| 1703 | goto return_bad_req; |
| 1704 | |
| 1705 | /* we have a full HTTP/1.0 request now and we know that |
| 1706 | * we have either a CR or an LF at <ptr>. |
| 1707 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1708 | 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] | 1709 | } |
| 1710 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | |
| 1712 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1713 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1714 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1715 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1716 | |
| 1717 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1719 | * As opposed to version 1.2, now they will be evaluated in the |
| 1720 | * filters order and not in the header order. This means that |
| 1721 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1722 | * |
| 1723 | * We can now check whether we want to switch to another |
| 1724 | * backend, in which case we will re-check the backend's |
| 1725 | * filters and various options. In order to support 3-level |
| 1726 | * switching, here's how we should proceed : |
| 1727 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1728 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1729 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1730 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1731 | * There cannot be any switch from there, so ->be cannot be |
| 1732 | * changed anymore. |
| 1733 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1734 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1735 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1736 | * The response path will be able to apply either ->be, or |
| 1737 | * ->be then ->fe filters in order to match the reverse of |
| 1738 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1739 | */ |
| 1740 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1741 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1742 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1743 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1744 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1746 | /* first check whether we have some ACLs set to block this request */ |
| 1747 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1748 | 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] | 1749 | if (cond->pol == ACL_COND_UNLESS) |
| 1750 | ret = !ret; |
| 1751 | |
| 1752 | if (ret) { |
| 1753 | txn->status = 403; |
| 1754 | /* let's log the request time */ |
| 1755 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1756 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1757 | goto return_prx_cond; |
| 1758 | } |
| 1759 | } |
| 1760 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1761 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1762 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1763 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1764 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1765 | } |
| 1766 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1767 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1768 | /* to ensure correct connection accounting on |
| 1769 | * the backend, we count the connection for the |
| 1770 | * one managing the queue. |
| 1771 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1772 | t->be->beconn++; |
| 1773 | if (t->be->beconn > t->be->beconn_max) |
| 1774 | t->be->beconn_max = t->be->beconn; |
| 1775 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1776 | t->flags |= SN_BE_ASSIGNED; |
| 1777 | } |
| 1778 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1779 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1780 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1781 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1782 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1783 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1784 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1785 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1786 | goto return_prx_cond; |
| 1787 | } |
| 1788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1790 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | !(t->flags & SN_CONN_CLOSED)) { |
| 1792 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1793 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1796 | 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] | 1797 | old_idx = 0; |
| 1798 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1799 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1800 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | cur_ptr = cur_next; |
| 1802 | cur_end = cur_ptr + cur_hdr->len; |
| 1803 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1804 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1805 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1806 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1807 | /* 3 possibilities : |
| 1808 | * - we have already set Connection: close, |
| 1809 | * so we remove this line. |
| 1810 | * - we have not yet set Connection: close, |
| 1811 | * but this line indicates close. We leave |
| 1812 | * it untouched and set the flag. |
| 1813 | * - we have not yet set Connection: close, |
| 1814 | * and this line indicates non-close. We |
| 1815 | * replace it. |
| 1816 | */ |
| 1817 | if (t->flags & SN_CONN_CLOSED) { |
| 1818 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1819 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1821 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1822 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | cur_hdr->len = 0; |
| 1824 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1825 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1826 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1827 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | cur_next += delta; |
| 1829 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1830 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | } |
| 1832 | t->flags |= SN_CONN_CLOSED; |
| 1833 | } |
| 1834 | } |
| 1835 | old_idx = cur_idx; |
| 1836 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1837 | } |
| 1838 | /* add request headers from the rule sets in the same order */ |
| 1839 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1840 | if (unlikely(http_header_add_tail(req, |
| 1841 | &txn->req, |
| 1842 | &txn->hdr_idx, |
| 1843 | rule_set->req_add[cur_idx])) < 0) |
| 1844 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1845 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1846 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1847 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1848 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1849 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1850 | /* we have to check the URI and auth for this request */ |
| 1851 | if (stats_check_uri_auth(t, rule_set)) |
| 1852 | return 1; |
| 1853 | } |
| 1854 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1855 | /* now check whether we have some switching rules for this request */ |
| 1856 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1857 | struct switching_rule *rule; |
| 1858 | |
| 1859 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1860 | int ret; |
| 1861 | |
| 1862 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1863 | if (cond->pol == ACL_COND_UNLESS) |
| 1864 | ret = !ret; |
| 1865 | |
| 1866 | if (ret) { |
| 1867 | t->be = rule->be.backend; |
| 1868 | t->be->beconn++; |
| 1869 | if (t->be->beconn > t->be->beconn_max) |
| 1870 | t->be->beconn_max = t->be->beconn; |
| 1871 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1872 | |
| 1873 | /* assign new parameters to the session from the new backend */ |
| 1874 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1875 | t->req->cto = t->be->contimeout; |
| 1876 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1877 | t->flags |= SN_BE_ASSIGNED; |
| 1878 | break; |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1883 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1884 | /* No backend was set, but there was a default |
| 1885 | * backend set in the frontend, so we use it and |
| 1886 | * loop again. |
| 1887 | */ |
| 1888 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1889 | t->be->beconn++; |
| 1890 | if (t->be->beconn > t->be->beconn_max) |
| 1891 | t->be->beconn_max = t->be->beconn; |
| 1892 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1893 | |
| 1894 | /* assign new parameters to the session from the new backend */ |
| 1895 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1896 | t->req->cto = t->be->contimeout; |
| 1897 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1898 | t->flags |= SN_BE_ASSIGNED; |
| 1899 | } |
| 1900 | } 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] | 1901 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1903 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1904 | /* To ensure correct connection accounting on |
| 1905 | * the backend, we count the connection for the |
| 1906 | * one managing the queue. |
| 1907 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1908 | t->be->beconn++; |
| 1909 | if (t->be->beconn > t->be->beconn_max) |
| 1910 | t->be->beconn_max = t->be->beconn; |
| 1911 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1912 | t->flags |= SN_BE_ASSIGNED; |
| 1913 | } |
| 1914 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1915 | /* |
| 1916 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1917 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1918 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1919 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1920 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1923 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1924 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1925 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1926 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1927 | * so let's do the same now. |
| 1928 | */ |
| 1929 | |
| 1930 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1931 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1932 | 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] | 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1937 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1938 | * Note that doing so might move headers in the request, but |
| 1939 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1940 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1941 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1942 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1943 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1944 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1946 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1947 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1948 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1949 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1950 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1951 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1952 | /* Add an X-Forwarded-For header unless the source IP is |
| 1953 | * in the 'except' network range. |
| 1954 | */ |
| 1955 | if ((!t->fe->except_mask.s_addr || |
| 1956 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1957 | != t->fe->except_net.s_addr) && |
| 1958 | (!t->be->except_mask.s_addr || |
| 1959 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1960 | != t->be->except_net.s_addr)) { |
| 1961 | int len; |
| 1962 | unsigned char *pn; |
| 1963 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1964 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1965 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1966 | pn[0], pn[1], pn[2], pn[3]); |
| 1967 | |
| 1968 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1969 | &txn->hdr_idx, trash, len)) < 0) |
| 1970 | goto return_bad_req; |
| 1971 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1972 | } |
| 1973 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1974 | /* FIXME: for the sake of completeness, we should also support |
| 1975 | * 'except' here, although it is mostly useless in this case. |
| 1976 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1977 | int len; |
| 1978 | char pn[INET6_ADDRSTRLEN]; |
| 1979 | inet_ntop(AF_INET6, |
| 1980 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1981 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1982 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 1983 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1984 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1985 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1986 | } |
| 1987 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1988 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1989 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1990 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1991 | * Note that we do not need to add it in case of HTTP/1.0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1992 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1993 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1994 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1995 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 1996 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 1997 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1998 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1999 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2000 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2002 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2003 | /************************************************************* |
| 2004 | * OK, that's finished for the headers. We have done what we * |
| 2005 | * could. Let's switch to the DATA state. * |
| 2006 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2007 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2008 | t->cli_state = CL_STDATA; |
| 2009 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2011 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2012 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2013 | if (!tv_isset(&t->fe->clitimeout) || |
| 2014 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2015 | /* If the client has no timeout, or if the server is not ready yet, |
| 2016 | * and we know for sure that it can expire, then it's cleaner to |
| 2017 | * disable the timeout on the client side so that too low values |
| 2018 | * cannot make the sessions abort too early. |
| 2019 | * |
| 2020 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2021 | * when it switches its state, otherwise a client can stay connected |
| 2022 | * indefinitely. This now seems to be OK. |
| 2023 | */ |
| 2024 | tv_eternity(&req->rex); |
| 2025 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2027 | /* When a connection is tarpitted, we use the queue timeout for the |
| 2028 | * tarpit delay, which currently happens to be the server's connect |
| 2029 | * timeout. If unset, then set it to zero because we really want it |
| 2030 | * to expire at one moment. |
| 2031 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2032 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2033 | t->req->l = 0; |
| 2034 | /* flush the request so that we can drop the connection early |
| 2035 | * if the client closes first. |
| 2036 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2037 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2038 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2039 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2040 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2041 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2042 | goto process_data; |
| 2043 | |
| 2044 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2045 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2046 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2047 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2048 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2049 | return_prx_cond: |
| 2050 | if (!(t->flags & SN_ERR_MASK)) |
| 2051 | t->flags |= SN_ERR_PRXCOND; |
| 2052 | if (!(t->flags & SN_FINST_MASK)) |
| 2053 | t->flags |= SN_FINST_R; |
| 2054 | return 1; |
| 2055 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2056 | } |
| 2057 | else if (c == CL_STDATA) { |
| 2058 | process_data: |
| 2059 | /* FIXME: this error handling is partly buggy because we always report |
| 2060 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2061 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2062 | * we're waiting for the server to connect. |
| 2063 | */ |
| 2064 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2065 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2066 | buffer_shutr(req); |
| 2067 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2068 | fd_delete(t->cli_fd); |
| 2069 | t->cli_state = CL_STCLOSE; |
| 2070 | if (!(t->flags & SN_ERR_MASK)) |
| 2071 | t->flags |= SN_ERR_CLICL; |
| 2072 | if (!(t->flags & SN_FINST_MASK)) { |
| 2073 | if (t->pend_pos) |
| 2074 | t->flags |= SN_FINST_Q; |
| 2075 | else if (s == SV_STCONN) |
| 2076 | t->flags |= SN_FINST_C; |
| 2077 | else |
| 2078 | t->flags |= SN_FINST_D; |
| 2079 | } |
| 2080 | return 1; |
| 2081 | } |
| 2082 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2083 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2084 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2085 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2086 | t->cli_state = CL_STSHUTR; |
| 2087 | return 1; |
| 2088 | } |
| 2089 | /* last server read and buffer empty */ |
| 2090 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2091 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2092 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2093 | shutdown(t->cli_fd, SHUT_WR); |
| 2094 | /* We must ensure that the read part is still alive when switching |
| 2095 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2096 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2097 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2098 | t->cli_state = CL_STSHUTW; |
| 2099 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2100 | return 1; |
| 2101 | } |
| 2102 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2103 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2104 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2105 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2106 | t->cli_state = CL_STSHUTR; |
| 2107 | if (!(t->flags & SN_ERR_MASK)) |
| 2108 | t->flags |= SN_ERR_CLITO; |
| 2109 | if (!(t->flags & SN_FINST_MASK)) { |
| 2110 | if (t->pend_pos) |
| 2111 | t->flags |= SN_FINST_Q; |
| 2112 | else if (s == SV_STCONN) |
| 2113 | t->flags |= SN_FINST_C; |
| 2114 | else |
| 2115 | t->flags |= SN_FINST_D; |
| 2116 | } |
| 2117 | return 1; |
| 2118 | } |
| 2119 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2120 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2121 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2122 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2123 | shutdown(t->cli_fd, SHUT_WR); |
| 2124 | /* We must ensure that the read part is still alive when switching |
| 2125 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2126 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2127 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2128 | |
| 2129 | t->cli_state = CL_STSHUTW; |
| 2130 | if (!(t->flags & SN_ERR_MASK)) |
| 2131 | t->flags |= SN_ERR_CLITO; |
| 2132 | if (!(t->flags & SN_FINST_MASK)) { |
| 2133 | if (t->pend_pos) |
| 2134 | t->flags |= SN_FINST_Q; |
| 2135 | else if (s == SV_STCONN) |
| 2136 | t->flags |= SN_FINST_C; |
| 2137 | else |
| 2138 | t->flags |= SN_FINST_D; |
| 2139 | } |
| 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | if (req->l >= req->rlim - req->data) { |
| 2144 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2145 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2146 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2147 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2148 | } |
| 2149 | } else { |
| 2150 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2151 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2152 | if (!tv_isset(&t->fe->clitimeout) || |
| 2153 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2154 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2155 | * know for sure that it can expire, then it's cleaner to disable the |
| 2156 | * timeout on the client side so that too low values cannot make the |
| 2157 | * sessions abort too early. |
| 2158 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2159 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2160 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2161 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | if ((rep->l == 0) || |
| 2166 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2167 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2168 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2169 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2170 | } |
| 2171 | } else { |
| 2172 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2173 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2174 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2175 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2176 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2177 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2178 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2179 | } |
| 2180 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2181 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | return 0; /* other cases change nothing */ |
| 2185 | } |
| 2186 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2187 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2188 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2189 | fd_delete(t->cli_fd); |
| 2190 | t->cli_state = CL_STCLOSE; |
| 2191 | if (!(t->flags & SN_ERR_MASK)) |
| 2192 | t->flags |= SN_ERR_CLICL; |
| 2193 | if (!(t->flags & SN_FINST_MASK)) { |
| 2194 | if (t->pend_pos) |
| 2195 | t->flags |= SN_FINST_Q; |
| 2196 | else if (s == SV_STCONN) |
| 2197 | t->flags |= SN_FINST_C; |
| 2198 | else |
| 2199 | t->flags |= SN_FINST_D; |
| 2200 | } |
| 2201 | return 1; |
| 2202 | } |
| 2203 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2204 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2205 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2206 | fd_delete(t->cli_fd); |
| 2207 | t->cli_state = CL_STCLOSE; |
| 2208 | return 1; |
| 2209 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2210 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2211 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2212 | fd_delete(t->cli_fd); |
| 2213 | t->cli_state = CL_STCLOSE; |
| 2214 | if (!(t->flags & SN_ERR_MASK)) |
| 2215 | t->flags |= SN_ERR_CLITO; |
| 2216 | if (!(t->flags & SN_FINST_MASK)) { |
| 2217 | if (t->pend_pos) |
| 2218 | t->flags |= SN_FINST_Q; |
| 2219 | else if (s == SV_STCONN) |
| 2220 | t->flags |= SN_FINST_C; |
| 2221 | else |
| 2222 | t->flags |= SN_FINST_D; |
| 2223 | } |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
| 2227 | if (t->flags & SN_SELF_GEN) { |
| 2228 | produce_content(t); |
| 2229 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2230 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2231 | fd_delete(t->cli_fd); |
| 2232 | t->cli_state = CL_STCLOSE; |
| 2233 | return 1; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | if ((rep->l == 0) |
| 2238 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2239 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2240 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2241 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2242 | } |
| 2243 | } else { |
| 2244 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2245 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2246 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 2247 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2248 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2254 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2255 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2256 | fd_delete(t->cli_fd); |
| 2257 | t->cli_state = CL_STCLOSE; |
| 2258 | if (!(t->flags & SN_ERR_MASK)) |
| 2259 | t->flags |= SN_ERR_CLICL; |
| 2260 | if (!(t->flags & SN_FINST_MASK)) { |
| 2261 | if (t->pend_pos) |
| 2262 | t->flags |= SN_FINST_Q; |
| 2263 | else if (s == SV_STCONN) |
| 2264 | t->flags |= SN_FINST_C; |
| 2265 | else |
| 2266 | t->flags |= SN_FINST_D; |
| 2267 | } |
| 2268 | return 1; |
| 2269 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2270 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2271 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2272 | fd_delete(t->cli_fd); |
| 2273 | t->cli_state = CL_STCLOSE; |
| 2274 | return 1; |
| 2275 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2276 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2277 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2278 | fd_delete(t->cli_fd); |
| 2279 | t->cli_state = CL_STCLOSE; |
| 2280 | if (!(t->flags & SN_ERR_MASK)) |
| 2281 | t->flags |= SN_ERR_CLITO; |
| 2282 | if (!(t->flags & SN_FINST_MASK)) { |
| 2283 | if (t->pend_pos) |
| 2284 | t->flags |= SN_FINST_Q; |
| 2285 | else if (s == SV_STCONN) |
| 2286 | t->flags |= SN_FINST_C; |
| 2287 | else |
| 2288 | t->flags |= SN_FINST_D; |
| 2289 | } |
| 2290 | return 1; |
| 2291 | } |
| 2292 | else if (req->l >= req->rlim - req->data) { |
| 2293 | /* no room to read more data */ |
| 2294 | |
| 2295 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2296 | * after the timeout by sending more data after it receives a close ? |
| 2297 | */ |
| 2298 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2299 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2300 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2301 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2302 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2303 | } |
| 2304 | } else { |
| 2305 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2306 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2307 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2308 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2309 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2310 | } |
| 2311 | } |
| 2312 | return 0; |
| 2313 | } |
| 2314 | else { /* CL_STCLOSE: nothing to do */ |
| 2315 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2316 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2317 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2318 | write(1, trash, len); |
| 2319 | } |
| 2320 | return 0; |
| 2321 | } |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | /* |
| 2327 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2328 | * (and a resync may be needed), 0 else. |
| 2329 | */ |
| 2330 | int process_srv(struct session *t) |
| 2331 | { |
| 2332 | int s = t->srv_state; |
| 2333 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2334 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2335 | struct buffer *req = t->req; |
| 2336 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2337 | int conn_err; |
| 2338 | |
| 2339 | #ifdef DEBUG_FULL |
| 2340 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2341 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2342 | |
| 2343 | #if 0 |
| 2344 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2345 | __FUNCTION__, __LINE__, |
| 2346 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2347 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2348 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2349 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2350 | __FUNCTION__, __LINE__, |
| 2351 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2352 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2353 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2354 | |
| 2355 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2356 | __FUNCTION__, __LINE__, |
| 2357 | req->cto.tv_sec, req->cto.tv_usec, |
| 2358 | req->rto.tv_sec, req->rto.tv_usec, |
| 2359 | req->wto.tv_sec, req->wto.tv_usec); |
| 2360 | |
| 2361 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2362 | __FUNCTION__, __LINE__, |
| 2363 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2364 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2365 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2366 | #endif |
| 2367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2368 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2369 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2370 | //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2371 | //); |
| 2372 | if (s == SV_STIDLE) { |
| 2373 | if (c == CL_STHEADERS) |
| 2374 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2375 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2376 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2377 | (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2378 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2379 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2380 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2381 | /* note that this must not return any error because it would be able to |
| 2382 | * overwrite the client_retnclose() output. |
| 2383 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2384 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2385 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
Willy Tarreau | 08fa2e3 | 2006-09-03 10:47:37 +0200 | [diff] [blame] | 2386 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2387 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2388 | |
| 2389 | return 1; |
| 2390 | } |
| 2391 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2392 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2393 | /* This connection is being tarpitted. The CLIENT side has |
| 2394 | * already set the connect expiration date to the right |
| 2395 | * timeout. We just have to check that it has not expired. |
| 2396 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2397 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2398 | return 0; |
| 2399 | |
| 2400 | /* We will set the queue timer to the time spent, just for |
| 2401 | * logging purposes. We fake a 500 server error, so that the |
| 2402 | * attacker will not suspect his connection has been tarpitted. |
| 2403 | * It will not cause trouble to the logs because we can exclude |
| 2404 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2405 | */ |
| 2406 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2407 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2408 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2409 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2410 | return 1; |
| 2411 | } |
| 2412 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2413 | /* Right now, we will need to create a connection to the server. |
| 2414 | * We might already have tried, and got a connection pending, in |
| 2415 | * which case we will not do anything till it's pending. It's up |
| 2416 | * to any other session to release it and wake us up again. |
| 2417 | */ |
| 2418 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2419 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2420 | return 0; |
| 2421 | else { |
| 2422 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2423 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2424 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2425 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2426 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2427 | if (t->srv) |
| 2428 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2429 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2430 | return 1; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | do { |
| 2435 | /* first, get a connection */ |
| 2436 | if (srv_redispatch_connect(t)) |
| 2437 | return t->srv_state != SV_STIDLE; |
| 2438 | |
| 2439 | /* try to (re-)connect to the server, and fail if we expire the |
| 2440 | * number of retries. |
| 2441 | */ |
| 2442 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2443 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2444 | return t->srv_state != SV_STIDLE; |
| 2445 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2446 | } while (1); |
| 2447 | } |
| 2448 | } |
| 2449 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2450 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2451 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2452 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2453 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2454 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2455 | fd_delete(t->srv_fd); |
| 2456 | if (t->srv) |
| 2457 | t->srv->cur_sess--; |
| 2458 | |
| 2459 | /* note that this must not return any error because it would be able to |
| 2460 | * overwrite the client_retnclose() output. |
| 2461 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2462 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2463 | return 1; |
| 2464 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2465 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2466 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2467 | return 0; /* nothing changed */ |
| 2468 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2469 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2470 | /* timeout, asynchronous connect error or first write error */ |
| 2471 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2472 | |
| 2473 | fd_delete(t->srv_fd); |
| 2474 | if (t->srv) |
| 2475 | t->srv->cur_sess--; |
| 2476 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2477 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2478 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2479 | else |
| 2480 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2481 | |
| 2482 | /* ensure that we have enough retries left */ |
| 2483 | if (srv_count_retry_down(t, conn_err)) |
| 2484 | return 1; |
| 2485 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2486 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2487 | /* We're on our last chance, and the REDISP option was specified. |
| 2488 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2489 | */ |
| 2490 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2491 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2492 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2493 | |
| 2494 | if (t->srv) |
| 2495 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2496 | t->be->failed_conns++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2497 | |
| 2498 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2499 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | a5e6575 | 2007-03-18 20:53:22 +0100 | [diff] [blame] | 2500 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2501 | |
| 2502 | /* first, get a connection */ |
| 2503 | if (srv_redispatch_connect(t)) |
| 2504 | return t->srv_state != SV_STIDLE; |
| 2505 | } |
| 2506 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2507 | do { |
| 2508 | /* Now we will try to either reconnect to the same server or |
| 2509 | * connect to another server. If the connection gets queued |
| 2510 | * because all servers are saturated, then we will go back to |
| 2511 | * the SV_STIDLE state. |
| 2512 | */ |
| 2513 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2514 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2515 | return t->srv_state != SV_STCONN; |
| 2516 | } |
| 2517 | |
| 2518 | /* we need to redispatch the connection to another server */ |
| 2519 | if (srv_redispatch_connect(t)) |
| 2520 | return t->srv_state != SV_STCONN; |
| 2521 | } while (1); |
| 2522 | } |
| 2523 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2524 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2525 | |
| 2526 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2527 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2528 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2529 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2530 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2531 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2532 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2533 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2534 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2535 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2536 | } |
| 2537 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2538 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2541 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2542 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2543 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2544 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2545 | |
| 2546 | t->srv_state = SV_STDATA; |
| 2547 | if (t->srv) |
| 2548 | t->srv->cum_sess++; |
| 2549 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2550 | |
| 2551 | /* if the user wants to log as soon as possible, without counting |
| 2552 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2553 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2554 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2555 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2556 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2557 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2558 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2559 | /* TCP splicing supported by both FE and BE */ |
| 2560 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2561 | } |
| 2562 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2563 | } |
| 2564 | else { |
| 2565 | t->srv_state = SV_STHEADERS; |
| 2566 | if (t->srv) |
| 2567 | t->srv->cum_sess++; |
| 2568 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2569 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2570 | /* reset hdr_idx which was already initialized by the request. |
| 2571 | * right now, the http parser does it. |
| 2572 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2573 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2574 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2575 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2576 | return 1; |
| 2577 | } |
| 2578 | } |
| 2579 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2580 | /* |
| 2581 | * Now parse the partial (or complete) lines. |
| 2582 | * We will check the response syntax, and also join multi-line |
| 2583 | * headers. An index of all the lines will be elaborated while |
| 2584 | * parsing. |
| 2585 | * |
| 2586 | * For the parsing, we use a 28 states FSM. |
| 2587 | * |
| 2588 | * Here is the information we currently have : |
| 2589 | * rep->data + req->som = beginning of response |
| 2590 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2591 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2592 | * rep->lr = first non-visited byte |
| 2593 | * rep->r = end of data |
| 2594 | */ |
| 2595 | |
| 2596 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2597 | struct http_msg *msg = &txn->rsp; |
| 2598 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2599 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2600 | if (likely(rep->lr < rep->r)) |
| 2601 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2602 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2603 | /* 1: we might have to print this header in debug mode */ |
| 2604 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2605 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2606 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2607 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2609 | sol = rep->data + msg->som; |
| 2610 | eol = sol + msg->sl.rq.l; |
| 2611 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2612 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2613 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2614 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2615 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2616 | while (cur_idx) { |
| 2617 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2618 | debug_hdr("srvhdr", t, sol, eol); |
| 2619 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2620 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2621 | } |
| 2622 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2624 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2625 | if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2626 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2627 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2628 | * rep->l == rlim-data |
| 2629 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2630 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2631 | tv_eternity(&rep->rex); |
| 2632 | } |
| 2633 | |
| 2634 | |
| 2635 | /* |
| 2636 | * Now we quickly check if we have found a full valid response. |
| 2637 | * If not so, we check the FD and buffer states before leaving. |
| 2638 | * A full response is indicated by the fact that we have seen |
| 2639 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2640 | * responses are checked first. |
| 2641 | * |
| 2642 | * Depending on whether the client is still there or not, we |
| 2643 | * may send an error response back or not. Note that normally |
| 2644 | * we should only check for HTTP status there, and check I/O |
| 2645 | * errors somewhere else. |
| 2646 | */ |
| 2647 | |
| 2648 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2649 | |
| 2650 | /* Invalid response, or read error or write error */ |
| 2651 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2652 | (req->flags & BF_WRITE_ERROR) || |
| 2653 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2654 | buffer_shutr(rep); |
| 2655 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2656 | fd_delete(t->srv_fd); |
| 2657 | if (t->srv) { |
| 2658 | t->srv->cur_sess--; |
| 2659 | t->srv->failed_resp++; |
| 2660 | } |
| 2661 | t->be->failed_resp++; |
| 2662 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2663 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2664 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2665 | if (!(t->flags & SN_ERR_MASK)) |
| 2666 | t->flags |= SN_ERR_SRVCL; |
| 2667 | if (!(t->flags & SN_FINST_MASK)) |
| 2668 | t->flags |= SN_FINST_H; |
| 2669 | /* We used to have a free connection slot. Since we'll never use it, |
| 2670 | * we have to inform the server that it may be used by another session. |
| 2671 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2672 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2673 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2674 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2675 | return 1; |
| 2676 | } |
| 2677 | |
| 2678 | /* end of client write or end of server read. |
| 2679 | * since we are in header mode, if there's no space left for headers, we |
| 2680 | * won't be able to free more later, so the session will never terminate. |
| 2681 | */ |
| 2682 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2683 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2684 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2685 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2686 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2687 | t->srv_state = SV_STSHUTR; |
| 2688 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | /* read timeout : return a 504 to the client. |
| 2693 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2694 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2695 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2696 | buffer_shutr(rep); |
| 2697 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2698 | fd_delete(t->srv_fd); |
| 2699 | if (t->srv) { |
| 2700 | t->srv->cur_sess--; |
| 2701 | t->srv->failed_resp++; |
| 2702 | } |
| 2703 | t->be->failed_resp++; |
| 2704 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2705 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2706 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2707 | if (!(t->flags & SN_ERR_MASK)) |
| 2708 | t->flags |= SN_ERR_SRVTO; |
| 2709 | if (!(t->flags & SN_FINST_MASK)) |
| 2710 | t->flags |= SN_FINST_H; |
| 2711 | /* We used to have a free connection slot. Since we'll never use it, |
| 2712 | * we have to inform the server that it may be used by another session. |
| 2713 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2714 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2715 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2716 | return 1; |
| 2717 | } |
| 2718 | |
| 2719 | /* last client read and buffer empty */ |
| 2720 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2721 | * client shuts read too early, because we may still have |
| 2722 | * some work to do on the headers. |
| 2723 | * The side-effect is that if the client completely closes its |
| 2724 | * connection during SV_STHEADER, the connection to the server |
| 2725 | * is kept until a response comes back or the timeout is reached. |
| 2726 | */ |
| 2727 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2728 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2729 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2730 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2731 | |
| 2732 | /* We must ensure that the read part is still |
| 2733 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2734 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2735 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2736 | |
| 2737 | shutdown(t->srv_fd, SHUT_WR); |
| 2738 | t->srv_state = SV_STSHUTW; |
| 2739 | return 1; |
| 2740 | } |
| 2741 | |
| 2742 | /* write timeout */ |
| 2743 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2744 | * client shuts read too early, because we may still have |
| 2745 | * some work to do on the headers. |
| 2746 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2747 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2748 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2749 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2750 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2751 | shutdown(t->srv_fd, SHUT_WR); |
| 2752 | /* We must ensure that the read part is still alive |
| 2753 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2754 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2755 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2756 | |
| 2757 | t->srv_state = SV_STSHUTW; |
| 2758 | if (!(t->flags & SN_ERR_MASK)) |
| 2759 | t->flags |= SN_ERR_SRVTO; |
| 2760 | if (!(t->flags & SN_FINST_MASK)) |
| 2761 | t->flags |= SN_FINST_H; |
| 2762 | return 1; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * And now the non-error cases. |
| 2767 | */ |
| 2768 | |
| 2769 | /* Data remaining in the request buffer. |
| 2770 | * This happens during the first pass here, and during |
| 2771 | * long posts. |
| 2772 | */ |
| 2773 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2774 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2775 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2776 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2777 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2778 | * we refresh it. */ |
| 2779 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2780 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2781 | else |
| 2782 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2784 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2786 | /* nothing left in the request buffer */ |
| 2787 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2788 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2789 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2790 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | return t->srv_state != SV_STHEADERS; |
| 2795 | } |
| 2796 | |
| 2797 | |
| 2798 | /***************************************************************** |
| 2799 | * More interesting part now : we know that we have a complete * |
| 2800 | * response which at least looks like HTTP. We have an indicator * |
| 2801 | * of each header's length, so we can parse them quickly. * |
| 2802 | ****************************************************************/ |
| 2803 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2804 | /* ensure we keep this pointer to the beginning of the message */ |
| 2805 | msg->sol = rep->data + msg->som; |
| 2806 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2807 | /* |
| 2808 | * 1: get the status code and check for cacheability. |
| 2809 | */ |
| 2810 | |
| 2811 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2812 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2813 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2814 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2815 | case 200: |
| 2816 | case 203: |
| 2817 | case 206: |
| 2818 | case 300: |
| 2819 | case 301: |
| 2820 | case 410: |
| 2821 | /* RFC2616 @13.4: |
| 2822 | * "A response received with a status code of |
| 2823 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2824 | * by a cache (...) unless a cache-control |
| 2825 | * directive prohibits caching." |
| 2826 | * |
| 2827 | * RFC2616 @9.5: POST method : |
| 2828 | * "Responses to this method are not cacheable, |
| 2829 | * unless the response includes appropriate |
| 2830 | * Cache-Control or Expires header fields." |
| 2831 | */ |
| 2832 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2833 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2834 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2835 | break; |
| 2836 | default: |
| 2837 | break; |
| 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * 2: we may need to capture headers |
| 2842 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2843 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2844 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2845 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* |
| 2848 | * 3: we will have to evaluate the filters. |
| 2849 | * As opposed to version 1.2, now they will be evaluated in the |
| 2850 | * filters order and not in the header order. This means that |
| 2851 | * each filter has to be validated among all headers. |
| 2852 | * |
| 2853 | * Filters are tried with ->be first, then with ->fe if it is |
| 2854 | * different from ->be. |
| 2855 | */ |
| 2856 | |
| 2857 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2858 | |
| 2859 | cur_proxy = t->be; |
| 2860 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2861 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2862 | |
| 2863 | /* try headers filters */ |
| 2864 | if (rule_set->rsp_exp != NULL) { |
| 2865 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2866 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2867 | if (t->srv) { |
| 2868 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2869 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2870 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2871 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2872 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2873 | buffer_shutr(rep); |
| 2874 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2875 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2876 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2877 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2878 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2879 | if (!(t->flags & SN_ERR_MASK)) |
| 2880 | t->flags |= SN_ERR_PRXCOND; |
| 2881 | if (!(t->flags & SN_FINST_MASK)) |
| 2882 | t->flags |= SN_FINST_H; |
| 2883 | /* We used to have a free connection slot. Since we'll never use it, |
| 2884 | * we have to inform the server that it may be used by another session. |
| 2885 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2886 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2887 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2888 | return 1; |
| 2889 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2890 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2891 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2892 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2893 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2894 | if (t->srv) { |
| 2895 | t->srv->cur_sess--; |
| 2896 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2897 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2898 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2899 | goto return_srv_prx_502; |
| 2900 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2901 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2902 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2903 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2904 | !(t->flags & SN_CONN_CLOSED)) { |
| 2905 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2906 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2907 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2908 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2909 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2910 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2912 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2913 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2914 | cur_ptr = cur_next; |
| 2915 | cur_end = cur_ptr + cur_hdr->len; |
| 2916 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2918 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2919 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2920 | /* 3 possibilities : |
| 2921 | * - we have already set Connection: close, |
| 2922 | * so we remove this line. |
| 2923 | * - we have not yet set Connection: close, |
| 2924 | * but this line indicates close. We leave |
| 2925 | * it untouched and set the flag. |
| 2926 | * - we have not yet set Connection: close, |
| 2927 | * and this line indicates non-close. We |
| 2928 | * replace it. |
| 2929 | */ |
| 2930 | if (t->flags & SN_CONN_CLOSED) { |
| 2931 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2932 | txn->rsp.eoh += delta; |
| 2933 | cur_next += delta; |
| 2934 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2935 | txn->hdr_idx.used--; |
| 2936 | cur_hdr->len = 0; |
| 2937 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2938 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2939 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2940 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2941 | cur_next += delta; |
| 2942 | cur_hdr->len += delta; |
| 2943 | txn->rsp.eoh += delta; |
| 2944 | } |
| 2945 | t->flags |= SN_CONN_CLOSED; |
| 2946 | } |
| 2947 | } |
| 2948 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2949 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2950 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2952 | /* add response headers from the rule sets in the same order */ |
| 2953 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2954 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2955 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2956 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2959 | /* check whether we're already working on the frontend */ |
| 2960 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2961 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2962 | cur_proxy = t->fe; |
| 2963 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2965 | /* |
| 2966 | * 4: check for server cookie. |
| 2967 | */ |
| 2968 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2969 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2971 | /* |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2972 | * 5: check for cache-control or pragma headers. |
| 2973 | */ |
| 2974 | check_response_for_cacheability(t, rep); |
| 2975 | |
| 2976 | |
| 2977 | /* |
| 2978 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2979 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2980 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2981 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2982 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2983 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2984 | /* the server is known, it's not the one the client requested, we have to |
| 2985 | * insert a set-cookie here, except if we want to insert only on POST |
| 2986 | * requests and this one isn't. Note that servers which don't have cookies |
| 2987 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2988 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2989 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2990 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2991 | 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] | 2992 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2993 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2994 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2995 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2996 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2997 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2998 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2999 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3000 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3001 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3002 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3003 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3004 | |
| 3005 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3006 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3007 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3008 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3009 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3010 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3015 | * 7: check if result will be cacheable with a cookie. |
| 3016 | * We'll block the response if security checks have caught |
| 3017 | * nasty things such as a cacheable cookie. |
| 3018 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3019 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3020 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3021 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3022 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3023 | /* we're in presence of a cacheable response containing |
| 3024 | * a set-cookie header. We'll block it as requested by |
| 3025 | * the 'checkcache' option, and send an alert. |
| 3026 | */ |
| 3027 | if (t->srv) { |
| 3028 | t->srv->cur_sess--; |
| 3029 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3030 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3031 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3032 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3033 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3034 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3035 | send_log(t->be, LOG_ALERT, |
| 3036 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3037 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3038 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3039 | } |
| 3040 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3041 | /* |
| 3042 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3043 | * 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] | 3044 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3045 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3046 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3047 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3048 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3049 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3050 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3051 | goto return_bad_resp; |
| 3052 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3053 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3054 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3055 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3056 | /************************************************************* |
| 3057 | * OK, that's finished for the headers. We have done what we * |
| 3058 | * could. Let's switch to the DATA state. * |
| 3059 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3061 | t->srv_state = SV_STDATA; |
| 3062 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3063 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3064 | |
| 3065 | /* client connection already closed or option 'forceclose' required : |
| 3066 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3067 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3068 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3069 | (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] | 3070 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3071 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3072 | |
| 3073 | /* We must ensure that the read part is still alive when switching |
| 3074 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3075 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3076 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3077 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3078 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3079 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3080 | } |
| 3081 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3082 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3083 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3084 | /* TCP splicing supported by both FE and BE */ |
| 3085 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3086 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3087 | #endif |
| 3088 | /* if the user wants to log as soon as possible, without counting |
| 3089 | bytes from the server, then this is the right moment. */ |
| 3090 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3091 | 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] | 3092 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3093 | if (t->fe->to_log & LW_REQ) |
| 3094 | http_sess_log(t); |
| 3095 | else |
| 3096 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3097 | } |
| 3098 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3099 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3100 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3101 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3102 | |
| 3103 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3104 | } |
| 3105 | else if (s == SV_STDATA) { |
| 3106 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3107 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3108 | buffer_shutr(rep); |
| 3109 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3110 | fd_delete(t->srv_fd); |
| 3111 | if (t->srv) { |
| 3112 | t->srv->cur_sess--; |
| 3113 | t->srv->failed_resp++; |
| 3114 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3115 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3116 | t->srv_state = SV_STCLOSE; |
| 3117 | if (!(t->flags & SN_ERR_MASK)) |
| 3118 | t->flags |= SN_ERR_SRVCL; |
| 3119 | if (!(t->flags & SN_FINST_MASK)) |
| 3120 | t->flags |= SN_FINST_D; |
| 3121 | /* We used to have a free connection slot. Since we'll never use it, |
| 3122 | * we have to inform the server that it may be used by another session. |
| 3123 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3124 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3125 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3126 | |
| 3127 | return 1; |
| 3128 | } |
| 3129 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3130 | 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] | 3131 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3132 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3133 | t->srv_state = SV_STSHUTR; |
| 3134 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3135 | return 1; |
| 3136 | } |
| 3137 | /* end of client read and no more data to send */ |
| 3138 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3139 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3140 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3141 | shutdown(t->srv_fd, SHUT_WR); |
| 3142 | /* We must ensure that the read part is still alive when switching |
| 3143 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3144 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3145 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3146 | |
| 3147 | t->srv_state = SV_STSHUTW; |
| 3148 | return 1; |
| 3149 | } |
| 3150 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3151 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3152 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3153 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3154 | t->srv_state = SV_STSHUTR; |
| 3155 | if (!(t->flags & SN_ERR_MASK)) |
| 3156 | t->flags |= SN_ERR_SRVTO; |
| 3157 | if (!(t->flags & SN_FINST_MASK)) |
| 3158 | t->flags |= SN_FINST_D; |
| 3159 | return 1; |
| 3160 | } |
| 3161 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3162 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3163 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3164 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3165 | shutdown(t->srv_fd, SHUT_WR); |
| 3166 | /* We must ensure that the read part is still alive when switching |
| 3167 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3168 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3169 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3170 | t->srv_state = SV_STSHUTW; |
| 3171 | if (!(t->flags & SN_ERR_MASK)) |
| 3172 | t->flags |= SN_ERR_SRVTO; |
| 3173 | if (!(t->flags & SN_FINST_MASK)) |
| 3174 | t->flags |= SN_FINST_D; |
| 3175 | return 1; |
| 3176 | } |
| 3177 | |
| 3178 | /* recompute request time-outs */ |
| 3179 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3180 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3181 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3182 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3186 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3187 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3188 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3189 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3190 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3191 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3192 | } |
| 3193 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3194 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | /* recompute response time-outs */ |
| 3199 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3200 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3201 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3205 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3206 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3207 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | return 0; /* other cases change nothing */ |
| 3212 | } |
| 3213 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3214 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3215 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3216 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3217 | fd_delete(t->srv_fd); |
| 3218 | if (t->srv) { |
| 3219 | t->srv->cur_sess--; |
| 3220 | t->srv->failed_resp++; |
| 3221 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3222 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3223 | //close(t->srv_fd); |
| 3224 | t->srv_state = SV_STCLOSE; |
| 3225 | if (!(t->flags & SN_ERR_MASK)) |
| 3226 | t->flags |= SN_ERR_SRVCL; |
| 3227 | if (!(t->flags & SN_FINST_MASK)) |
| 3228 | t->flags |= SN_FINST_D; |
| 3229 | /* We used to have a free connection slot. Since we'll never use it, |
| 3230 | * we have to inform the server that it may be used by another session. |
| 3231 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3232 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3233 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3234 | |
| 3235 | return 1; |
| 3236 | } |
| 3237 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3238 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3239 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3240 | fd_delete(t->srv_fd); |
| 3241 | if (t->srv) |
| 3242 | t->srv->cur_sess--; |
| 3243 | //close(t->srv_fd); |
| 3244 | t->srv_state = SV_STCLOSE; |
| 3245 | /* We used to have a free connection slot. Since we'll never use it, |
| 3246 | * we have to inform the server that it may be used by another session. |
| 3247 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3248 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3249 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3250 | |
| 3251 | return 1; |
| 3252 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3253 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3254 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3255 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3256 | fd_delete(t->srv_fd); |
| 3257 | if (t->srv) |
| 3258 | t->srv->cur_sess--; |
| 3259 | //close(t->srv_fd); |
| 3260 | t->srv_state = SV_STCLOSE; |
| 3261 | if (!(t->flags & SN_ERR_MASK)) |
| 3262 | t->flags |= SN_ERR_SRVTO; |
| 3263 | if (!(t->flags & SN_FINST_MASK)) |
| 3264 | t->flags |= SN_FINST_D; |
| 3265 | /* We used to have a free connection slot. Since we'll never use it, |
| 3266 | * we have to inform the server that it may be used by another session. |
| 3267 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3268 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3269 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3270 | |
| 3271 | return 1; |
| 3272 | } |
| 3273 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3274 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3275 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3276 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3280 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3281 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 3282 | if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3283 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | return 0; |
| 3287 | } |
| 3288 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3289 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3290 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3291 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3292 | fd_delete(t->srv_fd); |
| 3293 | if (t->srv) { |
| 3294 | t->srv->cur_sess--; |
| 3295 | t->srv->failed_resp++; |
| 3296 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3297 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3298 | //close(t->srv_fd); |
| 3299 | t->srv_state = SV_STCLOSE; |
| 3300 | if (!(t->flags & SN_ERR_MASK)) |
| 3301 | t->flags |= SN_ERR_SRVCL; |
| 3302 | if (!(t->flags & SN_FINST_MASK)) |
| 3303 | t->flags |= SN_FINST_D; |
| 3304 | /* We used to have a free connection slot. Since we'll never use it, |
| 3305 | * we have to inform the server that it may be used by another session. |
| 3306 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3307 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3308 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3309 | |
| 3310 | return 1; |
| 3311 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3312 | 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] | 3313 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3314 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3315 | fd_delete(t->srv_fd); |
| 3316 | if (t->srv) |
| 3317 | t->srv->cur_sess--; |
| 3318 | //close(t->srv_fd); |
| 3319 | t->srv_state = SV_STCLOSE; |
| 3320 | /* We used to have a free connection slot. Since we'll never use it, |
| 3321 | * we have to inform the server that it may be used by another session. |
| 3322 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3323 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3324 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3325 | |
| 3326 | return 1; |
| 3327 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3328 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3329 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3330 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3331 | fd_delete(t->srv_fd); |
| 3332 | if (t->srv) |
| 3333 | t->srv->cur_sess--; |
| 3334 | //close(t->srv_fd); |
| 3335 | t->srv_state = SV_STCLOSE; |
| 3336 | if (!(t->flags & SN_ERR_MASK)) |
| 3337 | t->flags |= SN_ERR_SRVTO; |
| 3338 | if (!(t->flags & SN_FINST_MASK)) |
| 3339 | t->flags |= SN_FINST_D; |
| 3340 | /* We used to have a free connection slot. Since we'll never use it, |
| 3341 | * we have to inform the server that it may be used by another session. |
| 3342 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3343 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3344 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3345 | |
| 3346 | return 1; |
| 3347 | } |
| 3348 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3349 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3350 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3351 | } |
| 3352 | } |
| 3353 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3354 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3355 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3356 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | return 0; |
| 3360 | } |
| 3361 | else { /* SV_STCLOSE : nothing to do */ |
| 3362 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3363 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3364 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3365 | 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] | 3366 | write(1, trash, len); |
| 3367 | } |
| 3368 | return 0; |
| 3369 | } |
| 3370 | return 0; |
| 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | /* |
| 3375 | * Produces data for the session <s> depending on its source. Expects to be |
| 3376 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3377 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3378 | * session, which it uses to keep on being called when there is free space in |
| 3379 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3380 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3381 | */ |
| 3382 | int produce_content(struct session *s) |
| 3383 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3384 | if (s->data_source == DATA_SRC_NONE) { |
| 3385 | s->flags &= ~SN_SELF_GEN; |
| 3386 | return 1; |
| 3387 | } |
| 3388 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3389 | /* dump server statistics */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame^] | 3390 | int ret = stats_dump_http(s, s->be->uri_auth, |
| 3391 | (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3392 | if (ret >= 0) |
| 3393 | return ret; |
| 3394 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3395 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3396 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3397 | /* unknown data source or internal error */ |
| 3398 | s->txn.status = 500; |
| 3399 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3400 | if (!(s->flags & SN_ERR_MASK)) |
| 3401 | s->flags |= SN_ERR_PRXCOND; |
| 3402 | if (!(s->flags & SN_FINST_MASK)) |
| 3403 | s->flags |= SN_FINST_R; |
| 3404 | s->flags &= ~SN_SELF_GEN; |
| 3405 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3409 | /* Iterate the same filter through all request headers. |
| 3410 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3411 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3412 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3413 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3414 | 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] | 3415 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3416 | char term; |
| 3417 | char *cur_ptr, *cur_end, *cur_next; |
| 3418 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3419 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3420 | struct hdr_idx_elem *cur_hdr; |
| 3421 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3423 | last_hdr = 0; |
| 3424 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3425 | 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] | 3426 | old_idx = 0; |
| 3427 | |
| 3428 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3429 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3430 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3431 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3432 | (exp->action == ACT_ALLOW || |
| 3433 | exp->action == ACT_DENY || |
| 3434 | exp->action == ACT_TARPIT)) |
| 3435 | return 0; |
| 3436 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3437 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3438 | if (!cur_idx) |
| 3439 | break; |
| 3440 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3441 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3442 | cur_ptr = cur_next; |
| 3443 | cur_end = cur_ptr + cur_hdr->len; |
| 3444 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3445 | |
| 3446 | /* Now we have one header between cur_ptr and cur_end, |
| 3447 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3448 | */ |
| 3449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3450 | /* The annoying part is that pattern matching needs |
| 3451 | * that we modify the contents to null-terminate all |
| 3452 | * strings before testing them. |
| 3453 | */ |
| 3454 | |
| 3455 | term = *cur_end; |
| 3456 | *cur_end = '\0'; |
| 3457 | |
| 3458 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3459 | switch (exp->action) { |
| 3460 | case ACT_SETBE: |
| 3461 | /* It is not possible to jump a second time. |
| 3462 | * FIXME: should we return an HTTP/500 here so that |
| 3463 | * the admin knows there's a problem ? |
| 3464 | */ |
| 3465 | if (t->be != t->fe) |
| 3466 | break; |
| 3467 | |
| 3468 | /* Swithing Proxy */ |
| 3469 | t->be = (struct proxy *) exp->replace; |
| 3470 | |
| 3471 | /* right now, the backend switch is not too much complicated |
| 3472 | * because we have associated req_cap and rsp_cap to the |
| 3473 | * frontend, and the beconn will be updated later. |
| 3474 | */ |
| 3475 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3476 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3477 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3478 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3479 | last_hdr = 1; |
| 3480 | break; |
| 3481 | |
| 3482 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3483 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3484 | last_hdr = 1; |
| 3485 | break; |
| 3486 | |
| 3487 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3488 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3489 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3490 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3491 | break; |
| 3492 | |
| 3493 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3494 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3495 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3496 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3497 | break; |
| 3498 | |
| 3499 | case ACT_REPLACE: |
| 3500 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3501 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3502 | /* FIXME: if the user adds a newline in the replacement, the |
| 3503 | * index will not be recalculated for now, and the new line |
| 3504 | * will not be counted as a new header. |
| 3505 | */ |
| 3506 | |
| 3507 | cur_end += delta; |
| 3508 | cur_next += delta; |
| 3509 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3510 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3511 | break; |
| 3512 | |
| 3513 | case ACT_REMOVE: |
| 3514 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3515 | cur_next += delta; |
| 3516 | |
| 3517 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3518 | txn->req.eoh += delta; |
| 3519 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3520 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3521 | cur_hdr->len = 0; |
| 3522 | cur_end = NULL; /* null-term has been rewritten */ |
| 3523 | break; |
| 3524 | |
| 3525 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3526 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3527 | if (cur_end) |
| 3528 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3529 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3530 | /* keep the link from this header to next one in case of later |
| 3531 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3532 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3533 | old_idx = cur_idx; |
| 3534 | } |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | |
| 3539 | /* Apply the filter to the request line. |
| 3540 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3541 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3542 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3543 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3544 | */ |
| 3545 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3546 | { |
| 3547 | char term; |
| 3548 | char *cur_ptr, *cur_end; |
| 3549 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3550 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3551 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3552 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3553 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3554 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3555 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3556 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3557 | (exp->action == ACT_ALLOW || |
| 3558 | exp->action == ACT_DENY || |
| 3559 | exp->action == ACT_TARPIT)) |
| 3560 | return 0; |
| 3561 | else if (exp->action == ACT_REMOVE) |
| 3562 | return 0; |
| 3563 | |
| 3564 | done = 0; |
| 3565 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3566 | 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] | 3567 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3568 | |
| 3569 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3570 | |
| 3571 | /* The annoying part is that pattern matching needs |
| 3572 | * that we modify the contents to null-terminate all |
| 3573 | * strings before testing them. |
| 3574 | */ |
| 3575 | |
| 3576 | term = *cur_end; |
| 3577 | *cur_end = '\0'; |
| 3578 | |
| 3579 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3580 | switch (exp->action) { |
| 3581 | case ACT_SETBE: |
| 3582 | /* It is not possible to jump a second time. |
| 3583 | * FIXME: should we return an HTTP/500 here so that |
| 3584 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3585 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3586 | if (t->be != t->fe) |
| 3587 | break; |
| 3588 | |
| 3589 | /* Swithing Proxy */ |
| 3590 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3592 | /* right now, the backend switch is not too much complicated |
| 3593 | * because we have associated req_cap and rsp_cap to the |
| 3594 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3595 | */ |
| 3596 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3597 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3598 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3599 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3600 | done = 1; |
| 3601 | break; |
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 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3604 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3605 | done = 1; |
| 3606 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3608 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3609 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3610 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3611 | done = 1; |
| 3612 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3614 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3615 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3616 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3617 | done = 1; |
| 3618 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3620 | case ACT_REPLACE: |
| 3621 | *cur_end = term; /* restore the string terminator */ |
| 3622 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3623 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3624 | /* FIXME: if the user adds a newline in the replacement, the |
| 3625 | * index will not be recalculated for now, and the new line |
| 3626 | * will not be counted as a new header. |
| 3627 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3628 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3629 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3630 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3631 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3632 | 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] | 3633 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3634 | HTTP_MSG_RQMETH, |
| 3635 | cur_ptr, cur_end + 1, |
| 3636 | NULL, NULL); |
| 3637 | if (unlikely(!cur_end)) |
| 3638 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3639 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3640 | /* we have a full request and we know that we have either a CR |
| 3641 | * or an LF at <ptr>. |
| 3642 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3643 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3644 | 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] | 3645 | /* there is no point trying this regex on headers */ |
| 3646 | return 1; |
| 3647 | } |
| 3648 | } |
| 3649 | *cur_end = term; /* restore the string terminator */ |
| 3650 | return done; |
| 3651 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3652 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3653 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3654 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3655 | /* |
| 3656 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3657 | * 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] | 3658 | * unparsable request. Since it can manage the switch to another backend, it |
| 3659 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3660 | */ |
| 3661 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3662 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3663 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3664 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3665 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3666 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3668 | /* |
| 3669 | * The interleaving of transformations and verdicts |
| 3670 | * makes it difficult to decide to continue or stop |
| 3671 | * the evaluation. |
| 3672 | */ |
| 3673 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3674 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3675 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3676 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3677 | exp = exp->next; |
| 3678 | continue; |
| 3679 | } |
| 3680 | |
| 3681 | /* Apply the filter to the request line. */ |
| 3682 | ret = apply_filter_to_req_line(t, req, exp); |
| 3683 | if (unlikely(ret < 0)) |
| 3684 | return -1; |
| 3685 | |
| 3686 | if (likely(ret == 0)) { |
| 3687 | /* The filter did not match the request, it can be |
| 3688 | * iterated through all headers. |
| 3689 | */ |
| 3690 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3691 | } |
| 3692 | exp = exp->next; |
| 3693 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3694 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3698 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3699 | /* |
| 3700 | * Manager client-side cookie |
| 3701 | */ |
| 3702 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3703 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3704 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3705 | char *p1, *p2, *p3, *p4; |
| 3706 | char *del_colon, *del_cookie, *colon; |
| 3707 | int app_cookies; |
| 3708 | |
| 3709 | appsess *asession_temp = NULL; |
| 3710 | appsess local_asession; |
| 3711 | |
| 3712 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3713 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3714 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3715 | if (t->be->cookie_name == NULL && |
| 3716 | t->be->appsession_name == NULL && |
| 3717 | t->be->capture_name == NULL) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3718 | return; |
| 3719 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3720 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3721 | * we start with the start line. |
| 3722 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3723 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3724 | 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] | 3725 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3726 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3727 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3728 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3729 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3730 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3731 | cur_ptr = cur_next; |
| 3732 | cur_end = cur_ptr + cur_hdr->len; |
| 3733 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3734 | |
| 3735 | /* We have one full header between cur_ptr and cur_end, and the |
| 3736 | * next header starts at cur_next. We're only interested in |
| 3737 | * "Cookie:" headers. |
| 3738 | */ |
| 3739 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3740 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3741 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3742 | old_idx = cur_idx; |
| 3743 | continue; |
| 3744 | } |
| 3745 | |
| 3746 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3747 | * attributes whose name begin with a '$', and associate them with |
| 3748 | * the right cookie, if we want to delete this cookie. |
| 3749 | * So there are 3 cases for each cookie read : |
| 3750 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3751 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3752 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3753 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3754 | * "special" cookie. |
| 3755 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3756 | * remove it. If no application cookie persists in the header, we |
| 3757 | * *MUST* delete it |
| 3758 | */ |
| 3759 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3760 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3761 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3762 | /* del_cookie == NULL => nothing to be deleted */ |
| 3763 | del_colon = del_cookie = NULL; |
| 3764 | app_cookies = 0; |
| 3765 | |
| 3766 | while (p1 < cur_end) { |
| 3767 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3768 | while (p1 < cur_end) { |
| 3769 | if (*p1 == ';' || *p1 == ',') |
| 3770 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3771 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3772 | break; |
| 3773 | p1++; |
| 3774 | } |
| 3775 | |
| 3776 | if (p1 == cur_end) |
| 3777 | break; |
| 3778 | |
| 3779 | /* p1 is at the beginning of the cookie name */ |
| 3780 | p2 = p1; |
| 3781 | while (p2 < cur_end && *p2 != '=') |
| 3782 | p2++; |
| 3783 | |
| 3784 | if (p2 == cur_end) |
| 3785 | break; |
| 3786 | |
| 3787 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3788 | if (p3 == cur_end) |
| 3789 | break; |
| 3790 | |
| 3791 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3792 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3793 | p4++; |
| 3794 | |
| 3795 | /* here, we have the cookie name between p1 and p2, |
| 3796 | * and its value between p3 and p4. |
| 3797 | * we can process it : |
| 3798 | * |
| 3799 | * Cookie: NAME=VALUE; |
| 3800 | * | || || | |
| 3801 | * | || || +--> p4 |
| 3802 | * | || |+-------> p3 |
| 3803 | * | || +--------> p2 |
| 3804 | * | |+------------> p1 |
| 3805 | * | +-------------> colon |
| 3806 | * +--------------------> cur_ptr |
| 3807 | */ |
| 3808 | |
| 3809 | if (*p1 == '$') { |
| 3810 | /* skip this one */ |
| 3811 | } |
| 3812 | else { |
| 3813 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3814 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3815 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3816 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3817 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3818 | int log_len = p4 - p1; |
| 3819 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3820 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3821 | Alert("HTTP logging : out of memory.\n"); |
| 3822 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3823 | if (log_len > t->fe->capture_len) |
| 3824 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3825 | memcpy(txn->cli_cookie, p1, log_len); |
| 3826 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3827 | } |
| 3828 | } |
| 3829 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3830 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3831 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3832 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3833 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3834 | char *delim; |
| 3835 | |
| 3836 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3837 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3838 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3839 | * |
| 3840 | * Cookie: NAME=SRV~VALUE; |
| 3841 | * | || || | | |
| 3842 | * | || || | +--> p4 |
| 3843 | * | || || +--------> delim |
| 3844 | * | || |+-----------> p3 |
| 3845 | * | || +------------> p2 |
| 3846 | * | |+----------------> p1 |
| 3847 | * | +-----------------> colon |
| 3848 | * +------------------------> cur_ptr |
| 3849 | */ |
| 3850 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3851 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3852 | for (delim = p3; delim < p4; delim++) |
| 3853 | if (*delim == COOKIE_DELIM) |
| 3854 | break; |
| 3855 | } |
| 3856 | else |
| 3857 | delim = p4; |
| 3858 | |
| 3859 | |
| 3860 | /* Here, we'll look for the first running server which supports the cookie. |
| 3861 | * This allows to share a same cookie between several servers, for example |
| 3862 | * to dedicate backup servers to specific servers only. |
| 3863 | * However, to prevent clients from sticking to cookie-less backup server |
| 3864 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3865 | * empty cookies and mark them as invalid. |
| 3866 | */ |
| 3867 | if (delim == p3) |
| 3868 | srv = NULL; |
| 3869 | |
| 3870 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3871 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3872 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3873 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3874 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3875 | txn->flags &= ~TX_CK_MASK; |
| 3876 | txn->flags |= TX_CK_VALID; |
| 3877 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3878 | t->srv = srv; |
| 3879 | break; |
| 3880 | } else { |
| 3881 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3882 | txn->flags &= ~TX_CK_MASK; |
| 3883 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | srv = srv->next; |
| 3887 | } |
| 3888 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3889 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3890 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3891 | txn->flags &= ~TX_CK_MASK; |
| 3892 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | /* depending on the cookie mode, we may have to either : |
| 3896 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3897 | * the server never sees it ; |
| 3898 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3899 | * application cookie so that it does not get accidentely removed later, |
| 3900 | * if we're in cookie prefix mode |
| 3901 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3902 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3903 | int delta; /* negative */ |
| 3904 | |
| 3905 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3906 | p4 += delta; |
| 3907 | cur_end += delta; |
| 3908 | cur_next += delta; |
| 3909 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3910 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3911 | |
| 3912 | del_cookie = del_colon = NULL; |
| 3913 | app_cookies++; /* protect the header from deletion */ |
| 3914 | } |
| 3915 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3916 | (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] | 3917 | del_cookie = p1; |
| 3918 | del_colon = colon; |
| 3919 | } |
| 3920 | } else { |
| 3921 | /* now we know that we must keep this cookie since it's |
| 3922 | * not ours. But if we wanted to delete our cookie |
| 3923 | * earlier, we cannot remove the complete header, but we |
| 3924 | * can remove the previous block itself. |
| 3925 | */ |
| 3926 | app_cookies++; |
| 3927 | |
| 3928 | if (del_cookie != NULL) { |
| 3929 | int delta; /* negative */ |
| 3930 | |
| 3931 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3932 | p4 += delta; |
| 3933 | cur_end += delta; |
| 3934 | cur_next += delta; |
| 3935 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3936 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3937 | del_cookie = del_colon = NULL; |
| 3938 | } |
| 3939 | } |
| 3940 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3941 | if ((t->be->appsession_name != NULL) && |
| 3942 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3943 | /* first, let's see if the cookie is our appcookie*/ |
| 3944 | |
| 3945 | /* Cool... it's the right one */ |
| 3946 | |
| 3947 | asession_temp = &local_asession; |
| 3948 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3949 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3950 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3951 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3952 | return; |
| 3953 | } |
| 3954 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3955 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3956 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3957 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3958 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3959 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3960 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3961 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3962 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3963 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3964 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3965 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3966 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3967 | return; |
| 3968 | } |
| 3969 | |
| 3970 | asession_temp->sessid = local_asession.sessid; |
| 3971 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3972 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3973 | } else { |
| 3974 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3975 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3976 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3977 | if (asession_temp->serverid == NULL) { |
| 3978 | Alert("Found Application Session without matching server.\n"); |
| 3979 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3980 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3981 | while (srv) { |
| 3982 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3983 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3984 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3985 | txn->flags &= ~TX_CK_MASK; |
| 3986 | txn->flags |= TX_CK_VALID; |
| 3987 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3988 | t->srv = srv; |
| 3989 | break; |
| 3990 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3991 | txn->flags &= ~TX_CK_MASK; |
| 3992 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | srv = srv->next; |
| 3996 | }/* end while(srv) */ |
| 3997 | }/* end else if server == NULL */ |
| 3998 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 3999 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4000 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4001 | } |
| 4002 | |
| 4003 | /* we'll have to look for another cookie ... */ |
| 4004 | p1 = p4; |
| 4005 | } /* while (p1 < cur_end) */ |
| 4006 | |
| 4007 | /* There's no more cookie on this line. |
| 4008 | * We may have marked the last one(s) for deletion. |
| 4009 | * We must do this now in two ways : |
| 4010 | * - if there is no app cookie, we simply delete the header ; |
| 4011 | * - if there are app cookies, we must delete the end of the |
| 4012 | * string properly, including the colon/semi-colon before |
| 4013 | * the cookie name. |
| 4014 | */ |
| 4015 | if (del_cookie != NULL) { |
| 4016 | int delta; |
| 4017 | if (app_cookies) { |
| 4018 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4019 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4020 | cur_hdr->len += delta; |
| 4021 | } else { |
| 4022 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4025 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4026 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4027 | cur_hdr->len = 0; |
| 4028 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4029 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4030 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | /* keep the link from this header to next one */ |
| 4034 | old_idx = cur_idx; |
| 4035 | } /* end of cookie processing on this header */ |
| 4036 | } |
| 4037 | |
| 4038 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4039 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4040 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4041 | */ |
| 4042 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4043 | { |
| 4044 | char term; |
| 4045 | char *cur_ptr, *cur_end, *cur_next; |
| 4046 | int cur_idx, old_idx, last_hdr; |
| 4047 | struct http_txn *txn = &t->txn; |
| 4048 | struct hdr_idx_elem *cur_hdr; |
| 4049 | int len, delta; |
| 4050 | |
| 4051 | last_hdr = 0; |
| 4052 | |
| 4053 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4054 | old_idx = 0; |
| 4055 | |
| 4056 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4057 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4058 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4059 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4060 | (exp->action == ACT_ALLOW || |
| 4061 | exp->action == ACT_DENY)) |
| 4062 | return 0; |
| 4063 | |
| 4064 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4065 | if (!cur_idx) |
| 4066 | break; |
| 4067 | |
| 4068 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4069 | cur_ptr = cur_next; |
| 4070 | cur_end = cur_ptr + cur_hdr->len; |
| 4071 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4072 | |
| 4073 | /* Now we have one header between cur_ptr and cur_end, |
| 4074 | * and the next header starts at cur_next. |
| 4075 | */ |
| 4076 | |
| 4077 | /* The annoying part is that pattern matching needs |
| 4078 | * that we modify the contents to null-terminate all |
| 4079 | * strings before testing them. |
| 4080 | */ |
| 4081 | |
| 4082 | term = *cur_end; |
| 4083 | *cur_end = '\0'; |
| 4084 | |
| 4085 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4086 | switch (exp->action) { |
| 4087 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4088 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4089 | last_hdr = 1; |
| 4090 | break; |
| 4091 | |
| 4092 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4093 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4094 | last_hdr = 1; |
| 4095 | break; |
| 4096 | |
| 4097 | case ACT_REPLACE: |
| 4098 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4099 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4100 | /* FIXME: if the user adds a newline in the replacement, the |
| 4101 | * index will not be recalculated for now, and the new line |
| 4102 | * will not be counted as a new header. |
| 4103 | */ |
| 4104 | |
| 4105 | cur_end += delta; |
| 4106 | cur_next += delta; |
| 4107 | cur_hdr->len += delta; |
| 4108 | txn->rsp.eoh += delta; |
| 4109 | break; |
| 4110 | |
| 4111 | case ACT_REMOVE: |
| 4112 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4113 | cur_next += delta; |
| 4114 | |
| 4115 | /* FIXME: this should be a separate function */ |
| 4116 | txn->rsp.eoh += delta; |
| 4117 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4118 | txn->hdr_idx.used--; |
| 4119 | cur_hdr->len = 0; |
| 4120 | cur_end = NULL; /* null-term has been rewritten */ |
| 4121 | break; |
| 4122 | |
| 4123 | } |
| 4124 | } |
| 4125 | if (cur_end) |
| 4126 | *cur_end = term; /* restore the string terminator */ |
| 4127 | |
| 4128 | /* keep the link from this header to next one in case of later |
| 4129 | * removal of next header. |
| 4130 | */ |
| 4131 | old_idx = cur_idx; |
| 4132 | } |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | |
| 4137 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4138 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4139 | * or -1 if a replacement resulted in an invalid status line. |
| 4140 | */ |
| 4141 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4142 | { |
| 4143 | char term; |
| 4144 | char *cur_ptr, *cur_end; |
| 4145 | int done; |
| 4146 | struct http_txn *txn = &t->txn; |
| 4147 | int len, delta; |
| 4148 | |
| 4149 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4150 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4151 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4152 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4153 | (exp->action == ACT_ALLOW || |
| 4154 | exp->action == ACT_DENY)) |
| 4155 | return 0; |
| 4156 | else if (exp->action == ACT_REMOVE) |
| 4157 | return 0; |
| 4158 | |
| 4159 | done = 0; |
| 4160 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4161 | 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] | 4162 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4163 | |
| 4164 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4165 | |
| 4166 | /* The annoying part is that pattern matching needs |
| 4167 | * that we modify the contents to null-terminate all |
| 4168 | * strings before testing them. |
| 4169 | */ |
| 4170 | |
| 4171 | term = *cur_end; |
| 4172 | *cur_end = '\0'; |
| 4173 | |
| 4174 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4175 | switch (exp->action) { |
| 4176 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4177 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4178 | done = 1; |
| 4179 | break; |
| 4180 | |
| 4181 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4182 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4183 | done = 1; |
| 4184 | break; |
| 4185 | |
| 4186 | case ACT_REPLACE: |
| 4187 | *cur_end = term; /* restore the string terminator */ |
| 4188 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4189 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4190 | /* FIXME: if the user adds a newline in the replacement, the |
| 4191 | * index will not be recalculated for now, and the new line |
| 4192 | * will not be counted as a new header. |
| 4193 | */ |
| 4194 | |
| 4195 | txn->rsp.eoh += delta; |
| 4196 | cur_end += delta; |
| 4197 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4198 | 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] | 4199 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4200 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4201 | cur_ptr, cur_end + 1, |
| 4202 | NULL, NULL); |
| 4203 | if (unlikely(!cur_end)) |
| 4204 | return -1; |
| 4205 | |
| 4206 | /* we have a full respnse and we know that we have either a CR |
| 4207 | * or an LF at <ptr>. |
| 4208 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4209 | 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] | 4210 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4211 | /* there is no point trying this regex on headers */ |
| 4212 | return 1; |
| 4213 | } |
| 4214 | } |
| 4215 | *cur_end = term; /* restore the string terminator */ |
| 4216 | return done; |
| 4217 | } |
| 4218 | |
| 4219 | |
| 4220 | |
| 4221 | /* |
| 4222 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4223 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4224 | * unparsable response. |
| 4225 | */ |
| 4226 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4227 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4228 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4229 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4230 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4231 | int ret; |
| 4232 | |
| 4233 | /* |
| 4234 | * The interleaving of transformations and verdicts |
| 4235 | * makes it difficult to decide to continue or stop |
| 4236 | * the evaluation. |
| 4237 | */ |
| 4238 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4239 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4240 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4241 | exp->action == ACT_PASS)) { |
| 4242 | exp = exp->next; |
| 4243 | continue; |
| 4244 | } |
| 4245 | |
| 4246 | /* Apply the filter to the status line. */ |
| 4247 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4248 | if (unlikely(ret < 0)) |
| 4249 | return -1; |
| 4250 | |
| 4251 | if (likely(ret == 0)) { |
| 4252 | /* The filter did not match the response, it can be |
| 4253 | * iterated through all headers. |
| 4254 | */ |
| 4255 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4256 | } |
| 4257 | exp = exp->next; |
| 4258 | } |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
| 4262 | |
| 4263 | |
| 4264 | /* |
| 4265 | * Manager server-side cookies |
| 4266 | */ |
| 4267 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4268 | { |
| 4269 | struct http_txn *txn = &t->txn; |
| 4270 | char *p1, *p2, *p3, *p4; |
| 4271 | |
| 4272 | appsess *asession_temp = NULL; |
| 4273 | appsess local_asession; |
| 4274 | |
| 4275 | char *cur_ptr, *cur_end, *cur_next; |
| 4276 | int cur_idx, old_idx, delta; |
| 4277 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4278 | if (t->be->cookie_name == NULL && |
| 4279 | t->be->appsession_name == NULL && |
| 4280 | t->be->capture_name == NULL && |
| 4281 | !(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4282 | return; |
| 4283 | |
| 4284 | /* Iterate through the headers. |
| 4285 | * we start with the start line. |
| 4286 | */ |
| 4287 | old_idx = 0; |
| 4288 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4289 | |
| 4290 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4291 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4292 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4293 | |
| 4294 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4295 | cur_ptr = cur_next; |
| 4296 | cur_end = cur_ptr + cur_hdr->len; |
| 4297 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4298 | |
| 4299 | /* We have one full header between cur_ptr and cur_end, and the |
| 4300 | * next header starts at cur_next. We're only interested in |
| 4301 | * "Cookie:" headers. |
| 4302 | */ |
| 4303 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4304 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4305 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4306 | old_idx = cur_idx; |
| 4307 | continue; |
| 4308 | } |
| 4309 | |
| 4310 | /* 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] | 4311 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4312 | |
| 4313 | |
| 4314 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4315 | if (t->be->cookie_name == NULL && |
| 4316 | t->be->appsession_name == NULL && |
| 4317 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4318 | return; |
| 4319 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4320 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4321 | |
| 4322 | 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] | 4323 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4324 | break; |
| 4325 | |
| 4326 | /* p1 is at the beginning of the cookie name */ |
| 4327 | p2 = p1; |
| 4328 | |
| 4329 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4330 | p2++; |
| 4331 | |
| 4332 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4333 | break; |
| 4334 | |
| 4335 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4336 | if (p3 == cur_end) |
| 4337 | break; |
| 4338 | |
| 4339 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4340 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4341 | p4++; |
| 4342 | |
| 4343 | /* here, we have the cookie name between p1 and p2, |
| 4344 | * and its value between p3 and p4. |
| 4345 | * we can process it. |
| 4346 | */ |
| 4347 | |
| 4348 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4349 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4350 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4351 | (p4 - p1 >= t->be->capture_namelen) && |
| 4352 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4353 | int log_len = p4 - p1; |
| 4354 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4355 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4356 | Alert("HTTP logging : out of memory.\n"); |
| 4357 | } |
| 4358 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4359 | if (log_len > t->be->capture_len) |
| 4360 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4361 | memcpy(txn->srv_cookie, p1, log_len); |
| 4362 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4366 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4367 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4368 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4369 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4370 | |
| 4371 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4372 | * this occurrence because we'll insert another one later. |
| 4373 | * We'll delete it too if the "indirect" option is set and we're in |
| 4374 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4375 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4376 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4377 | /* this header must be deleted */ |
| 4378 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4379 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4380 | txn->hdr_idx.used--; |
| 4381 | cur_hdr->len = 0; |
| 4382 | cur_next += delta; |
| 4383 | txn->rsp.eoh += delta; |
| 4384 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4385 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4386 | } |
| 4387 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4388 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4389 | /* replace bytes p3->p4 with the cookie name associated |
| 4390 | * with this server since we know it. |
| 4391 | */ |
| 4392 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4393 | cur_hdr->len += delta; |
| 4394 | cur_next += delta; |
| 4395 | txn->rsp.eoh += delta; |
| 4396 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4397 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4398 | } |
| 4399 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4400 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4401 | /* insert the cookie name associated with this server |
| 4402 | * before existing cookie, and insert a delimitor between them.. |
| 4403 | */ |
| 4404 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4405 | cur_hdr->len += delta; |
| 4406 | cur_next += delta; |
| 4407 | txn->rsp.eoh += delta; |
| 4408 | |
| 4409 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4410 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4414 | else if ((t->be->appsession_name != NULL) && |
| 4415 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4416 | |
| 4417 | /* Cool... it's the right one */ |
| 4418 | |
| 4419 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4420 | asession_temp = &local_asession; |
| 4421 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4422 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4423 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4424 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4425 | return; |
| 4426 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4427 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4428 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4429 | asession_temp->serverid = NULL; |
| 4430 | |
| 4431 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4432 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4433 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4434 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4435 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4436 | return; |
| 4437 | } |
| 4438 | asession_temp->sessid = local_asession.sessid; |
| 4439 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4440 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4441 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4442 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4443 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4444 | } |
| 4445 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4446 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4447 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4448 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4449 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4450 | return; |
| 4451 | } |
| 4452 | asession_temp->serverid[0] = '\0'; |
| 4453 | } |
| 4454 | |
| 4455 | if (asession_temp->serverid[0] == '\0') |
| 4456 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4457 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4458 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4459 | |
| 4460 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4461 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4462 | #endif |
| 4463 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4464 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4465 | } /* we're now at the end of the cookie value */ |
| 4466 | |
| 4467 | /* keep the link from this header to next one */ |
| 4468 | old_idx = cur_idx; |
| 4469 | } /* end of cookie processing on this header */ |
| 4470 | } |
| 4471 | |
| 4472 | |
| 4473 | |
| 4474 | /* |
| 4475 | * Check if response is cacheable or not. Updates t->flags. |
| 4476 | */ |
| 4477 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4478 | { |
| 4479 | struct http_txn *txn = &t->txn; |
| 4480 | char *p1, *p2; |
| 4481 | |
| 4482 | char *cur_ptr, *cur_end, *cur_next; |
| 4483 | int cur_idx; |
| 4484 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4485 | if (!txn->flags & TX_CACHEABLE) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4486 | return; |
| 4487 | |
| 4488 | /* Iterate through the headers. |
| 4489 | * we start with the start line. |
| 4490 | */ |
| 4491 | cur_idx = 0; |
| 4492 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4493 | |
| 4494 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4495 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4496 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4497 | |
| 4498 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4499 | cur_ptr = cur_next; |
| 4500 | cur_end = cur_ptr + cur_hdr->len; |
| 4501 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4502 | |
| 4503 | /* We have one full header between cur_ptr and cur_end, and the |
| 4504 | * next header starts at cur_next. We're only interested in |
| 4505 | * "Cookie:" headers. |
| 4506 | */ |
| 4507 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4508 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4509 | if (val) { |
| 4510 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4511 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4512 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4513 | return; |
| 4514 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4515 | } |
| 4516 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4517 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4518 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4519 | continue; |
| 4520 | |
| 4521 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4522 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4523 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4524 | |
| 4525 | if (p1 >= cur_end) /* no more info */ |
| 4526 | continue; |
| 4527 | |
| 4528 | /* p1 is at the beginning of the value */ |
| 4529 | p2 = p1; |
| 4530 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4531 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4532 | p2++; |
| 4533 | |
| 4534 | /* we have a complete value between p1 and p2 */ |
| 4535 | if (p2 < cur_end && *p2 == '=') { |
| 4536 | /* we have something of the form no-cache="set-cookie" */ |
| 4537 | if ((cur_end - p1 >= 21) && |
| 4538 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4539 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4540 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4541 | continue; |
| 4542 | } |
| 4543 | |
| 4544 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4545 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4546 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4547 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4548 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4549 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4550 | return; |
| 4551 | } |
| 4552 | |
| 4553 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4554 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4555 | continue; |
| 4556 | } |
| 4557 | } |
| 4558 | } |
| 4559 | |
| 4560 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4561 | /* |
| 4562 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4563 | * If the server is found, it's assigned to the session. |
| 4564 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4565 | 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] | 4566 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4567 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4568 | appsess *asession_temp = NULL; |
| 4569 | appsess local_asession; |
| 4570 | char *request_line; |
| 4571 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4572 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4573 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4574 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4575 | ((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] | 4576 | return; |
| 4577 | |
| 4578 | /* skip ';' */ |
| 4579 | request_line++; |
| 4580 | |
| 4581 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4582 | 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] | 4583 | return; |
| 4584 | |
| 4585 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4586 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4587 | |
| 4588 | /* First try if we already have an appsession */ |
| 4589 | asession_temp = &local_asession; |
| 4590 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4591 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4592 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4593 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4594 | return; |
| 4595 | } |
| 4596 | |
| 4597 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4598 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4599 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4600 | asession_temp->serverid = NULL; |
| 4601 | |
| 4602 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4603 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4604 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4605 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4606 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4607 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4608 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4609 | return; |
| 4610 | } |
| 4611 | asession_temp->sessid = local_asession.sessid; |
| 4612 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4613 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4614 | } |
| 4615 | else { |
| 4616 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4617 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4618 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4619 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4620 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4621 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4622 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4623 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4624 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4625 | #endif |
| 4626 | if (asession_temp->serverid == NULL) { |
| 4627 | Alert("Found Application Session without matching server.\n"); |
| 4628 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4629 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4630 | while (srv) { |
| 4631 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4632 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4633 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4634 | txn->flags &= ~TX_CK_MASK; |
| 4635 | txn->flags |= TX_CK_VALID; |
| 4636 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4637 | t->srv = srv; |
| 4638 | break; |
| 4639 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4640 | txn->flags &= ~TX_CK_MASK; |
| 4641 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4642 | } |
| 4643 | } |
| 4644 | srv = srv->next; |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4650 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4651 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4652 | * 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] | 4653 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4654 | * 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] | 4655 | * 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] | 4656 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4657 | * |
| 4658 | * Returns 1 if the session's state changes, otherwise 0. |
| 4659 | */ |
| 4660 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4661 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4662 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4663 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4664 | struct user_auth *user; |
| 4665 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4666 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4668 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4669 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4670 | return 0; |
| 4671 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4672 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4673 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4674 | /* the URI is in h */ |
| 4675 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4676 | return 0; |
| 4677 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4678 | h += uri_auth->uri_len; |
| 4679 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4680 | if (memcmp(h, ";up", 3) == 0) { |
| 4681 | t->flags |= SN_STAT_HIDEDWN; |
| 4682 | break; |
| 4683 | } |
| 4684 | h++; |
| 4685 | } |
| 4686 | |
| 4687 | if (uri_auth->refresh) { |
| 4688 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4689 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4690 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4691 | t->flags |= SN_STAT_NORFRSH; |
| 4692 | break; |
| 4693 | } |
| 4694 | h++; |
| 4695 | } |
| 4696 | } |
| 4697 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame^] | 4698 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4699 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4700 | if (memcmp(h, ";csv", 4) == 0) { |
| 4701 | t->flags |= SN_STAT_FMTCSV; |
| 4702 | break; |
| 4703 | } |
| 4704 | h++; |
| 4705 | } |
| 4706 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4707 | /* we are in front of a interceptable URI. Let's check |
| 4708 | * if there's an authentication and if it's valid. |
| 4709 | */ |
| 4710 | user = uri_auth->users; |
| 4711 | if (!user) { |
| 4712 | /* no user auth required, it's OK */ |
| 4713 | authenticated = 1; |
| 4714 | } else { |
| 4715 | authenticated = 0; |
| 4716 | |
| 4717 | /* a user list is defined, we have to check. |
| 4718 | * skip 21 chars for "Authorization: Basic ". |
| 4719 | */ |
| 4720 | |
| 4721 | /* FIXME: this should move to an earlier place */ |
| 4722 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4723 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4724 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4725 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4726 | if (len > 14 && |
| 4727 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4728 | txn->auth_hdr.str = h; |
| 4729 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4730 | break; |
| 4731 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4732 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4733 | } |
| 4734 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4735 | if (txn->auth_hdr.len < 21 || |
| 4736 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4737 | user = NULL; |
| 4738 | |
| 4739 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4740 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4741 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4742 | user->user_pwd, user->user_len)) { |
| 4743 | authenticated = 1; |
| 4744 | break; |
| 4745 | } |
| 4746 | user = user->next; |
| 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4751 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4754 | msg.str = trash; |
| 4755 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4756 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4757 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4758 | if (!(t->flags & SN_ERR_MASK)) |
| 4759 | t->flags |= SN_ERR_PRXCOND; |
| 4760 | if (!(t->flags & SN_FINST_MASK)) |
| 4761 | t->flags |= SN_FINST_R; |
| 4762 | return 1; |
| 4763 | } |
| 4764 | |
| 4765 | /* The request is valid, the user is authenticate. Let's start sending |
| 4766 | * data. |
| 4767 | */ |
| 4768 | t->cli_state = CL_STSHUTR; |
| 4769 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4770 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4771 | t->data_source = DATA_SRC_STATS; |
| 4772 | t->data_state = DATA_ST_INIT; |
| 4773 | produce_content(t); |
| 4774 | return 1; |
| 4775 | } |
| 4776 | |
| 4777 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4778 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4779 | * Print a debug line with a header |
| 4780 | */ |
| 4781 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4782 | { |
| 4783 | int len, max; |
| 4784 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4785 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4786 | max = end - start; |
| 4787 | UBOUND(max, sizeof(trash) - len - 1); |
| 4788 | len += strlcpy2(trash + len, start, max + 1); |
| 4789 | trash[len++] = '\n'; |
| 4790 | write(1, trash, len); |
| 4791 | } |
| 4792 | |
| 4793 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4794 | /************************************************************************/ |
| 4795 | /* The code below is dedicated to ACL parsing and matching */ |
| 4796 | /************************************************************************/ |
| 4797 | |
| 4798 | |
| 4799 | |
| 4800 | |
| 4801 | /* 1. Check on METHOD |
| 4802 | * We use the pre-parsed method if it is known, and store its number as an |
| 4803 | * integer. If it is unknown, we use the pointer and the length. |
| 4804 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4805 | 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] | 4806 | { |
| 4807 | int len, meth; |
| 4808 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4809 | len = strlen(*text); |
| 4810 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4811 | |
| 4812 | pattern->val.i = meth; |
| 4813 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4814 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4815 | if (!pattern->ptr.str) |
| 4816 | return 0; |
| 4817 | pattern->len = len; |
| 4818 | } |
| 4819 | return 1; |
| 4820 | } |
| 4821 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4822 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4823 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4824 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4825 | { |
| 4826 | int meth; |
| 4827 | struct http_txn *txn = l7; |
| 4828 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4829 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4830 | return 0; |
| 4831 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4832 | meth = txn->meth; |
| 4833 | test->i = meth; |
| 4834 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4835 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4836 | /* ensure the indexes are not affected */ |
| 4837 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4838 | test->len = txn->req.sl.rq.m_l; |
| 4839 | test->ptr = txn->req.sol; |
| 4840 | } |
| 4841 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4842 | return 1; |
| 4843 | } |
| 4844 | |
| 4845 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4846 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4847 | int icase; |
| 4848 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4849 | if (test->i != pattern->val.i) |
| 4850 | return 0; |
| 4851 | |
| 4852 | if (test->i != HTTP_METH_OTHER) |
| 4853 | return 1; |
| 4854 | |
| 4855 | /* Other method, we must compare the strings */ |
| 4856 | if (pattern->len != test->len) |
| 4857 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4858 | |
| 4859 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4860 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4861 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4862 | return 0; |
| 4863 | return 1; |
| 4864 | } |
| 4865 | |
| 4866 | /* 2. Check on Request/Status Version |
| 4867 | * We simply compare strings here. |
| 4868 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4869 | 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] | 4870 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4871 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4872 | if (!pattern->ptr.str) |
| 4873 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4874 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4875 | return 1; |
| 4876 | } |
| 4877 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4878 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4879 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4880 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4881 | { |
| 4882 | struct http_txn *txn = l7; |
| 4883 | char *ptr; |
| 4884 | int len; |
| 4885 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4886 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4887 | return 0; |
| 4888 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4889 | len = txn->req.sl.rq.v_l; |
| 4890 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4891 | |
| 4892 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4893 | if (len <= 0) |
| 4894 | return 0; |
| 4895 | |
| 4896 | test->ptr = ptr; |
| 4897 | test->len = len; |
| 4898 | |
| 4899 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4900 | return 1; |
| 4901 | } |
| 4902 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4903 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4904 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4905 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4906 | { |
| 4907 | struct http_txn *txn = l7; |
| 4908 | char *ptr; |
| 4909 | int len; |
| 4910 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4911 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4912 | return 0; |
| 4913 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4914 | len = txn->rsp.sl.st.v_l; |
| 4915 | ptr = txn->rsp.sol; |
| 4916 | |
| 4917 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4918 | if (len <= 0) |
| 4919 | return 0; |
| 4920 | |
| 4921 | test->ptr = ptr; |
| 4922 | test->len = len; |
| 4923 | |
| 4924 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4925 | return 1; |
| 4926 | } |
| 4927 | |
| 4928 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4929 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4930 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4931 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4932 | { |
| 4933 | struct http_txn *txn = l7; |
| 4934 | char *ptr; |
| 4935 | int len; |
| 4936 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4937 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4938 | return 0; |
| 4939 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4940 | len = txn->rsp.sl.st.c_l; |
| 4941 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4942 | |
| 4943 | test->i = __strl2ui(ptr, len); |
| 4944 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4945 | return 1; |
| 4946 | } |
| 4947 | |
| 4948 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4949 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4950 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4951 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4952 | { |
| 4953 | struct http_txn *txn = l7; |
| 4954 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4955 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4956 | return 0; |
| 4957 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4958 | /* ensure the indexes are not affected */ |
| 4959 | return 0; |
| 4960 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4961 | test->len = txn->req.sl.rq.u_l; |
| 4962 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4963 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4964 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4965 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4966 | return 1; |
| 4967 | } |
| 4968 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4969 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 4970 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 4971 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4972 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4973 | 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] | 4974 | struct acl_expr *expr, struct acl_test *test) |
| 4975 | { |
| 4976 | struct http_txn *txn = l7; |
| 4977 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4978 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4979 | |
| 4980 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4981 | /* search for header from the beginning */ |
| 4982 | ctx->idx = 0; |
| 4983 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4984 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4985 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4986 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4987 | test->len = ctx->vlen; |
| 4988 | test->ptr = (char *)ctx->line + ctx->val; |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
| 4992 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4993 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4997 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4998 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4999 | struct acl_expr *expr, struct acl_test *test) |
| 5000 | { |
| 5001 | struct http_txn *txn = l7; |
| 5002 | |
| 5003 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5004 | return 0; |
| 5005 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5006 | /* ensure the indexes are not affected */ |
| 5007 | return 0; |
| 5008 | |
| 5009 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5010 | } |
| 5011 | |
| 5012 | static int |
| 5013 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5014 | struct acl_expr *expr, struct acl_test *test) |
| 5015 | { |
| 5016 | struct http_txn *txn = l7; |
| 5017 | |
| 5018 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5019 | return 0; |
| 5020 | |
| 5021 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5022 | } |
| 5023 | |
| 5024 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5025 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5026 | */ |
| 5027 | static int |
| 5028 | 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] | 5029 | struct acl_expr *expr, struct acl_test *test) |
| 5030 | { |
| 5031 | struct http_txn *txn = l7; |
| 5032 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5033 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5034 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5035 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5036 | ctx.idx = 0; |
| 5037 | cnt = 0; |
| 5038 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5039 | cnt++; |
| 5040 | |
| 5041 | test->i = cnt; |
| 5042 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5043 | return 1; |
| 5044 | } |
| 5045 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5046 | static int |
| 5047 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5048 | struct acl_expr *expr, struct acl_test *test) |
| 5049 | { |
| 5050 | struct http_txn *txn = l7; |
| 5051 | |
| 5052 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5053 | return 0; |
| 5054 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5055 | /* ensure the indexes are not affected */ |
| 5056 | return 0; |
| 5057 | |
| 5058 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5059 | } |
| 5060 | |
| 5061 | static int |
| 5062 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5063 | struct acl_expr *expr, struct acl_test *test) |
| 5064 | { |
| 5065 | struct http_txn *txn = l7; |
| 5066 | |
| 5067 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5068 | return 0; |
| 5069 | |
| 5070 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5071 | } |
| 5072 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5073 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5074 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5075 | * 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] | 5076 | */ |
| 5077 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5078 | 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] | 5079 | struct acl_expr *expr, struct acl_test *test) |
| 5080 | { |
| 5081 | struct http_txn *txn = l7; |
| 5082 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5083 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5084 | |
| 5085 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5086 | /* search for header from the beginning */ |
| 5087 | ctx->idx = 0; |
| 5088 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5089 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5090 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5091 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5092 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5093 | return 1; |
| 5094 | } |
| 5095 | |
| 5096 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5097 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5098 | return 0; |
| 5099 | } |
| 5100 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5101 | static int |
| 5102 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5103 | struct acl_expr *expr, struct acl_test *test) |
| 5104 | { |
| 5105 | struct http_txn *txn = l7; |
| 5106 | |
| 5107 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5108 | return 0; |
| 5109 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5110 | /* ensure the indexes are not affected */ |
| 5111 | return 0; |
| 5112 | |
| 5113 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5114 | } |
| 5115 | |
| 5116 | static int |
| 5117 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5118 | struct acl_expr *expr, struct acl_test *test) |
| 5119 | { |
| 5120 | struct http_txn *txn = l7; |
| 5121 | |
| 5122 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5123 | return 0; |
| 5124 | |
| 5125 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5126 | } |
| 5127 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5128 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5129 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5130 | */ |
| 5131 | static int |
| 5132 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5133 | struct acl_expr *expr, struct acl_test *test) |
| 5134 | { |
| 5135 | struct http_txn *txn = l7; |
| 5136 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5137 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5138 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5139 | return 0; |
| 5140 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5141 | /* ensure the indexes are not affected */ |
| 5142 | return 0; |
| 5143 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5144 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5145 | end = ptr + txn->req.sl.rq.u_l; |
| 5146 | |
| 5147 | if (ptr >= end) |
| 5148 | return 0; |
| 5149 | |
| 5150 | /* RFC2616, par. 5.1.2 : |
| 5151 | * Request-URI = "*" | absuri | abspath | authority |
| 5152 | */ |
| 5153 | |
| 5154 | if (*ptr == '*') |
| 5155 | return 0; |
| 5156 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5157 | if (isalpha((unsigned char)*ptr)) { |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5158 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5159 | ptr++; |
| 5160 | while (ptr < end && |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5161 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5162 | ptr++; |
| 5163 | /* skip '://' */ |
| 5164 | if (ptr == end || *ptr++ != ':') |
| 5165 | return 0; |
| 5166 | if (ptr == end || *ptr++ != '/') |
| 5167 | return 0; |
| 5168 | if (ptr == end || *ptr++ != '/') |
| 5169 | return 0; |
| 5170 | } |
| 5171 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5172 | |
| 5173 | while (ptr < end && *ptr != '/') |
| 5174 | ptr++; |
| 5175 | |
| 5176 | if (ptr == end) |
| 5177 | return 0; |
| 5178 | |
| 5179 | /* OK, we got the '/' ! */ |
| 5180 | test->ptr = ptr; |
| 5181 | |
| 5182 | while (ptr < end && *ptr != '?') |
| 5183 | ptr++; |
| 5184 | |
| 5185 | test->len = ptr - test->ptr; |
| 5186 | |
| 5187 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5188 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5189 | return 1; |
| 5190 | } |
| 5191 | |
| 5192 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5193 | |
| 5194 | /************************************************************************/ |
| 5195 | /* All supported keywords must be declared here. */ |
| 5196 | /************************************************************************/ |
| 5197 | |
| 5198 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5199 | static struct acl_kw_list acl_kws = {{ },{ |
| 5200 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5201 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5202 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5203 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5204 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5205 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5206 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5207 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5208 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5209 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5210 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5211 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5212 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5213 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5214 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5215 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5216 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5217 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5218 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5219 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5220 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5221 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5222 | |
| 5223 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5224 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5225 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5226 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5227 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5228 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5229 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5230 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5231 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5232 | |
| 5233 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5234 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5235 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5236 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5237 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5238 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5239 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5240 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5241 | { NULL, NULL, NULL, NULL }, |
| 5242 | |
| 5243 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5244 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5245 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5246 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5247 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5248 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5249 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5250 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5251 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5252 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5253 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5254 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5255 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5256 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5257 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5258 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5259 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5260 | |
| 5261 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5262 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5263 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5264 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5265 | { NULL, NULL, NULL, NULL }, |
| 5266 | #endif |
| 5267 | }}; |
| 5268 | |
| 5269 | |
| 5270 | __attribute__((constructor)) |
| 5271 | static void __http_protocol_init(void) |
| 5272 | { |
| 5273 | acl_register_keywords(&acl_kws); |
| 5274 | } |
| 5275 | |
| 5276 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5277 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5278 | * Local variables: |
| 5279 | * c-indent-level: 8 |
| 5280 | * c-basic-offset: 8 |
| 5281 | * End: |
| 5282 | */ |