Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
| 33 | #include <common/time.h> |
| 34 | #include <common/uri_auth.h> |
| 35 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 37 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <types/capture.h> |
| 39 | #include <types/client.h> |
| 40 | #include <types/global.h> |
| 41 | #include <types/httperr.h> |
| 42 | #include <types/polling.h> |
| 43 | #include <types/proxy.h> |
| 44 | #include <types/server.h> |
| 45 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 46 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
| 51 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 52 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
| 54 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 55 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/session.h> |
| 57 | #include <proto/task.h> |
| 58 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_HAP_TCPSPLICE |
| 60 | #include <libtcpsplice.h> |
| 61 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 63 | #define DEBUG_PARSE_NO_SPEEDUP |
| 64 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 65 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 66 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 67 | * instruction. The first argument is the label for normal operation, and the |
| 68 | * second one is the break/continue instruction in the no_speedup mode. |
| 69 | */ |
| 70 | |
| 71 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 72 | #define QUICK_JUMP(x,y) y |
| 73 | #else |
| 74 | #define QUICK_JUMP(x,y) goto x |
| 75 | #endif |
| 76 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 79 | "HTTP/1.0 200 OK\r\n" |
| 80 | "Cache-Control: no-cache\r\n" |
| 81 | "Connection: close\r\n" |
| 82 | "Content-Type: text/html\r\n" |
| 83 | "\r\n" |
| 84 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 85 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | const struct chunk http_200_chunk = { |
| 87 | .str = (char *)&HTTP_200, |
| 88 | .len = sizeof(HTTP_200)-1 |
| 89 | }; |
| 90 | |
| 91 | const char *HTTP_302 = |
| 92 | "HTTP/1.0 302 Found\r\n" |
| 93 | "Cache-Control: no-cache\r\n" |
| 94 | "Connection: close\r\n" |
| 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
| 99 | "HTTP/1.0 303 See Other\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Connection: close\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 114 | |
| 115 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 133 | |
| 134 | [HTTP_ERR_403] = |
| 135 | "HTTP/1.0 403 Forbidden\r\n" |
| 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_408] = |
| 143 | "HTTP/1.0 408 Request Time-out\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_500] = |
| 151 | "HTTP/1.0 500 Server Error\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_502] = |
| 159 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_503] = |
| 167 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_504] = |
| 175 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 181 | |
| 182 | }; |
| 183 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 184 | /* We must put the messages here since GCC cannot initialize consts depending |
| 185 | * on strlen(). |
| 186 | */ |
| 187 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 188 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 189 | #define FD_SETS_ARE_BITFIELDS |
| 190 | #ifdef FD_SETS_ARE_BITFIELDS |
| 191 | /* |
| 192 | * This map is used with all the FD_* macros to check whether a particular bit |
| 193 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 194 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 195 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 196 | * exclusively to the macros. |
| 197 | */ |
| 198 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 199 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 200 | |
| 201 | #else |
| 202 | #error "Check if your OS uses bitfields for fd_sets" |
| 203 | #endif |
| 204 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 205 | void init_proto_http() |
| 206 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 207 | int i; |
| 208 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 212 | if (!http_err_msgs[msg]) { |
| 213 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 214 | abort(); |
| 215 | } |
| 216 | |
| 217 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 218 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 219 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
| 221 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 222 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 223 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 224 | * printable characters above. |
| 225 | */ |
| 226 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 227 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 228 | for (i = 0; i < 32; i++) { |
| 229 | FD_SET(i, hdr_encode_map); |
| 230 | FD_SET(i, url_encode_map); |
| 231 | } |
| 232 | for (i = 127; i < 256; i++) { |
| 233 | FD_SET(i, hdr_encode_map); |
| 234 | FD_SET(i, url_encode_map); |
| 235 | } |
| 236 | |
| 237 | tmp = "\"#{|}"; |
| 238 | while (*tmp) { |
| 239 | FD_SET(*tmp, hdr_encode_map); |
| 240 | tmp++; |
| 241 | } |
| 242 | |
| 243 | tmp = "\"#"; |
| 244 | while (*tmp) { |
| 245 | FD_SET(*tmp, url_encode_map); |
| 246 | tmp++; |
| 247 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 248 | |
| 249 | /* memory allocations */ |
| 250 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 251 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 252 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 254 | /* |
| 255 | * We have 26 list of methods (1 per first letter), each of which can have |
| 256 | * up to 3 entries (2 valid, 1 null). |
| 257 | */ |
| 258 | struct http_method_desc { |
| 259 | http_meth_t meth; |
| 260 | int len; |
| 261 | const char text[8]; |
| 262 | }; |
| 263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 264 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 265 | ['C' - 'A'] = { |
| 266 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 267 | }, |
| 268 | ['D' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 270 | }, |
| 271 | ['G' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 273 | }, |
| 274 | ['H' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 276 | }, |
| 277 | ['P' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 279 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 280 | }, |
| 281 | ['T' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 283 | }, |
| 284 | /* rest is empty like this : |
| 285 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 286 | */ |
| 287 | }; |
| 288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 289 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 290 | * table than two perform a boolean AND or OR between two tests. Refer to |
| 291 | * RFC2616 for those chars. |
| 292 | */ |
| 293 | |
| 294 | const char http_is_spht[256] = { |
| 295 | [' '] = 1, ['\t'] = 1, |
| 296 | }; |
| 297 | |
| 298 | const char http_is_crlf[256] = { |
| 299 | ['\r'] = 1, ['\n'] = 1, |
| 300 | }; |
| 301 | |
| 302 | const char http_is_lws[256] = { |
| 303 | [' '] = 1, ['\t'] = 1, |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_sep[256] = { |
| 308 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 309 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 310 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 311 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 312 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 313 | }; |
| 314 | |
| 315 | const char http_is_ctl[256] = { |
| 316 | [0 ... 31] = 1, |
| 317 | [127] = 1, |
| 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 322 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 323 | * them correctly. Instead, define every non-CTL char's status. |
| 324 | */ |
| 325 | const char http_is_token[256] = { |
| 326 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 327 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 328 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 329 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 330 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 331 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 332 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 333 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 334 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 335 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 336 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 337 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 338 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 339 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 340 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 341 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 342 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 343 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 344 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 345 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 346 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 347 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 348 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 349 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 350 | }; |
| 351 | |
| 352 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 353 | /* |
| 354 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 355 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 356 | */ |
| 357 | const char http_is_ver_token[256] = { |
| 358 | ['.'] = 1, ['/'] = 1, |
| 359 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 360 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 361 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 362 | }; |
| 363 | |
| 364 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | #ifdef DEBUG_FULL |
| 366 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 367 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 368 | #endif |
| 369 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 370 | static void http_sess_log(struct session *s); |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 538 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 539 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 540 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 541 | */ |
| 542 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 543 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 544 | { |
| 545 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 546 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 547 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 548 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 549 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 550 | } |
| 551 | if (!(t->flags & SN_ERR_MASK)) |
| 552 | t->flags |= err; |
| 553 | if (!(t->flags & SN_FINST_MASK)) |
| 554 | t->flags |= finst; |
| 555 | } |
| 556 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 557 | /* This function returns the appropriate error location for the given session |
| 558 | * and message. |
| 559 | */ |
| 560 | |
| 561 | struct chunk *error_message(struct session *s, int msgnum) |
| 562 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 563 | if (s->be->errmsg[msgnum].str) |
| 564 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 565 | else if (s->fe->errmsg[msgnum].str) |
| 566 | return &s->fe->errmsg[msgnum]; |
| 567 | else |
| 568 | return &http_err_chunks[msgnum]; |
| 569 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 571 | /* |
| 572 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 573 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 574 | */ |
| 575 | static http_meth_t find_http_meth(const char *str, const int len) |
| 576 | { |
| 577 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 578 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 579 | |
| 580 | m = ((unsigned)*str - 'A'); |
| 581 | |
| 582 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 583 | for (h = http_methods[m]; h->len > 0; h++) { |
| 584 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 585 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 587 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | }; |
| 589 | return HTTP_METH_OTHER; |
| 590 | } |
| 591 | return HTTP_METH_NONE; |
| 592 | |
| 593 | } |
| 594 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | /* Processes the client and server jobs of a session task, then |
| 596 | * puts it back to the wait queue in a clean state, or |
| 597 | * cleans up its resources if it must be deleted. Returns |
| 598 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 599 | * infinity. |
| 600 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 601 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 602 | { |
| 603 | struct session *s = t->context; |
| 604 | int fsm_resync = 0; |
| 605 | |
| 606 | do { |
| 607 | fsm_resync = 0; |
| 608 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 609 | fsm_resync |= process_cli(s); |
| 610 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 611 | fsm_resync |= process_srv(s); |
| 612 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 613 | } while (fsm_resync); |
| 614 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 615 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 616 | |
| 617 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 618 | session_process_counters(s); |
| 619 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 620 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 621 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 622 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 623 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 624 | tv_bound(&t->expire, &s->req->cex); |
| 625 | tv_bound(&t->expire, &s->rep->rex); |
| 626 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 627 | if (s->cli_state == CL_STHEADERS) |
| 628 | tv_bound(&t->expire, &s->txn.exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 629 | |
| 630 | /* restore t to its place in the task list */ |
| 631 | task_queue(t); |
| 632 | |
| 633 | #ifdef DEBUG_FULL |
| 634 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 635 | * that a task still has something to do and will provoke a quick loop. |
| 636 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 637 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | exit(100); |
| 639 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 640 | *next = t->expire; |
| 641 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 644 | s->fe->feconn--; |
| 645 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 646 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | actconn--; |
| 648 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 649 | if (unlikely((global.mode & MODE_DEBUG) && |
| 650 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 652 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 653 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 654 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 655 | write(1, trash, len); |
| 656 | } |
| 657 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 658 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 659 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 660 | |
| 661 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 662 | if (s->logs.logwait && |
| 663 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 664 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 665 | if (s->fe->to_log & LW_REQ) |
| 666 | http_sess_log(s); |
| 667 | else |
| 668 | tcp_sess_log(s); |
| 669 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 670 | |
| 671 | /* the task MUST not be in the run queue anymore */ |
| 672 | task_delete(t); |
| 673 | session_free(s); |
| 674 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 675 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 679 | extern const char sess_term_cond[8]; |
| 680 | extern const char sess_fin_state[8]; |
| 681 | extern const char *monthname[12]; |
| 682 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 683 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 684 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 685 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 686 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 687 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 688 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 689 | /* |
| 690 | * send a log for the session when we have enough info about it. |
| 691 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 692 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 693 | static void http_sess_log(struct session *s) |
| 694 | { |
| 695 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 696 | struct proxy *fe = s->fe; |
| 697 | struct proxy *be = s->be; |
| 698 | struct proxy *prx_log; |
| 699 | struct http_txn *txn = &s->txn; |
| 700 | int tolog; |
| 701 | char *uri, *h; |
| 702 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 703 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 704 | static char tmpline[MAX_SYSLOG_LEN]; |
| 705 | int hdr; |
| 706 | |
| 707 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 708 | return; |
| 709 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 710 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 711 | if (s->cli_addr.ss_family == AF_INET) |
| 712 | inet_ntop(AF_INET, |
| 713 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 714 | pn, sizeof(pn)); |
| 715 | else |
| 716 | inet_ntop(AF_INET6, |
| 717 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 718 | pn, sizeof(pn)); |
| 719 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 720 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 721 | |
| 722 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 723 | tolog = fe->to_log; |
| 724 | |
| 725 | h = tmpline; |
| 726 | if (fe->to_log & LW_REQHDR && |
| 727 | txn->req.cap && |
| 728 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 729 | *(h++) = ' '; |
| 730 | *(h++) = '{'; |
| 731 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 732 | if (hdr) |
| 733 | *(h++) = '|'; |
| 734 | if (txn->req.cap[hdr] != NULL) |
| 735 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 736 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 737 | } |
| 738 | *(h++) = '}'; |
| 739 | } |
| 740 | |
| 741 | if (fe->to_log & LW_RSPHDR && |
| 742 | txn->rsp.cap && |
| 743 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 744 | *(h++) = ' '; |
| 745 | *(h++) = '{'; |
| 746 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 747 | if (hdr) |
| 748 | *(h++) = '|'; |
| 749 | if (txn->rsp.cap[hdr] != NULL) |
| 750 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 751 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 752 | } |
| 753 | *(h++) = '}'; |
| 754 | } |
| 755 | |
| 756 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 757 | *(h++) = ' '; |
| 758 | *(h++) = '"'; |
| 759 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 760 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 761 | '#', url_encode_map, uri); |
| 762 | *(h++) = '"'; |
| 763 | } |
| 764 | *h = '\0'; |
| 765 | |
| 766 | svid = (tolog & LW_SVID) ? |
| 767 | (s->data_source != DATA_SRC_STATS) ? |
| 768 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 769 | |
| 770 | send_log(prx_log, LOG_INFO, |
| 771 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 772 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 773 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 774 | pn, |
| 775 | (s->cli_addr.ss_family == AF_INET) ? |
| 776 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 777 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 778 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 779 | tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 780 | fe->id, be->id, svid, |
| 781 | s->logs.t_request, |
| 782 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 783 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 784 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 785 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 786 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame^] | 787 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 788 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 789 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 790 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 791 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 792 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 793 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 794 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 795 | (s->flags & SN_REDISP)?"+":"", |
| 796 | (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 797 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 798 | |
| 799 | s->logs.logwait = 0; |
| 800 | } |
| 801 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 802 | |
| 803 | /* |
| 804 | * Capture headers from message starting at <som> according to header list |
| 805 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 806 | */ |
| 807 | void capture_headers(char *som, struct hdr_idx *idx, |
| 808 | char **cap, struct cap_hdr *cap_hdr) |
| 809 | { |
| 810 | char *eol, *sol, *col, *sov; |
| 811 | int cur_idx; |
| 812 | struct cap_hdr *h; |
| 813 | int len; |
| 814 | |
| 815 | sol = som + hdr_idx_first_pos(idx); |
| 816 | cur_idx = hdr_idx_first_idx(idx); |
| 817 | |
| 818 | while (cur_idx) { |
| 819 | eol = sol + idx->v[cur_idx].len; |
| 820 | |
| 821 | col = sol; |
| 822 | while (col < eol && *col != ':') |
| 823 | col++; |
| 824 | |
| 825 | sov = col + 1; |
| 826 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 827 | sov++; |
| 828 | |
| 829 | for (h = cap_hdr; h; h = h->next) { |
| 830 | if ((h->namelen == col - sol) && |
| 831 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 832 | if (cap[h->index] == NULL) |
| 833 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 834 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 835 | |
| 836 | if (cap[h->index] == NULL) { |
| 837 | Alert("HTTP capture : out of memory.\n"); |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | len = eol - sov; |
| 842 | if (len > h->len) |
| 843 | len = h->len; |
| 844 | |
| 845 | memcpy(cap[h->index], sov, len); |
| 846 | cap[h->index][len]=0; |
| 847 | } |
| 848 | } |
| 849 | sol = eol + idx->v[cur_idx].cr + 1; |
| 850 | cur_idx = idx->v[cur_idx].next; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 855 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 856 | */ |
| 857 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 858 | |
| 859 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 860 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 861 | */ |
| 862 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 863 | ptr++; \ |
| 864 | if (likely(ptr < end)) \ |
| 865 | goto good; \ |
| 866 | else { \ |
| 867 | state = (st); \ |
| 868 | goto http_msg_ood; \ |
| 869 | } \ |
| 870 | } while (0) |
| 871 | |
| 872 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 873 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 874 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 875 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 876 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 877 | * will give undefined results. |
| 878 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 879 | * and that msg->sol points to the beginning of the response. |
| 880 | * If a complete line is found (which implies that at least one CR or LF is |
| 881 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 882 | * returned indicating an incomplete line (which does not mean that parts have |
| 883 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 884 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 885 | * upon next call. |
| 886 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 887 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 888 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 889 | * 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] | 890 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 891 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 892 | 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] | 893 | const char *ptr, const char *end, |
| 894 | char **ret_ptr, int *ret_state) |
| 895 | { |
| 896 | __label__ |
| 897 | http_msg_rpver, |
| 898 | http_msg_rpver_sp, |
| 899 | http_msg_rpcode, |
| 900 | http_msg_rpcode_sp, |
| 901 | http_msg_rpreason, |
| 902 | http_msg_rpline_eol, |
| 903 | http_msg_ood, /* out of data */ |
| 904 | http_msg_invalid; |
| 905 | |
| 906 | switch (state) { |
| 907 | http_msg_rpver: |
| 908 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 909 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 910 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 911 | |
| 912 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 913 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 914 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 915 | } |
| 916 | goto http_msg_invalid; |
| 917 | |
| 918 | http_msg_rpver_sp: |
| 919 | case HTTP_MSG_RPVER_SP: |
| 920 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 921 | msg->sl.st.c = ptr - msg_buf; |
| 922 | goto http_msg_rpcode; |
| 923 | } |
| 924 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 925 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 926 | /* so it's a CR/LF, this is invalid */ |
| 927 | goto http_msg_invalid; |
| 928 | |
| 929 | http_msg_rpcode: |
| 930 | case HTTP_MSG_RPCODE: |
| 931 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 932 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 933 | |
| 934 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 935 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 936 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 937 | } |
| 938 | |
| 939 | /* so it's a CR/LF, so there is no reason phrase */ |
| 940 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 941 | http_msg_rsp_reason: |
| 942 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 943 | msg->sl.st.r = ptr - msg_buf; |
| 944 | msg->sl.st.r_l = 0; |
| 945 | goto http_msg_rpline_eol; |
| 946 | |
| 947 | http_msg_rpcode_sp: |
| 948 | case HTTP_MSG_RPCODE_SP: |
| 949 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 950 | msg->sl.st.r = ptr - msg_buf; |
| 951 | goto http_msg_rpreason; |
| 952 | } |
| 953 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 954 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 955 | /* so it's a CR/LF, so there is no reason phrase */ |
| 956 | goto http_msg_rsp_reason; |
| 957 | |
| 958 | http_msg_rpreason: |
| 959 | case HTTP_MSG_RPREASON: |
| 960 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 961 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 962 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 963 | http_msg_rpline_eol: |
| 964 | /* We have seen the end of line. Note that we do not |
| 965 | * necessarily have the \n yet, but at least we know that we |
| 966 | * have EITHER \r OR \n, otherwise the response would not be |
| 967 | * complete. We can then record the response length and return |
| 968 | * to the caller which will be able to register it. |
| 969 | */ |
| 970 | msg->sl.st.l = ptr - msg->sol; |
| 971 | return ptr; |
| 972 | |
| 973 | #ifdef DEBUG_FULL |
| 974 | default: |
| 975 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 976 | exit(1); |
| 977 | #endif |
| 978 | } |
| 979 | |
| 980 | http_msg_ood: |
| 981 | /* out of data */ |
| 982 | if (ret_state) |
| 983 | *ret_state = state; |
| 984 | if (ret_ptr) |
| 985 | *ret_ptr = (char *)ptr; |
| 986 | return NULL; |
| 987 | |
| 988 | http_msg_invalid: |
| 989 | /* invalid message */ |
| 990 | if (ret_state) |
| 991 | *ret_state = HTTP_MSG_ERROR; |
| 992 | return NULL; |
| 993 | } |
| 994 | |
| 995 | |
| 996 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 997 | * This function parses a request line between <ptr> and <end>, starting with |
| 998 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 999 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1000 | * will give undefined results. |
| 1001 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1002 | * and that msg->sol points to the beginning of the request. |
| 1003 | * If a complete line is found (which implies that at least one CR or LF is |
| 1004 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1005 | * returned indicating an incomplete line (which does not mean that parts have |
| 1006 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1007 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1008 | * upon next call. |
| 1009 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1010 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1011 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1012 | * 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] | 1013 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1014 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1015 | 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] | 1016 | const char *ptr, const char *end, |
| 1017 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1018 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1019 | __label__ |
| 1020 | http_msg_rqmeth, |
| 1021 | http_msg_rqmeth_sp, |
| 1022 | http_msg_rquri, |
| 1023 | http_msg_rquri_sp, |
| 1024 | http_msg_rqver, |
| 1025 | http_msg_rqline_eol, |
| 1026 | http_msg_ood, /* out of data */ |
| 1027 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1028 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1029 | switch (state) { |
| 1030 | http_msg_rqmeth: |
| 1031 | case HTTP_MSG_RQMETH: |
| 1032 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1033 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1034 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1035 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1036 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1037 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1038 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1039 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1040 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1041 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1042 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1043 | http_msg_req09_uri: |
| 1044 | msg->sl.rq.u = ptr - msg_buf; |
| 1045 | http_msg_req09_uri_e: |
| 1046 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1047 | http_msg_req09_ver: |
| 1048 | msg->sl.rq.v = ptr - msg_buf; |
| 1049 | msg->sl.rq.v_l = 0; |
| 1050 | goto http_msg_rqline_eol; |
| 1051 | } |
| 1052 | goto http_msg_invalid; |
| 1053 | |
| 1054 | http_msg_rqmeth_sp: |
| 1055 | case HTTP_MSG_RQMETH_SP: |
| 1056 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1057 | msg->sl.rq.u = ptr - msg_buf; |
| 1058 | goto http_msg_rquri; |
| 1059 | } |
| 1060 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1061 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1062 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1063 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1064 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1065 | http_msg_rquri: |
| 1066 | case HTTP_MSG_RQURI: |
| 1067 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1068 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1069 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1070 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1071 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1072 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1073 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1074 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1075 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1076 | goto http_msg_req09_uri_e; |
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 | http_msg_rquri_sp: |
| 1079 | case HTTP_MSG_RQURI_SP: |
| 1080 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1081 | msg->sl.rq.v = ptr - msg_buf; |
| 1082 | goto http_msg_rqver; |
| 1083 | } |
| 1084 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1085 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1086 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1087 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | http_msg_rqver: |
| 1090 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1091 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1092 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1093 | |
| 1094 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1095 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1096 | http_msg_rqline_eol: |
| 1097 | /* We have seen the end of line. Note that we do not |
| 1098 | * necessarily have the \n yet, but at least we know that we |
| 1099 | * have EITHER \r OR \n, otherwise the request would not be |
| 1100 | * complete. We can then record the request length and return |
| 1101 | * to the caller which will be able to register it. |
| 1102 | */ |
| 1103 | msg->sl.rq.l = ptr - msg->sol; |
| 1104 | return ptr; |
| 1105 | } |
| 1106 | |
| 1107 | /* neither an HTTP_VER token nor a CRLF */ |
| 1108 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1109 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1110 | #ifdef DEBUG_FULL |
| 1111 | default: |
| 1112 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1113 | exit(1); |
| 1114 | #endif |
| 1115 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1116 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1117 | http_msg_ood: |
| 1118 | /* out of data */ |
| 1119 | if (ret_state) |
| 1120 | *ret_state = state; |
| 1121 | if (ret_ptr) |
| 1122 | *ret_ptr = (char *)ptr; |
| 1123 | return NULL; |
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_invalid: |
| 1126 | /* invalid message */ |
| 1127 | if (ret_state) |
| 1128 | *ret_state = HTTP_MSG_ERROR; |
| 1129 | return NULL; |
| 1130 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1131 | |
| 1132 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1133 | /* |
| 1134 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1135 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1136 | * when data are missing and recalled at the exact same location with no |
| 1137 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1138 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1139 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1140 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1141 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1142 | { |
| 1143 | __label__ |
| 1144 | http_msg_rqbefore, |
| 1145 | http_msg_rqbefore_cr, |
| 1146 | http_msg_rqmeth, |
| 1147 | http_msg_rqline_end, |
| 1148 | http_msg_hdr_first, |
| 1149 | http_msg_hdr_name, |
| 1150 | http_msg_hdr_l1_sp, |
| 1151 | http_msg_hdr_l1_lf, |
| 1152 | http_msg_hdr_l1_lws, |
| 1153 | http_msg_hdr_val, |
| 1154 | http_msg_hdr_l2_lf, |
| 1155 | http_msg_hdr_l2_lws, |
| 1156 | http_msg_complete_header, |
| 1157 | http_msg_last_lf, |
| 1158 | http_msg_ood, /* out of data */ |
| 1159 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1160 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1161 | int state; /* updated only when leaving the FSM */ |
| 1162 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1163 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1164 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1165 | ptr = buf->lr; |
| 1166 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1168 | if (unlikely(ptr >= end)) |
| 1169 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1172 | /* |
| 1173 | * First, states that are specific to the response only. |
| 1174 | * We check them first so that request and headers are |
| 1175 | * closer to each other (accessed more often). |
| 1176 | */ |
| 1177 | http_msg_rpbefore: |
| 1178 | case HTTP_MSG_RPBEFORE: |
| 1179 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1180 | if (likely(ptr == buf->data)) { |
| 1181 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1182 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1183 | } else { |
| 1184 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1185 | /* only skip empty leading lines, don't remove them */ |
| 1186 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1187 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1188 | #else |
| 1189 | /* Remove empty leading lines, as recommended by |
| 1190 | * RFC2616. This takes a lot of time because we |
| 1191 | * must move all the buffer backwards, but this |
| 1192 | * is rarely needed. The method above will be |
| 1193 | * cleaner when we'll be able to start sending |
| 1194 | * the request from any place in the buffer. |
| 1195 | */ |
| 1196 | buf->lr = ptr; |
| 1197 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1198 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1199 | msg->sol = buf->data; |
| 1200 | ptr = buf->data; |
| 1201 | end = buf->r; |
| 1202 | #endif |
| 1203 | } |
| 1204 | hdr_idx_init(idx); |
| 1205 | state = HTTP_MSG_RPVER; |
| 1206 | goto http_msg_rpver; |
| 1207 | } |
| 1208 | |
| 1209 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1210 | goto http_msg_invalid; |
| 1211 | |
| 1212 | if (unlikely(*ptr == '\n')) |
| 1213 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1214 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1215 | /* stop here */ |
| 1216 | |
| 1217 | http_msg_rpbefore_cr: |
| 1218 | case HTTP_MSG_RPBEFORE_CR: |
| 1219 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1220 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1221 | /* stop here */ |
| 1222 | |
| 1223 | http_msg_rpver: |
| 1224 | case HTTP_MSG_RPVER: |
| 1225 | case HTTP_MSG_RPVER_SP: |
| 1226 | case HTTP_MSG_RPCODE: |
| 1227 | case HTTP_MSG_RPCODE_SP: |
| 1228 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1229 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1230 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1231 | if (unlikely(!ptr)) |
| 1232 | return; |
| 1233 | |
| 1234 | /* we have a full response and we know that we have either a CR |
| 1235 | * or an LF at <ptr>. |
| 1236 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1237 | //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] | 1238 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1239 | |
| 1240 | msg->sol = ptr; |
| 1241 | if (likely(*ptr == '\r')) |
| 1242 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1243 | goto http_msg_rpline_end; |
| 1244 | |
| 1245 | http_msg_rpline_end: |
| 1246 | case HTTP_MSG_RPLINE_END: |
| 1247 | /* msg->sol must point to the first of CR or LF. */ |
| 1248 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1249 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1250 | /* stop here */ |
| 1251 | |
| 1252 | /* |
| 1253 | * Second, states that are specific to the request only |
| 1254 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1255 | http_msg_rqbefore: |
| 1256 | case HTTP_MSG_RQBEFORE: |
| 1257 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1258 | if (likely(ptr == buf->data)) { |
| 1259 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1260 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1261 | } else { |
| 1262 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1263 | /* only skip empty leading lines, don't remove them */ |
| 1264 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1265 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1266 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1267 | /* Remove empty leading lines, as recommended by |
| 1268 | * RFC2616. This takes a lot of time because we |
| 1269 | * must move all the buffer backwards, but this |
| 1270 | * is rarely needed. The method above will be |
| 1271 | * cleaner when we'll be able to start sending |
| 1272 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1273 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1274 | buf->lr = ptr; |
| 1275 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1276 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | msg->sol = buf->data; |
| 1278 | ptr = buf->data; |
| 1279 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1280 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1282 | /* we will need this when keep-alive will be supported |
| 1283 | hdr_idx_init(idx); |
| 1284 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | state = HTTP_MSG_RQMETH; |
| 1286 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1287 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1290 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1292 | if (unlikely(*ptr == '\n')) |
| 1293 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1294 | 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] | 1295 | /* stop here */ |
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 | http_msg_rqbefore_cr: |
| 1298 | case HTTP_MSG_RQBEFORE_CR: |
| 1299 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1300 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1301 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1302 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1303 | http_msg_rqmeth: |
| 1304 | case HTTP_MSG_RQMETH: |
| 1305 | case HTTP_MSG_RQMETH_SP: |
| 1306 | case HTTP_MSG_RQURI: |
| 1307 | case HTTP_MSG_RQURI_SP: |
| 1308 | case HTTP_MSG_RQVER: |
| 1309 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1310 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | if (unlikely(!ptr)) |
| 1312 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1313 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1314 | /* we have a full request and we know that we have either a CR |
| 1315 | * or an LF at <ptr>. |
| 1316 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1317 | //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] | 1318 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | msg->sol = ptr; |
| 1321 | if (likely(*ptr == '\r')) |
| 1322 | 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] | 1323 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1324 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | http_msg_rqline_end: |
| 1326 | case HTTP_MSG_RQLINE_END: |
| 1327 | /* check for HTTP/0.9 request : no version information available. |
| 1328 | * msg->sol must point to the first of CR or LF. |
| 1329 | */ |
| 1330 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1331 | goto http_msg_last_lf; |
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 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1334 | 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] | 1335 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | /* |
| 1338 | * Common states below |
| 1339 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | http_msg_hdr_first: |
| 1341 | case HTTP_MSG_HDR_FIRST: |
| 1342 | msg->sol = ptr; |
| 1343 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1344 | goto http_msg_hdr_name; |
| 1345 | } |
| 1346 | |
| 1347 | if (likely(*ptr == '\r')) |
| 1348 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1349 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | http_msg_hdr_name: |
| 1352 | case HTTP_MSG_HDR_NAME: |
| 1353 | /* assumes msg->sol points to the first char */ |
| 1354 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1355 | 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] | 1356 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1357 | if (likely(*ptr == ':')) { |
| 1358 | msg->col = ptr - buf->data; |
| 1359 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1360 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | http_msg_hdr_l1_sp: |
| 1365 | case HTTP_MSG_HDR_L1_SP: |
| 1366 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1367 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1368 | 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] | 1369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | /* header value can be basically anything except CR/LF */ |
| 1371 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1372 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1373 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1374 | goto http_msg_hdr_val; |
| 1375 | } |
| 1376 | |
| 1377 | if (likely(*ptr == '\r')) |
| 1378 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1379 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | http_msg_hdr_l1_lf: |
| 1382 | case HTTP_MSG_HDR_L1_LF: |
| 1383 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1384 | 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] | 1385 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | http_msg_hdr_l1_lws: |
| 1387 | case HTTP_MSG_HDR_L1_LWS: |
| 1388 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1389 | /* replace HT,CR,LF with spaces */ |
| 1390 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1391 | buf->data[msg->sov] = ' '; |
| 1392 | goto http_msg_hdr_l1_sp; |
| 1393 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1394 | /* we had a header consisting only in spaces ! */ |
| 1395 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1396 | goto http_msg_complete_header; |
| 1397 | |
| 1398 | http_msg_hdr_val: |
| 1399 | case HTTP_MSG_HDR_VAL: |
| 1400 | /* assumes msg->sol points to the first char, msg->col to the |
| 1401 | * colon, and msg->sov points to the first character of the |
| 1402 | * value. |
| 1403 | */ |
| 1404 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1405 | 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] | 1406 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | msg->eol = ptr; |
| 1408 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1409 | * real header end in case it ends with lots of LWS, but is this |
| 1410 | * really needed ? |
| 1411 | */ |
| 1412 | if (likely(*ptr == '\r')) |
| 1413 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1414 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_hdr_l2_lf: |
| 1417 | case HTTP_MSG_HDR_L2_LF: |
| 1418 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1419 | 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] | 1420 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | http_msg_hdr_l2_lws: |
| 1422 | case HTTP_MSG_HDR_L2_LWS: |
| 1423 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1424 | /* LWS: replace HT,CR,LF with spaces */ |
| 1425 | for (; msg->eol < ptr; msg->eol++) |
| 1426 | *msg->eol = ' '; |
| 1427 | goto http_msg_hdr_val; |
| 1428 | } |
| 1429 | http_msg_complete_header: |
| 1430 | /* |
| 1431 | * It was a new header, so the last one is finished. |
| 1432 | * Assumes msg->sol points to the first char, msg->col to the |
| 1433 | * colon, msg->sov points to the first character of the value |
| 1434 | * and msg->eol to the first CR or LF so we know how the line |
| 1435 | * ends. We insert last header into the index. |
| 1436 | */ |
| 1437 | /* |
| 1438 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1439 | write(2, msg->sol, msg->eol-msg->sol); |
| 1440 | fprintf(stderr,"\n"); |
| 1441 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1442 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1444 | idx, idx->tail) < 0)) |
| 1445 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | msg->sol = ptr; |
| 1448 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1449 | goto http_msg_hdr_name; |
| 1450 | } |
| 1451 | |
| 1452 | if (likely(*ptr == '\r')) |
| 1453 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1454 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | http_msg_last_lf: |
| 1457 | case HTTP_MSG_LAST_LF: |
| 1458 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1459 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1460 | ptr++; |
| 1461 | buf->lr = ptr; |
| 1462 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1463 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | return; |
| 1465 | #ifdef DEBUG_FULL |
| 1466 | default: |
| 1467 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1468 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1469 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | } |
| 1471 | http_msg_ood: |
| 1472 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1473 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | buf->lr = ptr; |
| 1475 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | http_msg_invalid: |
| 1478 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1479 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | return; |
| 1481 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | /* |
| 1484 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1485 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1486 | * 0 else. |
| 1487 | */ |
| 1488 | int process_cli(struct session *t) |
| 1489 | { |
| 1490 | int s = t->srv_state; |
| 1491 | int c = t->cli_state; |
| 1492 | struct buffer *req = t->req; |
| 1493 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1494 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1496 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1497 | 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] | 1498 | req->rex.tv_sec, req->rex.tv_usec, |
| 1499 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | if (c == CL_STHEADERS) { |
| 1502 | /* |
| 1503 | * Now parse the partial (or complete) lines. |
| 1504 | * We will check the request syntax, and also join multi-line |
| 1505 | * headers. An index of all the lines will be elaborated while |
| 1506 | * parsing. |
| 1507 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1508 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1511 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | * req->data + req->eoh = end of processed headers / start of current one |
| 1513 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1514 | * req->lr = first non-visited byte |
| 1515 | * req->r = end of data |
| 1516 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1517 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1519 | struct http_txn *txn = &t->txn; |
| 1520 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1524 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1525 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | /* 1: we might have to print this header in debug mode */ |
| 1527 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1528 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1529 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1532 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | eol = sol + msg->sl.rq.l; |
| 1534 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1535 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1536 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1537 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1538 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1540 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1542 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1543 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1544 | } |
| 1545 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1546 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1547 | |
| 1548 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1550 | * If not so, we check the FD and buffer states before leaving. |
| 1551 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1553 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1554 | * |
| 1555 | */ |
| 1556 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1557 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | /* |
| 1559 | * First, let's catch bad requests. |
| 1560 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1561 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* 1: Since we are in header mode, if there's no space |
| 1565 | * left for headers, we won't be able to free more |
| 1566 | * later, so the session will never terminate. We |
| 1567 | * must terminate it now. |
| 1568 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1570 | /* FIXME: check if URI is set and return Status |
| 1571 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1572 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1573 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1578 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1579 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | fd_delete(t->cli_fd); |
| 1581 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1582 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1583 | if (!(t->flags & SN_ERR_MASK)) |
| 1584 | t->flags |= SN_ERR_CLICL; |
| 1585 | if (!(t->flags & SN_FINST_MASK)) |
| 1586 | t->flags |= SN_FINST_R; |
| 1587 | return 1; |
| 1588 | } |
| 1589 | |
| 1590 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 1591 | else if (unlikely(tv_isle(&req->rex, &now) || |
| 1592 | tv_isle(&txn->exp, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1593 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1594 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1595 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1596 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1597 | if (!(t->flags & SN_ERR_MASK)) |
| 1598 | t->flags |= SN_ERR_CLITO; |
| 1599 | if (!(t->flags & SN_FINST_MASK)) |
| 1600 | t->flags |= SN_FINST_R; |
| 1601 | return 1; |
| 1602 | } |
| 1603 | |
| 1604 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1605 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1606 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1608 | * req->l == rlim-data |
| 1609 | */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1610 | if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1611 | tv_eternity(&req->rex); |
| 1612 | } |
| 1613 | return t->cli_state != CL_STHEADERS; |
| 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | /**************************************************************** |
| 1618 | * More interesting part now : we know that we have a complete * |
| 1619 | * request which at least looks like HTTP. We have an indicator * |
| 1620 | * of each header's length, so we can parse them quickly. * |
| 1621 | ****************************************************************/ |
| 1622 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1623 | /* ensure we keep this pointer to the beginning of the message */ |
| 1624 | msg->sol = req->data + msg->som; |
| 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | /* |
| 1627 | * 1: identify the method |
| 1628 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1629 | 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] | 1630 | |
| 1631 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1633 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1635 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1637 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1638 | !memcmp(&req->data[msg->sl.rq.u], |
| 1639 | t->fe->monitor_uri, |
| 1640 | t->fe->monitor_uri_len))) { |
| 1641 | /* |
| 1642 | * We have found the monitor URI |
| 1643 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1644 | struct acl_cond *cond; |
| 1645 | cur_proxy = t->fe; |
| 1646 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1648 | |
| 1649 | /* Check if we want to fail this monitor request or not */ |
| 1650 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1651 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1652 | if (cond->pol == ACL_COND_UNLESS) |
| 1653 | ret = !ret; |
| 1654 | |
| 1655 | if (ret) { |
| 1656 | /* we fail this request, let's return 503 service unavail */ |
| 1657 | txn->status = 503; |
| 1658 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1659 | goto return_prx_cond; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1664 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | client_retnclose(t, &http_200_chunk); |
| 1666 | goto return_prx_cond; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1671 | * Note: we cannot log anymore if the request has been |
| 1672 | * classified as invalid. |
| 1673 | */ |
| 1674 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1675 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1676 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | int urilen = msg->sl.rq.l; |
| 1678 | |
| 1679 | if (urilen >= REQURI_LEN) |
| 1680 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1681 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1682 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1683 | |
| 1684 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1685 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1686 | } else { |
| 1687 | Alert("HTTP logging : out of memory.\n"); |
| 1688 | } |
| 1689 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1690 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1693 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1694 | int delta; |
| 1695 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1696 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1697 | cur_end = msg->sol + msg->sl.rq.l; |
| 1698 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | if (msg->sl.rq.u_l == 0) { |
| 1701 | /* if no URI was set, add "/" */ |
| 1702 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1703 | cur_end += delta; |
| 1704 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1705 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | /* add HTTP version */ |
| 1707 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1708 | msg->eoh += delta; |
| 1709 | cur_end += delta; |
| 1710 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1711 | HTTP_MSG_RQMETH, |
| 1712 | msg->sol, cur_end + 1, |
| 1713 | NULL, NULL); |
| 1714 | if (unlikely(!cur_end)) |
| 1715 | goto return_bad_req; |
| 1716 | |
| 1717 | /* we have a full HTTP/1.0 request now and we know that |
| 1718 | * we have either a CR or an LF at <ptr>. |
| 1719 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1720 | 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] | 1721 | } |
| 1722 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | |
| 1724 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1725 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1726 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1727 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1728 | |
| 1729 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1731 | * As opposed to version 1.2, now they will be evaluated in the |
| 1732 | * filters order and not in the header order. This means that |
| 1733 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1734 | * |
| 1735 | * We can now check whether we want to switch to another |
| 1736 | * backend, in which case we will re-check the backend's |
| 1737 | * filters and various options. In order to support 3-level |
| 1738 | * switching, here's how we should proceed : |
| 1739 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1740 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1741 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1742 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1743 | * There cannot be any switch from there, so ->be cannot be |
| 1744 | * changed anymore. |
| 1745 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1746 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1747 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1748 | * The response path will be able to apply either ->be, or |
| 1749 | * ->be then ->fe filters in order to match the reverse of |
| 1750 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1751 | */ |
| 1752 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1753 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1754 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1755 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1756 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1757 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1758 | /* first check whether we have some ACLs set to block this request */ |
| 1759 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1760 | 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] | 1761 | if (cond->pol == ACL_COND_UNLESS) |
| 1762 | ret = !ret; |
| 1763 | |
| 1764 | if (ret) { |
| 1765 | txn->status = 403; |
| 1766 | /* let's log the request time */ |
| 1767 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1768 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1769 | goto return_prx_cond; |
| 1770 | } |
| 1771 | } |
| 1772 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1773 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1774 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1776 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1779 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1780 | /* to ensure correct connection accounting on |
| 1781 | * the backend, we count the connection for the |
| 1782 | * one managing the queue. |
| 1783 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1784 | t->be->beconn++; |
| 1785 | if (t->be->beconn > t->be->beconn_max) |
| 1786 | t->be->beconn_max = t->be->beconn; |
| 1787 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1788 | t->flags |= SN_BE_ASSIGNED; |
| 1789 | } |
| 1790 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1791 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1792 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1793 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1794 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1795 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1796 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1797 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1798 | goto return_prx_cond; |
| 1799 | } |
| 1800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 1802 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | !(t->flags & SN_CONN_CLOSED)) { |
| 1804 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1805 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1807 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1808 | 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] | 1809 | old_idx = 0; |
| 1810 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1811 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1812 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | cur_ptr = cur_next; |
| 1814 | cur_end = cur_ptr + cur_hdr->len; |
| 1815 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1816 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1817 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1818 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | /* 3 possibilities : |
| 1820 | * - we have already set Connection: close, |
| 1821 | * so we remove this line. |
| 1822 | * - we have not yet set Connection: close, |
| 1823 | * but this line indicates close. We leave |
| 1824 | * it untouched and set the flag. |
| 1825 | * - we have not yet set Connection: close, |
| 1826 | * and this line indicates non-close. We |
| 1827 | * replace it. |
| 1828 | */ |
| 1829 | if (t->flags & SN_CONN_CLOSED) { |
| 1830 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1831 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1832 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1833 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1834 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | cur_hdr->len = 0; |
| 1836 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1837 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1838 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1839 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | cur_next += delta; |
| 1841 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1842 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | } |
| 1844 | t->flags |= SN_CONN_CLOSED; |
| 1845 | } |
| 1846 | } |
| 1847 | old_idx = cur_idx; |
| 1848 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1849 | } |
| 1850 | /* add request headers from the rule sets in the same order */ |
| 1851 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1852 | if (unlikely(http_header_add_tail(req, |
| 1853 | &txn->req, |
| 1854 | &txn->hdr_idx, |
| 1855 | rule_set->req_add[cur_idx])) < 0) |
| 1856 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1857 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1858 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1859 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1860 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1861 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1862 | /* we have to check the URI and auth for this request */ |
| 1863 | if (stats_check_uri_auth(t, rule_set)) |
| 1864 | return 1; |
| 1865 | } |
| 1866 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1867 | /* now check whether we have some switching rules for this request */ |
| 1868 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1869 | struct switching_rule *rule; |
| 1870 | |
| 1871 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1872 | int ret; |
| 1873 | |
| 1874 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1875 | if (cond->pol == ACL_COND_UNLESS) |
| 1876 | ret = !ret; |
| 1877 | |
| 1878 | if (ret) { |
| 1879 | t->be = rule->be.backend; |
| 1880 | t->be->beconn++; |
| 1881 | if (t->be->beconn > t->be->beconn_max) |
| 1882 | t->be->beconn_max = t->be->beconn; |
| 1883 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1884 | |
| 1885 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1886 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 1887 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1888 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1889 | t->flags |= SN_BE_ASSIGNED; |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1895 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1896 | /* No backend was set, but there was a default |
| 1897 | * backend set in the frontend, so we use it and |
| 1898 | * loop again. |
| 1899 | */ |
| 1900 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1901 | t->be->beconn++; |
| 1902 | if (t->be->beconn > t->be->beconn_max) |
| 1903 | t->be->beconn_max = t->be->beconn; |
| 1904 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1905 | |
| 1906 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 1907 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 1908 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1909 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1910 | t->flags |= SN_BE_ASSIGNED; |
| 1911 | } |
| 1912 | } 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] | 1913 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1914 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1915 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1916 | /* To ensure correct connection accounting on |
| 1917 | * the backend, we count the connection for the |
| 1918 | * one managing the queue. |
| 1919 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1920 | t->be->beconn++; |
| 1921 | if (t->be->beconn > t->be->beconn_max) |
| 1922 | t->be->beconn_max = t->be->beconn; |
| 1923 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1924 | t->flags |= SN_BE_ASSIGNED; |
| 1925 | } |
| 1926 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1927 | /* |
| 1928 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1929 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1930 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1931 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1932 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1933 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1934 | /* |
| 1935 | * If HTTP PROXY is set we simply get remote server address |
| 1936 | * parsing incoming request. |
| 1937 | */ |
| 1938 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 1939 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 1940 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1941 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1942 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1943 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1944 | * so let's do the same now. |
| 1945 | */ |
| 1946 | |
| 1947 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1948 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1949 | 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] | 1950 | } |
| 1951 | |
| 1952 | |
| 1953 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1954 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1955 | * Note that doing so might move headers in the request, but |
| 1956 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1957 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1958 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 1959 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 1960 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1961 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1962 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1963 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1964 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1965 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1966 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1967 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1968 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1969 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1970 | /* Add an X-Forwarded-For header unless the source IP is |
| 1971 | * in the 'except' network range. |
| 1972 | */ |
| 1973 | if ((!t->fe->except_mask.s_addr || |
| 1974 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1975 | != t->fe->except_net.s_addr) && |
| 1976 | (!t->be->except_mask.s_addr || |
| 1977 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1978 | != t->be->except_net.s_addr)) { |
| 1979 | int len; |
| 1980 | unsigned char *pn; |
| 1981 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1982 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1983 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1984 | pn[0], pn[1], pn[2], pn[3]); |
| 1985 | |
| 1986 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1987 | &txn->hdr_idx, trash, len)) < 0) |
| 1988 | goto return_bad_req; |
| 1989 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1990 | } |
| 1991 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1992 | /* FIXME: for the sake of completeness, we should also support |
| 1993 | * 'except' here, although it is mostly useless in this case. |
| 1994 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1995 | int len; |
| 1996 | char pn[INET6_ADDRSTRLEN]; |
| 1997 | inet_ntop(AF_INET6, |
| 1998 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1999 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2000 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 2001 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2002 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2003 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2004 | } |
| 2005 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2006 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2007 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2008 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2009 | * 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] | 2010 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2011 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2012 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2013 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2014 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2015 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2016 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2017 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2018 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2019 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2020 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2021 | /************************************************************* |
| 2022 | * OK, that's finished for the headers. We have done what we * |
| 2023 | * could. Let's switch to the DATA state. * |
| 2024 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2025 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2026 | t->cli_state = CL_STDATA; |
| 2027 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2028 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2029 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2030 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2031 | if (!tv_isset(&t->fe->timeout.client) || |
| 2032 | (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2033 | /* If the client has no timeout, or if the server is not ready yet, |
| 2034 | * and we know for sure that it can expire, then it's cleaner to |
| 2035 | * disable the timeout on the client side so that too low values |
| 2036 | * cannot make the sessions abort too early. |
| 2037 | * |
| 2038 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2039 | * when it switches its state, otherwise a client can stay connected |
| 2040 | * indefinitely. This now seems to be OK. |
| 2041 | */ |
| 2042 | tv_eternity(&req->rex); |
| 2043 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2044 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2045 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2046 | * which may be the same as the connect timeout if unspecified. |
| 2047 | * If unset, then set it to zero because we really want it to |
| 2048 | * eventually expire. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2049 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2050 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2051 | t->req->l = 0; |
| 2052 | /* flush the request so that we can drop the connection early |
| 2053 | * if the client closes first. |
| 2054 | */ |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2055 | if (!tv_add_ifset(&req->cex, &now, &t->be->timeout.tarpit)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2056 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2057 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2058 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2059 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2060 | goto process_data; |
| 2061 | |
| 2062 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2063 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2064 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2065 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2066 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2067 | return_prx_cond: |
| 2068 | if (!(t->flags & SN_ERR_MASK)) |
| 2069 | t->flags |= SN_ERR_PRXCOND; |
| 2070 | if (!(t->flags & SN_FINST_MASK)) |
| 2071 | t->flags |= SN_FINST_R; |
| 2072 | return 1; |
| 2073 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2074 | } |
| 2075 | else if (c == CL_STDATA) { |
| 2076 | process_data: |
| 2077 | /* FIXME: this error handling is partly buggy because we always report |
| 2078 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2079 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2080 | * we're waiting for the server to connect. |
| 2081 | */ |
| 2082 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2083 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2084 | buffer_shutr(req); |
| 2085 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2086 | fd_delete(t->cli_fd); |
| 2087 | t->cli_state = CL_STCLOSE; |
| 2088 | if (!(t->flags & SN_ERR_MASK)) |
| 2089 | t->flags |= SN_ERR_CLICL; |
| 2090 | if (!(t->flags & SN_FINST_MASK)) { |
| 2091 | if (t->pend_pos) |
| 2092 | t->flags |= SN_FINST_Q; |
| 2093 | else if (s == SV_STCONN) |
| 2094 | t->flags |= SN_FINST_C; |
| 2095 | else |
| 2096 | t->flags |= SN_FINST_D; |
| 2097 | } |
| 2098 | return 1; |
| 2099 | } |
| 2100 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2101 | 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] | 2102 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2103 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2104 | t->cli_state = CL_STSHUTR; |
| 2105 | return 1; |
| 2106 | } |
| 2107 | /* last server read and buffer empty */ |
| 2108 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2109 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2110 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2111 | shutdown(t->cli_fd, SHUT_WR); |
| 2112 | /* We must ensure that the read part is still alive when switching |
| 2113 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2114 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2115 | tv_add_ifset(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2116 | t->cli_state = CL_STSHUTW; |
| 2117 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2118 | return 1; |
| 2119 | } |
| 2120 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2121 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2122 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2123 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2124 | t->cli_state = CL_STSHUTR; |
| 2125 | if (!(t->flags & SN_ERR_MASK)) |
| 2126 | t->flags |= SN_ERR_CLITO; |
| 2127 | if (!(t->flags & SN_FINST_MASK)) { |
| 2128 | if (t->pend_pos) |
| 2129 | t->flags |= SN_FINST_Q; |
| 2130 | else if (s == SV_STCONN) |
| 2131 | t->flags |= SN_FINST_C; |
| 2132 | else |
| 2133 | t->flags |= SN_FINST_D; |
| 2134 | } |
| 2135 | return 1; |
| 2136 | } |
| 2137 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2138 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2139 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2140 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2141 | shutdown(t->cli_fd, SHUT_WR); |
| 2142 | /* We must ensure that the read part is still alive when switching |
| 2143 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2144 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2145 | tv_add_ifset(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2146 | |
| 2147 | t->cli_state = CL_STSHUTW; |
| 2148 | if (!(t->flags & SN_ERR_MASK)) |
| 2149 | t->flags |= SN_ERR_CLITO; |
| 2150 | if (!(t->flags & SN_FINST_MASK)) { |
| 2151 | if (t->pend_pos) |
| 2152 | t->flags |= SN_FINST_Q; |
| 2153 | else if (s == SV_STCONN) |
| 2154 | t->flags |= SN_FINST_C; |
| 2155 | else |
| 2156 | t->flags |= SN_FINST_D; |
| 2157 | } |
| 2158 | return 1; |
| 2159 | } |
| 2160 | |
| 2161 | if (req->l >= req->rlim - req->data) { |
| 2162 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2163 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2164 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2165 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2166 | } |
| 2167 | } else { |
| 2168 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2169 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2170 | if (!tv_isset(&t->fe->timeout.client) || |
| 2171 | (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2172 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2173 | * know for sure that it can expire, then it's cleaner to disable the |
| 2174 | * timeout on the client side so that too low values cannot make the |
| 2175 | * sessions abort too early. |
| 2176 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2177 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2178 | else |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2179 | tv_add(&req->rex, &now, &t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | if ((rep->l == 0) || |
| 2184 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2185 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2186 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2187 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2188 | } |
| 2189 | } else { |
| 2190 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2191 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2192 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2193 | if (tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2194 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2195 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2196 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2197 | } |
| 2198 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2199 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | return 0; /* other cases change nothing */ |
| 2203 | } |
| 2204 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2205 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2206 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2207 | fd_delete(t->cli_fd); |
| 2208 | t->cli_state = CL_STCLOSE; |
| 2209 | if (!(t->flags & SN_ERR_MASK)) |
| 2210 | t->flags |= SN_ERR_CLICL; |
| 2211 | if (!(t->flags & SN_FINST_MASK)) { |
| 2212 | if (t->pend_pos) |
| 2213 | t->flags |= SN_FINST_Q; |
| 2214 | else if (s == SV_STCONN) |
| 2215 | t->flags |= SN_FINST_C; |
| 2216 | else |
| 2217 | t->flags |= SN_FINST_D; |
| 2218 | } |
| 2219 | return 1; |
| 2220 | } |
| 2221 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2222 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2223 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2224 | fd_delete(t->cli_fd); |
| 2225 | t->cli_state = CL_STCLOSE; |
| 2226 | return 1; |
| 2227 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2228 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2229 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2230 | fd_delete(t->cli_fd); |
| 2231 | t->cli_state = CL_STCLOSE; |
| 2232 | if (!(t->flags & SN_ERR_MASK)) |
| 2233 | t->flags |= SN_ERR_CLITO; |
| 2234 | if (!(t->flags & SN_FINST_MASK)) { |
| 2235 | if (t->pend_pos) |
| 2236 | t->flags |= SN_FINST_Q; |
| 2237 | else if (s == SV_STCONN) |
| 2238 | t->flags |= SN_FINST_C; |
| 2239 | else |
| 2240 | t->flags |= SN_FINST_D; |
| 2241 | } |
| 2242 | return 1; |
| 2243 | } |
| 2244 | |
| 2245 | if (t->flags & SN_SELF_GEN) { |
| 2246 | produce_content(t); |
| 2247 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2248 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2249 | fd_delete(t->cli_fd); |
| 2250 | t->cli_state = CL_STCLOSE; |
| 2251 | return 1; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | if ((rep->l == 0) |
| 2256 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2257 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2258 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2259 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2260 | } |
| 2261 | } else { |
| 2262 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2263 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2264 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2265 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2266 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | return 0; |
| 2270 | } |
| 2271 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2272 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2273 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2274 | fd_delete(t->cli_fd); |
| 2275 | t->cli_state = CL_STCLOSE; |
| 2276 | if (!(t->flags & SN_ERR_MASK)) |
| 2277 | t->flags |= SN_ERR_CLICL; |
| 2278 | if (!(t->flags & SN_FINST_MASK)) { |
| 2279 | if (t->pend_pos) |
| 2280 | t->flags |= SN_FINST_Q; |
| 2281 | else if (s == SV_STCONN) |
| 2282 | t->flags |= SN_FINST_C; |
| 2283 | else |
| 2284 | t->flags |= SN_FINST_D; |
| 2285 | } |
| 2286 | return 1; |
| 2287 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2288 | 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] | 2289 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2290 | fd_delete(t->cli_fd); |
| 2291 | t->cli_state = CL_STCLOSE; |
| 2292 | return 1; |
| 2293 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2294 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2295 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2296 | fd_delete(t->cli_fd); |
| 2297 | t->cli_state = CL_STCLOSE; |
| 2298 | if (!(t->flags & SN_ERR_MASK)) |
| 2299 | t->flags |= SN_ERR_CLITO; |
| 2300 | if (!(t->flags & SN_FINST_MASK)) { |
| 2301 | if (t->pend_pos) |
| 2302 | t->flags |= SN_FINST_Q; |
| 2303 | else if (s == SV_STCONN) |
| 2304 | t->flags |= SN_FINST_C; |
| 2305 | else |
| 2306 | t->flags |= SN_FINST_D; |
| 2307 | } |
| 2308 | return 1; |
| 2309 | } |
| 2310 | else if (req->l >= req->rlim - req->data) { |
| 2311 | /* no room to read more data */ |
| 2312 | |
| 2313 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2314 | * after the timeout by sending more data after it receives a close ? |
| 2315 | */ |
| 2316 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2317 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2318 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2319 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2320 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2321 | } |
| 2322 | } else { |
| 2323 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2324 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2325 | if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2326 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2327 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2328 | } |
| 2329 | } |
| 2330 | return 0; |
| 2331 | } |
| 2332 | else { /* CL_STCLOSE: nothing to do */ |
| 2333 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2334 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2335 | 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] | 2336 | write(1, trash, len); |
| 2337 | } |
| 2338 | return 0; |
| 2339 | } |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
| 2343 | |
| 2344 | /* |
| 2345 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2346 | * (and a resync may be needed), 0 else. |
| 2347 | */ |
| 2348 | int process_srv(struct session *t) |
| 2349 | { |
| 2350 | int s = t->srv_state; |
| 2351 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2352 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2353 | struct buffer *req = t->req; |
| 2354 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2355 | int conn_err; |
| 2356 | |
| 2357 | #ifdef DEBUG_FULL |
| 2358 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2359 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2360 | |
| 2361 | #if 0 |
| 2362 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2363 | __FUNCTION__, __LINE__, |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2364 | t->fe->timeout.client.tv_sec, t->fe->timeout.client.tv_usec, |
| 2365 | t->fe->timeout.connect.tv_sec, t->fe->timeout.connect.tv_usec, |
| 2366 | t->fe->timeout.server.tv_sec, t->fe->timeout.server.tv_usec); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2367 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2368 | __FUNCTION__, __LINE__, |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2369 | t->be->timeout.client.tv_sec, t->be->timeout.client.tv_usec, |
| 2370 | t->be->timeout.connect.tv_sec, t->be->timeout.connect.tv_usec, |
| 2371 | t->be->timeout.server.tv_sec, t->be->timeout.server.tv_usec); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2372 | |
| 2373 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2374 | __FUNCTION__, __LINE__, |
| 2375 | req->cto.tv_sec, req->cto.tv_usec, |
| 2376 | req->rto.tv_sec, req->rto.tv_usec, |
| 2377 | req->wto.tv_sec, req->wto.tv_usec); |
| 2378 | |
| 2379 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2380 | __FUNCTION__, __LINE__, |
| 2381 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2382 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2383 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2384 | #endif |
| 2385 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2386 | //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] | 2387 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2388 | //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] | 2389 | //); |
| 2390 | if (s == SV_STIDLE) { |
| 2391 | if (c == CL_STHEADERS) |
| 2392 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2393 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2394 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2395 | (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] | 2396 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2397 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2398 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2399 | /* note that this must not return any error because it would be able to |
| 2400 | * overwrite the client_retnclose() output. |
| 2401 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2402 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2403 | 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] | 2404 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2405 | 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] | 2406 | |
| 2407 | return 1; |
| 2408 | } |
| 2409 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2410 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2411 | /* This connection is being tarpitted. The CLIENT side has |
| 2412 | * already set the connect expiration date to the right |
| 2413 | * timeout. We just have to check that it has not expired. |
| 2414 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2415 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2416 | return 0; |
| 2417 | |
| 2418 | /* We will set the queue timer to the time spent, just for |
| 2419 | * logging purposes. We fake a 500 server error, so that the |
| 2420 | * attacker will not suspect his connection has been tarpitted. |
| 2421 | * It will not cause trouble to the logs because we can exclude |
| 2422 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2423 | */ |
| 2424 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2425 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2426 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2427 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2428 | return 1; |
| 2429 | } |
| 2430 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2431 | /* Right now, we will need to create a connection to the server. |
| 2432 | * We might already have tried, and got a connection pending, in |
| 2433 | * which case we will not do anything till it's pending. It's up |
| 2434 | * to any other session to release it and wake us up again. |
| 2435 | */ |
| 2436 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2437 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2438 | return 0; |
| 2439 | else { |
| 2440 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2441 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2442 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2443 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2444 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2445 | if (t->srv) |
| 2446 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2447 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2448 | return 1; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | do { |
| 2453 | /* first, get a connection */ |
| 2454 | if (srv_redispatch_connect(t)) |
| 2455 | return t->srv_state != SV_STIDLE; |
| 2456 | |
| 2457 | /* try to (re-)connect to the server, and fail if we expire the |
| 2458 | * number of retries. |
| 2459 | */ |
| 2460 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2461 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2462 | return t->srv_state != SV_STIDLE; |
| 2463 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2464 | } while (1); |
| 2465 | } |
| 2466 | } |
| 2467 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2468 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2469 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2470 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2471 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2472 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2473 | fd_delete(t->srv_fd); |
| 2474 | if (t->srv) |
| 2475 | t->srv->cur_sess--; |
| 2476 | |
| 2477 | /* note that this must not return any error because it would be able to |
| 2478 | * overwrite the client_retnclose() output. |
| 2479 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2480 | 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] | 2481 | return 1; |
| 2482 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2483 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2484 | //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] | 2485 | return 0; /* nothing changed */ |
| 2486 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2487 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2488 | /* timeout, asynchronous connect error or first write error */ |
| 2489 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2490 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2491 | if (t->flags & SN_CONN_TAR) { |
| 2492 | /* We are doing a turn-around waiting for a new connection attempt. */ |
| 2493 | if (!tv_isle(&req->cex, &now)) |
| 2494 | return 0; |
| 2495 | t->flags &= ~SN_CONN_TAR; |
| 2496 | } |
| 2497 | else { |
| 2498 | fd_delete(t->srv_fd); |
| 2499 | if (t->srv) |
| 2500 | t->srv->cur_sess--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2502 | if (!(req->flags & BF_WRITE_STATUS)) |
| 2503 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2504 | else |
| 2505 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2506 | |
| 2507 | /* ensure that we have enough retries left */ |
| 2508 | if (srv_count_retry_down(t, conn_err)) |
| 2509 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2510 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2511 | if (req->flags & BF_WRITE_ERROR) { |
| 2512 | /* we encountered an immediate connection error, and we |
| 2513 | * will have to retry connecting to the same server, most |
| 2514 | * likely leading to the same result. To avoid this, we |
| 2515 | * fake a connection timeout to retry after a turn-around |
| 2516 | * time of 1 second. We will wait in the previous if block. |
| 2517 | */ |
| 2518 | t->flags |= SN_CONN_TAR; |
| 2519 | tv_ms_add(&req->cex, &now, 1000); |
| 2520 | return 0; |
| 2521 | } |
| 2522 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2523 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2524 | 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] | 2525 | /* We're on our last chance, and the REDISP option was specified. |
| 2526 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2527 | */ |
| 2528 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2529 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2530 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2531 | |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 2532 | if (t->srv) { |
Willy Tarreau | 98937b8 | 2007-12-10 15:05:42 +0100 | [diff] [blame] | 2533 | t->srv->cum_sess++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2534 | t->srv->failed_conns++; |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 2535 | t->srv->redispatches++; |
| 2536 | } |
Krzysztof Oledzki | 1cf36ba | 2007-10-18 19:12:30 +0200 | [diff] [blame] | 2537 | t->be->redispatches++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2538 | |
| 2539 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 2540 | t->flags |= SN_REDISP; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2541 | 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] | 2542 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2543 | |
| 2544 | /* first, get a connection */ |
| 2545 | if (srv_redispatch_connect(t)) |
Willy Tarreau | 00559e7 | 2008-01-06 23:46:19 +0100 | [diff] [blame] | 2546 | return t->srv_state != SV_STCONN; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2547 | } |
| 2548 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2549 | do { |
| 2550 | /* Now we will try to either reconnect to the same server or |
| 2551 | * connect to another server. If the connection gets queued |
| 2552 | * because all servers are saturated, then we will go back to |
| 2553 | * the SV_STIDLE state. |
| 2554 | */ |
| 2555 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2556 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2557 | return t->srv_state != SV_STCONN; |
| 2558 | } |
| 2559 | |
| 2560 | /* we need to redispatch the connection to another server */ |
| 2561 | if (srv_redispatch_connect(t)) |
| 2562 | return t->srv_state != SV_STCONN; |
| 2563 | } while (1); |
| 2564 | } |
| 2565 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2566 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2567 | |
| 2568 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2569 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2570 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2571 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2572 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2573 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2574 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2575 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2576 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2577 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2578 | } |
| 2579 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2580 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2583 | 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] | 2584 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2585 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2586 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2587 | |
| 2588 | t->srv_state = SV_STDATA; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2589 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2590 | |
| 2591 | /* if the user wants to log as soon as possible, without counting |
| 2592 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2593 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2594 | 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] | 2595 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2596 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2597 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2598 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2599 | /* TCP splicing supported by both FE and BE */ |
| 2600 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2601 | } |
| 2602 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2603 | } |
| 2604 | else { |
| 2605 | t->srv_state = SV_STHEADERS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2606 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2607 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2608 | /* reset hdr_idx which was already initialized by the request. |
| 2609 | * right now, the http parser does it. |
| 2610 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2611 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2612 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2613 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2614 | return 1; |
| 2615 | } |
| 2616 | } |
| 2617 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2618 | /* |
| 2619 | * Now parse the partial (or complete) lines. |
| 2620 | * We will check the response syntax, and also join multi-line |
| 2621 | * headers. An index of all the lines will be elaborated while |
| 2622 | * parsing. |
| 2623 | * |
| 2624 | * For the parsing, we use a 28 states FSM. |
| 2625 | * |
| 2626 | * Here is the information we currently have : |
| 2627 | * rep->data + req->som = beginning of response |
| 2628 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2629 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2630 | * rep->lr = first non-visited byte |
| 2631 | * rep->r = end of data |
| 2632 | */ |
| 2633 | |
| 2634 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2635 | struct http_msg *msg = &txn->rsp; |
| 2636 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2638 | if (likely(rep->lr < rep->r)) |
| 2639 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2640 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2641 | /* 1: we might have to print this header in debug mode */ |
| 2642 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2643 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2644 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2645 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2646 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2647 | sol = rep->data + msg->som; |
| 2648 | eol = sol + msg->sl.rq.l; |
| 2649 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2650 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2651 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2652 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2653 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2654 | while (cur_idx) { |
| 2655 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2656 | debug_hdr("srvhdr", t, sol, eol); |
| 2657 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2658 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2659 | } |
| 2660 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2663 | 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] | 2664 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2665 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2666 | * rep->l == rlim-data |
| 2667 | */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2668 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2669 | tv_eternity(&rep->rex); |
| 2670 | } |
| 2671 | |
| 2672 | |
| 2673 | /* |
| 2674 | * Now we quickly check if we have found a full valid response. |
| 2675 | * If not so, we check the FD and buffer states before leaving. |
| 2676 | * A full response is indicated by the fact that we have seen |
| 2677 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2678 | * responses are checked first. |
| 2679 | * |
| 2680 | * Depending on whether the client is still there or not, we |
| 2681 | * may send an error response back or not. Note that normally |
| 2682 | * we should only check for HTTP status there, and check I/O |
| 2683 | * errors somewhere else. |
| 2684 | */ |
| 2685 | |
| 2686 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2687 | |
| 2688 | /* Invalid response, or read error or write error */ |
| 2689 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2690 | (req->flags & BF_WRITE_ERROR) || |
| 2691 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2692 | buffer_shutr(rep); |
| 2693 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2694 | fd_delete(t->srv_fd); |
| 2695 | if (t->srv) { |
| 2696 | t->srv->cur_sess--; |
| 2697 | t->srv->failed_resp++; |
| 2698 | } |
| 2699 | t->be->failed_resp++; |
| 2700 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2701 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2702 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2703 | if (!(t->flags & SN_ERR_MASK)) |
| 2704 | t->flags |= SN_ERR_SRVCL; |
| 2705 | if (!(t->flags & SN_FINST_MASK)) |
| 2706 | t->flags |= SN_FINST_H; |
| 2707 | /* We used to have a free connection slot. Since we'll never use it, |
| 2708 | * we have to inform the server that it may be used by another session. |
| 2709 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2710 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2711 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2712 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2713 | return 1; |
| 2714 | } |
| 2715 | |
| 2716 | /* end of client write or end of server read. |
| 2717 | * since we are in header mode, if there's no space left for headers, we |
| 2718 | * won't be able to free more later, so the session will never terminate. |
| 2719 | */ |
| 2720 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2721 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2722 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2723 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2724 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2725 | t->srv_state = SV_STSHUTR; |
| 2726 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2727 | return 1; |
| 2728 | } |
| 2729 | |
| 2730 | /* read timeout : return a 504 to the client. |
| 2731 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2732 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2733 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2734 | buffer_shutr(rep); |
| 2735 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2736 | fd_delete(t->srv_fd); |
| 2737 | if (t->srv) { |
| 2738 | t->srv->cur_sess--; |
| 2739 | t->srv->failed_resp++; |
| 2740 | } |
| 2741 | t->be->failed_resp++; |
| 2742 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2743 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2744 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2745 | if (!(t->flags & SN_ERR_MASK)) |
| 2746 | t->flags |= SN_ERR_SRVTO; |
| 2747 | if (!(t->flags & SN_FINST_MASK)) |
| 2748 | t->flags |= SN_FINST_H; |
| 2749 | /* We used to have a free connection slot. Since we'll never use it, |
| 2750 | * we have to inform the server that it may be used by another session. |
| 2751 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2752 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2753 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2754 | return 1; |
| 2755 | } |
| 2756 | |
| 2757 | /* last client read and buffer empty */ |
| 2758 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2759 | * client shuts read too early, because we may still have |
| 2760 | * some work to do on the headers. |
| 2761 | * The side-effect is that if the client completely closes its |
| 2762 | * connection during SV_STHEADER, the connection to the server |
| 2763 | * is kept until a response comes back or the timeout is reached. |
| 2764 | */ |
| 2765 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2766 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2767 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2768 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2769 | |
| 2770 | /* We must ensure that the read part is still |
| 2771 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2772 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2773 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2774 | |
| 2775 | shutdown(t->srv_fd, SHUT_WR); |
| 2776 | t->srv_state = SV_STSHUTW; |
| 2777 | return 1; |
| 2778 | } |
| 2779 | |
| 2780 | /* write timeout */ |
| 2781 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2782 | * client shuts read too early, because we may still have |
| 2783 | * some work to do on the headers. |
| 2784 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2785 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2786 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2787 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2788 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2789 | shutdown(t->srv_fd, SHUT_WR); |
| 2790 | /* We must ensure that the read part is still alive |
| 2791 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2792 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2793 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2794 | |
| 2795 | t->srv_state = SV_STSHUTW; |
| 2796 | if (!(t->flags & SN_ERR_MASK)) |
| 2797 | t->flags |= SN_ERR_SRVTO; |
| 2798 | if (!(t->flags & SN_FINST_MASK)) |
| 2799 | t->flags |= SN_FINST_H; |
| 2800 | return 1; |
| 2801 | } |
| 2802 | |
| 2803 | /* |
| 2804 | * And now the non-error cases. |
| 2805 | */ |
| 2806 | |
| 2807 | /* Data remaining in the request buffer. |
| 2808 | * This happens during the first pass here, and during |
| 2809 | * long posts. |
| 2810 | */ |
| 2811 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2812 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2813 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2814 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2815 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2816 | * we refresh it. */ |
| 2817 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2818 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2819 | else |
| 2820 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2821 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2822 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2823 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2824 | /* nothing left in the request buffer */ |
| 2825 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2826 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2827 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2828 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | return t->srv_state != SV_STHEADERS; |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | /***************************************************************** |
| 2837 | * More interesting part now : we know that we have a complete * |
| 2838 | * response which at least looks like HTTP. We have an indicator * |
| 2839 | * of each header's length, so we can parse them quickly. * |
| 2840 | ****************************************************************/ |
| 2841 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2842 | /* ensure we keep this pointer to the beginning of the message */ |
| 2843 | msg->sol = rep->data + msg->som; |
| 2844 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2845 | /* |
| 2846 | * 1: get the status code and check for cacheability. |
| 2847 | */ |
| 2848 | |
| 2849 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2850 | 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] | 2851 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2852 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2853 | case 200: |
| 2854 | case 203: |
| 2855 | case 206: |
| 2856 | case 300: |
| 2857 | case 301: |
| 2858 | case 410: |
| 2859 | /* RFC2616 @13.4: |
| 2860 | * "A response received with a status code of |
| 2861 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2862 | * by a cache (...) unless a cache-control |
| 2863 | * directive prohibits caching." |
| 2864 | * |
| 2865 | * RFC2616 @9.5: POST method : |
| 2866 | * "Responses to this method are not cacheable, |
| 2867 | * unless the response includes appropriate |
| 2868 | * Cache-Control or Expires header fields." |
| 2869 | */ |
| 2870 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 2871 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2872 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2873 | break; |
| 2874 | default: |
| 2875 | break; |
| 2876 | } |
| 2877 | |
| 2878 | /* |
| 2879 | * 2: we may need to capture headers |
| 2880 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2881 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2882 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2883 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2884 | |
| 2885 | /* |
| 2886 | * 3: we will have to evaluate the filters. |
| 2887 | * As opposed to version 1.2, now they will be evaluated in the |
| 2888 | * filters order and not in the header order. This means that |
| 2889 | * each filter has to be validated among all headers. |
| 2890 | * |
| 2891 | * Filters are tried with ->be first, then with ->fe if it is |
| 2892 | * different from ->be. |
| 2893 | */ |
| 2894 | |
| 2895 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2896 | |
| 2897 | cur_proxy = t->be; |
| 2898 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2899 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2900 | |
| 2901 | /* try headers filters */ |
| 2902 | if (rule_set->rsp_exp != NULL) { |
| 2903 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2904 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2905 | if (t->srv) { |
| 2906 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2907 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2908 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2909 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2910 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2911 | buffer_shutr(rep); |
| 2912 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2913 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2914 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2915 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2916 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2917 | if (!(t->flags & SN_ERR_MASK)) |
| 2918 | t->flags |= SN_ERR_PRXCOND; |
| 2919 | if (!(t->flags & SN_FINST_MASK)) |
| 2920 | t->flags |= SN_FINST_H; |
| 2921 | /* We used to have a free connection slot. Since we'll never use it, |
| 2922 | * we have to inform the server that it may be used by another session. |
| 2923 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2924 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2925 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2926 | return 1; |
| 2927 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2928 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2929 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2930 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2931 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2932 | if (t->srv) { |
| 2933 | t->srv->cur_sess--; |
| 2934 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2935 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2936 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2937 | goto return_srv_prx_502; |
| 2938 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2939 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2940 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2941 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2942 | !(t->flags & SN_CONN_CLOSED)) { |
| 2943 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2944 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2945 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2946 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2947 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2948 | old_idx = 0; |
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 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2951 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2952 | cur_ptr = cur_next; |
| 2953 | cur_end = cur_ptr + cur_hdr->len; |
| 2954 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2956 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2957 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2958 | /* 3 possibilities : |
| 2959 | * - we have already set Connection: close, |
| 2960 | * so we remove this line. |
| 2961 | * - we have not yet set Connection: close, |
| 2962 | * but this line indicates close. We leave |
| 2963 | * it untouched and set the flag. |
| 2964 | * - we have not yet set Connection: close, |
| 2965 | * and this line indicates non-close. We |
| 2966 | * replace it. |
| 2967 | */ |
| 2968 | if (t->flags & SN_CONN_CLOSED) { |
| 2969 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2970 | txn->rsp.eoh += delta; |
| 2971 | cur_next += delta; |
| 2972 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2973 | txn->hdr_idx.used--; |
| 2974 | cur_hdr->len = 0; |
| 2975 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2976 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2977 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2978 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2979 | cur_next += delta; |
| 2980 | cur_hdr->len += delta; |
| 2981 | txn->rsp.eoh += delta; |
| 2982 | } |
| 2983 | t->flags |= SN_CONN_CLOSED; |
| 2984 | } |
| 2985 | } |
| 2986 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2987 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2988 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2989 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2990 | /* add response headers from the rule sets in the same order */ |
| 2991 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2992 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2993 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2994 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2995 | } |
| 2996 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2997 | /* check whether we're already working on the frontend */ |
| 2998 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2999 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3000 | cur_proxy = t->fe; |
| 3001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3002 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3003 | /* |
| 3004 | * 4: check for server cookie. |
| 3005 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3006 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3007 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3008 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3009 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3010 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3011 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3012 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3013 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3014 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3015 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3016 | |
| 3017 | /* |
| 3018 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3019 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3020 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3021 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3022 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3024 | /* the server is known, it's not the one the client requested, we have to |
| 3025 | * insert a set-cookie here, except if we want to insert only on POST |
| 3026 | * requests and this one isn't. Note that servers which don't have cookies |
| 3027 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3028 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3029 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3030 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3031 | 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] | 3032 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3033 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3034 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3035 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3036 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3037 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3038 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3039 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3040 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3041 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3042 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3043 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3044 | |
| 3045 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3046 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3047 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3048 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3049 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3050 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | |
| 3054 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3055 | * 7: check if result will be cacheable with a cookie. |
| 3056 | * We'll block the response if security checks have caught |
| 3057 | * nasty things such as a cacheable cookie. |
| 3058 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3059 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3060 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3061 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3062 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3063 | /* we're in presence of a cacheable response containing |
| 3064 | * a set-cookie header. We'll block it as requested by |
| 3065 | * the 'checkcache' option, and send an alert. |
| 3066 | */ |
| 3067 | if (t->srv) { |
| 3068 | t->srv->cur_sess--; |
| 3069 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3070 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3071 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3073 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3074 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3075 | send_log(t->be, LOG_ALERT, |
| 3076 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3077 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3078 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3081 | /* |
| 3082 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3083 | * 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] | 3084 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3085 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3086 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3087 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3088 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3089 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3090 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3091 | goto return_bad_resp; |
| 3092 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3094 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3095 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3096 | /************************************************************* |
| 3097 | * OK, that's finished for the headers. We have done what we * |
| 3098 | * could. Let's switch to the DATA state. * |
| 3099 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3100 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3101 | t->srv_state = SV_STDATA; |
| 3102 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3103 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3104 | |
| 3105 | /* client connection already closed or option 'forceclose' required : |
| 3106 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3107 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3108 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3109 | (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] | 3110 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3111 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3112 | |
| 3113 | /* We must ensure that the read part is still alive when switching |
| 3114 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3115 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3116 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3118 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3119 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3122 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3123 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3124 | /* TCP splicing supported by both FE and BE */ |
| 3125 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3126 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3127 | #endif |
| 3128 | /* if the user wants to log as soon as possible, without counting |
| 3129 | bytes from the server, then this is the right moment. */ |
| 3130 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3131 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame^] | 3132 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3133 | if (t->fe->to_log & LW_REQ) |
| 3134 | http_sess_log(t); |
| 3135 | else |
| 3136 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3137 | } |
| 3138 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3139 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3140 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3141 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3142 | |
| 3143 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3144 | } |
| 3145 | else if (s == SV_STDATA) { |
| 3146 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3147 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3148 | buffer_shutr(rep); |
| 3149 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3150 | fd_delete(t->srv_fd); |
| 3151 | if (t->srv) { |
| 3152 | t->srv->cur_sess--; |
| 3153 | t->srv->failed_resp++; |
| 3154 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3155 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3156 | t->srv_state = SV_STCLOSE; |
| 3157 | if (!(t->flags & SN_ERR_MASK)) |
| 3158 | t->flags |= SN_ERR_SRVCL; |
| 3159 | if (!(t->flags & SN_FINST_MASK)) |
| 3160 | t->flags |= SN_FINST_D; |
| 3161 | /* We used to have a free connection slot. Since we'll never use it, |
| 3162 | * we have to inform the server that it may be used by another session. |
| 3163 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3164 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3165 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3166 | |
| 3167 | return 1; |
| 3168 | } |
| 3169 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3170 | 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] | 3171 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3172 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3173 | t->srv_state = SV_STSHUTR; |
| 3174 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3175 | return 1; |
| 3176 | } |
| 3177 | /* end of client read and no more data to send */ |
| 3178 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3179 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3180 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3181 | shutdown(t->srv_fd, SHUT_WR); |
| 3182 | /* We must ensure that the read part is still alive when switching |
| 3183 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3184 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3185 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3186 | |
| 3187 | t->srv_state = SV_STSHUTW; |
| 3188 | return 1; |
| 3189 | } |
| 3190 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3191 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3192 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3193 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3194 | t->srv_state = SV_STSHUTR; |
| 3195 | if (!(t->flags & SN_ERR_MASK)) |
| 3196 | t->flags |= SN_ERR_SRVTO; |
| 3197 | if (!(t->flags & SN_FINST_MASK)) |
| 3198 | t->flags |= SN_FINST_D; |
| 3199 | return 1; |
| 3200 | } |
| 3201 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3202 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3203 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3204 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3205 | shutdown(t->srv_fd, SHUT_WR); |
| 3206 | /* We must ensure that the read part is still alive when switching |
| 3207 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3208 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3209 | tv_add_ifset(&rep->rex, &now, &t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3210 | t->srv_state = SV_STSHUTW; |
| 3211 | if (!(t->flags & SN_ERR_MASK)) |
| 3212 | t->flags |= SN_ERR_SRVTO; |
| 3213 | if (!(t->flags & SN_FINST_MASK)) |
| 3214 | t->flags |= SN_FINST_D; |
| 3215 | return 1; |
| 3216 | } |
| 3217 | |
| 3218 | /* recompute request time-outs */ |
| 3219 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3220 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3221 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3222 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3223 | } |
| 3224 | } |
| 3225 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3226 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3227 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3228 | if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3229 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3230 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3231 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3232 | } |
| 3233 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3234 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | /* recompute response time-outs */ |
| 3239 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3240 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3241 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3245 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3246 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3247 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | return 0; /* other cases change nothing */ |
| 3252 | } |
| 3253 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3254 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3255 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3256 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3257 | fd_delete(t->srv_fd); |
| 3258 | if (t->srv) { |
| 3259 | t->srv->cur_sess--; |
| 3260 | t->srv->failed_resp++; |
| 3261 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3262 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3263 | //close(t->srv_fd); |
| 3264 | t->srv_state = SV_STCLOSE; |
| 3265 | if (!(t->flags & SN_ERR_MASK)) |
| 3266 | t->flags |= SN_ERR_SRVCL; |
| 3267 | if (!(t->flags & SN_FINST_MASK)) |
| 3268 | t->flags |= SN_FINST_D; |
| 3269 | /* We used to have a free connection slot. Since we'll never use it, |
| 3270 | * we have to inform the server that it may be used by another session. |
| 3271 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3272 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3273 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3274 | |
| 3275 | return 1; |
| 3276 | } |
| 3277 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3278 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3279 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3280 | fd_delete(t->srv_fd); |
| 3281 | if (t->srv) |
| 3282 | t->srv->cur_sess--; |
| 3283 | //close(t->srv_fd); |
| 3284 | t->srv_state = SV_STCLOSE; |
| 3285 | /* We used to have a free connection slot. Since we'll never use it, |
| 3286 | * we have to inform the server that it may be used by another session. |
| 3287 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3288 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3289 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3290 | |
| 3291 | return 1; |
| 3292 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3293 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3294 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3295 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3296 | fd_delete(t->srv_fd); |
| 3297 | if (t->srv) |
| 3298 | t->srv->cur_sess--; |
| 3299 | //close(t->srv_fd); |
| 3300 | t->srv_state = SV_STCLOSE; |
| 3301 | if (!(t->flags & SN_ERR_MASK)) |
| 3302 | t->flags |= SN_ERR_SRVTO; |
| 3303 | if (!(t->flags & SN_FINST_MASK)) |
| 3304 | t->flags |= SN_FINST_D; |
| 3305 | /* We used to have a free connection slot. Since we'll never use it, |
| 3306 | * we have to inform the server that it may be used by another session. |
| 3307 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3308 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3309 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3310 | |
| 3311 | return 1; |
| 3312 | } |
| 3313 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3314 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3315 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3316 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3320 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3321 | /* restart writing */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3322 | if (!tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3323 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | return 0; |
| 3327 | } |
| 3328 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3329 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3330 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3331 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3332 | fd_delete(t->srv_fd); |
| 3333 | if (t->srv) { |
| 3334 | t->srv->cur_sess--; |
| 3335 | t->srv->failed_resp++; |
| 3336 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3337 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3338 | //close(t->srv_fd); |
| 3339 | t->srv_state = SV_STCLOSE; |
| 3340 | if (!(t->flags & SN_ERR_MASK)) |
| 3341 | t->flags |= SN_ERR_SRVCL; |
| 3342 | if (!(t->flags & SN_FINST_MASK)) |
| 3343 | t->flags |= SN_FINST_D; |
| 3344 | /* We used to have a free connection slot. Since we'll never use it, |
| 3345 | * we have to inform the server that it may be used by another session. |
| 3346 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3347 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3348 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3349 | |
| 3350 | return 1; |
| 3351 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3352 | 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] | 3353 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3354 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3355 | fd_delete(t->srv_fd); |
| 3356 | if (t->srv) |
| 3357 | t->srv->cur_sess--; |
| 3358 | //close(t->srv_fd); |
| 3359 | t->srv_state = SV_STCLOSE; |
| 3360 | /* We used to have a free connection slot. Since we'll never use it, |
| 3361 | * we have to inform the server that it may be used by another session. |
| 3362 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3363 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3364 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3365 | |
| 3366 | return 1; |
| 3367 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3368 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3369 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3370 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3371 | fd_delete(t->srv_fd); |
| 3372 | if (t->srv) |
| 3373 | t->srv->cur_sess--; |
| 3374 | //close(t->srv_fd); |
| 3375 | t->srv_state = SV_STCLOSE; |
| 3376 | if (!(t->flags & SN_ERR_MASK)) |
| 3377 | t->flags |= SN_ERR_SRVTO; |
| 3378 | if (!(t->flags & SN_FINST_MASK)) |
| 3379 | t->flags |= SN_FINST_D; |
| 3380 | /* We used to have a free connection slot. Since we'll never use it, |
| 3381 | * we have to inform the server that it may be used by another session. |
| 3382 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3383 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3384 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3385 | |
| 3386 | return 1; |
| 3387 | } |
| 3388 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3389 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3390 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3391 | } |
| 3392 | } |
| 3393 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3394 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3395 | if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3396 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | return 0; |
| 3400 | } |
| 3401 | else { /* SV_STCLOSE : nothing to do */ |
| 3402 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3403 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3404 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3405 | 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] | 3406 | write(1, trash, len); |
| 3407 | } |
| 3408 | return 0; |
| 3409 | } |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
| 3413 | |
| 3414 | /* |
| 3415 | * Produces data for the session <s> depending on its source. Expects to be |
| 3416 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3417 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3418 | * session, which it uses to keep on being called when there is free space in |
| 3419 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3420 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3421 | */ |
| 3422 | int produce_content(struct session *s) |
| 3423 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3424 | if (s->data_source == DATA_SRC_NONE) { |
| 3425 | s->flags &= ~SN_SELF_GEN; |
| 3426 | return 1; |
| 3427 | } |
| 3428 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3429 | /* dump server statistics */ |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 3430 | int ret = stats_dump_http(s, s->be->uri_auth, |
| 3431 | (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3432 | if (ret >= 0) |
| 3433 | return ret; |
| 3434 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3435 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3436 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3437 | /* unknown data source or internal error */ |
| 3438 | s->txn.status = 500; |
| 3439 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3440 | if (!(s->flags & SN_ERR_MASK)) |
| 3441 | s->flags |= SN_ERR_PRXCOND; |
| 3442 | if (!(s->flags & SN_FINST_MASK)) |
| 3443 | s->flags |= SN_FINST_R; |
| 3444 | s->flags &= ~SN_SELF_GEN; |
| 3445 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3449 | /* Iterate the same filter through all request headers. |
| 3450 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3451 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3452 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3453 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3454 | 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] | 3455 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3456 | char term; |
| 3457 | char *cur_ptr, *cur_end, *cur_next; |
| 3458 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3459 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3460 | struct hdr_idx_elem *cur_hdr; |
| 3461 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3463 | last_hdr = 0; |
| 3464 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3465 | 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] | 3466 | old_idx = 0; |
| 3467 | |
| 3468 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3469 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3470 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3471 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3472 | (exp->action == ACT_ALLOW || |
| 3473 | exp->action == ACT_DENY || |
| 3474 | exp->action == ACT_TARPIT)) |
| 3475 | return 0; |
| 3476 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3477 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3478 | if (!cur_idx) |
| 3479 | break; |
| 3480 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3481 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3482 | cur_ptr = cur_next; |
| 3483 | cur_end = cur_ptr + cur_hdr->len; |
| 3484 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3485 | |
| 3486 | /* Now we have one header between cur_ptr and cur_end, |
| 3487 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3488 | */ |
| 3489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3490 | /* The annoying part is that pattern matching needs |
| 3491 | * that we modify the contents to null-terminate all |
| 3492 | * strings before testing them. |
| 3493 | */ |
| 3494 | |
| 3495 | term = *cur_end; |
| 3496 | *cur_end = '\0'; |
| 3497 | |
| 3498 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3499 | switch (exp->action) { |
| 3500 | case ACT_SETBE: |
| 3501 | /* It is not possible to jump a second time. |
| 3502 | * FIXME: should we return an HTTP/500 here so that |
| 3503 | * the admin knows there's a problem ? |
| 3504 | */ |
| 3505 | if (t->be != t->fe) |
| 3506 | break; |
| 3507 | |
| 3508 | /* Swithing Proxy */ |
| 3509 | t->be = (struct proxy *) exp->replace; |
| 3510 | |
| 3511 | /* right now, the backend switch is not too much complicated |
| 3512 | * because we have associated req_cap and rsp_cap to the |
| 3513 | * frontend, and the beconn will be updated later. |
| 3514 | */ |
| 3515 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3516 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3517 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3518 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3519 | last_hdr = 1; |
| 3520 | break; |
| 3521 | |
| 3522 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3523 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3524 | last_hdr = 1; |
| 3525 | break; |
| 3526 | |
| 3527 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3528 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3529 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3530 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3531 | break; |
| 3532 | |
| 3533 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3534 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3535 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3536 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3537 | break; |
| 3538 | |
| 3539 | case ACT_REPLACE: |
| 3540 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3541 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3542 | /* FIXME: if the user adds a newline in the replacement, the |
| 3543 | * index will not be recalculated for now, and the new line |
| 3544 | * will not be counted as a new header. |
| 3545 | */ |
| 3546 | |
| 3547 | cur_end += delta; |
| 3548 | cur_next += delta; |
| 3549 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3550 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3551 | break; |
| 3552 | |
| 3553 | case ACT_REMOVE: |
| 3554 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3555 | cur_next += delta; |
| 3556 | |
| 3557 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3558 | txn->req.eoh += delta; |
| 3559 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3560 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3561 | cur_hdr->len = 0; |
| 3562 | cur_end = NULL; /* null-term has been rewritten */ |
| 3563 | break; |
| 3564 | |
| 3565 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3566 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3567 | if (cur_end) |
| 3568 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3570 | /* keep the link from this header to next one in case of later |
| 3571 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3572 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3573 | old_idx = cur_idx; |
| 3574 | } |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | |
| 3579 | /* Apply the filter to the request line. |
| 3580 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3581 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3582 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3583 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3584 | */ |
| 3585 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3586 | { |
| 3587 | char term; |
| 3588 | char *cur_ptr, *cur_end; |
| 3589 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3590 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3591 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3592 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3593 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3594 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3595 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3596 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3597 | (exp->action == ACT_ALLOW || |
| 3598 | exp->action == ACT_DENY || |
| 3599 | exp->action == ACT_TARPIT)) |
| 3600 | return 0; |
| 3601 | else if (exp->action == ACT_REMOVE) |
| 3602 | return 0; |
| 3603 | |
| 3604 | done = 0; |
| 3605 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3606 | 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] | 3607 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3608 | |
| 3609 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3610 | |
| 3611 | /* The annoying part is that pattern matching needs |
| 3612 | * that we modify the contents to null-terminate all |
| 3613 | * strings before testing them. |
| 3614 | */ |
| 3615 | |
| 3616 | term = *cur_end; |
| 3617 | *cur_end = '\0'; |
| 3618 | |
| 3619 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3620 | switch (exp->action) { |
| 3621 | case ACT_SETBE: |
| 3622 | /* It is not possible to jump a second time. |
| 3623 | * FIXME: should we return an HTTP/500 here so that |
| 3624 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3625 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3626 | if (t->be != t->fe) |
| 3627 | break; |
| 3628 | |
| 3629 | /* Swithing Proxy */ |
| 3630 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3632 | /* right now, the backend switch is not too much complicated |
| 3633 | * because we have associated req_cap and rsp_cap to the |
| 3634 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3635 | */ |
| 3636 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3637 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3638 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3639 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3640 | done = 1; |
| 3641 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3643 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3644 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3645 | done = 1; |
| 3646 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3647 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3648 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3649 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3650 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3651 | done = 1; |
| 3652 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3653 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3654 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3655 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3656 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3657 | done = 1; |
| 3658 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3659 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3660 | case ACT_REPLACE: |
| 3661 | *cur_end = term; /* restore the string terminator */ |
| 3662 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3663 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3664 | /* FIXME: if the user adds a newline in the replacement, the |
| 3665 | * index will not be recalculated for now, and the new line |
| 3666 | * will not be counted as a new header. |
| 3667 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3668 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3669 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3670 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3671 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3672 | 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] | 3673 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3674 | HTTP_MSG_RQMETH, |
| 3675 | cur_ptr, cur_end + 1, |
| 3676 | NULL, NULL); |
| 3677 | if (unlikely(!cur_end)) |
| 3678 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3679 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3680 | /* we have a full request and we know that we have either a CR |
| 3681 | * or an LF at <ptr>. |
| 3682 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3683 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3684 | 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] | 3685 | /* there is no point trying this regex on headers */ |
| 3686 | return 1; |
| 3687 | } |
| 3688 | } |
| 3689 | *cur_end = term; /* restore the string terminator */ |
| 3690 | return done; |
| 3691 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3692 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3693 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3694 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3695 | /* |
| 3696 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3697 | * 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] | 3698 | * unparsable request. Since it can manage the switch to another backend, it |
| 3699 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3700 | */ |
| 3701 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3702 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3703 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3704 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3705 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3706 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3707 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3708 | /* |
| 3709 | * The interleaving of transformations and verdicts |
| 3710 | * makes it difficult to decide to continue or stop |
| 3711 | * the evaluation. |
| 3712 | */ |
| 3713 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3714 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3715 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3716 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3717 | exp = exp->next; |
| 3718 | continue; |
| 3719 | } |
| 3720 | |
| 3721 | /* Apply the filter to the request line. */ |
| 3722 | ret = apply_filter_to_req_line(t, req, exp); |
| 3723 | if (unlikely(ret < 0)) |
| 3724 | return -1; |
| 3725 | |
| 3726 | if (likely(ret == 0)) { |
| 3727 | /* The filter did not match the request, it can be |
| 3728 | * iterated through all headers. |
| 3729 | */ |
| 3730 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3731 | } |
| 3732 | exp = exp->next; |
| 3733 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3734 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3738 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3739 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3740 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3741 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3742 | */ |
| 3743 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3744 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3745 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3746 | char *p1, *p2, *p3, *p4; |
| 3747 | char *del_colon, *del_cookie, *colon; |
| 3748 | int app_cookies; |
| 3749 | |
| 3750 | appsess *asession_temp = NULL; |
| 3751 | appsess local_asession; |
| 3752 | |
| 3753 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3754 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3755 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3756 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3757 | * we start with the start line. |
| 3758 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3759 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3760 | 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] | 3761 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3762 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3763 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3764 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3765 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3766 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3767 | cur_ptr = cur_next; |
| 3768 | cur_end = cur_ptr + cur_hdr->len; |
| 3769 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3770 | |
| 3771 | /* We have one full header between cur_ptr and cur_end, and the |
| 3772 | * next header starts at cur_next. We're only interested in |
| 3773 | * "Cookie:" headers. |
| 3774 | */ |
| 3775 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3776 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3777 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3778 | old_idx = cur_idx; |
| 3779 | continue; |
| 3780 | } |
| 3781 | |
| 3782 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3783 | * attributes whose name begin with a '$', and associate them with |
| 3784 | * the right cookie, if we want to delete this cookie. |
| 3785 | * So there are 3 cases for each cookie read : |
| 3786 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3787 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3788 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3789 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3790 | * "special" cookie. |
| 3791 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3792 | * remove it. If no application cookie persists in the header, we |
| 3793 | * *MUST* delete it |
| 3794 | */ |
| 3795 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3796 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3797 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3798 | /* del_cookie == NULL => nothing to be deleted */ |
| 3799 | del_colon = del_cookie = NULL; |
| 3800 | app_cookies = 0; |
| 3801 | |
| 3802 | while (p1 < cur_end) { |
| 3803 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3804 | while (p1 < cur_end) { |
| 3805 | if (*p1 == ';' || *p1 == ',') |
| 3806 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3807 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3808 | break; |
| 3809 | p1++; |
| 3810 | } |
| 3811 | |
| 3812 | if (p1 == cur_end) |
| 3813 | break; |
| 3814 | |
| 3815 | /* p1 is at the beginning of the cookie name */ |
| 3816 | p2 = p1; |
| 3817 | while (p2 < cur_end && *p2 != '=') |
| 3818 | p2++; |
| 3819 | |
| 3820 | if (p2 == cur_end) |
| 3821 | break; |
| 3822 | |
| 3823 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3824 | if (p3 == cur_end) |
| 3825 | break; |
| 3826 | |
| 3827 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3828 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3829 | p4++; |
| 3830 | |
| 3831 | /* here, we have the cookie name between p1 and p2, |
| 3832 | * and its value between p3 and p4. |
| 3833 | * we can process it : |
| 3834 | * |
| 3835 | * Cookie: NAME=VALUE; |
| 3836 | * | || || | |
| 3837 | * | || || +--> p4 |
| 3838 | * | || |+-------> p3 |
| 3839 | * | || +--------> p2 |
| 3840 | * | |+------------> p1 |
| 3841 | * | +-------------> colon |
| 3842 | * +--------------------> cur_ptr |
| 3843 | */ |
| 3844 | |
| 3845 | if (*p1 == '$') { |
| 3846 | /* skip this one */ |
| 3847 | } |
| 3848 | else { |
| 3849 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3850 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3851 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3852 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3853 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3854 | int log_len = p4 - p1; |
| 3855 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3856 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3857 | Alert("HTTP logging : out of memory.\n"); |
| 3858 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3859 | if (log_len > t->fe->capture_len) |
| 3860 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3861 | memcpy(txn->cli_cookie, p1, log_len); |
| 3862 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3863 | } |
| 3864 | } |
| 3865 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3866 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3867 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3868 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3869 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3870 | char *delim; |
| 3871 | |
| 3872 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3873 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3874 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3875 | * |
| 3876 | * Cookie: NAME=SRV~VALUE; |
| 3877 | * | || || | | |
| 3878 | * | || || | +--> p4 |
| 3879 | * | || || +--------> delim |
| 3880 | * | || |+-----------> p3 |
| 3881 | * | || +------------> p2 |
| 3882 | * | |+----------------> p1 |
| 3883 | * | +-----------------> colon |
| 3884 | * +------------------------> cur_ptr |
| 3885 | */ |
| 3886 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3887 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3888 | for (delim = p3; delim < p4; delim++) |
| 3889 | if (*delim == COOKIE_DELIM) |
| 3890 | break; |
| 3891 | } |
| 3892 | else |
| 3893 | delim = p4; |
| 3894 | |
| 3895 | |
| 3896 | /* Here, we'll look for the first running server which supports the cookie. |
| 3897 | * This allows to share a same cookie between several servers, for example |
| 3898 | * to dedicate backup servers to specific servers only. |
| 3899 | * However, to prevent clients from sticking to cookie-less backup server |
| 3900 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3901 | * empty cookies and mark them as invalid. |
| 3902 | */ |
| 3903 | if (delim == p3) |
| 3904 | srv = NULL; |
| 3905 | |
| 3906 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3907 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3908 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3909 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3910 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3911 | txn->flags &= ~TX_CK_MASK; |
| 3912 | txn->flags |= TX_CK_VALID; |
| 3913 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3914 | t->srv = srv; |
| 3915 | break; |
| 3916 | } else { |
| 3917 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3918 | txn->flags &= ~TX_CK_MASK; |
| 3919 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3920 | } |
| 3921 | } |
| 3922 | srv = srv->next; |
| 3923 | } |
| 3924 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3925 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3926 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3927 | txn->flags &= ~TX_CK_MASK; |
| 3928 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /* depending on the cookie mode, we may have to either : |
| 3932 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3933 | * the server never sees it ; |
| 3934 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3935 | * application cookie so that it does not get accidentely removed later, |
| 3936 | * if we're in cookie prefix mode |
| 3937 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3938 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3939 | int delta; /* negative */ |
| 3940 | |
| 3941 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3942 | p4 += delta; |
| 3943 | cur_end += delta; |
| 3944 | cur_next += delta; |
| 3945 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3946 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3947 | |
| 3948 | del_cookie = del_colon = NULL; |
| 3949 | app_cookies++; /* protect the header from deletion */ |
| 3950 | } |
| 3951 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3952 | (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] | 3953 | del_cookie = p1; |
| 3954 | del_colon = colon; |
| 3955 | } |
| 3956 | } else { |
| 3957 | /* now we know that we must keep this cookie since it's |
| 3958 | * not ours. But if we wanted to delete our cookie |
| 3959 | * earlier, we cannot remove the complete header, but we |
| 3960 | * can remove the previous block itself. |
| 3961 | */ |
| 3962 | app_cookies++; |
| 3963 | |
| 3964 | if (del_cookie != NULL) { |
| 3965 | int delta; /* negative */ |
| 3966 | |
| 3967 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3968 | p4 += delta; |
| 3969 | cur_end += delta; |
| 3970 | cur_next += delta; |
| 3971 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3972 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3973 | del_cookie = del_colon = NULL; |
| 3974 | } |
| 3975 | } |
| 3976 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3977 | if ((t->be->appsession_name != NULL) && |
| 3978 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3979 | /* first, let's see if the cookie is our appcookie*/ |
| 3980 | |
| 3981 | /* Cool... it's the right one */ |
| 3982 | |
| 3983 | asession_temp = &local_asession; |
| 3984 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3985 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3986 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3987 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3988 | return; |
| 3989 | } |
| 3990 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3991 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3992 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3993 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3994 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3995 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3996 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3997 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3998 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3999 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4000 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4001 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4002 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4003 | return; |
| 4004 | } |
| 4005 | |
| 4006 | asession_temp->sessid = local_asession.sessid; |
| 4007 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4008 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4009 | } else { |
| 4010 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4011 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4012 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4013 | if (asession_temp->serverid == NULL) { |
| 4014 | Alert("Found Application Session without matching server.\n"); |
| 4015 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4016 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4017 | while (srv) { |
| 4018 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4019 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4020 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4021 | txn->flags &= ~TX_CK_MASK; |
| 4022 | txn->flags |= TX_CK_VALID; |
| 4023 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4024 | t->srv = srv; |
| 4025 | break; |
| 4026 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4027 | txn->flags &= ~TX_CK_MASK; |
| 4028 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | srv = srv->next; |
| 4032 | }/* end while(srv) */ |
| 4033 | }/* end else if server == NULL */ |
| 4034 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4035 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4036 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4037 | } |
| 4038 | |
| 4039 | /* we'll have to look for another cookie ... */ |
| 4040 | p1 = p4; |
| 4041 | } /* while (p1 < cur_end) */ |
| 4042 | |
| 4043 | /* There's no more cookie on this line. |
| 4044 | * We may have marked the last one(s) for deletion. |
| 4045 | * We must do this now in two ways : |
| 4046 | * - if there is no app cookie, we simply delete the header ; |
| 4047 | * - if there are app cookies, we must delete the end of the |
| 4048 | * string properly, including the colon/semi-colon before |
| 4049 | * the cookie name. |
| 4050 | */ |
| 4051 | if (del_cookie != NULL) { |
| 4052 | int delta; |
| 4053 | if (app_cookies) { |
| 4054 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4055 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4056 | cur_hdr->len += delta; |
| 4057 | } else { |
| 4058 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4059 | |
| 4060 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4061 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4062 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4063 | cur_hdr->len = 0; |
| 4064 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4065 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4066 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* keep the link from this header to next one */ |
| 4070 | old_idx = cur_idx; |
| 4071 | } /* end of cookie processing on this header */ |
| 4072 | } |
| 4073 | |
| 4074 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4075 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4076 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4077 | */ |
| 4078 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4079 | { |
| 4080 | char term; |
| 4081 | char *cur_ptr, *cur_end, *cur_next; |
| 4082 | int cur_idx, old_idx, last_hdr; |
| 4083 | struct http_txn *txn = &t->txn; |
| 4084 | struct hdr_idx_elem *cur_hdr; |
| 4085 | int len, delta; |
| 4086 | |
| 4087 | last_hdr = 0; |
| 4088 | |
| 4089 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4090 | old_idx = 0; |
| 4091 | |
| 4092 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4093 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4094 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4095 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4096 | (exp->action == ACT_ALLOW || |
| 4097 | exp->action == ACT_DENY)) |
| 4098 | return 0; |
| 4099 | |
| 4100 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4101 | if (!cur_idx) |
| 4102 | break; |
| 4103 | |
| 4104 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4105 | cur_ptr = cur_next; |
| 4106 | cur_end = cur_ptr + cur_hdr->len; |
| 4107 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4108 | |
| 4109 | /* Now we have one header between cur_ptr and cur_end, |
| 4110 | * and the next header starts at cur_next. |
| 4111 | */ |
| 4112 | |
| 4113 | /* The annoying part is that pattern matching needs |
| 4114 | * that we modify the contents to null-terminate all |
| 4115 | * strings before testing them. |
| 4116 | */ |
| 4117 | |
| 4118 | term = *cur_end; |
| 4119 | *cur_end = '\0'; |
| 4120 | |
| 4121 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4122 | switch (exp->action) { |
| 4123 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4124 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4125 | last_hdr = 1; |
| 4126 | break; |
| 4127 | |
| 4128 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4129 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4130 | last_hdr = 1; |
| 4131 | break; |
| 4132 | |
| 4133 | case ACT_REPLACE: |
| 4134 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4135 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4136 | /* FIXME: if the user adds a newline in the replacement, the |
| 4137 | * index will not be recalculated for now, and the new line |
| 4138 | * will not be counted as a new header. |
| 4139 | */ |
| 4140 | |
| 4141 | cur_end += delta; |
| 4142 | cur_next += delta; |
| 4143 | cur_hdr->len += delta; |
| 4144 | txn->rsp.eoh += delta; |
| 4145 | break; |
| 4146 | |
| 4147 | case ACT_REMOVE: |
| 4148 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4149 | cur_next += delta; |
| 4150 | |
| 4151 | /* FIXME: this should be a separate function */ |
| 4152 | txn->rsp.eoh += delta; |
| 4153 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4154 | txn->hdr_idx.used--; |
| 4155 | cur_hdr->len = 0; |
| 4156 | cur_end = NULL; /* null-term has been rewritten */ |
| 4157 | break; |
| 4158 | |
| 4159 | } |
| 4160 | } |
| 4161 | if (cur_end) |
| 4162 | *cur_end = term; /* restore the string terminator */ |
| 4163 | |
| 4164 | /* keep the link from this header to next one in case of later |
| 4165 | * removal of next header. |
| 4166 | */ |
| 4167 | old_idx = cur_idx; |
| 4168 | } |
| 4169 | return 0; |
| 4170 | } |
| 4171 | |
| 4172 | |
| 4173 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4174 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4175 | * or -1 if a replacement resulted in an invalid status line. |
| 4176 | */ |
| 4177 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4178 | { |
| 4179 | char term; |
| 4180 | char *cur_ptr, *cur_end; |
| 4181 | int done; |
| 4182 | struct http_txn *txn = &t->txn; |
| 4183 | int len, delta; |
| 4184 | |
| 4185 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4186 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4187 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4188 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4189 | (exp->action == ACT_ALLOW || |
| 4190 | exp->action == ACT_DENY)) |
| 4191 | return 0; |
| 4192 | else if (exp->action == ACT_REMOVE) |
| 4193 | return 0; |
| 4194 | |
| 4195 | done = 0; |
| 4196 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4197 | 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] | 4198 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4199 | |
| 4200 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4201 | |
| 4202 | /* The annoying part is that pattern matching needs |
| 4203 | * that we modify the contents to null-terminate all |
| 4204 | * strings before testing them. |
| 4205 | */ |
| 4206 | |
| 4207 | term = *cur_end; |
| 4208 | *cur_end = '\0'; |
| 4209 | |
| 4210 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4211 | switch (exp->action) { |
| 4212 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4213 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4214 | done = 1; |
| 4215 | break; |
| 4216 | |
| 4217 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4218 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4219 | done = 1; |
| 4220 | break; |
| 4221 | |
| 4222 | case ACT_REPLACE: |
| 4223 | *cur_end = term; /* restore the string terminator */ |
| 4224 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4225 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4226 | /* FIXME: if the user adds a newline in the replacement, the |
| 4227 | * index will not be recalculated for now, and the new line |
| 4228 | * will not be counted as a new header. |
| 4229 | */ |
| 4230 | |
| 4231 | txn->rsp.eoh += delta; |
| 4232 | cur_end += delta; |
| 4233 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4234 | 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] | 4235 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4236 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4237 | cur_ptr, cur_end + 1, |
| 4238 | NULL, NULL); |
| 4239 | if (unlikely(!cur_end)) |
| 4240 | return -1; |
| 4241 | |
| 4242 | /* we have a full respnse and we know that we have either a CR |
| 4243 | * or an LF at <ptr>. |
| 4244 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4245 | 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] | 4246 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4247 | /* there is no point trying this regex on headers */ |
| 4248 | return 1; |
| 4249 | } |
| 4250 | } |
| 4251 | *cur_end = term; /* restore the string terminator */ |
| 4252 | return done; |
| 4253 | } |
| 4254 | |
| 4255 | |
| 4256 | |
| 4257 | /* |
| 4258 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4259 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4260 | * unparsable response. |
| 4261 | */ |
| 4262 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4263 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4264 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4265 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4266 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4267 | int ret; |
| 4268 | |
| 4269 | /* |
| 4270 | * The interleaving of transformations and verdicts |
| 4271 | * makes it difficult to decide to continue or stop |
| 4272 | * the evaluation. |
| 4273 | */ |
| 4274 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4275 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4276 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4277 | exp->action == ACT_PASS)) { |
| 4278 | exp = exp->next; |
| 4279 | continue; |
| 4280 | } |
| 4281 | |
| 4282 | /* Apply the filter to the status line. */ |
| 4283 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4284 | if (unlikely(ret < 0)) |
| 4285 | return -1; |
| 4286 | |
| 4287 | if (likely(ret == 0)) { |
| 4288 | /* The filter did not match the response, it can be |
| 4289 | * iterated through all headers. |
| 4290 | */ |
| 4291 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4292 | } |
| 4293 | exp = exp->next; |
| 4294 | } |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
| 4298 | |
| 4299 | |
| 4300 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4301 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4302 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4303 | */ |
| 4304 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4305 | { |
| 4306 | struct http_txn *txn = &t->txn; |
| 4307 | char *p1, *p2, *p3, *p4; |
| 4308 | |
| 4309 | appsess *asession_temp = NULL; |
| 4310 | appsess local_asession; |
| 4311 | |
| 4312 | char *cur_ptr, *cur_end, *cur_next; |
| 4313 | int cur_idx, old_idx, delta; |
| 4314 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4315 | /* Iterate through the headers. |
| 4316 | * we start with the start line. |
| 4317 | */ |
| 4318 | old_idx = 0; |
| 4319 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4320 | |
| 4321 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4322 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4323 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4324 | |
| 4325 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4326 | cur_ptr = cur_next; |
| 4327 | cur_end = cur_ptr + cur_hdr->len; |
| 4328 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4329 | |
| 4330 | /* We have one full header between cur_ptr and cur_end, and the |
| 4331 | * next header starts at cur_next. We're only interested in |
| 4332 | * "Cookie:" headers. |
| 4333 | */ |
| 4334 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4335 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4336 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4337 | old_idx = cur_idx; |
| 4338 | continue; |
| 4339 | } |
| 4340 | |
| 4341 | /* 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] | 4342 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4343 | |
| 4344 | |
| 4345 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4346 | if (t->be->cookie_name == NULL && |
| 4347 | t->be->appsession_name == NULL && |
| 4348 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4349 | return; |
| 4350 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4351 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4352 | |
| 4353 | 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] | 4354 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4355 | break; |
| 4356 | |
| 4357 | /* p1 is at the beginning of the cookie name */ |
| 4358 | p2 = p1; |
| 4359 | |
| 4360 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4361 | p2++; |
| 4362 | |
| 4363 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4364 | break; |
| 4365 | |
| 4366 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4367 | if (p3 == cur_end) |
| 4368 | break; |
| 4369 | |
| 4370 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4371 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4372 | p4++; |
| 4373 | |
| 4374 | /* here, we have the cookie name between p1 and p2, |
| 4375 | * and its value between p3 and p4. |
| 4376 | * we can process it. |
| 4377 | */ |
| 4378 | |
| 4379 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4380 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4381 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4382 | (p4 - p1 >= t->be->capture_namelen) && |
| 4383 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4384 | int log_len = p4 - p1; |
| 4385 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4386 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4387 | Alert("HTTP logging : out of memory.\n"); |
| 4388 | } |
| 4389 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4390 | if (log_len > t->be->capture_len) |
| 4391 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4392 | memcpy(txn->srv_cookie, p1, log_len); |
| 4393 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4397 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4398 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4399 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4400 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4401 | |
| 4402 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4403 | * this occurrence because we'll insert another one later. |
| 4404 | * We'll delete it too if the "indirect" option is set and we're in |
| 4405 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4406 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4407 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4408 | /* this header must be deleted */ |
| 4409 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4410 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4411 | txn->hdr_idx.used--; |
| 4412 | cur_hdr->len = 0; |
| 4413 | cur_next += delta; |
| 4414 | txn->rsp.eoh += delta; |
| 4415 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4416 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4417 | } |
| 4418 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4419 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4420 | /* replace bytes p3->p4 with the cookie name associated |
| 4421 | * with this server since we know it. |
| 4422 | */ |
| 4423 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4424 | cur_hdr->len += delta; |
| 4425 | cur_next += delta; |
| 4426 | txn->rsp.eoh += delta; |
| 4427 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4428 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4429 | } |
| 4430 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4431 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4432 | /* insert the cookie name associated with this server |
| 4433 | * before existing cookie, and insert a delimitor between them.. |
| 4434 | */ |
| 4435 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4436 | cur_hdr->len += delta; |
| 4437 | cur_next += delta; |
| 4438 | txn->rsp.eoh += delta; |
| 4439 | |
| 4440 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4441 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4445 | else if ((t->be->appsession_name != NULL) && |
| 4446 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4447 | |
| 4448 | /* Cool... it's the right one */ |
| 4449 | |
| 4450 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4451 | asession_temp = &local_asession; |
| 4452 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4453 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4454 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4455 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4456 | return; |
| 4457 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4458 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4459 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4460 | asession_temp->serverid = NULL; |
| 4461 | |
| 4462 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4463 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4464 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4465 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4466 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4467 | return; |
| 4468 | } |
| 4469 | asession_temp->sessid = local_asession.sessid; |
| 4470 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4471 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4472 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4473 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4474 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4475 | } |
| 4476 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4477 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4478 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4479 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4480 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4481 | return; |
| 4482 | } |
| 4483 | asession_temp->serverid[0] = '\0'; |
| 4484 | } |
| 4485 | |
| 4486 | if (asession_temp->serverid[0] == '\0') |
| 4487 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4488 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4489 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4490 | |
| 4491 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4492 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4493 | #endif |
| 4494 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4495 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4496 | } /* we're now at the end of the cookie value */ |
| 4497 | |
| 4498 | /* keep the link from this header to next one */ |
| 4499 | old_idx = cur_idx; |
| 4500 | } /* end of cookie processing on this header */ |
| 4501 | } |
| 4502 | |
| 4503 | |
| 4504 | |
| 4505 | /* |
| 4506 | * Check if response is cacheable or not. Updates t->flags. |
| 4507 | */ |
| 4508 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4509 | { |
| 4510 | struct http_txn *txn = &t->txn; |
| 4511 | char *p1, *p2; |
| 4512 | |
| 4513 | char *cur_ptr, *cur_end, *cur_next; |
| 4514 | int cur_idx; |
| 4515 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4516 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4517 | return; |
| 4518 | |
| 4519 | /* Iterate through the headers. |
| 4520 | * we start with the start line. |
| 4521 | */ |
| 4522 | cur_idx = 0; |
| 4523 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4524 | |
| 4525 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4526 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4527 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4528 | |
| 4529 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4530 | cur_ptr = cur_next; |
| 4531 | cur_end = cur_ptr + cur_hdr->len; |
| 4532 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4533 | |
| 4534 | /* We have one full header between cur_ptr and cur_end, and the |
| 4535 | * next header starts at cur_next. We're only interested in |
| 4536 | * "Cookie:" headers. |
| 4537 | */ |
| 4538 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4539 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4540 | if (val) { |
| 4541 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4542 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4543 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4544 | return; |
| 4545 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4548 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4549 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4550 | continue; |
| 4551 | |
| 4552 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4553 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4554 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4555 | |
| 4556 | if (p1 >= cur_end) /* no more info */ |
| 4557 | continue; |
| 4558 | |
| 4559 | /* p1 is at the beginning of the value */ |
| 4560 | p2 = p1; |
| 4561 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4562 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4563 | p2++; |
| 4564 | |
| 4565 | /* we have a complete value between p1 and p2 */ |
| 4566 | if (p2 < cur_end && *p2 == '=') { |
| 4567 | /* we have something of the form no-cache="set-cookie" */ |
| 4568 | if ((cur_end - p1 >= 21) && |
| 4569 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4570 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4571 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4572 | continue; |
| 4573 | } |
| 4574 | |
| 4575 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4576 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4577 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4578 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4579 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4580 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4581 | return; |
| 4582 | } |
| 4583 | |
| 4584 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4585 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4586 | continue; |
| 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4592 | /* |
| 4593 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4594 | * If the server is found, it's assigned to the session. |
| 4595 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4596 | 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] | 4597 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4598 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4599 | appsess *asession_temp = NULL; |
| 4600 | appsess local_asession; |
| 4601 | char *request_line; |
| 4602 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4603 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4604 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4605 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4606 | ((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] | 4607 | return; |
| 4608 | |
| 4609 | /* skip ';' */ |
| 4610 | request_line++; |
| 4611 | |
| 4612 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4613 | 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] | 4614 | return; |
| 4615 | |
| 4616 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4617 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4618 | |
| 4619 | /* First try if we already have an appsession */ |
| 4620 | asession_temp = &local_asession; |
| 4621 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4622 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4623 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4624 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4629 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4630 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4631 | asession_temp->serverid = NULL; |
| 4632 | |
| 4633 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4634 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4635 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4636 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4637 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4638 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4639 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4640 | return; |
| 4641 | } |
| 4642 | asession_temp->sessid = local_asession.sessid; |
| 4643 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4644 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4645 | } |
| 4646 | else { |
| 4647 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4648 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4649 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4650 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4651 | tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4652 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4653 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4654 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4655 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4656 | #endif |
| 4657 | if (asession_temp->serverid == NULL) { |
| 4658 | Alert("Found Application Session without matching server.\n"); |
| 4659 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4660 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4661 | while (srv) { |
| 4662 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4663 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4664 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4665 | txn->flags &= ~TX_CK_MASK; |
| 4666 | txn->flags |= TX_CK_VALID; |
| 4667 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4668 | t->srv = srv; |
| 4669 | break; |
| 4670 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4671 | txn->flags &= ~TX_CK_MASK; |
| 4672 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4673 | } |
| 4674 | } |
| 4675 | srv = srv->next; |
| 4676 | } |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4681 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4682 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4683 | * 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] | 4684 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4685 | * 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] | 4686 | * 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] | 4687 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4688 | * |
| 4689 | * Returns 1 if the session's state changes, otherwise 0. |
| 4690 | */ |
| 4691 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4692 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4693 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4694 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4695 | struct user_auth *user; |
| 4696 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4697 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4698 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4699 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4700 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4701 | return 0; |
| 4702 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4703 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4704 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4705 | /* the URI is in h */ |
| 4706 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4707 | return 0; |
| 4708 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4709 | h += uri_auth->uri_len; |
| 4710 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4711 | if (memcmp(h, ";up", 3) == 0) { |
| 4712 | t->flags |= SN_STAT_HIDEDWN; |
| 4713 | break; |
| 4714 | } |
| 4715 | h++; |
| 4716 | } |
| 4717 | |
| 4718 | if (uri_auth->refresh) { |
| 4719 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4720 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4721 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4722 | t->flags |= SN_STAT_NORFRSH; |
| 4723 | break; |
| 4724 | } |
| 4725 | h++; |
| 4726 | } |
| 4727 | } |
| 4728 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4729 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4730 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4731 | if (memcmp(h, ";csv", 4) == 0) { |
| 4732 | t->flags |= SN_STAT_FMTCSV; |
| 4733 | break; |
| 4734 | } |
| 4735 | h++; |
| 4736 | } |
| 4737 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4738 | /* we are in front of a interceptable URI. Let's check |
| 4739 | * if there's an authentication and if it's valid. |
| 4740 | */ |
| 4741 | user = uri_auth->users; |
| 4742 | if (!user) { |
| 4743 | /* no user auth required, it's OK */ |
| 4744 | authenticated = 1; |
| 4745 | } else { |
| 4746 | authenticated = 0; |
| 4747 | |
| 4748 | /* a user list is defined, we have to check. |
| 4749 | * skip 21 chars for "Authorization: Basic ". |
| 4750 | */ |
| 4751 | |
| 4752 | /* FIXME: this should move to an earlier place */ |
| 4753 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4754 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4755 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4756 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4757 | if (len > 14 && |
| 4758 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4759 | txn->auth_hdr.str = h; |
| 4760 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4761 | break; |
| 4762 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4763 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4764 | } |
| 4765 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4766 | if (txn->auth_hdr.len < 21 || |
| 4767 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4768 | user = NULL; |
| 4769 | |
| 4770 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4771 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4772 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4773 | user->user_pwd, user->user_len)) { |
| 4774 | authenticated = 1; |
| 4775 | break; |
| 4776 | } |
| 4777 | user = user->next; |
| 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4782 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4783 | |
| 4784 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4785 | msg.str = trash; |
| 4786 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4787 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4788 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4789 | if (!(t->flags & SN_ERR_MASK)) |
| 4790 | t->flags |= SN_ERR_PRXCOND; |
| 4791 | if (!(t->flags & SN_FINST_MASK)) |
| 4792 | t->flags |= SN_FINST_R; |
| 4793 | return 1; |
| 4794 | } |
| 4795 | |
| 4796 | /* The request is valid, the user is authenticate. Let's start sending |
| 4797 | * data. |
| 4798 | */ |
| 4799 | t->cli_state = CL_STSHUTR; |
| 4800 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4801 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4802 | t->data_source = DATA_SRC_STATS; |
| 4803 | t->data_state = DATA_ST_INIT; |
| 4804 | produce_content(t); |
| 4805 | return 1; |
| 4806 | } |
| 4807 | |
| 4808 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4809 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4810 | * Print a debug line with a header |
| 4811 | */ |
| 4812 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4813 | { |
| 4814 | int len, max; |
| 4815 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4816 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4817 | max = end - start; |
| 4818 | UBOUND(max, sizeof(trash) - len - 1); |
| 4819 | len += strlcpy2(trash + len, start, max + 1); |
| 4820 | trash[len++] = '\n'; |
| 4821 | write(1, trash, len); |
| 4822 | } |
| 4823 | |
| 4824 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4825 | /************************************************************************/ |
| 4826 | /* The code below is dedicated to ACL parsing and matching */ |
| 4827 | /************************************************************************/ |
| 4828 | |
| 4829 | |
| 4830 | |
| 4831 | |
| 4832 | /* 1. Check on METHOD |
| 4833 | * We use the pre-parsed method if it is known, and store its number as an |
| 4834 | * integer. If it is unknown, we use the pointer and the length. |
| 4835 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4836 | 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] | 4837 | { |
| 4838 | int len, meth; |
| 4839 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4840 | len = strlen(*text); |
| 4841 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4842 | |
| 4843 | pattern->val.i = meth; |
| 4844 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4845 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4846 | if (!pattern->ptr.str) |
| 4847 | return 0; |
| 4848 | pattern->len = len; |
| 4849 | } |
| 4850 | return 1; |
| 4851 | } |
| 4852 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4853 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4854 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4855 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4856 | { |
| 4857 | int meth; |
| 4858 | struct http_txn *txn = l7; |
| 4859 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4860 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4861 | return 0; |
| 4862 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4863 | meth = txn->meth; |
| 4864 | test->i = meth; |
| 4865 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4866 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4867 | /* ensure the indexes are not affected */ |
| 4868 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4869 | test->len = txn->req.sl.rq.m_l; |
| 4870 | test->ptr = txn->req.sol; |
| 4871 | } |
| 4872 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4873 | return 1; |
| 4874 | } |
| 4875 | |
| 4876 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4877 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4878 | int icase; |
| 4879 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4880 | if (test->i != pattern->val.i) |
| 4881 | return 0; |
| 4882 | |
| 4883 | if (test->i != HTTP_METH_OTHER) |
| 4884 | return 1; |
| 4885 | |
| 4886 | /* Other method, we must compare the strings */ |
| 4887 | if (pattern->len != test->len) |
| 4888 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4889 | |
| 4890 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4891 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4892 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4893 | return 0; |
| 4894 | return 1; |
| 4895 | } |
| 4896 | |
| 4897 | /* 2. Check on Request/Status Version |
| 4898 | * We simply compare strings here. |
| 4899 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4900 | 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] | 4901 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4902 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4903 | if (!pattern->ptr.str) |
| 4904 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4905 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4906 | return 1; |
| 4907 | } |
| 4908 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4909 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4910 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4911 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4912 | { |
| 4913 | struct http_txn *txn = l7; |
| 4914 | char *ptr; |
| 4915 | int len; |
| 4916 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4917 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4918 | return 0; |
| 4919 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4920 | len = txn->req.sl.rq.v_l; |
| 4921 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4922 | |
| 4923 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4924 | if (len <= 0) |
| 4925 | return 0; |
| 4926 | |
| 4927 | test->ptr = ptr; |
| 4928 | test->len = len; |
| 4929 | |
| 4930 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4931 | return 1; |
| 4932 | } |
| 4933 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4934 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4935 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4936 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4937 | { |
| 4938 | struct http_txn *txn = l7; |
| 4939 | char *ptr; |
| 4940 | int len; |
| 4941 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4942 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4943 | return 0; |
| 4944 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4945 | len = txn->rsp.sl.st.v_l; |
| 4946 | ptr = txn->rsp.sol; |
| 4947 | |
| 4948 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4949 | if (len <= 0) |
| 4950 | return 0; |
| 4951 | |
| 4952 | test->ptr = ptr; |
| 4953 | test->len = len; |
| 4954 | |
| 4955 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4956 | return 1; |
| 4957 | } |
| 4958 | |
| 4959 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4960 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4961 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4962 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4963 | { |
| 4964 | struct http_txn *txn = l7; |
| 4965 | char *ptr; |
| 4966 | int len; |
| 4967 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4968 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4969 | return 0; |
| 4970 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4971 | len = txn->rsp.sl.st.c_l; |
| 4972 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4973 | |
| 4974 | test->i = __strl2ui(ptr, len); |
| 4975 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4976 | return 1; |
| 4977 | } |
| 4978 | |
| 4979 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4980 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4981 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4982 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4983 | { |
| 4984 | struct http_txn *txn = l7; |
| 4985 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4986 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4987 | return 0; |
| 4988 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4989 | /* ensure the indexes are not affected */ |
| 4990 | return 0; |
| 4991 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4992 | test->len = txn->req.sl.rq.u_l; |
| 4993 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4994 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4995 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4996 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4997 | return 1; |
| 4998 | } |
| 4999 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5000 | static int |
| 5001 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5002 | struct acl_expr *expr, struct acl_test *test) |
| 5003 | { |
| 5004 | struct http_txn *txn = l7; |
| 5005 | |
| 5006 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5007 | return 0; |
| 5008 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5009 | /* ensure the indexes are not affected */ |
| 5010 | return 0; |
| 5011 | |
| 5012 | /* Parse HTTP request */ |
| 5013 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5014 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5015 | test->i = AF_INET; |
| 5016 | |
| 5017 | /* |
| 5018 | * If we are parsing url in frontend space, we prepare backend stage |
| 5019 | * to not parse again the same url ! optimization lazyness... |
| 5020 | */ |
| 5021 | if (px->options & PR_O_HTTP_PROXY) |
| 5022 | l4->flags |= SN_ADDR_SET; |
| 5023 | |
| 5024 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5025 | return 1; |
| 5026 | } |
| 5027 | |
| 5028 | static int |
| 5029 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5030 | struct acl_expr *expr, struct acl_test *test) |
| 5031 | { |
| 5032 | struct http_txn *txn = l7; |
| 5033 | |
| 5034 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5035 | return 0; |
| 5036 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5037 | /* ensure the indexes are not affected */ |
| 5038 | return 0; |
| 5039 | |
| 5040 | /* Same optimization as url_ip */ |
| 5041 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5042 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5043 | |
| 5044 | if (px->options & PR_O_HTTP_PROXY) |
| 5045 | l4->flags |= SN_ADDR_SET; |
| 5046 | |
| 5047 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5048 | return 1; |
| 5049 | } |
| 5050 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5051 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5052 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5053 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5054 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5055 | 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] | 5056 | struct acl_expr *expr, struct acl_test *test) |
| 5057 | { |
| 5058 | struct http_txn *txn = l7; |
| 5059 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5060 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5061 | |
| 5062 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5063 | /* search for header from the beginning */ |
| 5064 | ctx->idx = 0; |
| 5065 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5066 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5067 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5068 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5069 | test->len = ctx->vlen; |
| 5070 | test->ptr = (char *)ctx->line + ctx->val; |
| 5071 | return 1; |
| 5072 | } |
| 5073 | |
| 5074 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5075 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5076 | return 0; |
| 5077 | } |
| 5078 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5079 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5080 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5081 | struct acl_expr *expr, struct acl_test *test) |
| 5082 | { |
| 5083 | struct http_txn *txn = l7; |
| 5084 | |
| 5085 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5086 | return 0; |
| 5087 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5088 | /* ensure the indexes are not affected */ |
| 5089 | return 0; |
| 5090 | |
| 5091 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5092 | } |
| 5093 | |
| 5094 | static int |
| 5095 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5096 | struct acl_expr *expr, struct acl_test *test) |
| 5097 | { |
| 5098 | struct http_txn *txn = l7; |
| 5099 | |
| 5100 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5101 | return 0; |
| 5102 | |
| 5103 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5104 | } |
| 5105 | |
| 5106 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5107 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5108 | */ |
| 5109 | static int |
| 5110 | 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] | 5111 | struct acl_expr *expr, struct acl_test *test) |
| 5112 | { |
| 5113 | struct http_txn *txn = l7; |
| 5114 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5115 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5116 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5117 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5118 | ctx.idx = 0; |
| 5119 | cnt = 0; |
| 5120 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5121 | cnt++; |
| 5122 | |
| 5123 | test->i = cnt; |
| 5124 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5125 | return 1; |
| 5126 | } |
| 5127 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5128 | static int |
| 5129 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5130 | struct acl_expr *expr, struct acl_test *test) |
| 5131 | { |
| 5132 | struct http_txn *txn = l7; |
| 5133 | |
| 5134 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5135 | return 0; |
| 5136 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5137 | /* ensure the indexes are not affected */ |
| 5138 | return 0; |
| 5139 | |
| 5140 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5141 | } |
| 5142 | |
| 5143 | static int |
| 5144 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5145 | struct acl_expr *expr, struct acl_test *test) |
| 5146 | { |
| 5147 | struct http_txn *txn = l7; |
| 5148 | |
| 5149 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5150 | return 0; |
| 5151 | |
| 5152 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5153 | } |
| 5154 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5155 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5156 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5157 | * 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] | 5158 | */ |
| 5159 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5160 | 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] | 5161 | struct acl_expr *expr, struct acl_test *test) |
| 5162 | { |
| 5163 | struct http_txn *txn = l7; |
| 5164 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5165 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5166 | |
| 5167 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5168 | /* search for header from the beginning */ |
| 5169 | ctx->idx = 0; |
| 5170 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5171 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5172 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5173 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5174 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5175 | return 1; |
| 5176 | } |
| 5177 | |
| 5178 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5179 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5180 | return 0; |
| 5181 | } |
| 5182 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5183 | static int |
| 5184 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5185 | struct acl_expr *expr, struct acl_test *test) |
| 5186 | { |
| 5187 | struct http_txn *txn = l7; |
| 5188 | |
| 5189 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5190 | return 0; |
| 5191 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5192 | /* ensure the indexes are not affected */ |
| 5193 | return 0; |
| 5194 | |
| 5195 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5196 | } |
| 5197 | |
| 5198 | static int |
| 5199 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5200 | struct acl_expr *expr, struct acl_test *test) |
| 5201 | { |
| 5202 | struct http_txn *txn = l7; |
| 5203 | |
| 5204 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5205 | return 0; |
| 5206 | |
| 5207 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5208 | } |
| 5209 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5210 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5211 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5212 | */ |
| 5213 | static int |
| 5214 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5215 | struct acl_expr *expr, struct acl_test *test) |
| 5216 | { |
| 5217 | struct http_txn *txn = l7; |
| 5218 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5219 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5220 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5221 | return 0; |
| 5222 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5223 | /* ensure the indexes are not affected */ |
| 5224 | return 0; |
| 5225 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5226 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5227 | end = ptr + txn->req.sl.rq.u_l; |
| 5228 | |
| 5229 | if (ptr >= end) |
| 5230 | return 0; |
| 5231 | |
| 5232 | /* RFC2616, par. 5.1.2 : |
| 5233 | * Request-URI = "*" | absuri | abspath | authority |
| 5234 | */ |
| 5235 | |
| 5236 | if (*ptr == '*') |
| 5237 | return 0; |
| 5238 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5239 | if (isalpha((unsigned char)*ptr)) { |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5240 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5241 | ptr++; |
| 5242 | while (ptr < end && |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5243 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5244 | ptr++; |
| 5245 | /* skip '://' */ |
| 5246 | if (ptr == end || *ptr++ != ':') |
| 5247 | return 0; |
| 5248 | if (ptr == end || *ptr++ != '/') |
| 5249 | return 0; |
| 5250 | if (ptr == end || *ptr++ != '/') |
| 5251 | return 0; |
| 5252 | } |
| 5253 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5254 | |
| 5255 | while (ptr < end && *ptr != '/') |
| 5256 | ptr++; |
| 5257 | |
| 5258 | if (ptr == end) |
| 5259 | return 0; |
| 5260 | |
| 5261 | /* OK, we got the '/' ! */ |
| 5262 | test->ptr = ptr; |
| 5263 | |
| 5264 | while (ptr < end && *ptr != '?') |
| 5265 | ptr++; |
| 5266 | |
| 5267 | test->len = ptr - test->ptr; |
| 5268 | |
| 5269 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5270 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5271 | return 1; |
| 5272 | } |
| 5273 | |
| 5274 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5275 | |
| 5276 | /************************************************************************/ |
| 5277 | /* All supported keywords must be declared here. */ |
| 5278 | /************************************************************************/ |
| 5279 | |
| 5280 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5281 | static struct acl_kw_list acl_kws = {{ },{ |
| 5282 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5283 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5284 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5285 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5286 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5287 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5288 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5289 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5290 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5291 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5292 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5293 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
| 5294 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip }, |
| 5295 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5296 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5297 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5298 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5299 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5300 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5301 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5302 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5303 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5304 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5305 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5306 | |
| 5307 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5308 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5309 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5310 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5311 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5312 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5313 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5314 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5315 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5316 | |
| 5317 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5318 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5319 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5320 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5321 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5322 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5323 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5324 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5325 | { NULL, NULL, NULL, NULL }, |
| 5326 | |
| 5327 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5328 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5329 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5330 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5331 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5332 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5333 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5334 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5335 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5336 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5337 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5338 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5339 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5340 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5341 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5342 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5343 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5344 | |
| 5345 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5346 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5347 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5348 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5349 | { NULL, NULL, NULL, NULL }, |
| 5350 | #endif |
| 5351 | }}; |
| 5352 | |
| 5353 | |
| 5354 | __attribute__((constructor)) |
| 5355 | static void __http_protocol_init(void) |
| 5356 | { |
| 5357 | acl_register_keywords(&acl_kws); |
| 5358 | } |
| 5359 | |
| 5360 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5361 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5362 | * Local variables: |
| 5363 | * c-indent-level: 8 |
| 5364 | * c-basic-offset: 8 |
| 5365 | * End: |
| 5366 | */ |