Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 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> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/session.h> |
| 53 | #include <proto/task.h> |
| 54 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 55 | #ifdef CONFIG_HAP_TCPSPLICE |
| 56 | #include <libtcpsplice.h> |
| 57 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 59 | #define DEBUG_PARSE_NO_SPEEDUP |
| 60 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 61 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 62 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 63 | * instruction. The first argument is the label for normal operation, and the |
| 64 | * second one is the break/continue instruction in the no_speedup mode. |
| 65 | */ |
| 66 | |
| 67 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 68 | #define QUICK_JUMP(x,y) y |
| 69 | #else |
| 70 | #define QUICK_JUMP(x,y) goto x |
| 71 | #endif |
| 72 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 74 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 75 | "HTTP/1.0 200 OK\r\n" |
| 76 | "Cache-Control: no-cache\r\n" |
| 77 | "Connection: close\r\n" |
| 78 | "Content-Type: text/html\r\n" |
| 79 | "\r\n" |
| 80 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const struct chunk http_200_chunk = { |
| 83 | .str = (char *)&HTTP_200, |
| 84 | .len = sizeof(HTTP_200)-1 |
| 85 | }; |
| 86 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | const char *HTTP_301 = |
| 88 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 89 | "Cache-Control: no-cache\r\n" |
| 90 | "Connection: close\r\n" |
| 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | const char *HTTP_302 = |
| 94 | "HTTP/1.0 302 Found\r\n" |
| 95 | "Cache-Control: no-cache\r\n" |
| 96 | "Connection: close\r\n" |
| 97 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 98 | |
| 99 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 100 | const char *HTTP_303 = |
| 101 | "HTTP/1.0 303 See Other\r\n" |
| 102 | "Cache-Control: no-cache\r\n" |
| 103 | "Connection: close\r\n" |
| 104 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 105 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 107 | const char *HTTP_401_fmt = |
| 108 | "HTTP/1.0 401 Unauthorized\r\n" |
| 109 | "Cache-Control: no-cache\r\n" |
| 110 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 111 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 113 | "\r\n" |
| 114 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 115 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 116 | |
| 117 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 118 | [HTTP_ERR_400] = 400, |
| 119 | [HTTP_ERR_403] = 403, |
| 120 | [HTTP_ERR_408] = 408, |
| 121 | [HTTP_ERR_500] = 500, |
| 122 | [HTTP_ERR_502] = 502, |
| 123 | [HTTP_ERR_503] = 503, |
| 124 | [HTTP_ERR_504] = 504, |
| 125 | }; |
| 126 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 129 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | "Cache-Control: no-cache\r\n" |
| 131 | "Connection: close\r\n" |
| 132 | "Content-Type: text/html\r\n" |
| 133 | "\r\n" |
| 134 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 135 | |
| 136 | [HTTP_ERR_403] = |
| 137 | "HTTP/1.0 403 Forbidden\r\n" |
| 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_408] = |
| 145 | "HTTP/1.0 408 Request Time-out\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_500] = |
| 153 | "HTTP/1.0 500 Server Error\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_502] = |
| 161 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_503] = |
| 169 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_504] = |
| 177 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 183 | |
| 184 | }; |
| 185 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 186 | /* We must put the messages here since GCC cannot initialize consts depending |
| 187 | * on strlen(). |
| 188 | */ |
| 189 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 190 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 191 | #define FD_SETS_ARE_BITFIELDS |
| 192 | #ifdef FD_SETS_ARE_BITFIELDS |
| 193 | /* |
| 194 | * This map is used with all the FD_* macros to check whether a particular bit |
| 195 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 196 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 197 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 198 | * exclusively to the macros. |
| 199 | */ |
| 200 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 201 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | |
| 203 | #else |
| 204 | #error "Check if your OS uses bitfields for fd_sets" |
| 205 | #endif |
| 206 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | void init_proto_http() |
| 208 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 209 | int i; |
| 210 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 212 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 213 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 214 | if (!http_err_msgs[msg]) { |
| 215 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 216 | abort(); |
| 217 | } |
| 218 | |
| 219 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 220 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 221 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 222 | |
| 223 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 224 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 225 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 226 | * printable characters above. |
| 227 | */ |
| 228 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 229 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 230 | for (i = 0; i < 32; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | for (i = 127; i < 256; i++) { |
| 235 | FD_SET(i, hdr_encode_map); |
| 236 | FD_SET(i, url_encode_map); |
| 237 | } |
| 238 | |
| 239 | tmp = "\"#{|}"; |
| 240 | while (*tmp) { |
| 241 | FD_SET(*tmp, hdr_encode_map); |
| 242 | tmp++; |
| 243 | } |
| 244 | |
| 245 | tmp = "\"#"; |
| 246 | while (*tmp) { |
| 247 | FD_SET(*tmp, url_encode_map); |
| 248 | tmp++; |
| 249 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 250 | |
| 251 | /* memory allocations */ |
| 252 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 253 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 254 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 255 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 256 | /* |
| 257 | * We have 26 list of methods (1 per first letter), each of which can have |
| 258 | * up to 3 entries (2 valid, 1 null). |
| 259 | */ |
| 260 | struct http_method_desc { |
| 261 | http_meth_t meth; |
| 262 | int len; |
| 263 | const char text[8]; |
| 264 | }; |
| 265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 266 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 267 | ['C' - 'A'] = { |
| 268 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 269 | }, |
| 270 | ['D' - 'A'] = { |
| 271 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 272 | }, |
| 273 | ['G' - 'A'] = { |
| 274 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 275 | }, |
| 276 | ['H' - 'A'] = { |
| 277 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 278 | }, |
| 279 | ['P' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 281 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 282 | }, |
| 283 | ['T' - 'A'] = { |
| 284 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 285 | }, |
| 286 | /* rest is empty like this : |
| 287 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 288 | */ |
| 289 | }; |
| 290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 291 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 292 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 293 | * RFC2616 for those chars. |
| 294 | */ |
| 295 | |
| 296 | const char http_is_spht[256] = { |
| 297 | [' '] = 1, ['\t'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_crlf[256] = { |
| 301 | ['\r'] = 1, ['\n'] = 1, |
| 302 | }; |
| 303 | |
| 304 | const char http_is_lws[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | ['\r'] = 1, ['\n'] = 1, |
| 307 | }; |
| 308 | |
| 309 | const char http_is_sep[256] = { |
| 310 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 311 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 312 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 313 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 314 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_ctl[256] = { |
| 318 | [0 ... 31] = 1, |
| 319 | [127] = 1, |
| 320 | }; |
| 321 | |
| 322 | /* |
| 323 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 324 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 325 | * them correctly. Instead, define every non-CTL char's status. |
| 326 | */ |
| 327 | const char http_is_token[256] = { |
| 328 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 329 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 330 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 331 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 332 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 333 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 334 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 335 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 336 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 337 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 338 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 339 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 340 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 341 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 342 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 343 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 344 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 345 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 346 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 347 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 348 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 349 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 350 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 351 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 352 | }; |
| 353 | |
| 354 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 355 | /* |
| 356 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 357 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 358 | */ |
| 359 | const char http_is_ver_token[256] = { |
| 360 | ['.'] = 1, ['/'] = 1, |
| 361 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 362 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 363 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 364 | }; |
| 365 | |
| 366 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 367 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 368 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 369 | #endif |
| 370 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 371 | static void http_sess_log(struct session *s); |
| 372 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 373 | /* |
| 374 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 375 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 376 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 377 | * of headers is automatically adjusted. The number of bytes added is returned |
| 378 | * on success, otherwise <0 is returned indicating an error. |
| 379 | */ |
| 380 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 381 | struct hdr_idx *hdr_idx, const char *text) |
| 382 | { |
| 383 | int bytes, len; |
| 384 | |
| 385 | len = strlen(text); |
| 386 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 387 | if (!bytes) |
| 388 | return -1; |
| 389 | msg->eoh += bytes; |
| 390 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 395 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 396 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 397 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 398 | * of headers is automatically adjusted. The number of bytes added is returned |
| 399 | * on success, otherwise <0 is returned indicating an error. |
| 400 | */ |
| 401 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 402 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 403 | { |
| 404 | int bytes; |
| 405 | |
| 406 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 407 | if (!bytes) |
| 408 | return -1; |
| 409 | msg->eoh += bytes; |
| 410 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 411 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | |
| 413 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 414 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 415 | * If so, returns the position of the first non-space character relative to |
| 416 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 417 | * to return a pointer to the place after the first space. Returns 0 if the |
| 418 | * header name does not match. Checks are case-insensitive. |
| 419 | */ |
| 420 | int http_header_match2(const char *hdr, const char *end, |
| 421 | const char *name, int len) |
| 422 | { |
| 423 | const char *val; |
| 424 | |
| 425 | if (hdr + len >= end) |
| 426 | return 0; |
| 427 | if (hdr[len] != ':') |
| 428 | return 0; |
| 429 | if (strncasecmp(hdr, name, len) != 0) |
| 430 | return 0; |
| 431 | val = hdr + len + 1; |
| 432 | while (val < end && HTTP_IS_SPHT(*val)) |
| 433 | val++; |
| 434 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 435 | return len + 2; /* we may replace starting from second space */ |
| 436 | return val - hdr; |
| 437 | } |
| 438 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 439 | /* Find the end of the header value contained between <s> and <e>. |
| 440 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 441 | * a valid header to return a valid result. |
| 442 | */ |
| 443 | const char *find_hdr_value_end(const char *s, const char *e) |
| 444 | { |
| 445 | int quoted, qdpair; |
| 446 | |
| 447 | quoted = qdpair = 0; |
| 448 | for (; s < e; s++) { |
| 449 | if (qdpair) qdpair = 0; |
| 450 | else if (quoted && *s == '\\') qdpair = 1; |
| 451 | else if (quoted && *s == '"') quoted = 0; |
| 452 | else if (*s == '"') quoted = 1; |
| 453 | else if (*s == ',') return s; |
| 454 | } |
| 455 | return s; |
| 456 | } |
| 457 | |
| 458 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 459 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 460 | * structure holds everything necessary to use the header and find next |
| 461 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 462 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 463 | * 1 when it finds a value, and 0 when there is no more. |
| 464 | */ |
| 465 | int http_find_header2(const char *name, int len, |
| 466 | const char *sol, struct hdr_idx *idx, |
| 467 | struct hdr_ctx *ctx) |
| 468 | { |
| 469 | __label__ return_hdr, next_hdr; |
| 470 | const char *eol, *sov; |
| 471 | int cur_idx; |
| 472 | |
| 473 | if (ctx->idx) { |
| 474 | /* We have previously returned a value, let's search |
| 475 | * another one on the same line. |
| 476 | */ |
| 477 | cur_idx = ctx->idx; |
| 478 | sol = ctx->line; |
| 479 | sov = sol + ctx->val + ctx->vlen; |
| 480 | eol = sol + idx->v[cur_idx].len; |
| 481 | |
| 482 | if (sov >= eol) |
| 483 | /* no more values in this header */ |
| 484 | goto next_hdr; |
| 485 | |
| 486 | /* values remaining for this header, skip the comma */ |
| 487 | sov++; |
| 488 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 489 | sov++; |
| 490 | |
| 491 | goto return_hdr; |
| 492 | } |
| 493 | |
| 494 | /* first request for this header */ |
| 495 | sol += hdr_idx_first_pos(idx); |
| 496 | cur_idx = hdr_idx_first_idx(idx); |
| 497 | |
| 498 | while (cur_idx) { |
| 499 | eol = sol + idx->v[cur_idx].len; |
| 500 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 501 | if (len == 0) { |
| 502 | /* No argument was passed, we want any header. |
| 503 | * To achieve this, we simply build a fake request. */ |
| 504 | while (sol + len < eol && sol[len] != ':') |
| 505 | len++; |
| 506 | name = sol; |
| 507 | } |
| 508 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 509 | if ((len < eol - sol) && |
| 510 | (sol[len] == ':') && |
| 511 | (strncasecmp(sol, name, len) == 0)) { |
| 512 | |
| 513 | sov = sol + len + 1; |
| 514 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 515 | sov++; |
| 516 | return_hdr: |
| 517 | ctx->line = sol; |
| 518 | ctx->idx = cur_idx; |
| 519 | ctx->val = sov - sol; |
| 520 | |
| 521 | eol = find_hdr_value_end(sov, eol); |
| 522 | ctx->vlen = eol - sov; |
| 523 | return 1; |
| 524 | } |
| 525 | next_hdr: |
| 526 | sol = eol + idx->v[cur_idx].cr + 1; |
| 527 | cur_idx = idx->v[cur_idx].next; |
| 528 | } |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | int http_find_header(const char *name, |
| 533 | const char *sol, struct hdr_idx *idx, |
| 534 | struct hdr_ctx *ctx) |
| 535 | { |
| 536 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 537 | } |
| 538 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 539 | /* This function shuts down the buffers on the server side, and sets indicators |
| 540 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 541 | * <status> is 0, or if the message pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 542 | */ |
| 543 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 544 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 545 | { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 546 | t->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 547 | buffer_shutw(t->req); |
| 548 | buffer_shutr(t->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 549 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 550 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 551 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 552 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | } |
| 554 | if (!(t->flags & SN_ERR_MASK)) |
| 555 | t->flags |= err; |
| 556 | if (!(t->flags & SN_FINST_MASK)) |
| 557 | t->flags |= finst; |
| 558 | } |
| 559 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 560 | /* This function returns the appropriate error location for the given session |
| 561 | * and message. |
| 562 | */ |
| 563 | |
| 564 | struct chunk *error_message(struct session *s, int msgnum) |
| 565 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 566 | if (s->be->errmsg[msgnum].str) |
| 567 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 568 | else if (s->fe->errmsg[msgnum].str) |
| 569 | return &s->fe->errmsg[msgnum]; |
| 570 | else |
| 571 | return &http_err_chunks[msgnum]; |
| 572 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 574 | /* |
| 575 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 576 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 577 | */ |
| 578 | static http_meth_t find_http_meth(const char *str, const int len) |
| 579 | { |
| 580 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 581 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 582 | |
| 583 | m = ((unsigned)*str - 'A'); |
| 584 | |
| 585 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | for (h = http_methods[m]; h->len > 0; h++) { |
| 587 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 589 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 590 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | }; |
| 592 | return HTTP_METH_OTHER; |
| 593 | } |
| 594 | return HTTP_METH_NONE; |
| 595 | |
| 596 | } |
| 597 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 598 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 599 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 600 | * It is returned otherwise. |
| 601 | */ |
| 602 | static char * |
| 603 | http_get_path(struct http_txn *txn) |
| 604 | { |
| 605 | char *ptr, *end; |
| 606 | |
| 607 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 608 | end = ptr + txn->req.sl.rq.u_l; |
| 609 | |
| 610 | if (ptr >= end) |
| 611 | return NULL; |
| 612 | |
| 613 | /* RFC2616, par. 5.1.2 : |
| 614 | * Request-URI = "*" | absuri | abspath | authority |
| 615 | */ |
| 616 | |
| 617 | if (*ptr == '*') |
| 618 | return NULL; |
| 619 | |
| 620 | if (isalpha((unsigned char)*ptr)) { |
| 621 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 622 | ptr++; |
| 623 | while (ptr < end && |
| 624 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 625 | ptr++; |
| 626 | /* skip '://' */ |
| 627 | if (ptr == end || *ptr++ != ':') |
| 628 | return NULL; |
| 629 | if (ptr == end || *ptr++ != '/') |
| 630 | return NULL; |
| 631 | if (ptr == end || *ptr++ != '/') |
| 632 | return NULL; |
| 633 | } |
| 634 | /* skip [user[:passwd]@]host[:[port]] */ |
| 635 | |
| 636 | while (ptr < end && *ptr != '/') |
| 637 | ptr++; |
| 638 | |
| 639 | if (ptr == end) |
| 640 | return NULL; |
| 641 | |
| 642 | /* OK, we got the '/' ! */ |
| 643 | return ptr; |
| 644 | } |
| 645 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 646 | /* Processes the client, server, request and response jobs of a session task, |
| 647 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 648 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 649 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 650 | * nothing too many times, the request and response buffers flags are monitored |
| 651 | * and each function is called only if at least another function has changed at |
| 652 | * least one flag. If one of the functions called returns non-zero, then it |
| 653 | * will be called once again after all other functions. This permits explicit |
| 654 | * external loops which may be useful for complex state machines. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 655 | */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 656 | #define PROCESS_CLI 0x1 |
| 657 | #define PROCESS_SRV 0x2 |
| 658 | #define PROCESS_REQ 0x4 |
| 659 | #define PROCESS_RTR 0x8 |
| 660 | #define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR) |
| 661 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 662 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 663 | { |
| 664 | struct session *s = t->context; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 665 | unsigned resync = PROCESS_ALL; |
| 666 | unsigned int rqf; |
| 667 | unsigned int rpf; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 668 | |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 669 | /* check timeout expiration only once and adjust buffer flags |
| 670 | * accordingly. |
| 671 | */ |
| 672 | if (unlikely(tick_is_expired(t->expire, now_ms))) { |
| 673 | if (tick_is_expired(s->req->rex, now_ms)) |
| 674 | s->req->flags |= BF_READ_TIMEOUT; |
| 675 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 676 | //if (tick_is_expired(s->req->wex, now_ms)) |
| 677 | // s->req->flags |= BF_WRITE_TIMEOUT; |
| 678 | // |
| 679 | //if (tick_is_expired(s->rep->rex, now_ms)) |
| 680 | // s->rep->flags |= BF_READ_TIMEOUT; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 681 | |
| 682 | if (tick_is_expired(s->rep->wex, now_ms)) |
| 683 | s->rep->flags |= BF_WRITE_TIMEOUT; |
| 684 | } |
| 685 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 686 | //if (fdtab[s->cli_fd].state == FD_STERROR) { |
| 687 | // fprintf(stderr, "s=%p fd=%d req=%p rep=%p cs=%d ss=%d, term=%08x\n", |
| 688 | // s, s->cli_fd, s->req, s->rep, s->cli_state, |
| 689 | // s->si[1].state, s->term_trace); |
| 690 | // sleep(1); |
| 691 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 692 | do { |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 693 | if (resync & PROCESS_REQ) { |
| 694 | resync &= ~PROCESS_REQ; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 695 | rqf = s->req->flags; |
| 696 | rpf = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 697 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 698 | /* the analysers must block it themselves */ |
| 699 | s->req->flags |= BF_MAY_FORWARD; |
| 700 | |
| 701 | if (s->req->analysers) { |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 702 | if (process_request(s)) |
| 703 | resync |= PROCESS_REQ; |
| 704 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 705 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 706 | resync |= PROCESS_ALL & ~PROCESS_REQ; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | if (resync & PROCESS_RTR) { |
| 711 | resync &= ~PROCESS_RTR; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 712 | rqf = s->req->flags; |
| 713 | rpf = s->rep->flags; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 714 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 715 | /* the analysers must block it themselves */ |
| 716 | s->rep->flags |= BF_MAY_FORWARD; |
| 717 | |
| 718 | if (s->rep->analysers) { |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 719 | if (process_response(s)) |
| 720 | resync |= PROCESS_RTR; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 721 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 722 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 723 | resync |= PROCESS_ALL & ~PROCESS_RTR; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (resync & PROCESS_CLI) { |
| 728 | rqf = s->req->flags; |
| 729 | rpf = s->rep->flags; |
| 730 | |
| 731 | resync &= ~PROCESS_CLI; |
| 732 | if (process_cli(s)) |
| 733 | resync |= PROCESS_CLI; |
| 734 | |
| 735 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 736 | resync |= PROCESS_ALL & ~PROCESS_CLI; |
| 737 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 738 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 739 | if (resync & PROCESS_SRV) { |
| 740 | rqf = s->req->flags; |
| 741 | rpf = s->rep->flags; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 742 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 743 | resync &= ~PROCESS_SRV; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 744 | if (s->req->cons->state != SI_ST_CLO) { |
| 745 | if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_MAY_FORWARD) |
| 746 | process_srv_conn(s); |
| 747 | |
| 748 | if (s->req->cons->state == SI_ST_EST) { |
| 749 | if (process_srv_data(s)) |
| 750 | resync |= PROCESS_SRV; |
| 751 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 752 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 753 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 754 | (global.mode & MODE_DEBUG) && |
| 755 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 756 | int len; |
| 757 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 758 | s->uniq_id, s->be->id, (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd); |
| 759 | write(1, trash, len); |
| 760 | } |
| 761 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 762 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 763 | resync |= PROCESS_ALL & ~PROCESS_SRV; |
| 764 | } |
| 765 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 766 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 767 | if (likely(s->cli_state != CL_STCLOSE || |
| 768 | (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 769 | |
| 770 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 771 | session_process_counters(s); |
| 772 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 773 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 774 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 775 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 776 | /* Trick: if a request is being waiting for the server to respond, |
| 777 | * and if we know the server can timeout, we don't want the timeout |
| 778 | * to expire on the client side first, but we're still interested |
| 779 | * in passing data from the client to the server (eg: POST). Thus, |
| 780 | * we can cancel the client's request timeout if the server's |
| 781 | * request timeout is set and the server has not yet sent a response. |
| 782 | */ |
| 783 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 784 | if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 785 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 786 | s->req->rex = TICK_ETERNITY; |
| 787 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 788 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 789 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 790 | if (s->req->analysers) |
| 791 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 792 | |
| 793 | /* restore t to its place in the task list */ |
| 794 | task_queue(t); |
| 795 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 796 | #ifdef DEBUG_DEV |
| 797 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 798 | if (!t->expire) |
| 799 | ABORT_NOW(); |
| 800 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 801 | *next = t->expire; |
| 802 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 805 | s->fe->feconn--; |
| 806 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 807 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 808 | actconn--; |
| 809 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 810 | if (unlikely((global.mode & MODE_DEBUG) && |
| 811 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 812 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 813 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 814 | s->uniq_id, s->be->id, |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 815 | (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 816 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 817 | write(1, trash, len); |
| 818 | } |
| 819 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 820 | 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] | 821 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 822 | |
| 823 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 824 | if (s->logs.logwait && |
| 825 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 826 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 827 | if (s->fe->to_log & LW_REQ) |
| 828 | http_sess_log(s); |
| 829 | else |
| 830 | tcp_sess_log(s); |
| 831 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 832 | |
| 833 | /* the task MUST not be in the run queue anymore */ |
| 834 | task_delete(t); |
| 835 | session_free(s); |
| 836 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 837 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 841 | extern const char sess_term_cond[8]; |
| 842 | extern const char sess_fin_state[8]; |
| 843 | extern const char *monthname[12]; |
| 844 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 845 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 846 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 847 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 848 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 849 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 850 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 851 | /* |
| 852 | * send a log for the session when we have enough info about it. |
| 853 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 854 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 855 | static void http_sess_log(struct session *s) |
| 856 | { |
| 857 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 858 | struct proxy *fe = s->fe; |
| 859 | struct proxy *be = s->be; |
| 860 | struct proxy *prx_log; |
| 861 | struct http_txn *txn = &s->txn; |
| 862 | int tolog; |
| 863 | char *uri, *h; |
| 864 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 865 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 866 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 867 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 868 | int hdr; |
| 869 | |
| 870 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 871 | return; |
| 872 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 873 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 874 | if (s->cli_addr.ss_family == AF_INET) |
| 875 | inet_ntop(AF_INET, |
| 876 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 877 | pn, sizeof(pn)); |
| 878 | else |
| 879 | inet_ntop(AF_INET6, |
| 880 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 881 | pn, sizeof(pn)); |
| 882 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 883 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 884 | |
| 885 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 886 | tolog = fe->to_log; |
| 887 | |
| 888 | h = tmpline; |
| 889 | if (fe->to_log & LW_REQHDR && |
| 890 | txn->req.cap && |
| 891 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 892 | *(h++) = ' '; |
| 893 | *(h++) = '{'; |
| 894 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 895 | if (hdr) |
| 896 | *(h++) = '|'; |
| 897 | if (txn->req.cap[hdr] != NULL) |
| 898 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 899 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 900 | } |
| 901 | *(h++) = '}'; |
| 902 | } |
| 903 | |
| 904 | if (fe->to_log & LW_RSPHDR && |
| 905 | txn->rsp.cap && |
| 906 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 907 | *(h++) = ' '; |
| 908 | *(h++) = '{'; |
| 909 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 910 | if (hdr) |
| 911 | *(h++) = '|'; |
| 912 | if (txn->rsp.cap[hdr] != NULL) |
| 913 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 914 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 915 | } |
| 916 | *(h++) = '}'; |
| 917 | } |
| 918 | |
| 919 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 920 | *(h++) = ' '; |
| 921 | *(h++) = '"'; |
| 922 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 923 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 924 | '#', url_encode_map, uri); |
| 925 | *(h++) = '"'; |
| 926 | } |
| 927 | *h = '\0'; |
| 928 | |
| 929 | svid = (tolog & LW_SVID) ? |
| 930 | (s->data_source != DATA_SRC_STATS) ? |
| 931 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 932 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 933 | t_request = -1; |
| 934 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 935 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 936 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 937 | send_log(prx_log, LOG_INFO, |
| 938 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 939 | " %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] | 940 | " %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] | 941 | pn, |
| 942 | (s->cli_addr.ss_family == AF_INET) ? |
| 943 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 944 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 945 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 946 | tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 947 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 948 | t_request, |
| 949 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 950 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 951 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 952 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 953 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 954 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 955 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 956 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 957 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 958 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 959 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 960 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 961 | 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] | 962 | (s->flags & SN_REDISP)?"+":"", |
| 963 | (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] | 964 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 965 | |
| 966 | s->logs.logwait = 0; |
| 967 | } |
| 968 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 969 | |
| 970 | /* |
| 971 | * Capture headers from message starting at <som> according to header list |
| 972 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 973 | */ |
| 974 | void capture_headers(char *som, struct hdr_idx *idx, |
| 975 | char **cap, struct cap_hdr *cap_hdr) |
| 976 | { |
| 977 | char *eol, *sol, *col, *sov; |
| 978 | int cur_idx; |
| 979 | struct cap_hdr *h; |
| 980 | int len; |
| 981 | |
| 982 | sol = som + hdr_idx_first_pos(idx); |
| 983 | cur_idx = hdr_idx_first_idx(idx); |
| 984 | |
| 985 | while (cur_idx) { |
| 986 | eol = sol + idx->v[cur_idx].len; |
| 987 | |
| 988 | col = sol; |
| 989 | while (col < eol && *col != ':') |
| 990 | col++; |
| 991 | |
| 992 | sov = col + 1; |
| 993 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 994 | sov++; |
| 995 | |
| 996 | for (h = cap_hdr; h; h = h->next) { |
| 997 | if ((h->namelen == col - sol) && |
| 998 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 999 | if (cap[h->index] == NULL) |
| 1000 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1001 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1002 | |
| 1003 | if (cap[h->index] == NULL) { |
| 1004 | Alert("HTTP capture : out of memory.\n"); |
| 1005 | continue; |
| 1006 | } |
| 1007 | |
| 1008 | len = eol - sov; |
| 1009 | if (len > h->len) |
| 1010 | len = h->len; |
| 1011 | |
| 1012 | memcpy(cap[h->index], sov, len); |
| 1013 | cap[h->index][len]=0; |
| 1014 | } |
| 1015 | } |
| 1016 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1017 | cur_idx = idx->v[cur_idx].next; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1022 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1023 | */ |
| 1024 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1025 | |
| 1026 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1027 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1028 | */ |
| 1029 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1030 | ptr++; \ |
| 1031 | if (likely(ptr < end)) \ |
| 1032 | goto good; \ |
| 1033 | else { \ |
| 1034 | state = (st); \ |
| 1035 | goto http_msg_ood; \ |
| 1036 | } \ |
| 1037 | } while (0) |
| 1038 | |
| 1039 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1040 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1041 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1042 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1043 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1044 | * will give undefined results. |
| 1045 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1046 | * and that msg->sol points to the beginning of the response. |
| 1047 | * If a complete line is found (which implies that at least one CR or LF is |
| 1048 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1049 | * returned indicating an incomplete line (which does not mean that parts have |
| 1050 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1051 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1052 | * upon next call. |
| 1053 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1054 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1055 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1056 | * 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] | 1057 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1058 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1059 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1060 | unsigned int state, const char *ptr, const char *end, |
| 1061 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1062 | { |
| 1063 | __label__ |
| 1064 | http_msg_rpver, |
| 1065 | http_msg_rpver_sp, |
| 1066 | http_msg_rpcode, |
| 1067 | http_msg_rpcode_sp, |
| 1068 | http_msg_rpreason, |
| 1069 | http_msg_rpline_eol, |
| 1070 | http_msg_ood, /* out of data */ |
| 1071 | http_msg_invalid; |
| 1072 | |
| 1073 | switch (state) { |
| 1074 | http_msg_rpver: |
| 1075 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1076 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1077 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1078 | |
| 1079 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1080 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1081 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1082 | } |
| 1083 | goto http_msg_invalid; |
| 1084 | |
| 1085 | http_msg_rpver_sp: |
| 1086 | case HTTP_MSG_RPVER_SP: |
| 1087 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1088 | msg->sl.st.c = ptr - msg_buf; |
| 1089 | goto http_msg_rpcode; |
| 1090 | } |
| 1091 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1092 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1093 | /* so it's a CR/LF, this is invalid */ |
| 1094 | goto http_msg_invalid; |
| 1095 | |
| 1096 | http_msg_rpcode: |
| 1097 | case HTTP_MSG_RPCODE: |
| 1098 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1099 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1100 | |
| 1101 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1102 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1103 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1104 | } |
| 1105 | |
| 1106 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1107 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1108 | http_msg_rsp_reason: |
| 1109 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1110 | msg->sl.st.r = ptr - msg_buf; |
| 1111 | msg->sl.st.r_l = 0; |
| 1112 | goto http_msg_rpline_eol; |
| 1113 | |
| 1114 | http_msg_rpcode_sp: |
| 1115 | case HTTP_MSG_RPCODE_SP: |
| 1116 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1117 | msg->sl.st.r = ptr - msg_buf; |
| 1118 | goto http_msg_rpreason; |
| 1119 | } |
| 1120 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1121 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1122 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1123 | goto http_msg_rsp_reason; |
| 1124 | |
| 1125 | http_msg_rpreason: |
| 1126 | case HTTP_MSG_RPREASON: |
| 1127 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1128 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1129 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1130 | http_msg_rpline_eol: |
| 1131 | /* We have seen the end of line. Note that we do not |
| 1132 | * necessarily have the \n yet, but at least we know that we |
| 1133 | * have EITHER \r OR \n, otherwise the response would not be |
| 1134 | * complete. We can then record the response length and return |
| 1135 | * to the caller which will be able to register it. |
| 1136 | */ |
| 1137 | msg->sl.st.l = ptr - msg->sol; |
| 1138 | return ptr; |
| 1139 | |
| 1140 | #ifdef DEBUG_FULL |
| 1141 | default: |
| 1142 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1143 | exit(1); |
| 1144 | #endif |
| 1145 | } |
| 1146 | |
| 1147 | http_msg_ood: |
| 1148 | /* out of data */ |
| 1149 | if (ret_state) |
| 1150 | *ret_state = state; |
| 1151 | if (ret_ptr) |
| 1152 | *ret_ptr = (char *)ptr; |
| 1153 | return NULL; |
| 1154 | |
| 1155 | http_msg_invalid: |
| 1156 | /* invalid message */ |
| 1157 | if (ret_state) |
| 1158 | *ret_state = HTTP_MSG_ERROR; |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1164 | * This function parses a request line between <ptr> and <end>, starting with |
| 1165 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1166 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1167 | * will give undefined results. |
| 1168 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1169 | * and that msg->sol points to the beginning of the request. |
| 1170 | * If a complete line is found (which implies that at least one CR or LF is |
| 1171 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1172 | * returned indicating an incomplete line (which does not mean that parts have |
| 1173 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1174 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1175 | * upon next call. |
| 1176 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1177 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1179 | * 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] | 1180 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1181 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1182 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1183 | unsigned int state, const char *ptr, const char *end, |
| 1184 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1185 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1186 | __label__ |
| 1187 | http_msg_rqmeth, |
| 1188 | http_msg_rqmeth_sp, |
| 1189 | http_msg_rquri, |
| 1190 | http_msg_rquri_sp, |
| 1191 | http_msg_rqver, |
| 1192 | http_msg_rqline_eol, |
| 1193 | http_msg_ood, /* out of data */ |
| 1194 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1195 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1196 | switch (state) { |
| 1197 | http_msg_rqmeth: |
| 1198 | case HTTP_MSG_RQMETH: |
| 1199 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1200 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1201 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1202 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1203 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1204 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1205 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1206 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1207 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1208 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1209 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1210 | http_msg_req09_uri: |
| 1211 | msg->sl.rq.u = ptr - msg_buf; |
| 1212 | http_msg_req09_uri_e: |
| 1213 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1214 | http_msg_req09_ver: |
| 1215 | msg->sl.rq.v = ptr - msg_buf; |
| 1216 | msg->sl.rq.v_l = 0; |
| 1217 | goto http_msg_rqline_eol; |
| 1218 | } |
| 1219 | goto http_msg_invalid; |
| 1220 | |
| 1221 | http_msg_rqmeth_sp: |
| 1222 | case HTTP_MSG_RQMETH_SP: |
| 1223 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1224 | msg->sl.rq.u = ptr - msg_buf; |
| 1225 | goto http_msg_rquri; |
| 1226 | } |
| 1227 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1228 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1229 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1230 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1232 | http_msg_rquri: |
| 1233 | case HTTP_MSG_RQURI: |
| 1234 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1235 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1236 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1238 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1239 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1240 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1241 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1242 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1243 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1245 | http_msg_rquri_sp: |
| 1246 | case HTTP_MSG_RQURI_SP: |
| 1247 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1248 | msg->sl.rq.v = ptr - msg_buf; |
| 1249 | goto http_msg_rqver; |
| 1250 | } |
| 1251 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1252 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1253 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1254 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1255 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1256 | http_msg_rqver: |
| 1257 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1258 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1259 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1262 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1263 | http_msg_rqline_eol: |
| 1264 | /* We have seen the end of line. Note that we do not |
| 1265 | * necessarily have the \n yet, but at least we know that we |
| 1266 | * have EITHER \r OR \n, otherwise the request would not be |
| 1267 | * complete. We can then record the request length and return |
| 1268 | * to the caller which will be able to register it. |
| 1269 | */ |
| 1270 | msg->sl.rq.l = ptr - msg->sol; |
| 1271 | return ptr; |
| 1272 | } |
| 1273 | |
| 1274 | /* neither an HTTP_VER token nor a CRLF */ |
| 1275 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | #ifdef DEBUG_FULL |
| 1278 | default: |
| 1279 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1280 | exit(1); |
| 1281 | #endif |
| 1282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | http_msg_ood: |
| 1285 | /* out of data */ |
| 1286 | if (ret_state) |
| 1287 | *ret_state = state; |
| 1288 | if (ret_ptr) |
| 1289 | *ret_ptr = (char *)ptr; |
| 1290 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1292 | http_msg_invalid: |
| 1293 | /* invalid message */ |
| 1294 | if (ret_state) |
| 1295 | *ret_state = HTTP_MSG_ERROR; |
| 1296 | return NULL; |
| 1297 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1298 | |
| 1299 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | /* |
| 1301 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1302 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | * when data are missing and recalled at the exact same location with no |
| 1304 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1305 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1306 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1309 | { |
| 1310 | __label__ |
| 1311 | http_msg_rqbefore, |
| 1312 | http_msg_rqbefore_cr, |
| 1313 | http_msg_rqmeth, |
| 1314 | http_msg_rqline_end, |
| 1315 | http_msg_hdr_first, |
| 1316 | http_msg_hdr_name, |
| 1317 | http_msg_hdr_l1_sp, |
| 1318 | http_msg_hdr_l1_lf, |
| 1319 | http_msg_hdr_l1_lws, |
| 1320 | http_msg_hdr_val, |
| 1321 | http_msg_hdr_l2_lf, |
| 1322 | http_msg_hdr_l2_lws, |
| 1323 | http_msg_complete_header, |
| 1324 | http_msg_last_lf, |
| 1325 | http_msg_ood, /* out of data */ |
| 1326 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1328 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1329 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1330 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1331 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1332 | ptr = buf->lr; |
| 1333 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1334 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | if (unlikely(ptr >= end)) |
| 1336 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1337 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | /* |
| 1340 | * First, states that are specific to the response only. |
| 1341 | * We check them first so that request and headers are |
| 1342 | * closer to each other (accessed more often). |
| 1343 | */ |
| 1344 | http_msg_rpbefore: |
| 1345 | case HTTP_MSG_RPBEFORE: |
| 1346 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1347 | if (likely(ptr == buf->data)) { |
| 1348 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1349 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | } else { |
| 1351 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1352 | /* only skip empty leading lines, don't remove them */ |
| 1353 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1354 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | #else |
| 1356 | /* Remove empty leading lines, as recommended by |
| 1357 | * RFC2616. This takes a lot of time because we |
| 1358 | * must move all the buffer backwards, but this |
| 1359 | * is rarely needed. The method above will be |
| 1360 | * cleaner when we'll be able to start sending |
| 1361 | * the request from any place in the buffer. |
| 1362 | */ |
| 1363 | buf->lr = ptr; |
| 1364 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1365 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1366 | msg->sol = buf->data; |
| 1367 | ptr = buf->data; |
| 1368 | end = buf->r; |
| 1369 | #endif |
| 1370 | } |
| 1371 | hdr_idx_init(idx); |
| 1372 | state = HTTP_MSG_RPVER; |
| 1373 | goto http_msg_rpver; |
| 1374 | } |
| 1375 | |
| 1376 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1377 | goto http_msg_invalid; |
| 1378 | |
| 1379 | if (unlikely(*ptr == '\n')) |
| 1380 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1381 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1382 | /* stop here */ |
| 1383 | |
| 1384 | http_msg_rpbefore_cr: |
| 1385 | case HTTP_MSG_RPBEFORE_CR: |
| 1386 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1387 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1388 | /* stop here */ |
| 1389 | |
| 1390 | http_msg_rpver: |
| 1391 | case HTTP_MSG_RPVER: |
| 1392 | case HTTP_MSG_RPVER_SP: |
| 1393 | case HTTP_MSG_RPCODE: |
| 1394 | case HTTP_MSG_RPCODE_SP: |
| 1395 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1396 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1397 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1398 | if (unlikely(!ptr)) |
| 1399 | return; |
| 1400 | |
| 1401 | /* we have a full response and we know that we have either a CR |
| 1402 | * or an LF at <ptr>. |
| 1403 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1404 | //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] | 1405 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1406 | |
| 1407 | msg->sol = ptr; |
| 1408 | if (likely(*ptr == '\r')) |
| 1409 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1410 | goto http_msg_rpline_end; |
| 1411 | |
| 1412 | http_msg_rpline_end: |
| 1413 | case HTTP_MSG_RPLINE_END: |
| 1414 | /* msg->sol must point to the first of CR or LF. */ |
| 1415 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1416 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1417 | /* stop here */ |
| 1418 | |
| 1419 | /* |
| 1420 | * Second, states that are specific to the request only |
| 1421 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | http_msg_rqbefore: |
| 1423 | case HTTP_MSG_RQBEFORE: |
| 1424 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1425 | if (likely(ptr == buf->data)) { |
| 1426 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1427 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | } else { |
| 1429 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1430 | /* only skip empty leading lines, don't remove them */ |
| 1431 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1432 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1433 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | /* Remove empty leading lines, as recommended by |
| 1435 | * RFC2616. This takes a lot of time because we |
| 1436 | * must move all the buffer backwards, but this |
| 1437 | * is rarely needed. The method above will be |
| 1438 | * cleaner when we'll be able to start sending |
| 1439 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1440 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | buf->lr = ptr; |
| 1442 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1443 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | msg->sol = buf->data; |
| 1445 | ptr = buf->data; |
| 1446 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1447 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1449 | /* we will need this when keep-alive will be supported |
| 1450 | hdr_idx_init(idx); |
| 1451 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1452 | state = HTTP_MSG_RQMETH; |
| 1453 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1457 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1458 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | if (unlikely(*ptr == '\n')) |
| 1460 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1461 | 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] | 1462 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | http_msg_rqbefore_cr: |
| 1465 | case HTTP_MSG_RQBEFORE_CR: |
| 1466 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1467 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1468 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1469 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | http_msg_rqmeth: |
| 1471 | case HTTP_MSG_RQMETH: |
| 1472 | case HTTP_MSG_RQMETH_SP: |
| 1473 | case HTTP_MSG_RQURI: |
| 1474 | case HTTP_MSG_RQURI_SP: |
| 1475 | case HTTP_MSG_RQVER: |
| 1476 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1477 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | if (unlikely(!ptr)) |
| 1479 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1480 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | /* we have a full request and we know that we have either a CR |
| 1482 | * or an LF at <ptr>. |
| 1483 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1484 | //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] | 1485 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | msg->sol = ptr; |
| 1488 | if (likely(*ptr == '\r')) |
| 1489 | 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] | 1490 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1491 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | http_msg_rqline_end: |
| 1493 | case HTTP_MSG_RQLINE_END: |
| 1494 | /* check for HTTP/0.9 request : no version information available. |
| 1495 | * msg->sol must point to the first of CR or LF. |
| 1496 | */ |
| 1497 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1498 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1501 | 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] | 1502 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1504 | /* |
| 1505 | * Common states below |
| 1506 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | http_msg_hdr_first: |
| 1508 | case HTTP_MSG_HDR_FIRST: |
| 1509 | msg->sol = ptr; |
| 1510 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1511 | goto http_msg_hdr_name; |
| 1512 | } |
| 1513 | |
| 1514 | if (likely(*ptr == '\r')) |
| 1515 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1516 | goto http_msg_last_lf; |
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 | http_msg_hdr_name: |
| 1519 | case HTTP_MSG_HDR_NAME: |
| 1520 | /* assumes msg->sol points to the first char */ |
| 1521 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1522 | 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] | 1523 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | if (likely(*ptr == ':')) { |
| 1525 | msg->col = ptr - buf->data; |
| 1526 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1527 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | http_msg_hdr_l1_sp: |
| 1532 | case HTTP_MSG_HDR_L1_SP: |
| 1533 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1534 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1535 | 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] | 1536 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | /* header value can be basically anything except CR/LF */ |
| 1538 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1539 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1541 | goto http_msg_hdr_val; |
| 1542 | } |
| 1543 | |
| 1544 | if (likely(*ptr == '\r')) |
| 1545 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1546 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | http_msg_hdr_l1_lf: |
| 1549 | case HTTP_MSG_HDR_L1_LF: |
| 1550 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1551 | 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] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | http_msg_hdr_l1_lws: |
| 1554 | case HTTP_MSG_HDR_L1_LWS: |
| 1555 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1556 | /* replace HT,CR,LF with spaces */ |
| 1557 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1558 | buf->data[msg->sov] = ' '; |
| 1559 | goto http_msg_hdr_l1_sp; |
| 1560 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1561 | /* we had a header consisting only in spaces ! */ |
| 1562 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | goto http_msg_complete_header; |
| 1564 | |
| 1565 | http_msg_hdr_val: |
| 1566 | case HTTP_MSG_HDR_VAL: |
| 1567 | /* assumes msg->sol points to the first char, msg->col to the |
| 1568 | * colon, and msg->sov points to the first character of the |
| 1569 | * value. |
| 1570 | */ |
| 1571 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1572 | 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] | 1573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | msg->eol = ptr; |
| 1575 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1576 | * real header end in case it ends with lots of LWS, but is this |
| 1577 | * really needed ? |
| 1578 | */ |
| 1579 | if (likely(*ptr == '\r')) |
| 1580 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1581 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1582 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | http_msg_hdr_l2_lf: |
| 1584 | case HTTP_MSG_HDR_L2_LF: |
| 1585 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1586 | 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] | 1587 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | http_msg_hdr_l2_lws: |
| 1589 | case HTTP_MSG_HDR_L2_LWS: |
| 1590 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1591 | /* LWS: replace HT,CR,LF with spaces */ |
| 1592 | for (; msg->eol < ptr; msg->eol++) |
| 1593 | *msg->eol = ' '; |
| 1594 | goto http_msg_hdr_val; |
| 1595 | } |
| 1596 | http_msg_complete_header: |
| 1597 | /* |
| 1598 | * It was a new header, so the last one is finished. |
| 1599 | * Assumes msg->sol points to the first char, msg->col to the |
| 1600 | * colon, msg->sov points to the first character of the value |
| 1601 | * and msg->eol to the first CR or LF so we know how the line |
| 1602 | * ends. We insert last header into the index. |
| 1603 | */ |
| 1604 | /* |
| 1605 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1606 | write(2, msg->sol, msg->eol-msg->sol); |
| 1607 | fprintf(stderr,"\n"); |
| 1608 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1611 | idx, idx->tail) < 0)) |
| 1612 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | msg->sol = ptr; |
| 1615 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1616 | goto http_msg_hdr_name; |
| 1617 | } |
| 1618 | |
| 1619 | if (likely(*ptr == '\r')) |
| 1620 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1621 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | http_msg_last_lf: |
| 1624 | case HTTP_MSG_LAST_LF: |
| 1625 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1626 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1627 | ptr++; |
| 1628 | buf->lr = ptr; |
| 1629 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1630 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | return; |
| 1632 | #ifdef DEBUG_FULL |
| 1633 | default: |
| 1634 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1635 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1636 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | } |
| 1638 | http_msg_ood: |
| 1639 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1640 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | buf->lr = ptr; |
| 1642 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1644 | http_msg_invalid: |
| 1645 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1646 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | return; |
| 1648 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1649 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1650 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1651 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1652 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1653 | * t->req->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1654 | * functions. Its behaviour is rather simple : |
| 1655 | * - all enabled analysers are called in turn from the lower to the higher |
| 1656 | * bit. |
| 1657 | * - if an analyser does not have enough data, it must return without calling |
| 1658 | * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure |
| 1659 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1660 | * the protocol. Generally it is not reset in case of errors. |
| 1661 | * - if an analyser has enough data, it just has to pass on to the next |
| 1662 | * analyser without touching BF_MAY_FORWARD (it is enabled prior to |
| 1663 | * analysis). |
| 1664 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1665 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1666 | * |
| 1667 | * In the future, analysers should be able to indicate that they want to be |
| 1668 | * called after XXX bytes have been received (or transfered), and the min of |
| 1669 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1670 | * |
| 1671 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1673 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | struct buffer *req = t->req; |
| 1676 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 1678 | DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n", |
| 1679 | now_ms, __FUNCTION__, |
| 1680 | cli_stnames[t->cli_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 1681 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 1682 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1683 | req->rex, rep->wex, req->flags, rep->flags, req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1684 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1685 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1686 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1687 | struct tcp_rule *rule; |
| 1688 | int partial; |
| 1689 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1690 | /* We will abort if we encounter a read error. In theory, we |
| 1691 | * should not abort if we get a close, it might be valid, |
| 1692 | * although very unlikely. FIXME: we'll abort for now, this |
| 1693 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1694 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1695 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1696 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1697 | t->fe->failed_req++; |
| 1698 | if (!(t->flags & SN_ERR_MASK)) |
| 1699 | t->flags |= SN_ERR_CLICL; |
| 1700 | if (!(t->flags & SN_FINST_MASK)) |
| 1701 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1702 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1706 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1707 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1708 | t->fe->failed_req++; |
| 1709 | if (!(t->flags & SN_ERR_MASK)) |
| 1710 | t->flags |= SN_ERR_CLITO; |
| 1711 | if (!(t->flags & SN_FINST_MASK)) |
| 1712 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1713 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /* We don't know whether we have enough data, so must proceed |
| 1717 | * this way : |
| 1718 | * - iterate through all rules in their declaration order |
| 1719 | * - if one rule returns MISS, it means the inspect delay is |
| 1720 | * not over yet, then return immediately, otherwise consider |
| 1721 | * it as a non-match. |
| 1722 | * - if one rule returns OK, then return OK |
| 1723 | * - if one rule returns KO, then return KO |
| 1724 | */ |
| 1725 | |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1726 | if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1727 | partial = 0; |
| 1728 | else |
| 1729 | partial = ACL_PARTIAL; |
| 1730 | |
| 1731 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1732 | int ret = ACL_PAT_PASS; |
| 1733 | |
| 1734 | if (rule->cond) { |
| 1735 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1736 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1737 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1738 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1739 | if (!tick_isset(req->analyse_exp)) |
| 1740 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1741 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1742 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1743 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1744 | ret = acl_pass(ret); |
| 1745 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1746 | ret = !ret; |
| 1747 | } |
| 1748 | |
| 1749 | if (ret) { |
| 1750 | /* we have a matching rule. */ |
| 1751 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 1752 | buffer_abort(req); |
| 1753 | buffer_abort(rep); |
| 1754 | //FIXME: this delete this |
| 1755 | //fd_delete(t->cli_fd); |
| 1756 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1757 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1758 | t->fe->failed_req++; |
| 1759 | if (!(t->flags & SN_ERR_MASK)) |
| 1760 | t->flags |= SN_ERR_PRXCOND; |
| 1761 | if (!(t->flags & SN_FINST_MASK)) |
| 1762 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1763 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1764 | } |
| 1765 | /* otherwise accept */ |
| 1766 | break; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1770 | /* if we get there, it means we have no rule which matches, or |
| 1771 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1772 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1773 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1774 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1775 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1776 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1777 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | /* |
| 1779 | * Now parse the partial (or complete) lines. |
| 1780 | * We will check the request syntax, and also join multi-line |
| 1781 | * headers. An index of all the lines will be elaborated while |
| 1782 | * parsing. |
| 1783 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1784 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1785 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1787 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | * req->data + req->eoh = end of processed headers / start of current one |
| 1789 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1790 | * req->lr = first non-visited byte |
| 1791 | * req->r = end of data |
| 1792 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1793 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1795 | struct http_txn *txn = &t->txn; |
| 1796 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1798 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1800 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1801 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1802 | /* 1: we might have to print this header in debug mode */ |
| 1803 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1804 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1805 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1807 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1808 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1809 | eol = sol + msg->sl.rq.l; |
| 1810 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1811 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1812 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1813 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1814 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1816 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1818 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1819 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | } |
| 1821 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1823 | |
| 1824 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1826 | * If not so, we check the FD and buffer states before leaving. |
| 1827 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1829 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1830 | * |
| 1831 | */ |
| 1832 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1833 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1834 | /* |
| 1835 | * First, let's catch bad requests. |
| 1836 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1837 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1839 | |
| 1840 | /* 1: Since we are in header mode, if there's no space |
| 1841 | * left for headers, we won't be able to free more |
| 1842 | * later, so the session will never terminate. We |
| 1843 | * must terminate it now. |
| 1844 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1845 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | /* FIXME: check if URI is set and return Status |
| 1847 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1848 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1849 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1852 | /* 2: have we encountered a close ? */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 1853 | else if (req->flags & (BF_READ_NULL | BF_SHUTR)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1854 | txn->status = 400; |
| 1855 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
| 1856 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1857 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1858 | t->fe->failed_req++; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1859 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1860 | if (!(t->flags & SN_ERR_MASK)) |
| 1861 | t->flags |= SN_ERR_CLICL; |
| 1862 | if (!(t->flags & SN_FINST_MASK)) |
| 1863 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1864 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1868 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1869 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1870 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1871 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1872 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1873 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1874 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1875 | if (!(t->flags & SN_ERR_MASK)) |
| 1876 | t->flags |= SN_ERR_CLITO; |
| 1877 | if (!(t->flags & SN_FINST_MASK)) |
| 1878 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1879 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1880 | } |
| 1881 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 1882 | /* 4: have we encountered a read error ? */ |
| 1883 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1884 | /* we cannot return any message on error */ |
| 1885 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1886 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1887 | t->fe->failed_req++; |
| 1888 | if (!(t->flags & SN_ERR_MASK)) |
| 1889 | t->flags |= SN_ERR_CLICL; |
| 1890 | if (!(t->flags & SN_FINST_MASK)) |
| 1891 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1892 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1893 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1894 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1895 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1896 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1897 | if (!tick_isset(req->analyse_exp)) |
| 1898 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1899 | |
| 1900 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1901 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | /**************************************************************** |
| 1906 | * More interesting part now : we know that we have a complete * |
| 1907 | * request which at least looks like HTTP. We have an indicator * |
| 1908 | * of each header's length, so we can parse them quickly. * |
| 1909 | ****************************************************************/ |
| 1910 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1911 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1912 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1913 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1914 | /* ensure we keep this pointer to the beginning of the message */ |
| 1915 | msg->sol = req->data + msg->som; |
| 1916 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1917 | /* |
| 1918 | * 1: identify the method |
| 1919 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1920 | 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] | 1921 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 1922 | /* we can make use of server redirect on GET and HEAD */ |
| 1923 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1924 | t->flags |= SN_REDIRECTABLE; |
| 1925 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1926 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1928 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1929 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1930 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1931 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1932 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1933 | !memcmp(&req->data[msg->sl.rq.u], |
| 1934 | t->fe->monitor_uri, |
| 1935 | t->fe->monitor_uri_len))) { |
| 1936 | /* |
| 1937 | * We have found the monitor URI |
| 1938 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1939 | struct acl_cond *cond; |
| 1940 | cur_proxy = t->fe; |
| 1941 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1942 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1943 | |
| 1944 | /* Check if we want to fail this monitor request or not */ |
| 1945 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1946 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1947 | |
| 1948 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1949 | if (cond->pol == ACL_COND_UNLESS) |
| 1950 | ret = !ret; |
| 1951 | |
| 1952 | if (ret) { |
| 1953 | /* we fail this request, let's return 503 service unavail */ |
| 1954 | txn->status = 503; |
| 1955 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1956 | goto return_prx_cond; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1961 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1962 | client_retnclose(t, &http_200_chunk); |
| 1963 | goto return_prx_cond; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1968 | * Note: we cannot log anymore if the request has been |
| 1969 | * classified as invalid. |
| 1970 | */ |
| 1971 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1972 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1973 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1974 | int urilen = msg->sl.rq.l; |
| 1975 | |
| 1976 | if (urilen >= REQURI_LEN) |
| 1977 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1978 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1979 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1980 | |
| 1981 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1982 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1983 | } else { |
| 1984 | Alert("HTTP logging : out of memory.\n"); |
| 1985 | } |
| 1986 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1987 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1989 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1990 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1991 | int delta; |
| 1992 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1993 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1994 | cur_end = msg->sol + msg->sl.rq.l; |
| 1995 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1996 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1997 | if (msg->sl.rq.u_l == 0) { |
| 1998 | /* if no URI was set, add "/" */ |
| 1999 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2000 | cur_end += delta; |
| 2001 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2002 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2003 | /* add HTTP version */ |
| 2004 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2005 | msg->eoh += delta; |
| 2006 | cur_end += delta; |
| 2007 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2008 | HTTP_MSG_RQMETH, |
| 2009 | msg->sol, cur_end + 1, |
| 2010 | NULL, NULL); |
| 2011 | if (unlikely(!cur_end)) |
| 2012 | goto return_bad_req; |
| 2013 | |
| 2014 | /* we have a full HTTP/1.0 request now and we know that |
| 2015 | * we have either a CR or an LF at <ptr>. |
| 2016 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2017 | 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] | 2018 | } |
| 2019 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2020 | |
| 2021 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2022 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2023 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2024 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2025 | |
| 2026 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2027 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2028 | * As opposed to version 1.2, now they will be evaluated in the |
| 2029 | * filters order and not in the header order. This means that |
| 2030 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2031 | * |
| 2032 | * We can now check whether we want to switch to another |
| 2033 | * backend, in which case we will re-check the backend's |
| 2034 | * filters and various options. In order to support 3-level |
| 2035 | * switching, here's how we should proceed : |
| 2036 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2037 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2038 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2039 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2040 | * There cannot be any switch from there, so ->be cannot be |
| 2041 | * changed anymore. |
| 2042 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2043 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2044 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2045 | * The response path will be able to apply either ->be, or |
| 2046 | * ->be then ->fe filters in order to match the reverse of |
| 2047 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2048 | */ |
| 2049 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2050 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2051 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2052 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2053 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2054 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2055 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2056 | /* first check whether we have some ACLs set to redirect this request */ |
| 2057 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2058 | int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2059 | |
| 2060 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2061 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2062 | ret = !ret; |
| 2063 | |
| 2064 | if (ret) { |
| 2065 | struct chunk rdr = { trash, 0 }; |
| 2066 | const char *msg_fmt; |
| 2067 | |
| 2068 | /* build redirect message */ |
| 2069 | switch(rule->code) { |
| 2070 | case 303: |
| 2071 | rdr.len = strlen(HTTP_303); |
| 2072 | msg_fmt = HTTP_303; |
| 2073 | break; |
| 2074 | case 301: |
| 2075 | rdr.len = strlen(HTTP_301); |
| 2076 | msg_fmt = HTTP_301; |
| 2077 | break; |
| 2078 | case 302: |
| 2079 | default: |
| 2080 | rdr.len = strlen(HTTP_302); |
| 2081 | msg_fmt = HTTP_302; |
| 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | if (unlikely(rdr.len > sizeof(trash))) |
| 2086 | goto return_bad_req; |
| 2087 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2088 | |
| 2089 | switch(rule->type) { |
| 2090 | case REDIRECT_TYPE_PREFIX: { |
| 2091 | const char *path; |
| 2092 | int pathlen; |
| 2093 | |
| 2094 | path = http_get_path(txn); |
| 2095 | /* build message using path */ |
| 2096 | if (path) { |
| 2097 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2098 | } else { |
| 2099 | path = "/"; |
| 2100 | pathlen = 1; |
| 2101 | } |
| 2102 | |
| 2103 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2104 | goto return_bad_req; |
| 2105 | |
| 2106 | /* add prefix */ |
| 2107 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2108 | rdr.len += rule->rdr_len; |
| 2109 | |
| 2110 | /* add path */ |
| 2111 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2112 | rdr.len += pathlen; |
| 2113 | break; |
| 2114 | } |
| 2115 | case REDIRECT_TYPE_LOCATION: |
| 2116 | default: |
| 2117 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2118 | goto return_bad_req; |
| 2119 | |
| 2120 | /* add location */ |
| 2121 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2122 | rdr.len += rule->rdr_len; |
| 2123 | break; |
| 2124 | } |
| 2125 | |
| 2126 | /* add end of headers */ |
| 2127 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2128 | rdr.len += 4; |
| 2129 | |
| 2130 | txn->status = rule->code; |
| 2131 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2132 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2133 | client_retnclose(t, &rdr); |
| 2134 | goto return_prx_cond; |
| 2135 | } |
| 2136 | } |
| 2137 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2138 | /* first check whether we have some ACLs set to block this request */ |
| 2139 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2140 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2141 | |
| 2142 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2143 | if (cond->pol == ACL_COND_UNLESS) |
| 2144 | ret = !ret; |
| 2145 | |
| 2146 | if (ret) { |
| 2147 | txn->status = 403; |
| 2148 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2149 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2150 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2151 | goto return_prx_cond; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2155 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2156 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2157 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2158 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2161 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2162 | /* to ensure correct connection accounting on |
| 2163 | * the backend, we count the connection for the |
| 2164 | * one managing the queue. |
| 2165 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2166 | t->be->beconn++; |
| 2167 | if (t->be->beconn > t->be->beconn_max) |
| 2168 | t->be->beconn_max = t->be->beconn; |
| 2169 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2170 | t->flags |= SN_BE_ASSIGNED; |
| 2171 | } |
| 2172 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2173 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2174 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2175 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2176 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2177 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2178 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2179 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2180 | goto return_prx_cond; |
| 2181 | } |
| 2182 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2183 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2184 | 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] | 2185 | !(t->flags & SN_CONN_CLOSED)) { |
| 2186 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2187 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2188 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2189 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2190 | 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] | 2191 | old_idx = 0; |
| 2192 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2193 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2194 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2195 | cur_ptr = cur_next; |
| 2196 | cur_end = cur_ptr + cur_hdr->len; |
| 2197 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2198 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2199 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2200 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2201 | /* 3 possibilities : |
| 2202 | * - we have already set Connection: close, |
| 2203 | * so we remove this line. |
| 2204 | * - we have not yet set Connection: close, |
| 2205 | * but this line indicates close. We leave |
| 2206 | * it untouched and set the flag. |
| 2207 | * - we have not yet set Connection: close, |
| 2208 | * and this line indicates non-close. We |
| 2209 | * replace it. |
| 2210 | */ |
| 2211 | if (t->flags & SN_CONN_CLOSED) { |
| 2212 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2213 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2214 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2215 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2216 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2217 | cur_hdr->len = 0; |
| 2218 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2219 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2220 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2221 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2222 | cur_next += delta; |
| 2223 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2224 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2225 | } |
| 2226 | t->flags |= SN_CONN_CLOSED; |
| 2227 | } |
| 2228 | } |
| 2229 | old_idx = cur_idx; |
| 2230 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2231 | } |
| 2232 | /* add request headers from the rule sets in the same order */ |
| 2233 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2234 | if (unlikely(http_header_add_tail(req, |
| 2235 | &txn->req, |
| 2236 | &txn->hdr_idx, |
| 2237 | rule_set->req_add[cur_idx])) < 0) |
| 2238 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2239 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2240 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2241 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2242 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2243 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2244 | /* we have to check the URI and auth for this request. |
| 2245 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2246 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2247 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2248 | if (stats_check_uri_auth(t, rule_set)) |
| 2249 | return 1; |
| 2250 | } |
| 2251 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2252 | /* now check whether we have some switching rules for this request */ |
| 2253 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2254 | struct switching_rule *rule; |
| 2255 | |
| 2256 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2257 | int ret; |
| 2258 | |
| 2259 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2260 | |
| 2261 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2262 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2263 | ret = !ret; |
| 2264 | |
| 2265 | if (ret) { |
| 2266 | t->be = rule->be.backend; |
| 2267 | t->be->beconn++; |
| 2268 | if (t->be->beconn > t->be->beconn_max) |
| 2269 | t->be->beconn_max = t->be->beconn; |
| 2270 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2271 | |
| 2272 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2273 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2274 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2275 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2276 | t->flags |= SN_BE_ASSIGNED; |
| 2277 | break; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2282 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2283 | /* No backend was set, but there was a default |
| 2284 | * backend set in the frontend, so we use it and |
| 2285 | * loop again. |
| 2286 | */ |
| 2287 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2288 | t->be->beconn++; |
| 2289 | if (t->be->beconn > t->be->beconn_max) |
| 2290 | t->be->beconn_max = t->be->beconn; |
| 2291 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2292 | |
| 2293 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2294 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2295 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2296 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2297 | t->flags |= SN_BE_ASSIGNED; |
| 2298 | } |
| 2299 | } 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] | 2300 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2301 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2302 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2303 | /* To ensure correct connection accounting on |
| 2304 | * the backend, we count the connection for the |
| 2305 | * one managing the queue. |
| 2306 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2307 | t->be->beconn++; |
| 2308 | if (t->be->beconn > t->be->beconn_max) |
| 2309 | t->be->beconn_max = t->be->beconn; |
| 2310 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2311 | t->flags |= SN_BE_ASSIGNED; |
| 2312 | } |
| 2313 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2314 | /* |
| 2315 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2316 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2317 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2318 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2319 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2320 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2321 | /* |
| 2322 | * If HTTP PROXY is set we simply get remote server address |
| 2323 | * parsing incoming request. |
| 2324 | */ |
| 2325 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2326 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2327 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2328 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2329 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2330 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2331 | * so let's do the same now. |
| 2332 | */ |
| 2333 | |
| 2334 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2335 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2336 | 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] | 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2341 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2342 | * Note that doing so might move headers in the request, but |
| 2343 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2344 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2345 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2346 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2347 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2348 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2349 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2350 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2351 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2352 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2353 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2354 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2355 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2356 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2357 | /* Add an X-Forwarded-For header unless the source IP is |
| 2358 | * in the 'except' network range. |
| 2359 | */ |
| 2360 | if ((!t->fe->except_mask.s_addr || |
| 2361 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2362 | != t->fe->except_net.s_addr) && |
| 2363 | (!t->be->except_mask.s_addr || |
| 2364 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2365 | != t->be->except_net.s_addr)) { |
| 2366 | int len; |
| 2367 | unsigned char *pn; |
| 2368 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2369 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2370 | /* Note: we rely on the backend to get the header name to be used for |
| 2371 | * x-forwarded-for, because the header is really meant for the backends. |
| 2372 | * However, if the backend did not specify any option, we have to rely |
| 2373 | * on the frontend's header name. |
| 2374 | */ |
| 2375 | if (t->be->fwdfor_hdr_len) { |
| 2376 | len = t->be->fwdfor_hdr_len; |
| 2377 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2378 | } else { |
| 2379 | len = t->fe->fwdfor_hdr_len; |
| 2380 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2381 | } |
| 2382 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2383 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2384 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2385 | &txn->hdr_idx, trash, len)) < 0) |
| 2386 | goto return_bad_req; |
| 2387 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2388 | } |
| 2389 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2390 | /* FIXME: for the sake of completeness, we should also support |
| 2391 | * 'except' here, although it is mostly useless in this case. |
| 2392 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2393 | int len; |
| 2394 | char pn[INET6_ADDRSTRLEN]; |
| 2395 | inet_ntop(AF_INET6, |
| 2396 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2397 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2398 | |
| 2399 | /* Note: we rely on the backend to get the header name to be used for |
| 2400 | * x-forwarded-for, because the header is really meant for the backends. |
| 2401 | * However, if the backend did not specify any option, we have to rely |
| 2402 | * on the frontend's header name. |
| 2403 | */ |
| 2404 | if (t->be->fwdfor_hdr_len) { |
| 2405 | len = t->be->fwdfor_hdr_len; |
| 2406 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2407 | } else { |
| 2408 | len = t->fe->fwdfor_hdr_len; |
| 2409 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2410 | } |
| 2411 | len += sprintf(trash + len, ": %s", pn); |
| 2412 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2413 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2414 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2415 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2416 | } |
| 2417 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2418 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2419 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2420 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2421 | * 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] | 2422 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2423 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2424 | ((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] | 2425 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2426 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2427 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2428 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2429 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2430 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2431 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2432 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2433 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2434 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2435 | * (unless headers overflowed the buffer?) |
| 2436 | */ |
| 2437 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2438 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2439 | t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2440 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2441 | /* are there enough bytes here? total == l || r || rlim ? |
| 2442 | * len is unsigned, but eoh is int, |
| 2443 | * how many bytes of body have we received? |
| 2444 | * eoh is the first empty line of the header |
| 2445 | */ |
| 2446 | /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */ |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 2447 | unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2448 | |
| 2449 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2450 | * We can't assume responsibility for the server's decision, |
| 2451 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2452 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2453 | */ |
| 2454 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2455 | struct hdr_ctx ctx; |
| 2456 | ctx.idx = 0; |
| 2457 | /* Expect is allowed in 1.1, look for it */ |
| 2458 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2459 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2460 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2461 | /* We can't reliablly stall and wait for data, because of |
| 2462 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2463 | * the next block to check length expectations. |
| 2464 | * We could send 100 status back to the client, but then we need to |
| 2465 | * re-write headers, and send the message. And this isn't the right |
| 2466 | * place for that action. |
| 2467 | * TODO: support Expect elsewhere and delete this block. |
| 2468 | */ |
| 2469 | goto end_check_maybe_wait_for_body; |
| 2470 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2471 | |
| 2472 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2473 | /* nothing to do, we got enough */ |
| 2474 | } else { |
| 2475 | /* limit implies we are supposed to need this many bytes |
| 2476 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2477 | */ |
| 2478 | long long hint = len; |
| 2479 | struct hdr_ctx ctx; |
| 2480 | ctx.idx = 0; |
| 2481 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2482 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2483 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2484 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2485 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2486 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2487 | ctx.idx = 0; |
| 2488 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2489 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2490 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2491 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2492 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2493 | hint = 0; /* parse failure, untrusted client */ |
| 2494 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2495 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2496 | msg->hdr_content_len = hint; |
| 2497 | else |
| 2498 | hint = 0; /* bad client, sent negative length */ |
| 2499 | } |
| 2500 | } |
| 2501 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2502 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2503 | hint = t->be->url_param_post_limit; |
| 2504 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2505 | if (len < hint) { |
| 2506 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2507 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2508 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2509 | /* else... There are no body bytes to wait for */ |
| 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2514 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2515 | /************************************************************* |
| 2516 | * OK, that's finished for the headers. We have done what we * |
| 2517 | * could. Let's switch to the DATA state. * |
| 2518 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2519 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2520 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2521 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2522 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2523 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2524 | * which may be the same as the connect timeout if unspecified. |
| 2525 | * If unset, then set it to zero because we really want it to |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2526 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2527 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2528 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2529 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2530 | /* flush the request so that we can drop the connection early |
| 2531 | * if the client closes first. |
| 2532 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2533 | req->flags &= ~BF_MAY_FORWARD; |
| 2534 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2535 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2536 | if (!req->analyse_exp) |
| 2537 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2538 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2540 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2541 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2542 | |
| 2543 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2544 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2545 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2546 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2547 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2548 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2549 | return_prx_cond: |
| 2550 | if (!(t->flags & SN_ERR_MASK)) |
| 2551 | t->flags |= SN_ERR_PRXCOND; |
| 2552 | if (!(t->flags & SN_FINST_MASK)) |
| 2553 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2554 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2555 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2556 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2557 | } |
| 2558 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2559 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2560 | struct http_txn *txn = &t->txn; |
| 2561 | |
| 2562 | /* This connection is being tarpitted. The CLIENT side has |
| 2563 | * already set the connect expiration date to the right |
| 2564 | * timeout. We just have to check that the client is still |
| 2565 | * there and that the timeout has not expired. |
| 2566 | */ |
| 2567 | if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 && |
| 2568 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2569 | return 0; |
| 2570 | |
| 2571 | /* We will set the queue timer to the time spent, just for |
| 2572 | * logging purposes. We fake a 500 server error, so that the |
| 2573 | * attacker will not suspect his connection has been tarpitted. |
| 2574 | * It will not cause trouble to the logs because we can exclude |
| 2575 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2576 | */ |
| 2577 | trace_term(t, TT_HTTP_SRV_2); |
| 2578 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2579 | |
| 2580 | txn->status = 500; |
| 2581 | if (req->flags != BF_READ_ERROR) |
| 2582 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2583 | |
| 2584 | req->analysers = 0; |
| 2585 | req->analyse_exp = TICK_ETERNITY; |
| 2586 | |
| 2587 | t->fe->failed_req++; |
| 2588 | if (!(t->flags & SN_ERR_MASK)) |
| 2589 | t->flags |= SN_ERR_PRXCOND; |
| 2590 | if (!(t->flags & SN_FINST_MASK)) |
| 2591 | t->flags |= SN_FINST_T; |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2595 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2596 | /* We have to parse the HTTP request body to find any required data. |
| 2597 | * "balance url_param check_post" should have been the only way to get |
| 2598 | * into this. We were brought here after HTTP header analysis, so all |
| 2599 | * related structures are ready. |
| 2600 | */ |
| 2601 | struct http_msg *msg = &t->txn.req; |
| 2602 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2603 | long long limit = t->be->url_param_post_limit; |
| 2604 | struct hdr_ctx ctx; |
| 2605 | |
| 2606 | ctx.idx = 0; |
| 2607 | |
| 2608 | /* now if we have a length, we'll take the hint */ |
| 2609 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2610 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2611 | unsigned int chunk = 0; |
| 2612 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2613 | char c = msg->sol[body]; |
| 2614 | if (ishex(c)) { |
| 2615 | unsigned int hex = toupper(c) - '0'; |
| 2616 | if (hex > 9) |
| 2617 | hex -= 'A' - '9' - 1; |
| 2618 | chunk = (chunk << 4) | hex; |
| 2619 | } else |
| 2620 | break; |
| 2621 | body++; |
| 2622 | } |
| 2623 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2624 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2625 | |
| 2626 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2627 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2628 | |
| 2629 | body += 2; // skip CRLF |
| 2630 | |
| 2631 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2632 | * 1. how much entity data do we have? new var |
| 2633 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2634 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2635 | */ |
| 2636 | |
| 2637 | if (chunk < limit) |
| 2638 | limit = chunk; /* only reading one chunk */ |
| 2639 | } else { |
| 2640 | if (msg->hdr_content_len < limit) |
| 2641 | limit = msg->hdr_content_len; |
| 2642 | } |
| 2643 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2644 | http_body_end: |
| 2645 | /* we leave once we know we have nothing left to do. This means that we have |
| 2646 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2647 | * buffer closed). |
| 2648 | */ |
| 2649 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2650 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2651 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2652 | /* The situation will not evolve, so let's give up on the analysis. */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2653 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2654 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2655 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2656 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2657 | else { |
| 2658 | /* Not enough data. We'll re-use the http-request |
| 2659 | * timeout here. Ideally, we should set the timeout |
| 2660 | * relative to the accept() date. We just set the |
| 2661 | * request timeout once at the beginning of the |
| 2662 | * request. |
| 2663 | */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2664 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2665 | if (!tick_isset(req->analyse_exp)) |
| 2666 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2667 | return 0; |
| 2668 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2669 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2670 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2671 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2672 | * probably reduce one day's debugging session. |
| 2673 | */ |
| 2674 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2675 | if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2676 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2677 | __FILE__, __LINE__, req->analysers); |
| 2678 | ABORT_NOW(); |
| 2679 | } |
| 2680 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2681 | req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2682 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2683 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2684 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2685 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2686 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2687 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2688 | * t->rep->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2689 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2690 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2691 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2692 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2693 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2694 | struct buffer *req = t->req; |
| 2695 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2696 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2697 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n", |
| 2698 | now_ms, __FUNCTION__, |
| 2699 | cli_stnames[t->cli_state], |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2700 | req->rex, rep->wex, req->flags, rep->flags, rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2702 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2703 | /* |
| 2704 | * Now parse the partial (or complete) lines. |
| 2705 | * We will check the response syntax, and also join multi-line |
| 2706 | * headers. An index of all the lines will be elaborated while |
| 2707 | * parsing. |
| 2708 | * |
| 2709 | * For the parsing, we use a 28 states FSM. |
| 2710 | * |
| 2711 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2712 | * rep->data + rep->som = beginning of response |
| 2713 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2714 | * rep->data + rep->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2715 | * rep->lr = first non-visited byte |
| 2716 | * rep->r = end of data |
| 2717 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2718 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2719 | int cur_idx; |
| 2720 | struct http_msg *msg = &txn->rsp; |
| 2721 | struct proxy *cur_proxy; |
| 2722 | |
| 2723 | if (likely(rep->lr < rep->r)) |
| 2724 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2725 | |
| 2726 | /* 1: we might have to print this header in debug mode */ |
| 2727 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2728 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2729 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2730 | char *eol, *sol; |
| 2731 | |
| 2732 | sol = rep->data + msg->som; |
| 2733 | eol = sol + msg->sl.rq.l; |
| 2734 | debug_hdr("srvrep", t, sol, eol); |
| 2735 | |
| 2736 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2737 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2738 | |
| 2739 | while (cur_idx) { |
| 2740 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2741 | debug_hdr("srvhdr", t, sol, eol); |
| 2742 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2743 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2744 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2745 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2746 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2747 | /* |
| 2748 | * Now we quickly check if we have found a full valid response. |
| 2749 | * If not so, we check the FD and buffer states before leaving. |
| 2750 | * A full response is indicated by the fact that we have seen |
| 2751 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2752 | * responses are checked first. |
| 2753 | * |
| 2754 | * Depending on whether the client is still there or not, we |
| 2755 | * may send an error response back or not. Note that normally |
| 2756 | * we should only check for HTTP status there, and check I/O |
| 2757 | * errors somewhere else. |
| 2758 | */ |
| 2759 | |
| 2760 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2761 | /* Invalid response */ |
| 2762 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2763 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2764 | //buffer_shutr(rep); |
| 2765 | //buffer_shutw(req); |
| 2766 | //fd_delete(req->cons->fd); |
| 2767 | //req->cons->state = SI_ST_CLO; |
| 2768 | buffer_shutr_now(rep); |
| 2769 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2770 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2771 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2772 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2773 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2774 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2775 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2776 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2777 | txn->status = 502; |
| 2778 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2779 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2780 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2781 | if (!(t->flags & SN_FINST_MASK)) |
| 2782 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2783 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2784 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2785 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2787 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2788 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2789 | /* write error to client, read error or close from server */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2790 | if (rep->flags & (BF_WRITE_ERROR|BF_SHUTW|BF_READ_ERROR|BF_SHUTR|BF_READ_NULL)) { |
| 2791 | buffer_shutr_now(rep); |
| 2792 | buffer_shutw_now(req); |
| 2793 | //fd_delete(req->cons->fd); |
| 2794 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2795 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2796 | //t->srv->cur_sess--; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2797 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2798 | //sess_change_server(t, NULL); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2799 | } |
| 2800 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2801 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2802 | txn->status = 502; |
| 2803 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2804 | if (!(t->flags & SN_ERR_MASK)) |
| 2805 | t->flags |= SN_ERR_SRVCL; |
| 2806 | if (!(t->flags & SN_FINST_MASK)) |
| 2807 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2808 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2809 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2810 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2811 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2812 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2813 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2814 | /* too large response does not fit in buffer. */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2815 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2816 | goto hdr_response_bad; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2817 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2818 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2819 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2820 | buffer_shutr_now(rep); |
| 2821 | buffer_shutw_now(req); |
| 2822 | //fd_delete(req->cons->fd); |
| 2823 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2824 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2825 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2826 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2827 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2828 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2829 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2830 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2831 | txn->status = 504; |
| 2832 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2833 | if (!(t->flags & SN_ERR_MASK)) |
| 2834 | t->flags |= SN_ERR_SRVTO; |
| 2835 | if (!(t->flags & SN_FINST_MASK)) |
| 2836 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2837 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2838 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2839 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2840 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2841 | } |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2842 | |
| 2843 | rep->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2844 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2845 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2847 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2848 | /***************************************************************** |
| 2849 | * More interesting part now : we know that we have a complete * |
| 2850 | * response which at least looks like HTTP. We have an indicator * |
| 2851 | * of each header's length, so we can parse them quickly. * |
| 2852 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2854 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2856 | /* ensure we keep this pointer to the beginning of the message */ |
| 2857 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2858 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2859 | /* |
| 2860 | * 1: get the status code and check for cacheability. |
| 2861 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2862 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2863 | t->logs.logwait &= ~LW_RESP; |
| 2864 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2866 | switch (txn->status) { |
| 2867 | case 200: |
| 2868 | case 203: |
| 2869 | case 206: |
| 2870 | case 300: |
| 2871 | case 301: |
| 2872 | case 410: |
| 2873 | /* RFC2616 @13.4: |
| 2874 | * "A response received with a status code of |
| 2875 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2876 | * by a cache (...) unless a cache-control |
| 2877 | * directive prohibits caching." |
| 2878 | * |
| 2879 | * RFC2616 @9.5: POST method : |
| 2880 | * "Responses to this method are not cacheable, |
| 2881 | * unless the response includes appropriate |
| 2882 | * Cache-Control or Expires header fields." |
| 2883 | */ |
| 2884 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2885 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2886 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2887 | break; |
| 2888 | default: |
| 2889 | break; |
| 2890 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2891 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2892 | /* |
| 2893 | * 2: we may need to capture headers |
| 2894 | */ |
| 2895 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2896 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2897 | txn->rsp.cap, t->fe->rsp_cap); |
| 2898 | |
| 2899 | /* |
| 2900 | * 3: we will have to evaluate the filters. |
| 2901 | * As opposed to version 1.2, now they will be evaluated in the |
| 2902 | * filters order and not in the header order. This means that |
| 2903 | * each filter has to be validated among all headers. |
| 2904 | * |
| 2905 | * Filters are tried with ->be first, then with ->fe if it is |
| 2906 | * different from ->be. |
| 2907 | */ |
| 2908 | |
| 2909 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2910 | |
| 2911 | cur_proxy = t->be; |
| 2912 | while (1) { |
| 2913 | struct proxy *rule_set = cur_proxy; |
| 2914 | |
| 2915 | /* try headers filters */ |
| 2916 | if (rule_set->rsp_exp != NULL) { |
| 2917 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2918 | return_bad_resp: |
| 2919 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2920 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2921 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2922 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2923 | } |
| 2924 | cur_proxy->failed_resp++; |
| 2925 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2926 | buffer_shutr_now(rep); |
| 2927 | buffer_shutw_now(req); |
| 2928 | //fd_delete(req->cons->fd); |
| 2929 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2930 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2931 | txn->status = 502; |
| 2932 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2933 | if (!(t->flags & SN_ERR_MASK)) |
| 2934 | t->flags |= SN_ERR_PRXCOND; |
| 2935 | if (!(t->flags & SN_FINST_MASK)) |
| 2936 | t->flags |= SN_FINST_H; |
| 2937 | /* We used to have a free connection slot. Since we'll never use it, |
| 2938 | * we have to inform the server that it may be used by another session. |
| 2939 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2940 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2941 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2942 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2943 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2944 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2945 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2946 | /* has the response been denied ? */ |
| 2947 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2948 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2949 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2950 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 2951 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2952 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2953 | cur_proxy->denied_resp++; |
| 2954 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2955 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2956 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2957 | /* We might have to check for "Connection:" */ |
| 2958 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2959 | !(t->flags & SN_CONN_CLOSED)) { |
| 2960 | char *cur_ptr, *cur_end, *cur_next; |
| 2961 | int cur_idx, old_idx, delta, val; |
| 2962 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2963 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2964 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2965 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2966 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2967 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2968 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2969 | cur_ptr = cur_next; |
| 2970 | cur_end = cur_ptr + cur_hdr->len; |
| 2971 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2973 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2974 | if (val) { |
| 2975 | /* 3 possibilities : |
| 2976 | * - we have already set Connection: close, |
| 2977 | * so we remove this line. |
| 2978 | * - we have not yet set Connection: close, |
| 2979 | * but this line indicates close. We leave |
| 2980 | * it untouched and set the flag. |
| 2981 | * - we have not yet set Connection: close, |
| 2982 | * and this line indicates non-close. We |
| 2983 | * replace it. |
| 2984 | */ |
| 2985 | if (t->flags & SN_CONN_CLOSED) { |
| 2986 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2987 | txn->rsp.eoh += delta; |
| 2988 | cur_next += delta; |
| 2989 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2990 | txn->hdr_idx.used--; |
| 2991 | cur_hdr->len = 0; |
| 2992 | } else { |
| 2993 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2994 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2995 | "close", 5); |
| 2996 | cur_next += delta; |
| 2997 | cur_hdr->len += delta; |
| 2998 | txn->rsp.eoh += delta; |
| 2999 | } |
| 3000 | t->flags |= SN_CONN_CLOSED; |
| 3001 | } |
| 3002 | } |
| 3003 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3004 | } |
| 3005 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3006 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3007 | /* add response headers from the rule sets in the same order */ |
| 3008 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3009 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3010 | rule_set->rsp_add[cur_idx])) < 0) |
| 3011 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3012 | } |
| 3013 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3014 | /* check whether we're already working on the frontend */ |
| 3015 | if (cur_proxy == t->fe) |
| 3016 | break; |
| 3017 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3018 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3020 | /* |
| 3021 | * 4: check for server cookie. |
| 3022 | */ |
| 3023 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3024 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3025 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3026 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3028 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3029 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3030 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3031 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3032 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3033 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3034 | /* |
| 3035 | * 6: add server cookie in the response if needed |
| 3036 | */ |
| 3037 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3038 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3039 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3041 | /* the server is known, it's not the one the client requested, we have to |
| 3042 | * insert a set-cookie here, except if we want to insert only on POST |
| 3043 | * requests and this one isn't. Note that servers which don't have cookies |
| 3044 | * (eg: some backup servers) will return a full cookie removal request. |
| 3045 | */ |
| 3046 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3047 | t->be->cookie_name, |
| 3048 | 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] | 3049 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3050 | if (t->be->cookie_domain) |
| 3051 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3053 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3054 | trash, len)) < 0) |
| 3055 | goto return_bad_resp; |
| 3056 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3057 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3058 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3059 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3060 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3061 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3062 | */ |
| 3063 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3064 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3065 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3066 | |
| 3067 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3068 | "Cache-control: private", 22)) < 0) |
| 3069 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3070 | } |
| 3071 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3073 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3074 | /* |
| 3075 | * 7: check if result will be cacheable with a cookie. |
| 3076 | * We'll block the response if security checks have caught |
| 3077 | * nasty things such as a cacheable cookie. |
| 3078 | */ |
| 3079 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3080 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3081 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3082 | |
| 3083 | /* we're in presence of a cacheable response containing |
| 3084 | * a set-cookie header. We'll block it as requested by |
| 3085 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3086 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3087 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3088 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3089 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3090 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3091 | } |
| 3092 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3093 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3094 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3095 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3096 | send_log(t->be, LOG_ALERT, |
| 3097 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3098 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3099 | goto return_srv_prx_502; |
| 3100 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3101 | |
| 3102 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3103 | * 8: add "Connection: close" if needed and not yet set. |
| 3104 | * 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] | 3105 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3106 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3107 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3108 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3109 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3110 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3111 | "Connection: close", 17)) < 0) |
| 3112 | goto return_bad_resp; |
| 3113 | t->flags |= SN_CONN_CLOSED; |
| 3114 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3115 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3116 | /************************************************************* |
| 3117 | * OK, that's finished for the headers. We have done what we * |
| 3118 | * could. Let's switch to the DATA state. * |
| 3119 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3120 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3121 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3122 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3123 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3124 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3125 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3126 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3127 | tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3128 | } |
| 3129 | #endif |
| 3130 | /* if the user wants to log as soon as possible, without counting |
| 3131 | * bytes from the server, then this is the right moment. We have |
| 3132 | * to temporarily assign bytes_out to log what we currently have. |
| 3133 | */ |
| 3134 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3135 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3136 | t->logs.bytes_out = txn->rsp.eoh; |
| 3137 | if (t->fe->to_log & LW_REQ) |
| 3138 | http_sess_log(t); |
| 3139 | else |
| 3140 | tcp_sess_log(t); |
| 3141 | t->logs.bytes_out = 0; |
| 3142 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3143 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3144 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3145 | * otherwise we would not let the client side wake up. |
| 3146 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3147 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3148 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3149 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3150 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3151 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3152 | * probably reduce one day's debugging session. |
| 3153 | */ |
| 3154 | #ifdef DEBUG_DEV |
| 3155 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3156 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3157 | __FILE__, __LINE__, rep->analysers); |
| 3158 | ABORT_NOW(); |
| 3159 | } |
| 3160 | #endif |
| 3161 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3164 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3165 | /* |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3166 | * Manages the client FSM and its socket. It normally returns zero, but may |
| 3167 | * return 1 if it absolutely wants to be called again. |
| 3168 | * |
| 3169 | * Note: process_cli is the ONLY function allowed to set cli_state to anything |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3170 | * but CL_STCLOSE. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3171 | */ |
| 3172 | int process_cli(struct session *t) |
| 3173 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3174 | struct buffer *req = t->req; |
| 3175 | struct buffer *rep = t->rep; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3176 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3177 | DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3178 | now_ms, __FUNCTION__, |
| 3179 | t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3180 | cli_stnames[t->cli_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3181 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3182 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3183 | req->rex, rep->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3184 | req->flags, rep->flags, |
| 3185 | req->l, rep->l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3186 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3187 | update_state: |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3188 | /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3189 | * still set this state (and will do until unix sockets are converted). |
| 3190 | */ |
| 3191 | if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3192 | /* we can skip most of the tests at once if some conditions are not met */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3193 | if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3194 | (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3195 | (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3196 | (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3197 | (!(rep->flags & BF_SHUTW) && |
| 3198 | (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3199 | goto update_timeouts; |
| 3200 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3201 | /* read or write error */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3202 | if (fdtab[t->cli_fd].state == FD_STERROR) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3203 | buffer_shutr(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3204 | req->flags |= BF_READ_ERROR; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3205 | buffer_shutw(rep); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3206 | rep->flags |= BF_WRITE_ERROR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3207 | fd_delete(t->cli_fd); |
| 3208 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3209 | trace_term(t, TT_HTTP_CLI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3210 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3211 | if (!(t->flags & SN_ERR_MASK)) |
| 3212 | t->flags |= SN_ERR_CLICL; |
| 3213 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3214 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3215 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3216 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3217 | t->flags |= SN_FINST_C; |
| 3218 | else |
| 3219 | t->flags |= SN_FINST_D; |
| 3220 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3221 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3222 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3223 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3224 | /* last read, or end of server write */ |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3225 | else if (!(req->flags & BF_SHUTR) && /* not already done */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3226 | req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3227 | buffer_shutr(req); |
| 3228 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3229 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3230 | trace_term(t, TT_HTTP_CLI_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3231 | } else { |
| 3232 | /* output was already closed */ |
| 3233 | fd_delete(t->cli_fd); |
| 3234 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3235 | trace_term(t, TT_HTTP_CLI_3); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3236 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3237 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3238 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3239 | /* last server read and buffer empty : we only check them when we're |
| 3240 | * allowed to forward the data. |
| 3241 | */ |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3242 | else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3243 | ((rep->flags & BF_SHUTW_NOW) || |
| 3244 | (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3245 | rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3246 | buffer_shutw(rep); |
| 3247 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3248 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3249 | shutdown(t->cli_fd, SHUT_WR); |
| 3250 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3251 | /* FIXME: is this still true ? */ |
| 3252 | EV_FD_SET(t->cli_fd, DIR_RD); |
| 3253 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3254 | trace_term(t, TT_HTTP_CLI_4); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3255 | } else { |
| 3256 | fd_delete(t->cli_fd); |
| 3257 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3258 | trace_term(t, TT_HTTP_CLI_5); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3259 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3260 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3261 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3262 | /* read timeout */ |
Willy Tarreau | 2500981 | 2008-08-17 18:16:38 +0200 | [diff] [blame] | 3263 | else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3264 | buffer_shutr(req); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3265 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3266 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3267 | trace_term(t, TT_HTTP_CLI_6); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3268 | } else { |
| 3269 | /* output was already closed */ |
| 3270 | fd_delete(t->cli_fd); |
| 3271 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3272 | trace_term(t, TT_HTTP_CLI_7); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3273 | } |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3274 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3275 | if (!(t->flags & SN_ERR_MASK)) |
| 3276 | t->flags |= SN_ERR_CLITO; |
| 3277 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3278 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3279 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3280 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3281 | t->flags |= SN_FINST_C; |
| 3282 | else |
| 3283 | t->flags |= SN_FINST_D; |
| 3284 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3285 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3286 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3287 | } |
| 3288 | /* write timeout */ |
Willy Tarreau | 2500981 | 2008-08-17 18:16:38 +0200 | [diff] [blame] | 3289 | else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3290 | buffer_shutw(rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3291 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3292 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3293 | shutdown(t->cli_fd, SHUT_WR); |
| 3294 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3295 | /* FIXME: is this still true ? */ |
| 3296 | EV_FD_SET(t->cli_fd, DIR_RD); |
| 3297 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3298 | trace_term(t, TT_HTTP_CLI_8); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3299 | } else { |
| 3300 | fd_delete(t->cli_fd); |
| 3301 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3302 | trace_term(t, TT_HTTP_CLI_9); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3303 | } |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3304 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3305 | if (!(t->flags & SN_ERR_MASK)) |
| 3306 | t->flags |= SN_ERR_CLITO; |
| 3307 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3308 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3309 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3310 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3311 | t->flags |= SN_FINST_C; |
| 3312 | else |
| 3313 | t->flags |= SN_FINST_D; |
| 3314 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3315 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3316 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3319 | update_timeouts: |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3320 | /* manage read timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3321 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3322 | if (req->flags & BF_FULL) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3323 | /* no room to read more data */ |
| 3324 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3325 | /* stop reading until we get some space */ |
| 3326 | req->rex = TICK_ETERNITY; |
| 3327 | } |
| 3328 | } else { |
| 3329 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3330 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | /* manage write timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3335 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3336 | /* first, we may have to produce data (eg: stats). |
| 3337 | * right now, this is limited to the SHUTR state. |
| 3338 | */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3339 | if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3340 | produce_content(t); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3341 | if (rep->flags & BF_EMPTY) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3342 | buffer_shutw(rep); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3343 | fd_delete(t->cli_fd); |
| 3344 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3345 | trace_term(t, TT_HTTP_CLI_10); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3346 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3347 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3348 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3349 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3350 | /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3351 | if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3352 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3353 | /* stop writing */ |
| 3354 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3355 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3356 | } else { |
| 3357 | /* buffer not empty */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3358 | EV_FD_COND_S(t->cli_fd, DIR_WR); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3359 | if (!tick_isset(rep->wex)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3360 | /* restart writing */ |
| 3361 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3362 | if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3363 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3364 | * we refresh it, except if it was already infinite. */ |
| 3365 | req->rex = rep->wex; |
| 3366 | } |
| 3367 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3368 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3369 | } |
| 3370 | return 0; /* other cases change nothing */ |
| 3371 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3372 | else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3373 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3374 | int len; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3375 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3376 | write(1, trash, len); |
| 3377 | } |
| 3378 | return 0; |
| 3379 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3380 | #ifdef DEBUG_DEV |
| 3381 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3382 | ABORT_NOW(); |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3383 | #endif |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3384 | return 0; |
| 3385 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3386 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3387 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3388 | /* Return 1 if the pending connection has failed and should be retried, |
| 3389 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3390 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3391 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3392 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3393 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3394 | struct buffer *req = t->req; |
| 3395 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3396 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3397 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3398 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3399 | now_ms, __FUNCTION__, |
| 3400 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3401 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3402 | req->flags, rep->flags, |
| 3403 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3404 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3405 | if ((req->flags & BF_SHUTW_NOW) || |
| 3406 | (rep->flags & BF_SHUTW) || |
| 3407 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3408 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
| 3409 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3410 | |
| 3411 | trace_term(t, TT_HTTP_SRV_5); |
| 3412 | req->wex = TICK_ETERNITY; |
| 3413 | fd_delete(req->cons->fd); |
| 3414 | if (t->srv) { |
| 3415 | t->srv->cur_sess--; |
| 3416 | sess_change_server(t, NULL); |
| 3417 | } |
| 3418 | /* note that this must not return any error because it would be able to |
| 3419 | * overwrite the client_retnclose() output. |
| 3420 | */ |
| 3421 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3422 | |
| 3423 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3424 | buffer_shutw(req); |
| 3425 | buffer_shutr(rep); |
| 3426 | req->cons->state = SI_ST_CLO; |
| 3427 | if (!req->cons->err_type) |
| 3428 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3429 | req->cons->err_loc = t->srv; |
| 3430 | return 0; |
| 3431 | } |
| 3432 | |
| 3433 | /* check for timeouts and asynchronous connect errors */ |
| 3434 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3435 | conn_err = SI_ET_CONN_ERR; |
| 3436 | if (!req->cons->err_type) |
| 3437 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3438 | } |
| 3439 | else if (!(req->flags & BF_WRITE_STATUS)) { |
| 3440 | /* nothing happened, maybe we timed out */ |
| 3441 | if (tick_is_expired(req->wex, now_ms)) { |
| 3442 | conn_err = SI_ET_CONN_TO; |
| 3443 | if (!req->cons->err_type) |
| 3444 | req->cons->err_type = SI_ET_CONN_TO; |
| 3445 | } |
| 3446 | else |
| 3447 | return 0; /* let's wait a bit more */ |
| 3448 | } |
| 3449 | |
| 3450 | if (conn_err) { |
| 3451 | fd_delete(req->cons->fd); |
| 3452 | req->cons->state = SI_ST_CLO; |
| 3453 | |
| 3454 | if (t->srv) { |
| 3455 | t->srv->cur_sess--; |
| 3456 | sess_change_server(t, NULL); |
| 3457 | req->cons->err_loc = t->srv; |
| 3458 | } |
| 3459 | |
| 3460 | /* ensure that we have enough retries left */ |
| 3461 | if (srv_count_retry_down(t, conn_err)) |
| 3462 | return 0; |
| 3463 | |
| 3464 | if (conn_err == SI_ET_CONN_ERR) { |
| 3465 | /* we encountered an immediate connection error, and we |
| 3466 | * will have to retry connecting to the same server, most |
| 3467 | * likely leading to the same result. To avoid this, we |
| 3468 | * fake a connection timeout to retry after a turn-around |
| 3469 | * time of 1 second. We will wait in the previous if block. |
| 3470 | */ |
| 3471 | req->cons->state = SI_ST_TAR; |
| 3472 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
| 3476 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3477 | /* We're on our last chance, and the REDISP option was specified. |
| 3478 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3479 | */ |
| 3480 | /* let's try to offer this slot to anybody */ |
| 3481 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3482 | process_srv_queue(t->srv); |
| 3483 | |
| 3484 | /* it's left to the dispatcher to choose a server */ |
| 3485 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3486 | t->prev_srv = t->srv; |
| 3487 | } else { |
| 3488 | /* we just want to retry */ |
| 3489 | if (t->srv) |
| 3490 | t->srv->retries++; |
| 3491 | t->be->retries++; |
| 3492 | |
| 3493 | /* Now we will try to either reconnect to the same server or |
| 3494 | * connect to another server. If the connection gets queued |
| 3495 | * because all servers are saturated, then we will go back to |
| 3496 | * the idle state where the buffer's consumer is marked as |
| 3497 | * unknown. |
| 3498 | */ |
| 3499 | if (srv_retryable_connect(t)) { |
| 3500 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3501 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3502 | return 0; |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | /* We'll rely on the caller to try to get a connection again */ |
| 3507 | return 1; |
| 3508 | } |
| 3509 | else { |
| 3510 | /* no error and write OK : connection succeeded */ |
| 3511 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3512 | req->cons->state = SI_ST_EST; |
| 3513 | req->cons->err_type = SI_ET_NONE; |
| 3514 | req->cons->err_loc = NULL; |
| 3515 | |
| 3516 | if (req->flags & BF_EMPTY) { |
| 3517 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3518 | req->wex = TICK_ETERNITY; |
| 3519 | } else { |
| 3520 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3521 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3522 | if (tick_isset(req->wex)) { |
| 3523 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3524 | * we refresh it. */ |
| 3525 | rep->rex = req->wex; |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3530 | if (!(rep->flags & BF_HIJACK)) { |
| 3531 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3532 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3533 | } |
| 3534 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3535 | |
| 3536 | /* if the user wants to log as soon as possible, without counting |
| 3537 | bytes from the server, then this is the right moment. */ |
| 3538 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3539 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3540 | tcp_sess_log(t); |
| 3541 | } |
| 3542 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3543 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3544 | /* TCP splicing supported by both FE and BE */ |
| 3545 | tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
| 3546 | } |
| 3547 | #endif |
| 3548 | } |
| 3549 | else { |
| 3550 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3551 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3552 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3553 | /* reset hdr_idx which was already initialized by the request. |
| 3554 | * right now, the http parser does it. |
| 3555 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3556 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3557 | } |
| 3558 | |
| 3559 | if (!rep->analysers) |
| 3560 | t->rep->flags |= BF_MAY_FORWARD; |
| 3561 | req->wex = TICK_ETERNITY; |
| 3562 | return 0; |
| 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | |
| 3567 | /* |
| 3568 | * This function tries to assign a server to a stream_sock interface. |
| 3569 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3570 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3571 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3572 | * it returns SI_ST_ASS, otherwise zero. |
| 3573 | */ |
| 3574 | int stream_sock_assign_server(struct session *t) |
| 3575 | { |
| 3576 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3577 | now_ms, __FUNCTION__, |
| 3578 | cli_stnames[t->cli_state], |
| 3579 | t->rep->rex, t->req->wex, |
| 3580 | t->req->flags, t->rep->flags, |
| 3581 | t->req->l, t->rep->l); |
| 3582 | |
| 3583 | if (t->req->cons->state == SI_ST_TAR) { |
| 3584 | /* connection might be aborted */ |
| 3585 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3586 | (t->rep->flags & BF_SHUTW) || |
| 3587 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3588 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3589 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3590 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3591 | t->req->wex = TICK_ETERNITY; |
| 3592 | |
| 3593 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3594 | buffer_shutr(t->rep); |
| 3595 | buffer_shutw(t->req); |
| 3596 | if (!t->req->cons->err_type) |
| 3597 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3598 | t->req->cons->state = SI_ST_CLO; |
| 3599 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3600 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3601 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3602 | if (!tick_is_expired(t->req->wex, now_ms)) |
| 3603 | return 0; /* still in turn-around */ |
| 3604 | |
| 3605 | t->req->cons->state = SI_ST_INI; |
| 3606 | } |
| 3607 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3608 | if (t->pend_pos) { |
| 3609 | /* request still in queue... */ |
| 3610 | if (tick_is_expired(t->req->wex, now_ms)) { |
| 3611 | /* ... and timeout expired */ |
| 3612 | trace_term(t, TT_HTTP_SRV_3); |
| 3613 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3614 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3615 | if (t->srv) |
| 3616 | t->srv->failed_conns++; |
| 3617 | t->be->failed_conns++; |
| 3618 | |
| 3619 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3620 | buffer_shutr(t->rep); |
| 3621 | buffer_shutw(t->req); |
| 3622 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3623 | if (!t->req->cons->err_type) |
| 3624 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3625 | t->req->cons->state = SI_ST_CLO; |
| 3626 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3627 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3628 | /* connection remains in queue, check if we have to abort it */ |
| 3629 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3630 | (t->rep->flags & BF_SHUTW) || |
| 3631 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3632 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3633 | /* give up */ |
| 3634 | trace_term(t, TT_HTTP_SRV_1); |
| 3635 | t->req->wex = TICK_ETERNITY; |
| 3636 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3637 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3638 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3639 | buffer_shutr(t->rep); |
| 3640 | buffer_shutw(t->req); |
| 3641 | if (!t->req->cons->err_type) |
| 3642 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3643 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3644 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3645 | return 0; |
| 3646 | } |
| 3647 | /* The connection is not in the queue anymore */ |
| 3648 | t->req->cons->state = SI_ST_INI; |
| 3649 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3650 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3651 | /* we may get here from above */ |
| 3652 | if (t->req->cons->state == SI_ST_INI) { |
| 3653 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3654 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3655 | /* first, check if the connection has been aborted */ |
| 3656 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3657 | (t->rep->flags & BF_SHUTW) || |
| 3658 | ((t->req->flags & BF_SHUTR) && |
| 3659 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3660 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3661 | trace_term(t, TT_HTTP_SRV_1); |
| 3662 | t->req->wex = TICK_ETERNITY; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3663 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3664 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3665 | buffer_shutr(t->rep); |
| 3666 | buffer_shutw(t->req); |
| 3667 | if (!t->req->cons->err_type) |
| 3668 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3669 | t->req->cons->state = SI_ST_CLO; |
| 3670 | return 0; |
| 3671 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3672 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3673 | /* try to get a server assigned */ |
| 3674 | if (srv_redispatch_connect(t) != 0) { |
| 3675 | /* we did not get any server, let's check the cause */ |
| 3676 | if (t->req->cons->state == SI_ST_QUE) { |
| 3677 | /* the connection was queued, that's OK */ |
| 3678 | return 0; |
| 3679 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3680 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3681 | trace_term(t, TT_HTTP_SRV_2); |
| 3682 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3683 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3684 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3685 | buffer_shutr(t->rep); |
| 3686 | buffer_shutw(t->req); |
| 3687 | t->req->flags |= BF_WRITE_ERROR; |
| 3688 | if (!t->req->cons->err_type) |
| 3689 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3690 | t->req->cons->state = SI_ST_CLO; |
| 3691 | return 0; |
| 3692 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3694 | t->req->cons->state = SI_ST_ASS; |
| 3695 | /* Once the server is assigned, we have to return because |
| 3696 | * the caller might be interested in checking several |
| 3697 | * things before connecting. |
| 3698 | */ |
| 3699 | return 1; |
| 3700 | } |
| 3701 | return 0; |
| 3702 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3703 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3704 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3705 | /* |
| 3706 | * This function tries to establish a connection to an assigned server. It also |
| 3707 | * performs connection retries. It may only be called with t->req->cons->state |
| 3708 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3709 | * SI_ST_EST, or SI_ST_CLO. |
| 3710 | */ |
| 3711 | int stream_sock_connect_server(struct session *t) |
| 3712 | { |
| 3713 | if (t->req->cons->state == SI_ST_ASS) { |
| 3714 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3715 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3716 | if (!srv_retryable_connect(t)) { |
| 3717 | /* we need to redispatch */ |
| 3718 | t->req->cons->state = SI_ST_INI; |
| 3719 | return 0; |
| 3720 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3722 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3723 | if (t->req->cons->state != SI_ST_CON) { |
| 3724 | /* it was an error */ |
| 3725 | trace_term(t, TT_HTTP_SRV_4); |
| 3726 | t->req->wex = TICK_ETERNITY; |
| 3727 | |
| 3728 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3729 | buffer_shutr(t->rep); |
| 3730 | buffer_shutw(t->req); |
| 3731 | t->req->flags |= BF_WRITE_ERROR; |
| 3732 | if (!t->req->cons->err_type) |
| 3733 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3734 | t->req->cons->state = SI_ST_CLO; |
| 3735 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3736 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3737 | /* We have a socket and switched to SI_ST_CON */ |
| 3738 | } |
| 3739 | |
| 3740 | /* we may also get here from above */ |
| 3741 | if (t->req->cons->state == SI_ST_CON) { |
| 3742 | /* connection in progress or just completed */ |
| 3743 | if (!tcp_connection_status(t)) |
| 3744 | return 0; |
| 3745 | } |
| 3746 | return 0; |
| 3747 | } |
| 3748 | |
| 3749 | |
| 3750 | /* |
| 3751 | * Tries to establish a connection to the server and associate it to the |
| 3752 | * request buffer's consumer side. It is assumed that this function will not be |
| 3753 | * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally |
| 3754 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3755 | */ |
| 3756 | int process_srv_conn(struct session *t) |
| 3757 | { |
| 3758 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3759 | now_ms, __FUNCTION__, |
| 3760 | cli_stnames[t->cli_state], |
| 3761 | t->rep->rex, t->req->wex, |
| 3762 | t->req->flags, t->rep->flags, |
| 3763 | t->req->l, t->rep->l); |
| 3764 | |
| 3765 | do { |
| 3766 | if (t->req->cons->state == SI_ST_INI || |
| 3767 | t->req->cons->state == SI_ST_TAR || |
| 3768 | t->req->cons->state == SI_ST_QUE) { |
| 3769 | /* try to assign a server */ |
| 3770 | if (!stream_sock_assign_server(t)) |
| 3771 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3772 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3773 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3774 | if (t->req->cons->state == SI_ST_ASS && |
| 3775 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3776 | /* Server supporting redirection and it is possible. |
| 3777 | * Invalid requests are reported as such. It concerns all |
| 3778 | * the largest ones. |
| 3779 | */ |
| 3780 | struct http_txn *txn = &t->txn; |
| 3781 | struct chunk rdr; |
| 3782 | char *path; |
| 3783 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3784 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3785 | /* 1: create the response header */ |
| 3786 | rdr.len = strlen(HTTP_302); |
| 3787 | rdr.str = trash; |
| 3788 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3789 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3790 | /* 2: add the server's prefix */ |
| 3791 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3792 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3793 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3794 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3795 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3796 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3797 | /* 3: add the request URI */ |
| 3798 | path = http_get_path(txn); |
| 3799 | if (!path) |
| 3800 | goto cancel_redir; |
| 3801 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3802 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3803 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3804 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3805 | memcpy(rdr.str + rdr.len, path, len); |
| 3806 | rdr.len += len; |
| 3807 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3808 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3809 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3810 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3811 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3812 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3813 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3814 | if (t->srv) |
| 3815 | t->srv->cum_sess++; |
| 3816 | |
| 3817 | t->req->cons->state = SI_ST_CLO; |
| 3818 | return 0; |
| 3819 | cancel_redir: |
| 3820 | //txn->status = 400; |
| 3821 | //t->fe->failed_req++; |
| 3822 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3823 | // 400, error_message(t, HTTP_ERR_400)); |
| 3824 | trace_term(t, TT_HTTP_SRV_4); |
| 3825 | |
| 3826 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3827 | buffer_shutw(t->req); |
| 3828 | buffer_shutr(t->rep); |
| 3829 | if (!t->req->cons->err_type) |
| 3830 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3831 | t->req->cons->state = SI_ST_CLO; |
| 3832 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3833 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3834 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3835 | if (t->req->cons->state == SI_ST_CON || |
| 3836 | t->req->cons->state == SI_ST_ASS) { |
| 3837 | stream_sock_connect_server(t); |
| 3838 | } |
| 3839 | } while (t->req->cons->state != SI_ST_CLO && |
| 3840 | t->req->cons->state != SI_ST_CON && |
| 3841 | t->req->cons->state != SI_ST_EST); |
| 3842 | return 0; |
| 3843 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3844 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3845 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3846 | /* |
| 3847 | * Manages the server FSM and its socket during the DATA phase. It must not be |
| 3848 | * called when a file descriptor is not attached to the buffer. It must only be |
| 3849 | * called during SI_ST_EST. It normally returns zero, but may return 1 if it |
| 3850 | * absolutely wants to be called again. |
| 3851 | */ |
| 3852 | int process_srv_data(struct session *t) |
| 3853 | { |
| 3854 | struct buffer *req = t->req; |
| 3855 | struct buffer *rep = t->rep; |
| 3856 | int fd = req->cons->fd; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3857 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3858 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3859 | now_ms, __FUNCTION__, |
| 3860 | cli_stnames[t->cli_state], |
| 3861 | rep->rex, req->wex, |
| 3862 | req->flags, rep->flags, |
| 3863 | req->l, rep->l); |
| 3864 | |
| 3865 | if (req->flags & (BF_WRITE_ERROR | BF_WRITE_TIMEOUT) || |
| 3866 | rep->flags & (BF_READ_ERROR | BF_READ_TIMEOUT)) { |
| 3867 | /* nothing more to be done here */ |
| 3868 | fprintf(stderr, "Hey what are you doing there? t=%p fd=%d state=%d\n", |
| 3869 | t, t->req->cons->fd, t->req->cons->state); |
| 3870 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3871 | } |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3872 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3873 | /* we can skip most of the tests at once if some conditions are not met */ |
| 3874 | /* FIXME: place req->BF_SHUTW_NOW here */ |
| 3875 | //if (!((fdtab[fd].state == FD_STERROR) || |
| 3876 | // (!(req->flags & BF_SHUTW) && |
| 3877 | // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) || |
| 3878 | // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 3879 | // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)))) |
| 3880 | // goto update_timeouts; |
| 3881 | |
| 3882 | /* read or write error */ |
| 3883 | /* FIXME: what happens when we have to deal with HTTP ??? */ |
| 3884 | if (fdtab[fd].state == FD_STERROR) { |
| 3885 | trace_term(t, TT_HTTP_SRV_6); |
| 3886 | buffer_shutw(req); |
| 3887 | req->flags |= BF_WRITE_ERROR; |
| 3888 | buffer_shutr(rep); |
| 3889 | rep->flags |= BF_READ_ERROR; |
| 3890 | fd_delete(fd); |
| 3891 | req->cons->state = SI_ST_CLO; |
| 3892 | if (t->srv) { |
| 3893 | t->srv->cur_sess--; |
| 3894 | //t->srv->failed_resp++; |
| 3895 | //FIXME: si on ne traite pas l'erreur ici, le serveur est perdu et on ne la comptabilisera plus ensuite. |
| 3896 | //il va donc falloir stocker l'info du dernier serveur en erreur pour que les couches du dessus traitent. |
| 3897 | sess_change_server(t, NULL); |
| 3898 | } |
| 3899 | //t->be->failed_resp++; |
| 3900 | //if (!rep->analysers) { |
| 3901 | // if (!(t->flags & SN_ERR_MASK)) |
| 3902 | // t->flags |= SN_ERR_SRVCL; |
| 3903 | // if (!(t->flags & SN_FINST_MASK)) |
| 3904 | // t->flags |= SN_FINST_D; |
| 3905 | //} |
| 3906 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3907 | process_srv_queue(t->srv); |
| 3908 | |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | /* last read, or end of client write */ |
| 3913 | if (!(rep->flags & BF_SHUTR) && /* not already done */ |
| 3914 | rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3915 | buffer_shutr(rep); |
| 3916 | if (!(req->flags & BF_SHUTW)) { |
| 3917 | EV_FD_CLR(fd, DIR_RD); |
| 3918 | trace_term(t, TT_HTTP_SRV_7); |
| 3919 | } else { |
| 3920 | /* output was already closed */ |
| 3921 | trace_term(t, TT_HTTP_SRV_8); |
| 3922 | fd_delete(fd); |
| 3923 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3924 | if (t->srv) { |
| 3925 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3926 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3927 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3928 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3929 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3930 | process_srv_queue(t->srv); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3931 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3932 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3933 | } |
| 3934 | /* end of client read and no more data to send. We can forward |
| 3935 | * the close when we're allowed to forward data (anytime right |
| 3936 | * now). If we're using option forceclose, then we may also |
| 3937 | * shutdown the outgoing write channel once the response starts |
| 3938 | * coming from the server. |
| 3939 | */ |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3940 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3941 | // FIXME: option FORCE_CLOSE should move to upper layer. |
| 3942 | if (!(req->flags & BF_SHUTW) && /* not already done */ |
| 3943 | (req->flags & BF_SHUTW_NOW || |
| 3944 | (req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD && |
| 3945 | (req->flags & BF_SHUTR || |
| 3946 | (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))))) { |
| 3947 | buffer_shutw(req); |
| 3948 | if (!(rep->flags & BF_SHUTR)) { |
| 3949 | trace_term(t, TT_HTTP_SRV_9); |
| 3950 | EV_FD_CLR(fd, DIR_WR); |
| 3951 | shutdown(fd, SHUT_WR); |
| 3952 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3953 | /* FIXME: is this still true ? */ |
| 3954 | EV_FD_SET(fd, DIR_RD); |
| 3955 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3956 | } else { |
| 3957 | trace_term(t, TT_HTTP_SRV_10); |
| 3958 | fd_delete(fd); |
| 3959 | req->cons->state = SI_ST_CLO; |
| 3960 | if (t->srv) { |
| 3961 | t->srv->cur_sess--; |
| 3962 | sess_change_server(t, NULL); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3963 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3964 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3965 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3966 | process_srv_queue(t->srv); |
| 3967 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3968 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3969 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3970 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3971 | /* read timeout */ |
| 3972 | if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == 0 && |
| 3973 | tick_is_expired(rep->rex, now_ms)) { |
| 3974 | rep->flags |= BF_READ_TIMEOUT; |
| 3975 | //if (!rep->analysers) { |
| 3976 | // if (!(t->flags & SN_ERR_MASK)) |
| 3977 | // t->flags |= SN_ERR_SRVTO; |
| 3978 | // if (!(t->flags & SN_FINST_MASK)) |
| 3979 | // t->flags |= SN_FINST_D; |
| 3980 | //} |
| 3981 | buffer_shutr(rep); |
| 3982 | if (!(req->flags & BF_SHUTW)) { |
| 3983 | trace_term(t, TT_HTTP_SRV_11); |
| 3984 | EV_FD_CLR(fd, DIR_RD); |
| 3985 | } else { |
| 3986 | trace_term(t, TT_HTTP_SRV_12); |
| 3987 | fd_delete(fd); |
| 3988 | req->cons->state = SI_ST_CLO; |
| 3989 | if (t->srv) { |
| 3990 | t->srv->cur_sess--; |
| 3991 | sess_change_server(t, NULL); |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3992 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3993 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3994 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3995 | process_srv_queue(t->srv); |
| 3996 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3997 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 3998 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3999 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4000 | /* write timeout */ |
| 4001 | if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == 0 && |
| 4002 | tick_is_expired(req->wex, now_ms)) { |
| 4003 | req->flags |= BF_WRITE_TIMEOUT; |
| 4004 | //if (!rep->analysers) { |
| 4005 | // if (!(t->flags & SN_ERR_MASK)) |
| 4006 | // t->flags |= SN_ERR_SRVTO; |
| 4007 | // if (!(t->flags & SN_FINST_MASK)) |
| 4008 | // t->flags |= SN_FINST_D; |
| 4009 | //} |
| 4010 | buffer_shutw(req); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 4011 | if (!(rep->flags & BF_SHUTR)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4012 | trace_term(t, TT_HTTP_SRV_13); |
| 4013 | EV_FD_CLR(fd, DIR_WR); |
| 4014 | shutdown(fd, SHUT_WR); |
| 4015 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 4016 | /* FIXME: is this still needed ? */ |
| 4017 | EV_FD_SET(fd, DIR_RD); |
| 4018 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4019 | } else { |
| 4020 | trace_term(t, TT_HTTP_SRV_14); |
| 4021 | fd_delete(fd); |
| 4022 | req->cons->state = SI_ST_CLO; |
| 4023 | if (t->srv) { |
| 4024 | t->srv->cur_sess--; |
| 4025 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4026 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4027 | |
| 4028 | if (may_dequeue_tasks(t->srv, t->be)) |
| 4029 | process_srv_queue(t->srv); |
| 4030 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4031 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4032 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4033 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4034 | update_timeouts: |
| 4035 | /* manage read timeout */ |
| 4036 | if (!(rep->flags & BF_SHUTR)) { |
| 4037 | if (rep->flags & (BF_FULL|BF_HIJACK)) { |
| 4038 | if (EV_FD_COND_C(fd, DIR_RD)) |
| 4039 | rep->rex = TICK_ETERNITY; |
| 4040 | } else { |
| 4041 | EV_FD_COND_S(fd, DIR_RD); |
| 4042 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4043 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4044 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4045 | |
| 4046 | /* manage write timeout */ |
| 4047 | if (!(req->flags & BF_SHUTW)) { |
| 4048 | if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 4049 | /* stop writing */ |
| 4050 | if (EV_FD_COND_C(fd, DIR_WR)) |
| 4051 | req->wex = TICK_ETERNITY; |
| 4052 | } else { |
| 4053 | /* buffer not empty, there are still data to be transferred */ |
| 4054 | EV_FD_COND_S(fd, DIR_WR); |
| 4055 | if (!tick_isset(req->wex)) { |
| 4056 | /* restart writing */ |
| 4057 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4058 | if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) { |
| 4059 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4060 | * we refresh it, except if it was already infinite. |
| 4061 | */ |
| 4062 | rep->rex = req->wex; |
| 4063 | } |
| 4064 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4065 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4066 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4067 | return 0; /* other cases change nothing */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 4071 | ///* |
| 4072 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 4073 | // * return 1 if it absolutely wants to be called again. |
| 4074 | // * |
| 4075 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 4076 | // * but CL_STCLOSE. |
| 4077 | // */ |
| 4078 | //int process_cli(struct session *t) |
| 4079 | //{ |
| 4080 | // struct buffer *req = t->req; |
| 4081 | // struct buffer *rep = t->rep; |
| 4082 | // |
| 4083 | // DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4084 | // now_ms, __FUNCTION__, |
| 4085 | // cli_stnames[t->cli_state], |
| 4086 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 4087 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 4088 | // req->rex, rep->wex, |
| 4089 | // req->flags, rep->flags, |
| 4090 | // req->l, rep->l); |
| 4091 | // |
| 4092 | // update_state: |
| 4093 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 4094 | // * still set this state (and will do until unix sockets are converted). |
| 4095 | // */ |
| 4096 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 4097 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 4098 | // if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 4099 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) || |
| 4100 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 4101 | // (!(rep->flags & BF_SHUTW) && |
| 4102 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 4103 | // goto update_timeouts; |
| 4104 | // |
| 4105 | // /* read or write error */ |
| 4106 | // if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
| 4107 | // buffer_shutr(req); |
| 4108 | // buffer_shutw(rep); |
| 4109 | // fd_delete(t->cli_fd); |
| 4110 | // t->cli_state = CL_STCLOSE; |
| 4111 | // trace_term(t, TT_HTTP_CLI_1); |
| 4112 | // if (!req->analysers) { |
| 4113 | // if (!(t->flags & SN_ERR_MASK)) |
| 4114 | // t->flags |= SN_ERR_CLICL; |
| 4115 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4116 | // if (t->pend_pos) |
| 4117 | // t->flags |= SN_FINST_Q; |
| 4118 | // else if (!(req->flags & BF_CONNECTED)) |
| 4119 | // t->flags |= SN_FINST_C; |
| 4120 | // else |
| 4121 | // t->flags |= SN_FINST_D; |
| 4122 | // } |
| 4123 | // } |
| 4124 | // goto update_state; |
| 4125 | // } |
| 4126 | // /* last read, or end of server write */ |
| 4127 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 4128 | // req->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 4129 | // buffer_shutr(req); |
| 4130 | // if (!(rep->flags & BF_SHUTW)) { |
| 4131 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 4132 | // trace_term(t, TT_HTTP_CLI_2); |
| 4133 | // } else { |
| 4134 | // /* output was already closed */ |
| 4135 | // fd_delete(t->cli_fd); |
| 4136 | // t->cli_state = CL_STCLOSE; |
| 4137 | // trace_term(t, TT_HTTP_CLI_3); |
| 4138 | // } |
| 4139 | // goto update_state; |
| 4140 | // } |
| 4141 | // /* last server read and buffer empty : we only check them when we're |
| 4142 | // * allowed to forward the data. |
| 4143 | // */ |
| 4144 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 4145 | // rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 4146 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) { |
| 4147 | // buffer_shutw(rep); |
| 4148 | // if (!(req->flags & BF_SHUTR)) { |
| 4149 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 4150 | // shutdown(t->cli_fd, SHUT_WR); |
| 4151 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4152 | // /* FIXME: is this still true ? */ |
| 4153 | // EV_FD_SET(t->cli_fd, DIR_RD); |
| 4154 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4155 | // trace_term(t, TT_HTTP_CLI_4); |
| 4156 | // } else { |
| 4157 | // fd_delete(t->cli_fd); |
| 4158 | // t->cli_state = CL_STCLOSE; |
| 4159 | // trace_term(t, TT_HTTP_CLI_5); |
| 4160 | // } |
| 4161 | // goto update_state; |
| 4162 | // } |
| 4163 | // /* read timeout */ |
| 4164 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 4165 | // buffer_shutr(req); |
| 4166 | // if (!(rep->flags & BF_SHUTW)) { |
| 4167 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 4168 | // trace_term(t, TT_HTTP_CLI_6); |
| 4169 | // } else { |
| 4170 | // /* output was already closed */ |
| 4171 | // fd_delete(t->cli_fd); |
| 4172 | // t->cli_state = CL_STCLOSE; |
| 4173 | // trace_term(t, TT_HTTP_CLI_7); |
| 4174 | // } |
| 4175 | // if (!req->analysers) { |
| 4176 | // if (!(t->flags & SN_ERR_MASK)) |
| 4177 | // t->flags |= SN_ERR_CLITO; |
| 4178 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4179 | // if (t->pend_pos) |
| 4180 | // t->flags |= SN_FINST_Q; |
| 4181 | // else if (!(req->flags & BF_CONNECTED)) |
| 4182 | // t->flags |= SN_FINST_C; |
| 4183 | // else |
| 4184 | // t->flags |= SN_FINST_D; |
| 4185 | // } |
| 4186 | // } |
| 4187 | // goto update_state; |
| 4188 | // } |
| 4189 | // /* write timeout */ |
| 4190 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 4191 | // buffer_shutw(rep); |
| 4192 | // if (!(req->flags & BF_SHUTR)) { |
| 4193 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 4194 | // shutdown(t->cli_fd, SHUT_WR); |
| 4195 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4196 | // /* FIXME: is this still true ? */ |
| 4197 | // EV_FD_SET(t->cli_fd, DIR_RD); |
| 4198 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4199 | // trace_term(t, TT_HTTP_CLI_8); |
| 4200 | // } else { |
| 4201 | // fd_delete(t->cli_fd); |
| 4202 | // t->cli_state = CL_STCLOSE; |
| 4203 | // trace_term(t, TT_HTTP_CLI_9); |
| 4204 | // } |
| 4205 | // if (!req->analysers) { |
| 4206 | // if (!(t->flags & SN_ERR_MASK)) |
| 4207 | // t->flags |= SN_ERR_CLITO; |
| 4208 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4209 | // if (t->pend_pos) |
| 4210 | // t->flags |= SN_FINST_Q; |
| 4211 | // else if (!(req->flags & BF_CONNECTED)) |
| 4212 | // t->flags |= SN_FINST_C; |
| 4213 | // else |
| 4214 | // t->flags |= SN_FINST_D; |
| 4215 | // } |
| 4216 | // } |
| 4217 | // goto update_state; |
| 4218 | // } |
| 4219 | // |
| 4220 | // update_timeouts: |
| 4221 | // /* manage read timeout */ |
| 4222 | // if (!(req->flags & BF_SHUTR)) { |
| 4223 | // if (req->flags & BF_FULL) { |
| 4224 | // /* no room to read more data */ |
| 4225 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 4226 | // /* stop reading until we get some space */ |
| 4227 | // req->rex = TICK_ETERNITY; |
| 4228 | // } |
| 4229 | // } else { |
| 4230 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 4231 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4232 | // } |
| 4233 | // } |
| 4234 | // |
| 4235 | // /* manage write timeout */ |
| 4236 | // if (!(rep->flags & BF_SHUTW)) { |
| 4237 | // /* first, we may have to produce data (eg: stats). |
| 4238 | // * right now, this is limited to the SHUTR state. |
| 4239 | // */ |
| 4240 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 4241 | // produce_content(t); |
| 4242 | // if (rep->flags & BF_EMPTY) { |
| 4243 | // buffer_shutw(rep); |
| 4244 | // fd_delete(t->cli_fd); |
| 4245 | // t->cli_state = CL_STCLOSE; |
| 4246 | // trace_term(t, TT_HTTP_CLI_10); |
| 4247 | // goto update_state; |
| 4248 | // } |
| 4249 | // } |
| 4250 | // |
| 4251 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 4252 | // if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) { |
| 4253 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 4254 | // /* stop writing */ |
| 4255 | // rep->wex = TICK_ETERNITY; |
| 4256 | // } |
| 4257 | // } else { |
| 4258 | // /* buffer not empty */ |
| 4259 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 4260 | // if (!tick_isset(rep->wex)) { |
| 4261 | // /* restart writing */ |
| 4262 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4263 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 4264 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 4265 | // * we refresh it, except if it was already infinite. */ |
| 4266 | // req->rex = rep->wex; |
| 4267 | // } |
| 4268 | // } |
| 4269 | // } |
| 4270 | // } |
| 4271 | // return 0; /* other cases change nothing */ |
| 4272 | // } |
| 4273 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 4274 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 4275 | // int len; |
| 4276 | // len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd); |
| 4277 | // write(1, trash, len); |
| 4278 | // } |
| 4279 | // return 0; |
| 4280 | // } |
| 4281 | //#ifdef DEBUG_DEV |
| 4282 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 4283 | // ABORT_NOW(); |
| 4284 | //#endif |
| 4285 | // return 0; |
| 4286 | //} |
| 4287 | // |
| 4288 | // |
| 4289 | ///* Return 1 if we could get a new connection for session t, otherwise zero */ |
| 4290 | //int tcp_get_connection(struct session *t) |
| 4291 | //{ |
| 4292 | // struct http_txn *txn = &t->txn; |
| 4293 | // struct buffer *req = t->req; |
| 4294 | // struct buffer *rep = t->rep; |
| 4295 | // |
| 4296 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4297 | // now_ms, __FUNCTION__, |
| 4298 | // cli_stnames[t->cli_state], |
| 4299 | // rep->rex, req->wex, |
| 4300 | // req->flags, rep->flags, |
| 4301 | // req->l, rep->l); |
| 4302 | // |
| 4303 | // |
| 4304 | // if ((rep->flags & BF_SHUTW) || |
| 4305 | // ((req->flags & BF_SHUTR) && |
| 4306 | // (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 4307 | // req->wex = TICK_ETERNITY; |
| 4308 | // if (t->pend_pos) |
| 4309 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4310 | // /* note that this must not return any error because it would be able to |
| 4311 | // * overwrite the client_retnclose() output. |
| 4312 | // */ |
| 4313 | // if (txn->flags & TX_CLTARPIT) |
| 4314 | // srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
| 4315 | // else |
| 4316 | // srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
| 4317 | // |
| 4318 | // trace_term(t, TT_HTTP_SRV_1); |
| 4319 | // return 0; |
| 4320 | // } |
| 4321 | // |
| 4322 | // /* stop here if we're not allowed to connect */ |
| 4323 | // if (!(req->flags & BF_MAY_FORWARD)) |
| 4324 | // return 0; |
| 4325 | // |
| 4326 | // /* the client allows the server to connect */ |
| 4327 | // if (txn->flags & TX_CLTARPIT) { |
| 4328 | // /* This connection is being tarpitted. The CLIENT side has |
| 4329 | // * already set the connect expiration date to the right |
| 4330 | // * timeout. We just have to check that it has not expired. |
| 4331 | // */ |
| 4332 | // if (!(req->flags & BF_WRITE_TIMEOUT)) |
| 4333 | // return 0; |
| 4334 | // |
| 4335 | // /* We will set the queue timer to the time spent, just for |
| 4336 | // * logging purposes. We fake a 500 server error, so that the |
| 4337 | // * attacker will not suspect his connection has been tarpitted. |
| 4338 | // * It will not cause trouble to the logs because we can exclude |
| 4339 | // * the tarpitted connections by filtering on the 'PT' status flags. |
| 4340 | // */ |
| 4341 | // req->wex = TICK_ETERNITY; |
| 4342 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4343 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
| 4344 | // 500, error_message(t, HTTP_ERR_500)); |
| 4345 | // trace_term(t, TT_HTTP_SRV_2); |
| 4346 | // return 0; |
| 4347 | // } |
| 4348 | // |
| 4349 | // /* Right now, we will need to create a connection to the server. |
| 4350 | // * We might already have tried, and got a connection pending, in |
| 4351 | // * which case we will not do anything till it's pending. It's up |
| 4352 | // * to any other session to release it and wake us up again. |
| 4353 | // */ |
| 4354 | // if (t->pend_pos) { |
| 4355 | // if (!(req->flags & BF_WRITE_TIMEOUT)) { |
| 4356 | // return 0; |
| 4357 | // } else { |
| 4358 | // /* we've been waiting too long here */ |
| 4359 | // req->wex = TICK_ETERNITY; |
| 4360 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4361 | // srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
| 4362 | // 503, error_message(t, HTTP_ERR_503)); |
| 4363 | // trace_term(t, TT_HTTP_SRV_3); |
| 4364 | // if (t->srv) |
| 4365 | // t->srv->failed_conns++; |
| 4366 | // t->be->failed_conns++; |
| 4367 | // return 0; |
| 4368 | // } |
| 4369 | // } |
| 4370 | // |
| 4371 | // do { |
| 4372 | // if (srv_redispatch_connect(t) != 0) |
| 4373 | // return 0; |
| 4374 | // |
| 4375 | // if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 4376 | // /* Server supporting redirection and it is possible. |
| 4377 | // * Invalid requests are reported as such. It concerns all |
| 4378 | // * the largest ones. |
| 4379 | // */ |
| 4380 | // struct chunk rdr; |
| 4381 | // char *path; |
| 4382 | // int len; |
| 4383 | // |
| 4384 | // /* 1: create the response header */ |
| 4385 | // rdr.len = strlen(HTTP_302); |
| 4386 | // rdr.str = trash; |
| 4387 | // memcpy(rdr.str, HTTP_302, rdr.len); |
| 4388 | // |
| 4389 | // /* 2: add the server's prefix */ |
| 4390 | // if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 4391 | // goto cancel_redir; |
| 4392 | // |
| 4393 | // memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 4394 | // rdr.len += t->srv->rdr_len; |
| 4395 | // |
| 4396 | // /* 3: add the request URI */ |
| 4397 | // path = http_get_path(txn); |
| 4398 | // if (!path) |
| 4399 | // goto cancel_redir; |
| 4400 | // len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 4401 | // if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 4402 | // goto cancel_redir; |
| 4403 | // |
| 4404 | // memcpy(rdr.str + rdr.len, path, len); |
| 4405 | // rdr.len += len; |
| 4406 | // memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 4407 | // rdr.len += 4; |
| 4408 | // |
| 4409 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 4410 | // trace_term(t, TT_HTTP_SRV_3); |
| 4411 | // |
| 4412 | // /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4413 | // if (t->srv) |
| 4414 | // t->srv->cum_sess++; |
| 4415 | // return 0; |
| 4416 | // cancel_redir: |
| 4417 | // txn->status = 400; |
| 4418 | // t->fe->failed_req++; |
| 4419 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 4420 | // 400, error_message(t, HTTP_ERR_400)); |
| 4421 | // trace_term(t, TT_HTTP_SRV_4); |
| 4422 | // return 0; |
| 4423 | // } |
| 4424 | // |
| 4425 | // /* try to (re-)connect to the server, and fail if we expire the |
| 4426 | // * number of retries. |
| 4427 | // */ |
| 4428 | // if (srv_retryable_connect(t)) { |
| 4429 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4430 | // if (!(req->cons.flags & BC_KNOWN)) |
| 4431 | // return 0; |
| 4432 | // /* We got an FD */ |
| 4433 | // return 1; |
| 4434 | // } |
| 4435 | // } while (1); |
| 4436 | //} |
| 4437 | // |
| 4438 | // |
| 4439 | ///* Return 1 if the pending connection has failed and should be retried, |
| 4440 | // * otherwise zero. |
| 4441 | // */ |
| 4442 | //int tcp_connection_failed(struct session *t) |
| 4443 | //{ |
| 4444 | // struct buffer *req = t->req; |
| 4445 | // struct buffer *rep = t->rep; |
| 4446 | // int conn_err; |
| 4447 | // |
| 4448 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4449 | // now_ms, __FUNCTION__, |
| 4450 | // cli_stnames[t->cli_state], |
| 4451 | // rep->rex, req->wex, |
| 4452 | // req->flags, rep->flags, |
| 4453 | // req->l, rep->l); |
| 4454 | // |
| 4455 | // if ((rep->flags & BF_SHUTW) || |
| 4456 | // ((req->flags & BF_SHUTR) && |
| 4457 | // ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
| 4458 | // t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 4459 | // req->wex = TICK_ETERNITY; |
| 4460 | // if (!(t->flags & SN_CONN_TAR)) { |
| 4461 | // /* if we are in turn-around, we have already closed the FD */ |
| 4462 | // fd_delete(req->cons->fd); |
| 4463 | // req->cons->state = SI_ST_CLO; |
| 4464 | // if (t->srv) { |
| 4465 | // t->srv->cur_sess--; |
| 4466 | // sess_change_server(t, NULL); |
| 4467 | // } |
| 4468 | // } |
| 4469 | // |
| 4470 | // /* note that this must not return any error because it would be able to |
| 4471 | // * overwrite the client_retnclose() output. |
| 4472 | // */ |
| 4473 | // srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 4474 | // trace_term(t, TT_HTTP_SRV_5); |
| 4475 | // return 0; |
| 4476 | // } |
| 4477 | // |
| 4478 | // if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) |
| 4479 | // return 0; /* nothing changed */ |
| 4480 | // |
| 4481 | // if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
| 4482 | // /* timeout, asynchronous connect error or first write error */ |
| 4483 | // if (t->flags & SN_CONN_TAR) { |
| 4484 | // /* We are doing a turn-around waiting for a new connection attempt. */ |
| 4485 | // if (!(req->flags & BF_WRITE_TIMEOUT)) |
| 4486 | // return 0; |
| 4487 | // t->flags &= ~SN_CONN_TAR; |
| 4488 | // } |
| 4489 | // else { |
| 4490 | // fd_delete(req->cons->fd); |
| 4491 | // req->cons->state = SI_ST_CLO; |
| 4492 | // if (t->srv) { |
| 4493 | // t->srv->cur_sess--; |
| 4494 | // sess_change_server(t, NULL); |
| 4495 | // } |
| 4496 | // |
| 4497 | // if (!(req->flags & BF_WRITE_STATUS)) |
| 4498 | // conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4499 | // else |
| 4500 | // conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 4501 | // |
| 4502 | // /* ensure that we have enough retries left */ |
| 4503 | // if (srv_count_retry_down(t, conn_err)) |
| 4504 | // return 0; |
| 4505 | // |
| 4506 | // if (req->flags & BF_WRITE_ERROR) { |
| 4507 | // /* we encountered an immediate connection error, and we |
| 4508 | // * will have to retry connecting to the same server, most |
| 4509 | // * likely leading to the same result. To avoid this, we |
| 4510 | // * fake a connection timeout to retry after a turn-around |
| 4511 | // * time of 1 second. We will wait in the previous if block. |
| 4512 | // */ |
| 4513 | // t->flags |= SN_CONN_TAR; |
| 4514 | // req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 4515 | // return 0; |
| 4516 | // } |
| 4517 | // } |
| 4518 | // |
| 4519 | // if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 4520 | // /* We're on our last chance, and the REDISP option was specified. |
| 4521 | // * We will ignore cookie and force to balance or use the dispatcher. |
| 4522 | // */ |
| 4523 | // /* let's try to offer this slot to anybody */ |
| 4524 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4525 | // process_srv_queue(t->srv); |
| 4526 | // |
| 4527 | // /* it's left to the dispatcher to choose a server */ |
| 4528 | // t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 4529 | // t->prev_srv = t->srv; |
| 4530 | // |
| 4531 | // /* first, get a connection */ |
| 4532 | // if (srv_redispatch_connect(t)) { |
| 4533 | // if (req->cons.flags & BC_KNOWN) |
| 4534 | // return 0; |
| 4535 | // /* we need to get a connection */ |
| 4536 | // return 1; |
| 4537 | // } |
| 4538 | // } else { |
| 4539 | // if (t->srv) |
| 4540 | // t->srv->retries++; |
| 4541 | // t->be->retries++; |
| 4542 | // } |
| 4543 | // |
| 4544 | // do { |
| 4545 | // /* Now we will try to either reconnect to the same server or |
| 4546 | // * connect to another server. If the connection gets queued |
| 4547 | // * because all servers are saturated, then we will go back to |
| 4548 | // * the idle state where the buffer's consumer is marked as |
| 4549 | // * unknown. |
| 4550 | // */ |
| 4551 | // if (srv_retryable_connect(t)) { |
| 4552 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4553 | // if (req->cons.flags & BC_KNOWN) |
| 4554 | // return 0; |
| 4555 | // /* we did not get a connection */ |
| 4556 | // return 1; |
| 4557 | // } |
| 4558 | // |
| 4559 | // /* we need to redispatch the connection to another server */ |
| 4560 | // if (srv_redispatch_connect(t)) { |
| 4561 | // if (req->cons.flags & BC_KNOWN) |
| 4562 | // return 0; |
| 4563 | // /* we need to get a connection */ |
| 4564 | // return 1; |
| 4565 | // } |
| 4566 | // } while (1); |
| 4567 | // } |
| 4568 | // else { /* no error and write OK */ |
| 4569 | // t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4570 | // |
| 4571 | // if (req->flags & BF_EMPTY) { |
| 4572 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4573 | // req->wex = TICK_ETERNITY; |
| 4574 | // } else { |
| 4575 | // EV_FD_SET(req->cons->fd, DIR_WR); |
| 4576 | // req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4577 | // if (tick_isset(req->wex)) { |
| 4578 | // /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4579 | // * we refresh it. */ |
| 4580 | // rep->rex = req->wex; |
| 4581 | // } |
| 4582 | // } |
| 4583 | // |
| 4584 | // if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4585 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4586 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4587 | // buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 4588 | // |
| 4589 | // /* if the user wants to log as soon as possible, without counting |
| 4590 | // bytes from the server, then this is the right moment. */ |
| 4591 | // if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4592 | // t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4593 | // tcp_sess_log(t); |
| 4594 | // } |
| 4595 | //#ifdef CONFIG_HAP_TCPSPLICE |
| 4596 | // if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 4597 | // /* TCP splicing supported by both FE and BE */ |
| 4598 | // tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
| 4599 | // } |
| 4600 | //#endif |
| 4601 | // } |
| 4602 | // else { |
| 4603 | // rep->analysers |= AN_RTR_HTTP_HDR; |
| 4604 | // buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 4605 | // t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 4606 | // /* reset hdr_idx which was already initialized by the request. |
| 4607 | // * right now, the http parser does it. |
| 4608 | // * hdr_idx_init(&t->txn.hdr_idx); |
| 4609 | // */ |
| 4610 | // } |
| 4611 | // |
| 4612 | // req->flags |= BF_CONNECTED; |
| 4613 | // if (!rep->analysers) |
| 4614 | // t->rep->flags |= BF_MAY_FORWARD; |
| 4615 | // req->wex = TICK_ETERNITY; |
| 4616 | // return 0; |
| 4617 | // } |
| 4618 | //} |
| 4619 | // |
| 4620 | // |
| 4621 | ///* |
| 4622 | // * Tries to establish a connection to the server and associate it to the |
| 4623 | // * request buffer's consumer side. It normally returns zero, but may return 1 |
| 4624 | // * if it absolutely wants to be called again. |
| 4625 | // */ |
| 4626 | //int process_srv_conn(struct session *t) |
| 4627 | //{ |
| 4628 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4629 | // now_ms, __FUNCTION__, |
| 4630 | // cli_stnames[t->cli_state], |
| 4631 | // t->rep->rex, t->req->wex, |
| 4632 | // t->req->flags, t->rep->flags, |
| 4633 | // t->req->l, t->rep->l); |
| 4634 | // |
| 4635 | // while (!(t->req->flags & BF_CONNECTED)) { |
| 4636 | // if (!(t->req->cons.flags & BC_KNOWN)) { |
| 4637 | // /* no connection in progress, get a new one */ |
| 4638 | // if (!tcp_get_connection(t)) |
| 4639 | // break; |
| 4640 | // } else { |
| 4641 | // /* connection in progress or just completed */ |
| 4642 | // if (!tcp_connection_failed(t)) |
| 4643 | // break; |
| 4644 | // } |
| 4645 | // } |
| 4646 | // return 0; |
| 4647 | //} |
| 4648 | // |
| 4649 | // |
| 4650 | ///* |
| 4651 | // * Manages the server FSM and its socket during the DATA phase. It must not |
| 4652 | // * be called when a file descriptor is not attached to the buffer. It normally |
| 4653 | // * returns zero, but may return 1 if it absolutely wants to be called again. |
| 4654 | // */ |
| 4655 | //int process_srv_data(struct session *t) |
| 4656 | //{ |
| 4657 | // struct buffer *req = t->req; |
| 4658 | // struct buffer *rep = t->rep; |
| 4659 | // |
| 4660 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4661 | // now_ms, __FUNCTION__, |
| 4662 | // cli_stnames[t->cli_state], |
| 4663 | // rep->rex, req->wex, |
| 4664 | // req->flags, rep->flags, |
| 4665 | // req->l, rep->l); |
| 4666 | // |
| 4667 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 4668 | // if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) || |
| 4669 | // (!(req->flags & BF_SHUTW) && |
| 4670 | // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) || |
| 4671 | // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 4672 | // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW)))) |
| 4673 | // goto update_timeouts; |
| 4674 | // |
| 4675 | // /* read or write error */ |
| 4676 | // /* FIXME: what happens when we have to deal with HTTP ??? */ |
| 4677 | // if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
| 4678 | // buffer_shutr(rep); |
| 4679 | // buffer_shutw(req); |
| 4680 | // fd_delete(req->cons->fd); |
| 4681 | // req->cons->state = SI_ST_CLO; |
| 4682 | // if (t->srv) { |
| 4683 | // t->srv->cur_sess--; |
| 4684 | // t->srv->failed_resp++; |
| 4685 | // sess_change_server(t, NULL); |
| 4686 | // } |
| 4687 | // t->be->failed_resp++; |
| 4688 | // trace_term(t, TT_HTTP_SRV_6); |
| 4689 | // if (!rep->analysers) { |
| 4690 | // if (!(t->flags & SN_ERR_MASK)) |
| 4691 | // t->flags |= SN_ERR_SRVCL; |
| 4692 | // if (!(t->flags & SN_FINST_MASK)) |
| 4693 | // t->flags |= SN_FINST_D; |
| 4694 | // } |
| 4695 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4696 | // process_srv_queue(t->srv); |
| 4697 | // |
| 4698 | // return 0; |
| 4699 | // } |
| 4700 | // |
| 4701 | // /* last read, or end of client write */ |
| 4702 | // if (!(rep->flags & BF_SHUTR) && /* not already done */ |
| 4703 | // rep->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 4704 | // buffer_shutr(rep); |
| 4705 | // if (!(req->flags & BF_SHUTW)) { |
| 4706 | // EV_FD_CLR(req->cons->fd, DIR_RD); |
| 4707 | // trace_term(t, TT_HTTP_SRV_7); |
| 4708 | // } else { |
| 4709 | // /* output was already closed */ |
| 4710 | // fd_delete(req->cons->fd); |
| 4711 | // req->cons->state = SI_ST_CLO; |
| 4712 | // if (t->srv) { |
| 4713 | // t->srv->cur_sess--; |
| 4714 | // sess_change_server(t, NULL); |
| 4715 | // } |
| 4716 | // trace_term(t, TT_HTTP_SRV_8); |
| 4717 | // |
| 4718 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4719 | // process_srv_queue(t->srv); |
| 4720 | // return 0; |
| 4721 | // } |
| 4722 | // } |
| 4723 | // /* end of client read and no more data to send. We can forward |
| 4724 | // * the close when we're allowed to forward data (anytime right |
| 4725 | // * now). If we're using option forceclose, then we may also |
| 4726 | // * shutdown the outgoing write channel once the response starts |
| 4727 | // * coming from the server. |
| 4728 | // */ |
| 4729 | // if (!(req->flags & BF_SHUTW) && /* not already done */ |
| 4730 | // req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD && |
| 4731 | // (req->flags & BF_SHUTR || |
| 4732 | // (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) { |
| 4733 | // buffer_shutw(req); |
| 4734 | // if (!(rep->flags & BF_SHUTR)) { |
| 4735 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4736 | // shutdown(req->cons->fd, SHUT_WR); |
| 4737 | // trace_term(t, TT_HTTP_SRV_9); |
| 4738 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4739 | // /* FIXME: is this still true ? */ |
| 4740 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4741 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4742 | // } else { |
| 4743 | // fd_delete(req->cons->fd); |
| 4744 | // req->cons->state = SI_ST_CLO; |
| 4745 | // if (t->srv) { |
| 4746 | // t->srv->cur_sess--; |
| 4747 | // sess_change_server(t, NULL); |
| 4748 | // } |
| 4749 | // trace_term(t, TT_HTTP_SRV_10); |
| 4750 | // |
| 4751 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4752 | // process_srv_queue(t->srv); |
| 4753 | // return 0; |
| 4754 | // } |
| 4755 | // } |
| 4756 | // |
| 4757 | // /* read timeout */ |
| 4758 | // if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 4759 | // if (!rep->analysers) { |
| 4760 | // if (!(t->flags & SN_ERR_MASK)) |
| 4761 | // t->flags |= SN_ERR_SRVTO; |
| 4762 | // if (!(t->flags & SN_FINST_MASK)) |
| 4763 | // t->flags |= SN_FINST_D; |
| 4764 | // } |
| 4765 | // buffer_shutr(rep); |
| 4766 | // if (!(req->flags & BF_SHUTW)) { |
| 4767 | // EV_FD_CLR(req->cons->fd, DIR_RD); |
| 4768 | // trace_term(t, TT_HTTP_SRV_11); |
| 4769 | // } else { |
| 4770 | // fd_delete(req->cons->fd); |
| 4771 | // req->cons->state = SI_ST_CLO; |
| 4772 | // if (t->srv) { |
| 4773 | // t->srv->cur_sess--; |
| 4774 | // sess_change_server(t, NULL); |
| 4775 | // } |
| 4776 | // trace_term(t, TT_HTTP_SRV_12); |
| 4777 | // |
| 4778 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4779 | // process_srv_queue(t->srv); |
| 4780 | // return 0; |
| 4781 | // } |
| 4782 | // } |
| 4783 | // |
| 4784 | // /* write timeout */ |
| 4785 | // if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 4786 | // if (!rep->analysers) { |
| 4787 | // if (!(t->flags & SN_ERR_MASK)) |
| 4788 | // t->flags |= SN_ERR_SRVTO; |
| 4789 | // if (!(t->flags & SN_FINST_MASK)) |
| 4790 | // t->flags |= SN_FINST_D; |
| 4791 | // } |
| 4792 | // buffer_shutw(req); |
| 4793 | // if (!(rep->flags & BF_SHUTR)) { |
| 4794 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4795 | // shutdown(req->cons->fd, SHUT_WR); |
| 4796 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4797 | // /* FIXME: is this still needed ? */ |
| 4798 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4799 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4800 | // trace_term(t, TT_HTTP_SRV_13); |
| 4801 | // } else { |
| 4802 | // fd_delete(req->cons->fd); |
| 4803 | // req->cons->state = SI_ST_CLO; |
| 4804 | // if (t->srv) { |
| 4805 | // t->srv->cur_sess--; |
| 4806 | // sess_change_server(t, NULL); |
| 4807 | // } |
| 4808 | // trace_term(t, TT_HTTP_SRV_14); |
| 4809 | // |
| 4810 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4811 | // process_srv_queue(t->srv); |
| 4812 | // return 0; |
| 4813 | // } |
| 4814 | // } |
| 4815 | // |
| 4816 | // update_timeouts: |
| 4817 | // /* manage read timeout */ |
| 4818 | // if (!(rep->flags & BF_SHUTR)) { |
| 4819 | // if (rep->flags & BF_FULL) { |
| 4820 | // if (EV_FD_COND_C(req->cons->fd, DIR_RD)) |
| 4821 | // rep->rex = TICK_ETERNITY; |
| 4822 | // } else { |
| 4823 | // EV_FD_COND_S(req->cons->fd, DIR_RD); |
| 4824 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4825 | // } |
| 4826 | // } |
| 4827 | // |
| 4828 | // /* manage write timeout */ |
| 4829 | // if (!(req->flags & BF_SHUTW)) { |
| 4830 | // if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) { |
| 4831 | // /* stop writing */ |
| 4832 | // if (EV_FD_COND_C(req->cons->fd, DIR_WR)) |
| 4833 | // req->wex = TICK_ETERNITY; |
| 4834 | // } else { |
| 4835 | // /* buffer not empty, there are still data to be transferred */ |
| 4836 | // EV_FD_COND_S(req->cons->fd, DIR_WR); |
| 4837 | // if (!tick_isset(req->wex)) { |
| 4838 | // /* restart writing */ |
| 4839 | // req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4840 | // if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) { |
| 4841 | // /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4842 | // * we refresh it, except if it was already infinite. |
| 4843 | // */ |
| 4844 | // rep->rex = req->wex; |
| 4845 | // } |
| 4846 | // } |
| 4847 | // } |
| 4848 | // } |
| 4849 | // return 0; /* other cases change nothing */ |
| 4850 | //} |
| 4851 | // |
| 4852 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4853 | /* |
| 4854 | * Produces data for the session <s> depending on its source. Expects to be |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 4855 | * called with client socket shut down on input. Right now, only statistics can |
| 4856 | * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4857 | * session, which it uses to keep on being called when there is free space in |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4858 | * the buffer, or simply by letting an empty buffer upon return. It returns 1 |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 4859 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4860 | */ |
| 4861 | int produce_content(struct session *s) |
| 4862 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4863 | if (s->data_source == DATA_SRC_NONE) { |
| 4864 | s->flags &= ~SN_SELF_GEN; |
| 4865 | return 1; |
| 4866 | } |
| 4867 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4868 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4869 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4870 | if (ret >= 0) |
| 4871 | return ret; |
| 4872 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4873 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4874 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4875 | /* unknown data source or internal error */ |
| 4876 | s->txn.status = 500; |
| 4877 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4878 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4879 | if (!(s->flags & SN_ERR_MASK)) |
| 4880 | s->flags |= SN_ERR_PRXCOND; |
| 4881 | if (!(s->flags & SN_FINST_MASK)) |
| 4882 | s->flags |= SN_FINST_R; |
| 4883 | s->flags &= ~SN_SELF_GEN; |
| 4884 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4888 | /* Iterate the same filter through all request headers. |
| 4889 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4890 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4891 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4892 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4893 | 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] | 4894 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4895 | char term; |
| 4896 | char *cur_ptr, *cur_end, *cur_next; |
| 4897 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4898 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4899 | struct hdr_idx_elem *cur_hdr; |
| 4900 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4901 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4902 | last_hdr = 0; |
| 4903 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4904 | 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] | 4905 | old_idx = 0; |
| 4906 | |
| 4907 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4908 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4909 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4910 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4911 | (exp->action == ACT_ALLOW || |
| 4912 | exp->action == ACT_DENY || |
| 4913 | exp->action == ACT_TARPIT)) |
| 4914 | return 0; |
| 4915 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4916 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4917 | if (!cur_idx) |
| 4918 | break; |
| 4919 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4920 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4921 | cur_ptr = cur_next; |
| 4922 | cur_end = cur_ptr + cur_hdr->len; |
| 4923 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4924 | |
| 4925 | /* Now we have one header between cur_ptr and cur_end, |
| 4926 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4927 | */ |
| 4928 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4929 | /* The annoying part is that pattern matching needs |
| 4930 | * that we modify the contents to null-terminate all |
| 4931 | * strings before testing them. |
| 4932 | */ |
| 4933 | |
| 4934 | term = *cur_end; |
| 4935 | *cur_end = '\0'; |
| 4936 | |
| 4937 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4938 | switch (exp->action) { |
| 4939 | case ACT_SETBE: |
| 4940 | /* It is not possible to jump a second time. |
| 4941 | * FIXME: should we return an HTTP/500 here so that |
| 4942 | * the admin knows there's a problem ? |
| 4943 | */ |
| 4944 | if (t->be != t->fe) |
| 4945 | break; |
| 4946 | |
| 4947 | /* Swithing Proxy */ |
| 4948 | t->be = (struct proxy *) exp->replace; |
| 4949 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4950 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4951 | * because we have associated req_cap and rsp_cap to the |
| 4952 | * frontend, and the beconn will be updated later. |
| 4953 | */ |
| 4954 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4955 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4956 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4957 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4958 | last_hdr = 1; |
| 4959 | break; |
| 4960 | |
| 4961 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4962 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4963 | last_hdr = 1; |
| 4964 | break; |
| 4965 | |
| 4966 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4967 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4968 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4969 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4970 | break; |
| 4971 | |
| 4972 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4973 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4974 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4975 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4976 | break; |
| 4977 | |
| 4978 | case ACT_REPLACE: |
| 4979 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4980 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4981 | /* FIXME: if the user adds a newline in the replacement, the |
| 4982 | * index will not be recalculated for now, and the new line |
| 4983 | * will not be counted as a new header. |
| 4984 | */ |
| 4985 | |
| 4986 | cur_end += delta; |
| 4987 | cur_next += delta; |
| 4988 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4989 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4990 | break; |
| 4991 | |
| 4992 | case ACT_REMOVE: |
| 4993 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4994 | cur_next += delta; |
| 4995 | |
| 4996 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4997 | txn->req.eoh += delta; |
| 4998 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4999 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5000 | cur_hdr->len = 0; |
| 5001 | cur_end = NULL; /* null-term has been rewritten */ |
| 5002 | break; |
| 5003 | |
| 5004 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5005 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5006 | if (cur_end) |
| 5007 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5008 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5009 | /* keep the link from this header to next one in case of later |
| 5010 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5011 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5012 | old_idx = cur_idx; |
| 5013 | } |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | /* Apply the filter to the request line. |
| 5019 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5020 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5021 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5022 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5023 | */ |
| 5024 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5025 | { |
| 5026 | char term; |
| 5027 | char *cur_ptr, *cur_end; |
| 5028 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5029 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5030 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5032 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5033 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5034 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5035 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5036 | (exp->action == ACT_ALLOW || |
| 5037 | exp->action == ACT_DENY || |
| 5038 | exp->action == ACT_TARPIT)) |
| 5039 | return 0; |
| 5040 | else if (exp->action == ACT_REMOVE) |
| 5041 | return 0; |
| 5042 | |
| 5043 | done = 0; |
| 5044 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5045 | 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] | 5046 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5047 | |
| 5048 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5049 | |
| 5050 | /* The annoying part is that pattern matching needs |
| 5051 | * that we modify the contents to null-terminate all |
| 5052 | * strings before testing them. |
| 5053 | */ |
| 5054 | |
| 5055 | term = *cur_end; |
| 5056 | *cur_end = '\0'; |
| 5057 | |
| 5058 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5059 | switch (exp->action) { |
| 5060 | case ACT_SETBE: |
| 5061 | /* It is not possible to jump a second time. |
| 5062 | * FIXME: should we return an HTTP/500 here so that |
| 5063 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5064 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5065 | if (t->be != t->fe) |
| 5066 | break; |
| 5067 | |
| 5068 | /* Swithing Proxy */ |
| 5069 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5070 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5071 | /* right now, the backend switch is not too much complicated |
| 5072 | * because we have associated req_cap and rsp_cap to the |
| 5073 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5074 | */ |
| 5075 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 5076 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 5077 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 5078 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5079 | done = 1; |
| 5080 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5081 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5082 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5083 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5084 | done = 1; |
| 5085 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5086 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5087 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5088 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5089 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5090 | done = 1; |
| 5091 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5093 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5094 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5095 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5096 | done = 1; |
| 5097 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5098 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5099 | case ACT_REPLACE: |
| 5100 | *cur_end = term; /* restore the string terminator */ |
| 5101 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5102 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5103 | /* FIXME: if the user adds a newline in the replacement, the |
| 5104 | * index will not be recalculated for now, and the new line |
| 5105 | * will not be counted as a new header. |
| 5106 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5107 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5108 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5109 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5110 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5111 | 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] | 5112 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5113 | HTTP_MSG_RQMETH, |
| 5114 | cur_ptr, cur_end + 1, |
| 5115 | NULL, NULL); |
| 5116 | if (unlikely(!cur_end)) |
| 5117 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5118 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5119 | /* we have a full request and we know that we have either a CR |
| 5120 | * or an LF at <ptr>. |
| 5121 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5122 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5123 | 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] | 5124 | /* there is no point trying this regex on headers */ |
| 5125 | return 1; |
| 5126 | } |
| 5127 | } |
| 5128 | *cur_end = term; /* restore the string terminator */ |
| 5129 | return done; |
| 5130 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5131 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5132 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5133 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5134 | /* |
| 5135 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5136 | * 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] | 5137 | * unparsable request. Since it can manage the switch to another backend, it |
| 5138 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5139 | */ |
| 5140 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5141 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5142 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5143 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5144 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5145 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5146 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5147 | /* |
| 5148 | * The interleaving of transformations and verdicts |
| 5149 | * makes it difficult to decide to continue or stop |
| 5150 | * the evaluation. |
| 5151 | */ |
| 5152 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5153 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5154 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5155 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5156 | exp = exp->next; |
| 5157 | continue; |
| 5158 | } |
| 5159 | |
| 5160 | /* Apply the filter to the request line. */ |
| 5161 | ret = apply_filter_to_req_line(t, req, exp); |
| 5162 | if (unlikely(ret < 0)) |
| 5163 | return -1; |
| 5164 | |
| 5165 | if (likely(ret == 0)) { |
| 5166 | /* The filter did not match the request, it can be |
| 5167 | * iterated through all headers. |
| 5168 | */ |
| 5169 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5170 | } |
| 5171 | exp = exp->next; |
| 5172 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5173 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5177 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5178 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5179 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5180 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5181 | */ |
| 5182 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5183 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5184 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5185 | char *p1, *p2, *p3, *p4; |
| 5186 | char *del_colon, *del_cookie, *colon; |
| 5187 | int app_cookies; |
| 5188 | |
| 5189 | appsess *asession_temp = NULL; |
| 5190 | appsess local_asession; |
| 5191 | |
| 5192 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5193 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5194 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5195 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5196 | * we start with the start line. |
| 5197 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5198 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5199 | 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] | 5200 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5201 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5202 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5203 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5204 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5205 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5206 | cur_ptr = cur_next; |
| 5207 | cur_end = cur_ptr + cur_hdr->len; |
| 5208 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5209 | |
| 5210 | /* We have one full header between cur_ptr and cur_end, and the |
| 5211 | * next header starts at cur_next. We're only interested in |
| 5212 | * "Cookie:" headers. |
| 5213 | */ |
| 5214 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5215 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5216 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5217 | old_idx = cur_idx; |
| 5218 | continue; |
| 5219 | } |
| 5220 | |
| 5221 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5222 | * attributes whose name begin with a '$', and associate them with |
| 5223 | * the right cookie, if we want to delete this cookie. |
| 5224 | * So there are 3 cases for each cookie read : |
| 5225 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5226 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5227 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5228 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5229 | * "special" cookie. |
| 5230 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5231 | * remove it. If no application cookie persists in the header, we |
| 5232 | * *MUST* delete it |
| 5233 | */ |
| 5234 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5235 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5236 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5237 | /* del_cookie == NULL => nothing to be deleted */ |
| 5238 | del_colon = del_cookie = NULL; |
| 5239 | app_cookies = 0; |
| 5240 | |
| 5241 | while (p1 < cur_end) { |
| 5242 | /* skip spaces and colons, but keep an eye on these ones */ |
| 5243 | while (p1 < cur_end) { |
| 5244 | if (*p1 == ';' || *p1 == ',') |
| 5245 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5246 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | break; |
| 5248 | p1++; |
| 5249 | } |
| 5250 | |
| 5251 | if (p1 == cur_end) |
| 5252 | break; |
| 5253 | |
| 5254 | /* p1 is at the beginning of the cookie name */ |
| 5255 | p2 = p1; |
| 5256 | while (p2 < cur_end && *p2 != '=') |
| 5257 | p2++; |
| 5258 | |
| 5259 | if (p2 == cur_end) |
| 5260 | break; |
| 5261 | |
| 5262 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5263 | if (p3 == cur_end) |
| 5264 | break; |
| 5265 | |
| 5266 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5267 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5268 | p4++; |
| 5269 | |
| 5270 | /* here, we have the cookie name between p1 and p2, |
| 5271 | * and its value between p3 and p4. |
| 5272 | * we can process it : |
| 5273 | * |
| 5274 | * Cookie: NAME=VALUE; |
| 5275 | * | || || | |
| 5276 | * | || || +--> p4 |
| 5277 | * | || |+-------> p3 |
| 5278 | * | || +--------> p2 |
| 5279 | * | |+------------> p1 |
| 5280 | * | +-------------> colon |
| 5281 | * +--------------------> cur_ptr |
| 5282 | */ |
| 5283 | |
| 5284 | if (*p1 == '$') { |
| 5285 | /* skip this one */ |
| 5286 | } |
| 5287 | else { |
| 5288 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5289 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5290 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5291 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5292 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5293 | int log_len = p4 - p1; |
| 5294 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5295 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5296 | Alert("HTTP logging : out of memory.\n"); |
| 5297 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5298 | if (log_len > t->fe->capture_len) |
| 5299 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5300 | memcpy(txn->cli_cookie, p1, log_len); |
| 5301 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5302 | } |
| 5303 | } |
| 5304 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5305 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5306 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5307 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5308 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5309 | char *delim; |
| 5310 | |
| 5311 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5312 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5313 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5314 | * |
| 5315 | * Cookie: NAME=SRV~VALUE; |
| 5316 | * | || || | | |
| 5317 | * | || || | +--> p4 |
| 5318 | * | || || +--------> delim |
| 5319 | * | || |+-----------> p3 |
| 5320 | * | || +------------> p2 |
| 5321 | * | |+----------------> p1 |
| 5322 | * | +-----------------> colon |
| 5323 | * +------------------------> cur_ptr |
| 5324 | */ |
| 5325 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5326 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5327 | for (delim = p3; delim < p4; delim++) |
| 5328 | if (*delim == COOKIE_DELIM) |
| 5329 | break; |
| 5330 | } |
| 5331 | else |
| 5332 | delim = p4; |
| 5333 | |
| 5334 | |
| 5335 | /* Here, we'll look for the first running server which supports the cookie. |
| 5336 | * This allows to share a same cookie between several servers, for example |
| 5337 | * to dedicate backup servers to specific servers only. |
| 5338 | * However, to prevent clients from sticking to cookie-less backup server |
| 5339 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5340 | * empty cookies and mark them as invalid. |
| 5341 | */ |
| 5342 | if (delim == p3) |
| 5343 | srv = NULL; |
| 5344 | |
| 5345 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5346 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5347 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5348 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5349 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5350 | txn->flags &= ~TX_CK_MASK; |
| 5351 | txn->flags |= TX_CK_VALID; |
| 5352 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5353 | t->srv = srv; |
| 5354 | break; |
| 5355 | } else { |
| 5356 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5357 | txn->flags &= ~TX_CK_MASK; |
| 5358 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | srv = srv->next; |
| 5362 | } |
| 5363 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5364 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5365 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5366 | txn->flags &= ~TX_CK_MASK; |
| 5367 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | /* depending on the cookie mode, we may have to either : |
| 5371 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5372 | * the server never sees it ; |
| 5373 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5374 | * application cookie so that it does not get accidentely removed later, |
| 5375 | * if we're in cookie prefix mode |
| 5376 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5377 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5378 | int delta; /* negative */ |
| 5379 | |
| 5380 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5381 | p4 += delta; |
| 5382 | cur_end += delta; |
| 5383 | cur_next += delta; |
| 5384 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5385 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5386 | |
| 5387 | del_cookie = del_colon = NULL; |
| 5388 | app_cookies++; /* protect the header from deletion */ |
| 5389 | } |
| 5390 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5391 | (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] | 5392 | del_cookie = p1; |
| 5393 | del_colon = colon; |
| 5394 | } |
| 5395 | } else { |
| 5396 | /* now we know that we must keep this cookie since it's |
| 5397 | * not ours. But if we wanted to delete our cookie |
| 5398 | * earlier, we cannot remove the complete header, but we |
| 5399 | * can remove the previous block itself. |
| 5400 | */ |
| 5401 | app_cookies++; |
| 5402 | |
| 5403 | if (del_cookie != NULL) { |
| 5404 | int delta; /* negative */ |
| 5405 | |
| 5406 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5407 | p4 += delta; |
| 5408 | cur_end += delta; |
| 5409 | cur_next += delta; |
| 5410 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5411 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5412 | del_cookie = del_colon = NULL; |
| 5413 | } |
| 5414 | } |
| 5415 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5416 | if ((t->be->appsession_name != NULL) && |
| 5417 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5418 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5419 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5420 | /* Cool... it's the right one */ |
| 5421 | |
| 5422 | asession_temp = &local_asession; |
| 5423 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5424 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5425 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5426 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5427 | return; |
| 5428 | } |
| 5429 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5430 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5431 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5432 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5433 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5434 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5435 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5436 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5437 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5438 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5439 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5440 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5441 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5442 | return; |
| 5443 | } |
| 5444 | |
| 5445 | asession_temp->sessid = local_asession.sessid; |
| 5446 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5447 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5448 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5449 | } else { |
| 5450 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5451 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5452 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5453 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5454 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5455 | Alert("Found Application Session without matching server.\n"); |
| 5456 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5457 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5458 | while (srv) { |
| 5459 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5460 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5461 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5462 | txn->flags &= ~TX_CK_MASK; |
| 5463 | txn->flags |= TX_CK_VALID; |
| 5464 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5465 | t->srv = srv; |
| 5466 | break; |
| 5467 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5468 | txn->flags &= ~TX_CK_MASK; |
| 5469 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5470 | } |
| 5471 | } |
| 5472 | srv = srv->next; |
| 5473 | }/* end while(srv) */ |
| 5474 | }/* end else if server == NULL */ |
| 5475 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5476 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5477 | asession_temp->request_count++; |
| 5478 | #if defined(DEBUG_HASH) |
| 5479 | Alert("manage_client_side_cookies\n"); |
| 5480 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5481 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5482 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5483 | } |
| 5484 | |
| 5485 | /* we'll have to look for another cookie ... */ |
| 5486 | p1 = p4; |
| 5487 | } /* while (p1 < cur_end) */ |
| 5488 | |
| 5489 | /* There's no more cookie on this line. |
| 5490 | * We may have marked the last one(s) for deletion. |
| 5491 | * We must do this now in two ways : |
| 5492 | * - if there is no app cookie, we simply delete the header ; |
| 5493 | * - if there are app cookies, we must delete the end of the |
| 5494 | * string properly, including the colon/semi-colon before |
| 5495 | * the cookie name. |
| 5496 | */ |
| 5497 | if (del_cookie != NULL) { |
| 5498 | int delta; |
| 5499 | if (app_cookies) { |
| 5500 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5501 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5502 | cur_hdr->len += delta; |
| 5503 | } else { |
| 5504 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5505 | |
| 5506 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5507 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5508 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5509 | cur_hdr->len = 0; |
| 5510 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5511 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5512 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5513 | } |
| 5514 | |
| 5515 | /* keep the link from this header to next one */ |
| 5516 | old_idx = cur_idx; |
| 5517 | } /* end of cookie processing on this header */ |
| 5518 | } |
| 5519 | |
| 5520 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5521 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5522 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5523 | */ |
| 5524 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5525 | { |
| 5526 | char term; |
| 5527 | char *cur_ptr, *cur_end, *cur_next; |
| 5528 | int cur_idx, old_idx, last_hdr; |
| 5529 | struct http_txn *txn = &t->txn; |
| 5530 | struct hdr_idx_elem *cur_hdr; |
| 5531 | int len, delta; |
| 5532 | |
| 5533 | last_hdr = 0; |
| 5534 | |
| 5535 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5536 | old_idx = 0; |
| 5537 | |
| 5538 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5539 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5540 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5541 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5542 | (exp->action == ACT_ALLOW || |
| 5543 | exp->action == ACT_DENY)) |
| 5544 | return 0; |
| 5545 | |
| 5546 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5547 | if (!cur_idx) |
| 5548 | break; |
| 5549 | |
| 5550 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5551 | cur_ptr = cur_next; |
| 5552 | cur_end = cur_ptr + cur_hdr->len; |
| 5553 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5554 | |
| 5555 | /* Now we have one header between cur_ptr and cur_end, |
| 5556 | * and the next header starts at cur_next. |
| 5557 | */ |
| 5558 | |
| 5559 | /* The annoying part is that pattern matching needs |
| 5560 | * that we modify the contents to null-terminate all |
| 5561 | * strings before testing them. |
| 5562 | */ |
| 5563 | |
| 5564 | term = *cur_end; |
| 5565 | *cur_end = '\0'; |
| 5566 | |
| 5567 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5568 | switch (exp->action) { |
| 5569 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5570 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5571 | last_hdr = 1; |
| 5572 | break; |
| 5573 | |
| 5574 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5575 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5576 | last_hdr = 1; |
| 5577 | break; |
| 5578 | |
| 5579 | case ACT_REPLACE: |
| 5580 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5581 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5582 | /* FIXME: if the user adds a newline in the replacement, the |
| 5583 | * index will not be recalculated for now, and the new line |
| 5584 | * will not be counted as a new header. |
| 5585 | */ |
| 5586 | |
| 5587 | cur_end += delta; |
| 5588 | cur_next += delta; |
| 5589 | cur_hdr->len += delta; |
| 5590 | txn->rsp.eoh += delta; |
| 5591 | break; |
| 5592 | |
| 5593 | case ACT_REMOVE: |
| 5594 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5595 | cur_next += delta; |
| 5596 | |
| 5597 | /* FIXME: this should be a separate function */ |
| 5598 | txn->rsp.eoh += delta; |
| 5599 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5600 | txn->hdr_idx.used--; |
| 5601 | cur_hdr->len = 0; |
| 5602 | cur_end = NULL; /* null-term has been rewritten */ |
| 5603 | break; |
| 5604 | |
| 5605 | } |
| 5606 | } |
| 5607 | if (cur_end) |
| 5608 | *cur_end = term; /* restore the string terminator */ |
| 5609 | |
| 5610 | /* keep the link from this header to next one in case of later |
| 5611 | * removal of next header. |
| 5612 | */ |
| 5613 | old_idx = cur_idx; |
| 5614 | } |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
| 5618 | |
| 5619 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5620 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5621 | * or -1 if a replacement resulted in an invalid status line. |
| 5622 | */ |
| 5623 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5624 | { |
| 5625 | char term; |
| 5626 | char *cur_ptr, *cur_end; |
| 5627 | int done; |
| 5628 | struct http_txn *txn = &t->txn; |
| 5629 | int len, delta; |
| 5630 | |
| 5631 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5632 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5633 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5634 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5635 | (exp->action == ACT_ALLOW || |
| 5636 | exp->action == ACT_DENY)) |
| 5637 | return 0; |
| 5638 | else if (exp->action == ACT_REMOVE) |
| 5639 | return 0; |
| 5640 | |
| 5641 | done = 0; |
| 5642 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5643 | 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] | 5644 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5645 | |
| 5646 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5647 | |
| 5648 | /* The annoying part is that pattern matching needs |
| 5649 | * that we modify the contents to null-terminate all |
| 5650 | * strings before testing them. |
| 5651 | */ |
| 5652 | |
| 5653 | term = *cur_end; |
| 5654 | *cur_end = '\0'; |
| 5655 | |
| 5656 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5657 | switch (exp->action) { |
| 5658 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5659 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5660 | done = 1; |
| 5661 | break; |
| 5662 | |
| 5663 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5664 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5665 | done = 1; |
| 5666 | break; |
| 5667 | |
| 5668 | case ACT_REPLACE: |
| 5669 | *cur_end = term; /* restore the string terminator */ |
| 5670 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5671 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5672 | /* FIXME: if the user adds a newline in the replacement, the |
| 5673 | * index will not be recalculated for now, and the new line |
| 5674 | * will not be counted as a new header. |
| 5675 | */ |
| 5676 | |
| 5677 | txn->rsp.eoh += delta; |
| 5678 | cur_end += delta; |
| 5679 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5680 | 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] | 5681 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5682 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5683 | cur_ptr, cur_end + 1, |
| 5684 | NULL, NULL); |
| 5685 | if (unlikely(!cur_end)) |
| 5686 | return -1; |
| 5687 | |
| 5688 | /* we have a full respnse and we know that we have either a CR |
| 5689 | * or an LF at <ptr>. |
| 5690 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5691 | 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] | 5692 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5693 | /* there is no point trying this regex on headers */ |
| 5694 | return 1; |
| 5695 | } |
| 5696 | } |
| 5697 | *cur_end = term; /* restore the string terminator */ |
| 5698 | return done; |
| 5699 | } |
| 5700 | |
| 5701 | |
| 5702 | |
| 5703 | /* |
| 5704 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5705 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5706 | * unparsable response. |
| 5707 | */ |
| 5708 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5709 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5710 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5711 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5712 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5713 | int ret; |
| 5714 | |
| 5715 | /* |
| 5716 | * The interleaving of transformations and verdicts |
| 5717 | * makes it difficult to decide to continue or stop |
| 5718 | * the evaluation. |
| 5719 | */ |
| 5720 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5721 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5722 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5723 | exp->action == ACT_PASS)) { |
| 5724 | exp = exp->next; |
| 5725 | continue; |
| 5726 | } |
| 5727 | |
| 5728 | /* Apply the filter to the status line. */ |
| 5729 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5730 | if (unlikely(ret < 0)) |
| 5731 | return -1; |
| 5732 | |
| 5733 | if (likely(ret == 0)) { |
| 5734 | /* The filter did not match the response, it can be |
| 5735 | * iterated through all headers. |
| 5736 | */ |
| 5737 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5738 | } |
| 5739 | exp = exp->next; |
| 5740 | } |
| 5741 | return 0; |
| 5742 | } |
| 5743 | |
| 5744 | |
| 5745 | |
| 5746 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5747 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5748 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5749 | */ |
| 5750 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5751 | { |
| 5752 | struct http_txn *txn = &t->txn; |
| 5753 | char *p1, *p2, *p3, *p4; |
| 5754 | |
| 5755 | appsess *asession_temp = NULL; |
| 5756 | appsess local_asession; |
| 5757 | |
| 5758 | char *cur_ptr, *cur_end, *cur_next; |
| 5759 | int cur_idx, old_idx, delta; |
| 5760 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5761 | /* Iterate through the headers. |
| 5762 | * we start with the start line. |
| 5763 | */ |
| 5764 | old_idx = 0; |
| 5765 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5766 | |
| 5767 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5768 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5769 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5770 | |
| 5771 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5772 | cur_ptr = cur_next; |
| 5773 | cur_end = cur_ptr + cur_hdr->len; |
| 5774 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5775 | |
| 5776 | /* We have one full header between cur_ptr and cur_end, and the |
| 5777 | * next header starts at cur_next. We're only interested in |
| 5778 | * "Cookie:" headers. |
| 5779 | */ |
| 5780 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5781 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5782 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5783 | old_idx = cur_idx; |
| 5784 | continue; |
| 5785 | } |
| 5786 | |
| 5787 | /* 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] | 5788 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5789 | |
| 5790 | |
| 5791 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5792 | if (t->be->cookie_name == NULL && |
| 5793 | t->be->appsession_name == NULL && |
| 5794 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5795 | return; |
| 5796 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5797 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5798 | |
| 5799 | 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] | 5800 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5801 | break; |
| 5802 | |
| 5803 | /* p1 is at the beginning of the cookie name */ |
| 5804 | p2 = p1; |
| 5805 | |
| 5806 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5807 | p2++; |
| 5808 | |
| 5809 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5810 | break; |
| 5811 | |
| 5812 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5813 | if (p3 == cur_end) |
| 5814 | break; |
| 5815 | |
| 5816 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5817 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5818 | p4++; |
| 5819 | |
| 5820 | /* here, we have the cookie name between p1 and p2, |
| 5821 | * and its value between p3 and p4. |
| 5822 | * we can process it. |
| 5823 | */ |
| 5824 | |
| 5825 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5826 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5827 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5828 | (p4 - p1 >= t->be->capture_namelen) && |
| 5829 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5830 | int log_len = p4 - p1; |
| 5831 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5832 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5833 | Alert("HTTP logging : out of memory.\n"); |
| 5834 | } |
| 5835 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5836 | if (log_len > t->be->capture_len) |
| 5837 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5838 | memcpy(txn->srv_cookie, p1, log_len); |
| 5839 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5843 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5844 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5845 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5846 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5849 | * this occurrence because we'll insert another one later. |
| 5850 | * We'll delete it too if the "indirect" option is set and we're in |
| 5851 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5852 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5853 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5854 | /* this header must be deleted */ |
| 5855 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5856 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5857 | txn->hdr_idx.used--; |
| 5858 | cur_hdr->len = 0; |
| 5859 | cur_next += delta; |
| 5860 | txn->rsp.eoh += delta; |
| 5861 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5862 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5863 | } |
| 5864 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5865 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5866 | /* replace bytes p3->p4 with the cookie name associated |
| 5867 | * with this server since we know it. |
| 5868 | */ |
| 5869 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5870 | cur_hdr->len += delta; |
| 5871 | cur_next += delta; |
| 5872 | txn->rsp.eoh += delta; |
| 5873 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5874 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5875 | } |
| 5876 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5877 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5878 | /* insert the cookie name associated with this server |
| 5879 | * before existing cookie, and insert a delimitor between them.. |
| 5880 | */ |
| 5881 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5882 | cur_hdr->len += delta; |
| 5883 | cur_next += delta; |
| 5884 | txn->rsp.eoh += delta; |
| 5885 | |
| 5886 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5887 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5888 | } |
| 5889 | } |
| 5890 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5891 | else if ((t->be->appsession_name != NULL) && |
| 5892 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5893 | |
| 5894 | /* Cool... it's the right one */ |
| 5895 | |
| 5896 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5897 | asession_temp = &local_asession; |
| 5898 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5899 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5900 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5901 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5902 | return; |
| 5903 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5904 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5905 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5906 | asession_temp->serverid = NULL; |
| 5907 | |
| 5908 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5909 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5910 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5911 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5912 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5913 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5914 | return; |
| 5915 | } |
| 5916 | asession_temp->sessid = local_asession.sessid; |
| 5917 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5918 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5919 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5920 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5921 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5922 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5923 | } |
| 5924 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5925 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5926 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5927 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5928 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5929 | return; |
| 5930 | } |
| 5931 | asession_temp->serverid[0] = '\0'; |
| 5932 | } |
| 5933 | |
| 5934 | if (asession_temp->serverid[0] == '\0') |
| 5935 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5936 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5937 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5938 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5939 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5940 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5941 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5942 | #endif |
| 5943 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5944 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5945 | } /* we're now at the end of the cookie value */ |
| 5946 | |
| 5947 | /* keep the link from this header to next one */ |
| 5948 | old_idx = cur_idx; |
| 5949 | } /* end of cookie processing on this header */ |
| 5950 | } |
| 5951 | |
| 5952 | |
| 5953 | |
| 5954 | /* |
| 5955 | * Check if response is cacheable or not. Updates t->flags. |
| 5956 | */ |
| 5957 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5958 | { |
| 5959 | struct http_txn *txn = &t->txn; |
| 5960 | char *p1, *p2; |
| 5961 | |
| 5962 | char *cur_ptr, *cur_end, *cur_next; |
| 5963 | int cur_idx; |
| 5964 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5965 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5966 | return; |
| 5967 | |
| 5968 | /* Iterate through the headers. |
| 5969 | * we start with the start line. |
| 5970 | */ |
| 5971 | cur_idx = 0; |
| 5972 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5973 | |
| 5974 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5975 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5976 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5977 | |
| 5978 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5979 | cur_ptr = cur_next; |
| 5980 | cur_end = cur_ptr + cur_hdr->len; |
| 5981 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5982 | |
| 5983 | /* We have one full header between cur_ptr and cur_end, and the |
| 5984 | * next header starts at cur_next. We're only interested in |
| 5985 | * "Cookie:" headers. |
| 5986 | */ |
| 5987 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5988 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5989 | if (val) { |
| 5990 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5991 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5992 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5993 | return; |
| 5994 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5995 | } |
| 5996 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5997 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5998 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5999 | continue; |
| 6000 | |
| 6001 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6002 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6003 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6004 | |
| 6005 | if (p1 >= cur_end) /* no more info */ |
| 6006 | continue; |
| 6007 | |
| 6008 | /* p1 is at the beginning of the value */ |
| 6009 | p2 = p1; |
| 6010 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6011 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6012 | p2++; |
| 6013 | |
| 6014 | /* we have a complete value between p1 and p2 */ |
| 6015 | if (p2 < cur_end && *p2 == '=') { |
| 6016 | /* we have something of the form no-cache="set-cookie" */ |
| 6017 | if ((cur_end - p1 >= 21) && |
| 6018 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6019 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6020 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6021 | continue; |
| 6022 | } |
| 6023 | |
| 6024 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6025 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6026 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6027 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6028 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6029 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6030 | return; |
| 6031 | } |
| 6032 | |
| 6033 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6034 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6035 | continue; |
| 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | |
| 6040 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6041 | /* |
| 6042 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6043 | * If the server is found, it's assigned to the session. |
| 6044 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6045 | 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] | 6046 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6047 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6048 | appsess *asession_temp = NULL; |
| 6049 | appsess local_asession; |
| 6050 | char *request_line; |
| 6051 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6052 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 6053 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6054 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6055 | ((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] | 6056 | return; |
| 6057 | |
| 6058 | /* skip ';' */ |
| 6059 | request_line++; |
| 6060 | |
| 6061 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6062 | 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] | 6063 | return; |
| 6064 | |
| 6065 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6066 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6067 | |
| 6068 | /* First try if we already have an appsession */ |
| 6069 | asession_temp = &local_asession; |
| 6070 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6071 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6072 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 6073 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 6074 | return; |
| 6075 | } |
| 6076 | |
| 6077 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6078 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 6079 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6080 | asession_temp->serverid = NULL; |
| 6081 | |
| 6082 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 6083 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 6084 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6085 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6086 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6087 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6088 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 6089 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 6090 | return; |
| 6091 | } |
| 6092 | asession_temp->sessid = local_asession.sessid; |
| 6093 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6094 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6095 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6096 | } |
| 6097 | else { |
| 6098 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6099 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6100 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6101 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 6102 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6103 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6104 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6105 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6106 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6107 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6108 | #endif |
| 6109 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6110 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6111 | Alert("Found Application Session without matching server.\n"); |
| 6112 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6113 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6114 | while (srv) { |
| 6115 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6116 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6117 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6118 | txn->flags &= ~TX_CK_MASK; |
| 6119 | txn->flags |= TX_CK_VALID; |
| 6120 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6121 | t->srv = srv; |
| 6122 | break; |
| 6123 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6124 | txn->flags &= ~TX_CK_MASK; |
| 6125 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | srv = srv->next; |
| 6129 | } |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6134 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6135 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6136 | * 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] | 6137 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6138 | * 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] | 6139 | * 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] | 6140 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6141 | * |
| 6142 | * Returns 1 if the session's state changes, otherwise 0. |
| 6143 | */ |
| 6144 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6145 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6146 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6147 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6148 | struct user_auth *user; |
| 6149 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6150 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6151 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6152 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6153 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6154 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6155 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6156 | return 0; |
| 6157 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6158 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6159 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6160 | /* the URI is in h */ |
| 6161 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6162 | return 0; |
| 6163 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6164 | h += uri_auth->uri_len; |
| 6165 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 6166 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6167 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6168 | break; |
| 6169 | } |
| 6170 | h++; |
| 6171 | } |
| 6172 | |
| 6173 | if (uri_auth->refresh) { |
| 6174 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6175 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 6176 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6177 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6178 | break; |
| 6179 | } |
| 6180 | h++; |
| 6181 | } |
| 6182 | } |
| 6183 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6184 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6185 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 6186 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6187 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6188 | break; |
| 6189 | } |
| 6190 | h++; |
| 6191 | } |
| 6192 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6193 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6194 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6195 | /* we are in front of a interceptable URI. Let's check |
| 6196 | * if there's an authentication and if it's valid. |
| 6197 | */ |
| 6198 | user = uri_auth->users; |
| 6199 | if (!user) { |
| 6200 | /* no user auth required, it's OK */ |
| 6201 | authenticated = 1; |
| 6202 | } else { |
| 6203 | authenticated = 0; |
| 6204 | |
| 6205 | /* a user list is defined, we have to check. |
| 6206 | * skip 21 chars for "Authorization: Basic ". |
| 6207 | */ |
| 6208 | |
| 6209 | /* FIXME: this should move to an earlier place */ |
| 6210 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6211 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 6212 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6213 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6214 | if (len > 14 && |
| 6215 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6216 | txn->auth_hdr.str = h; |
| 6217 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6218 | break; |
| 6219 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6220 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6221 | } |
| 6222 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6223 | if (txn->auth_hdr.len < 21 || |
| 6224 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6225 | user = NULL; |
| 6226 | |
| 6227 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6228 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6229 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6230 | user->user_pwd, user->user_len)) { |
| 6231 | authenticated = 1; |
| 6232 | break; |
| 6233 | } |
| 6234 | user = user->next; |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6239 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6240 | |
| 6241 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6242 | msg.str = trash; |
| 6243 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6244 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6245 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 6246 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6247 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6248 | if (!(t->flags & SN_ERR_MASK)) |
| 6249 | t->flags |= SN_ERR_PRXCOND; |
| 6250 | if (!(t->flags & SN_FINST_MASK)) |
| 6251 | t->flags |= SN_FINST_R; |
| 6252 | return 1; |
| 6253 | } |
| 6254 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6255 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6256 | * data. |
| 6257 | */ |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 6258 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 6259 | buffer_shutr(t->req); |
| 6260 | buffer_shutr(t->rep); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 6261 | buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6262 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6263 | t->data_source = DATA_SRC_STATS; |
| 6264 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6265 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6266 | produce_content(t); |
| 6267 | return 1; |
| 6268 | } |
| 6269 | |
| 6270 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6271 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6272 | * Print a debug line with a header |
| 6273 | */ |
| 6274 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6275 | { |
| 6276 | int len, max; |
| 6277 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame^] | 6278 | dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6279 | max = end - start; |
| 6280 | UBOUND(max, sizeof(trash) - len - 1); |
| 6281 | len += strlcpy2(trash + len, start, max + 1); |
| 6282 | trash[len++] = '\n'; |
| 6283 | write(1, trash, len); |
| 6284 | } |
| 6285 | |
| 6286 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6287 | /************************************************************************/ |
| 6288 | /* The code below is dedicated to ACL parsing and matching */ |
| 6289 | /************************************************************************/ |
| 6290 | |
| 6291 | |
| 6292 | |
| 6293 | |
| 6294 | /* 1. Check on METHOD |
| 6295 | * We use the pre-parsed method if it is known, and store its number as an |
| 6296 | * integer. If it is unknown, we use the pointer and the length. |
| 6297 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6298 | 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] | 6299 | { |
| 6300 | int len, meth; |
| 6301 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6302 | len = strlen(*text); |
| 6303 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6304 | |
| 6305 | pattern->val.i = meth; |
| 6306 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6307 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6308 | if (!pattern->ptr.str) |
| 6309 | return 0; |
| 6310 | pattern->len = len; |
| 6311 | } |
| 6312 | return 1; |
| 6313 | } |
| 6314 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6315 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6316 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6317 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6318 | { |
| 6319 | int meth; |
| 6320 | struct http_txn *txn = l7; |
| 6321 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6322 | if (!txn) |
| 6323 | return 0; |
| 6324 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6325 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6326 | return 0; |
| 6327 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6328 | meth = txn->meth; |
| 6329 | test->i = meth; |
| 6330 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6331 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6332 | /* ensure the indexes are not affected */ |
| 6333 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6334 | test->len = txn->req.sl.rq.m_l; |
| 6335 | test->ptr = txn->req.sol; |
| 6336 | } |
| 6337 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6338 | return 1; |
| 6339 | } |
| 6340 | |
| 6341 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6342 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6343 | int icase; |
| 6344 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6345 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6346 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6347 | |
| 6348 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6349 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6350 | |
| 6351 | /* Other method, we must compare the strings */ |
| 6352 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6353 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6354 | |
| 6355 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6356 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6357 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6358 | return ACL_PAT_FAIL; |
| 6359 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | /* 2. Check on Request/Status Version |
| 6363 | * We simply compare strings here. |
| 6364 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6365 | 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] | 6366 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6367 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6368 | if (!pattern->ptr.str) |
| 6369 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6370 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6371 | return 1; |
| 6372 | } |
| 6373 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6374 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6375 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6376 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6377 | { |
| 6378 | struct http_txn *txn = l7; |
| 6379 | char *ptr; |
| 6380 | int len; |
| 6381 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6382 | if (!txn) |
| 6383 | return 0; |
| 6384 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6385 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6386 | return 0; |
| 6387 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6388 | len = txn->req.sl.rq.v_l; |
| 6389 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 6390 | |
| 6391 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6392 | if (len <= 0) |
| 6393 | return 0; |
| 6394 | |
| 6395 | test->ptr = ptr; |
| 6396 | test->len = len; |
| 6397 | |
| 6398 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6399 | return 1; |
| 6400 | } |
| 6401 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6402 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6403 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6404 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6405 | { |
| 6406 | struct http_txn *txn = l7; |
| 6407 | char *ptr; |
| 6408 | int len; |
| 6409 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6410 | if (!txn) |
| 6411 | return 0; |
| 6412 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6413 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6414 | return 0; |
| 6415 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6416 | len = txn->rsp.sl.st.v_l; |
| 6417 | ptr = txn->rsp.sol; |
| 6418 | |
| 6419 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6420 | if (len <= 0) |
| 6421 | return 0; |
| 6422 | |
| 6423 | test->ptr = ptr; |
| 6424 | test->len = len; |
| 6425 | |
| 6426 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6427 | return 1; |
| 6428 | } |
| 6429 | |
| 6430 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6431 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6432 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6433 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6434 | { |
| 6435 | struct http_txn *txn = l7; |
| 6436 | char *ptr; |
| 6437 | int len; |
| 6438 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6439 | if (!txn) |
| 6440 | return 0; |
| 6441 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6442 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6443 | return 0; |
| 6444 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6445 | len = txn->rsp.sl.st.c_l; |
| 6446 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 6447 | |
| 6448 | test->i = __strl2ui(ptr, len); |
| 6449 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6450 | return 1; |
| 6451 | } |
| 6452 | |
| 6453 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6454 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6455 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6456 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6457 | { |
| 6458 | struct http_txn *txn = l7; |
| 6459 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6460 | if (!txn) |
| 6461 | return 0; |
| 6462 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6463 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6464 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6465 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6466 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6467 | /* ensure the indexes are not affected */ |
| 6468 | return 0; |
| 6469 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6470 | test->len = txn->req.sl.rq.u_l; |
| 6471 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 6472 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6473 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6474 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6475 | return 1; |
| 6476 | } |
| 6477 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6478 | static int |
| 6479 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6480 | struct acl_expr *expr, struct acl_test *test) |
| 6481 | { |
| 6482 | struct http_txn *txn = l7; |
| 6483 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6484 | if (!txn) |
| 6485 | return 0; |
| 6486 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6487 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6488 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6489 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6490 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6491 | /* ensure the indexes are not affected */ |
| 6492 | return 0; |
| 6493 | |
| 6494 | /* Parse HTTP request */ |
| 6495 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 6496 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6497 | test->i = AF_INET; |
| 6498 | |
| 6499 | /* |
| 6500 | * If we are parsing url in frontend space, we prepare backend stage |
| 6501 | * to not parse again the same url ! optimization lazyness... |
| 6502 | */ |
| 6503 | if (px->options & PR_O_HTTP_PROXY) |
| 6504 | l4->flags |= SN_ADDR_SET; |
| 6505 | |
| 6506 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6507 | return 1; |
| 6508 | } |
| 6509 | |
| 6510 | static int |
| 6511 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6512 | struct acl_expr *expr, struct acl_test *test) |
| 6513 | { |
| 6514 | struct http_txn *txn = l7; |
| 6515 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6516 | if (!txn) |
| 6517 | return 0; |
| 6518 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6519 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6520 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6521 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6522 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6523 | /* ensure the indexes are not affected */ |
| 6524 | return 0; |
| 6525 | |
| 6526 | /* Same optimization as url_ip */ |
| 6527 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 6528 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6529 | |
| 6530 | if (px->options & PR_O_HTTP_PROXY) |
| 6531 | l4->flags |= SN_ADDR_SET; |
| 6532 | |
| 6533 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6534 | return 1; |
| 6535 | } |
| 6536 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6537 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6538 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6539 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6540 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6541 | 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] | 6542 | struct acl_expr *expr, struct acl_test *test) |
| 6543 | { |
| 6544 | struct http_txn *txn = l7; |
| 6545 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6546 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6547 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6548 | if (!txn) |
| 6549 | return 0; |
| 6550 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6551 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6552 | /* search for header from the beginning */ |
| 6553 | ctx->idx = 0; |
| 6554 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6555 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6556 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6557 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6558 | test->len = ctx->vlen; |
| 6559 | test->ptr = (char *)ctx->line + ctx->val; |
| 6560 | return 1; |
| 6561 | } |
| 6562 | |
| 6563 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6564 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6565 | return 0; |
| 6566 | } |
| 6567 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6568 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6569 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6570 | struct acl_expr *expr, struct acl_test *test) |
| 6571 | { |
| 6572 | struct http_txn *txn = l7; |
| 6573 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6574 | if (!txn) |
| 6575 | return 0; |
| 6576 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6577 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6578 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6579 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6580 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6581 | /* ensure the indexes are not affected */ |
| 6582 | return 0; |
| 6583 | |
| 6584 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6585 | } |
| 6586 | |
| 6587 | static int |
| 6588 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6589 | struct acl_expr *expr, struct acl_test *test) |
| 6590 | { |
| 6591 | struct http_txn *txn = l7; |
| 6592 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6593 | if (!txn) |
| 6594 | return 0; |
| 6595 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6596 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6597 | return 0; |
| 6598 | |
| 6599 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6600 | } |
| 6601 | |
| 6602 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6603 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6604 | */ |
| 6605 | static int |
| 6606 | 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] | 6607 | struct acl_expr *expr, struct acl_test *test) |
| 6608 | { |
| 6609 | struct http_txn *txn = l7; |
| 6610 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6611 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6612 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6613 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6614 | if (!txn) |
| 6615 | return 0; |
| 6616 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6617 | ctx.idx = 0; |
| 6618 | cnt = 0; |
| 6619 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6620 | cnt++; |
| 6621 | |
| 6622 | test->i = cnt; |
| 6623 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6624 | return 1; |
| 6625 | } |
| 6626 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6627 | static int |
| 6628 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6629 | struct acl_expr *expr, struct acl_test *test) |
| 6630 | { |
| 6631 | struct http_txn *txn = l7; |
| 6632 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6633 | if (!txn) |
| 6634 | return 0; |
| 6635 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6636 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6637 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6638 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6639 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6640 | /* ensure the indexes are not affected */ |
| 6641 | return 0; |
| 6642 | |
| 6643 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6644 | } |
| 6645 | |
| 6646 | static int |
| 6647 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6648 | struct acl_expr *expr, struct acl_test *test) |
| 6649 | { |
| 6650 | struct http_txn *txn = l7; |
| 6651 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6652 | if (!txn) |
| 6653 | return 0; |
| 6654 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6655 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6656 | return 0; |
| 6657 | |
| 6658 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6659 | } |
| 6660 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6661 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6662 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6663 | * 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] | 6664 | */ |
| 6665 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6666 | 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] | 6667 | struct acl_expr *expr, struct acl_test *test) |
| 6668 | { |
| 6669 | struct http_txn *txn = l7; |
| 6670 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6671 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6672 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6673 | if (!txn) |
| 6674 | return 0; |
| 6675 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6676 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6677 | /* search for header from the beginning */ |
| 6678 | ctx->idx = 0; |
| 6679 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6680 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6681 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6682 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6683 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6684 | return 1; |
| 6685 | } |
| 6686 | |
| 6687 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6688 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6689 | return 0; |
| 6690 | } |
| 6691 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6692 | static int |
| 6693 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6694 | struct acl_expr *expr, struct acl_test *test) |
| 6695 | { |
| 6696 | struct http_txn *txn = l7; |
| 6697 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6698 | if (!txn) |
| 6699 | return 0; |
| 6700 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6701 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6702 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6703 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6704 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6705 | /* ensure the indexes are not affected */ |
| 6706 | return 0; |
| 6707 | |
| 6708 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6709 | } |
| 6710 | |
| 6711 | static int |
| 6712 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6713 | struct acl_expr *expr, struct acl_test *test) |
| 6714 | { |
| 6715 | struct http_txn *txn = l7; |
| 6716 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6717 | if (!txn) |
| 6718 | return 0; |
| 6719 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6720 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6721 | return 0; |
| 6722 | |
| 6723 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6724 | } |
| 6725 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6726 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6727 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6728 | */ |
| 6729 | static int |
| 6730 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6731 | struct acl_expr *expr, struct acl_test *test) |
| 6732 | { |
| 6733 | struct http_txn *txn = l7; |
| 6734 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6735 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6736 | if (!txn) |
| 6737 | return 0; |
| 6738 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6739 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6740 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6741 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6742 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6743 | /* ensure the indexes are not affected */ |
| 6744 | return 0; |
| 6745 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6746 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 6747 | ptr = http_get_path(txn); |
| 6748 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6749 | return 0; |
| 6750 | |
| 6751 | /* OK, we got the '/' ! */ |
| 6752 | test->ptr = ptr; |
| 6753 | |
| 6754 | while (ptr < end && *ptr != '?') |
| 6755 | ptr++; |
| 6756 | |
| 6757 | test->len = ptr - test->ptr; |
| 6758 | |
| 6759 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6760 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6761 | return 1; |
| 6762 | } |
| 6763 | |
| 6764 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6765 | |
| 6766 | /************************************************************************/ |
| 6767 | /* All supported keywords must be declared here. */ |
| 6768 | /************************************************************************/ |
| 6769 | |
| 6770 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 6771 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6772 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 6773 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6774 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6775 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6776 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6777 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6778 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6779 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6780 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6781 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6782 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6783 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6784 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 6785 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6786 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6787 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 6788 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6789 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6790 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6791 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6792 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6793 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6794 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6795 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 6796 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6797 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6798 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6799 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 6800 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 6801 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 6802 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 6803 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 6804 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 6805 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 6806 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6807 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6808 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6809 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6810 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6811 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6812 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6813 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6814 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6815 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6816 | { NULL, NULL, NULL, NULL }, |
| 6817 | |
| 6818 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6819 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 6820 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 6821 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 6822 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 6823 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 6824 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 6825 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 6826 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6827 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 6828 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 6829 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 6830 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 6831 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 6832 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 6833 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 6834 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 6835 | |
| 6836 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 6837 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 6838 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 6839 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 6840 | { NULL, NULL, NULL, NULL }, |
| 6841 | #endif |
| 6842 | }}; |
| 6843 | |
| 6844 | |
| 6845 | __attribute__((constructor)) |
| 6846 | static void __http_protocol_init(void) |
| 6847 | { |
| 6848 | acl_register_keywords(&acl_kws); |
| 6849 | } |
| 6850 | |
| 6851 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6852 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6853 | * Local variables: |
| 6854 | * c-indent-level: 8 |
| 6855 | * c-basic-offset: 8 |
| 6856 | * End: |
| 6857 | */ |