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) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 749 | if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD) && |
| 750 | s->be->options & PR_O_FORCE_CLO && |
| 751 | s->rep->flags & BF_READ_STATUS) { |
| 752 | /* We want to force the connection to the server to close, |
| 753 | * and the server has begun to respond. That's the right |
| 754 | * time. |
| 755 | */ |
| 756 | buffer_shutw_now(s->req); |
| 757 | } |
| 758 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 759 | if (process_srv_data(s)) |
| 760 | resync |= PROCESS_SRV; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 761 | |
| 762 | /* Count server-side errors (but not timeouts). */ |
| 763 | if (s->req->flags & BF_WRITE_ERROR) { |
| 764 | s->be->failed_resp++; |
| 765 | if (s->srv) |
| 766 | s->srv->failed_resp++; |
| 767 | } |
| 768 | |
| 769 | /* When a server-side connection is released, we have to |
| 770 | * count it and check for pending connections on this server. |
| 771 | */ |
| 772 | if (s->req->cons->state == SI_ST_CLO) { |
| 773 | if (s->srv) { |
| 774 | s->srv->cur_sess--; |
| 775 | sess_change_server(s, NULL); |
| 776 | if (may_dequeue_tasks(s->srv, s->be)) |
| 777 | process_srv_queue(s->srv); |
| 778 | } |
| 779 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 780 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 782 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 783 | (global.mode & MODE_DEBUG) && |
| 784 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 785 | int len; |
| 786 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 787 | s->uniq_id, s->be->id, (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd); |
| 788 | write(1, trash, len); |
| 789 | } |
| 790 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 791 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 792 | resync |= PROCESS_ALL & ~PROCESS_SRV; |
| 793 | } |
| 794 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 795 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 796 | if (likely(s->cli_state != CL_STCLOSE || |
| 797 | (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] | 798 | |
| 799 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 800 | session_process_counters(s); |
| 801 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 802 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 803 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 804 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 805 | /* Trick: if a request is being waiting for the server to respond, |
| 806 | * and if we know the server can timeout, we don't want the timeout |
| 807 | * to expire on the client side first, but we're still interested |
| 808 | * in passing data from the client to the server (eg: POST). Thus, |
| 809 | * we can cancel the client's request timeout if the server's |
| 810 | * request timeout is set and the server has not yet sent a response. |
| 811 | */ |
| 812 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 813 | if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 814 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 815 | s->req->rex = TICK_ETERNITY; |
| 816 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 817 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 818 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 819 | if (s->req->analysers) |
| 820 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 821 | |
| 822 | /* restore t to its place in the task list */ |
| 823 | task_queue(t); |
| 824 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 825 | #ifdef DEBUG_DEV |
| 826 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 827 | if (!t->expire) |
| 828 | ABORT_NOW(); |
| 829 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 830 | *next = t->expire; |
| 831 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 834 | s->fe->feconn--; |
| 835 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 836 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 837 | actconn--; |
| 838 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 839 | if (unlikely((global.mode & MODE_DEBUG) && |
| 840 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 841 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 842 | 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] | 843 | s->uniq_id, s->be->id, |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 844 | (unsigned short)s->cli_fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 845 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 846 | write(1, trash, len); |
| 847 | } |
| 848 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 849 | 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] | 850 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 851 | |
| 852 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 853 | if (s->logs.logwait && |
| 854 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 855 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 856 | if (s->fe->to_log & LW_REQ) |
| 857 | http_sess_log(s); |
| 858 | else |
| 859 | tcp_sess_log(s); |
| 860 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 861 | |
| 862 | /* the task MUST not be in the run queue anymore */ |
| 863 | task_delete(t); |
| 864 | session_free(s); |
| 865 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 866 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 870 | extern const char sess_term_cond[8]; |
| 871 | extern const char sess_fin_state[8]; |
| 872 | extern const char *monthname[12]; |
| 873 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 874 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 875 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 876 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 877 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 878 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 879 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 880 | /* |
| 881 | * send a log for the session when we have enough info about it. |
| 882 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 883 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 884 | static void http_sess_log(struct session *s) |
| 885 | { |
| 886 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 887 | struct proxy *fe = s->fe; |
| 888 | struct proxy *be = s->be; |
| 889 | struct proxy *prx_log; |
| 890 | struct http_txn *txn = &s->txn; |
| 891 | int tolog; |
| 892 | char *uri, *h; |
| 893 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 894 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 895 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 896 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 897 | int hdr; |
| 898 | |
| 899 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 900 | return; |
| 901 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 902 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 903 | if (s->cli_addr.ss_family == AF_INET) |
| 904 | inet_ntop(AF_INET, |
| 905 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 906 | pn, sizeof(pn)); |
| 907 | else |
| 908 | inet_ntop(AF_INET6, |
| 909 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 910 | pn, sizeof(pn)); |
| 911 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 912 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 913 | |
| 914 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 915 | tolog = fe->to_log; |
| 916 | |
| 917 | h = tmpline; |
| 918 | if (fe->to_log & LW_REQHDR && |
| 919 | txn->req.cap && |
| 920 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 921 | *(h++) = ' '; |
| 922 | *(h++) = '{'; |
| 923 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 924 | if (hdr) |
| 925 | *(h++) = '|'; |
| 926 | if (txn->req.cap[hdr] != NULL) |
| 927 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 928 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 929 | } |
| 930 | *(h++) = '}'; |
| 931 | } |
| 932 | |
| 933 | if (fe->to_log & LW_RSPHDR && |
| 934 | txn->rsp.cap && |
| 935 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 936 | *(h++) = ' '; |
| 937 | *(h++) = '{'; |
| 938 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 939 | if (hdr) |
| 940 | *(h++) = '|'; |
| 941 | if (txn->rsp.cap[hdr] != NULL) |
| 942 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 943 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 944 | } |
| 945 | *(h++) = '}'; |
| 946 | } |
| 947 | |
| 948 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 949 | *(h++) = ' '; |
| 950 | *(h++) = '"'; |
| 951 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 952 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 953 | '#', url_encode_map, uri); |
| 954 | *(h++) = '"'; |
| 955 | } |
| 956 | *h = '\0'; |
| 957 | |
| 958 | svid = (tolog & LW_SVID) ? |
| 959 | (s->data_source != DATA_SRC_STATS) ? |
| 960 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 961 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 962 | t_request = -1; |
| 963 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 964 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 965 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 966 | send_log(prx_log, LOG_INFO, |
| 967 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 968 | " %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] | 969 | " %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] | 970 | pn, |
| 971 | (s->cli_addr.ss_family == AF_INET) ? |
| 972 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 973 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 974 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 975 | 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] | 976 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 977 | t_request, |
| 978 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 979 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 980 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 981 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 982 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 983 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 984 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 985 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 986 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 987 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 988 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 989 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 990 | 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] | 991 | (s->flags & SN_REDISP)?"+":"", |
| 992 | (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] | 993 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 994 | |
| 995 | s->logs.logwait = 0; |
| 996 | } |
| 997 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 998 | |
| 999 | /* |
| 1000 | * Capture headers from message starting at <som> according to header list |
| 1001 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1002 | */ |
| 1003 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1004 | char **cap, struct cap_hdr *cap_hdr) |
| 1005 | { |
| 1006 | char *eol, *sol, *col, *sov; |
| 1007 | int cur_idx; |
| 1008 | struct cap_hdr *h; |
| 1009 | int len; |
| 1010 | |
| 1011 | sol = som + hdr_idx_first_pos(idx); |
| 1012 | cur_idx = hdr_idx_first_idx(idx); |
| 1013 | |
| 1014 | while (cur_idx) { |
| 1015 | eol = sol + idx->v[cur_idx].len; |
| 1016 | |
| 1017 | col = sol; |
| 1018 | while (col < eol && *col != ':') |
| 1019 | col++; |
| 1020 | |
| 1021 | sov = col + 1; |
| 1022 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1023 | sov++; |
| 1024 | |
| 1025 | for (h = cap_hdr; h; h = h->next) { |
| 1026 | if ((h->namelen == col - sol) && |
| 1027 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1028 | if (cap[h->index] == NULL) |
| 1029 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1030 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1031 | |
| 1032 | if (cap[h->index] == NULL) { |
| 1033 | Alert("HTTP capture : out of memory.\n"); |
| 1034 | continue; |
| 1035 | } |
| 1036 | |
| 1037 | len = eol - sov; |
| 1038 | if (len > h->len) |
| 1039 | len = h->len; |
| 1040 | |
| 1041 | memcpy(cap[h->index], sov, len); |
| 1042 | cap[h->index][len]=0; |
| 1043 | } |
| 1044 | } |
| 1045 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1046 | cur_idx = idx->v[cur_idx].next; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1051 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1052 | */ |
| 1053 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1054 | |
| 1055 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1056 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1057 | */ |
| 1058 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1059 | ptr++; \ |
| 1060 | if (likely(ptr < end)) \ |
| 1061 | goto good; \ |
| 1062 | else { \ |
| 1063 | state = (st); \ |
| 1064 | goto http_msg_ood; \ |
| 1065 | } \ |
| 1066 | } while (0) |
| 1067 | |
| 1068 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1069 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1070 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1071 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1072 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1073 | * will give undefined results. |
| 1074 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1075 | * and that msg->sol points to the beginning of the response. |
| 1076 | * If a complete line is found (which implies that at least one CR or LF is |
| 1077 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1078 | * returned indicating an incomplete line (which does not mean that parts have |
| 1079 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1080 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1081 | * upon next call. |
| 1082 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1083 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1084 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1085 | * 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] | 1086 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1087 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1088 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1089 | unsigned int state, const char *ptr, const char *end, |
| 1090 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1091 | { |
| 1092 | __label__ |
| 1093 | http_msg_rpver, |
| 1094 | http_msg_rpver_sp, |
| 1095 | http_msg_rpcode, |
| 1096 | http_msg_rpcode_sp, |
| 1097 | http_msg_rpreason, |
| 1098 | http_msg_rpline_eol, |
| 1099 | http_msg_ood, /* out of data */ |
| 1100 | http_msg_invalid; |
| 1101 | |
| 1102 | switch (state) { |
| 1103 | http_msg_rpver: |
| 1104 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1105 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1106 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1107 | |
| 1108 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1109 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1110 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1111 | } |
| 1112 | goto http_msg_invalid; |
| 1113 | |
| 1114 | http_msg_rpver_sp: |
| 1115 | case HTTP_MSG_RPVER_SP: |
| 1116 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1117 | msg->sl.st.c = ptr - msg_buf; |
| 1118 | goto http_msg_rpcode; |
| 1119 | } |
| 1120 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1121 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1122 | /* so it's a CR/LF, this is invalid */ |
| 1123 | goto http_msg_invalid; |
| 1124 | |
| 1125 | http_msg_rpcode: |
| 1126 | case HTTP_MSG_RPCODE: |
| 1127 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1128 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1129 | |
| 1130 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1131 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1132 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1133 | } |
| 1134 | |
| 1135 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1136 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1137 | http_msg_rsp_reason: |
| 1138 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1139 | msg->sl.st.r = ptr - msg_buf; |
| 1140 | msg->sl.st.r_l = 0; |
| 1141 | goto http_msg_rpline_eol; |
| 1142 | |
| 1143 | http_msg_rpcode_sp: |
| 1144 | case HTTP_MSG_RPCODE_SP: |
| 1145 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1146 | msg->sl.st.r = ptr - msg_buf; |
| 1147 | goto http_msg_rpreason; |
| 1148 | } |
| 1149 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1150 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1151 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1152 | goto http_msg_rsp_reason; |
| 1153 | |
| 1154 | http_msg_rpreason: |
| 1155 | case HTTP_MSG_RPREASON: |
| 1156 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1157 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1158 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1159 | http_msg_rpline_eol: |
| 1160 | /* We have seen the end of line. Note that we do not |
| 1161 | * necessarily have the \n yet, but at least we know that we |
| 1162 | * have EITHER \r OR \n, otherwise the response would not be |
| 1163 | * complete. We can then record the response length and return |
| 1164 | * to the caller which will be able to register it. |
| 1165 | */ |
| 1166 | msg->sl.st.l = ptr - msg->sol; |
| 1167 | return ptr; |
| 1168 | |
| 1169 | #ifdef DEBUG_FULL |
| 1170 | default: |
| 1171 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1172 | exit(1); |
| 1173 | #endif |
| 1174 | } |
| 1175 | |
| 1176 | http_msg_ood: |
| 1177 | /* out of data */ |
| 1178 | if (ret_state) |
| 1179 | *ret_state = state; |
| 1180 | if (ret_ptr) |
| 1181 | *ret_ptr = (char *)ptr; |
| 1182 | return NULL; |
| 1183 | |
| 1184 | http_msg_invalid: |
| 1185 | /* invalid message */ |
| 1186 | if (ret_state) |
| 1187 | *ret_state = HTTP_MSG_ERROR; |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1193 | * This function parses a request line between <ptr> and <end>, starting with |
| 1194 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1195 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1196 | * will give undefined results. |
| 1197 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1198 | * and that msg->sol points to the beginning of the request. |
| 1199 | * If a complete line is found (which implies that at least one CR or LF is |
| 1200 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1201 | * returned indicating an incomplete line (which does not mean that parts have |
| 1202 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1203 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1204 | * upon next call. |
| 1205 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1206 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1207 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1208 | * 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] | 1209 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1210 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1211 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1212 | unsigned int state, const char *ptr, const char *end, |
| 1213 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1214 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1215 | __label__ |
| 1216 | http_msg_rqmeth, |
| 1217 | http_msg_rqmeth_sp, |
| 1218 | http_msg_rquri, |
| 1219 | http_msg_rquri_sp, |
| 1220 | http_msg_rqver, |
| 1221 | http_msg_rqline_eol, |
| 1222 | http_msg_ood, /* out of data */ |
| 1223 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1225 | switch (state) { |
| 1226 | http_msg_rqmeth: |
| 1227 | case HTTP_MSG_RQMETH: |
| 1228 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1230 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1231 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1232 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1233 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1234 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1237 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1238 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | http_msg_req09_uri: |
| 1240 | msg->sl.rq.u = ptr - msg_buf; |
| 1241 | http_msg_req09_uri_e: |
| 1242 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1243 | http_msg_req09_ver: |
| 1244 | msg->sl.rq.v = ptr - msg_buf; |
| 1245 | msg->sl.rq.v_l = 0; |
| 1246 | goto http_msg_rqline_eol; |
| 1247 | } |
| 1248 | goto http_msg_invalid; |
| 1249 | |
| 1250 | http_msg_rqmeth_sp: |
| 1251 | case HTTP_MSG_RQMETH_SP: |
| 1252 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1253 | msg->sl.rq.u = ptr - msg_buf; |
| 1254 | goto http_msg_rquri; |
| 1255 | } |
| 1256 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1257 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1258 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1259 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1260 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1261 | http_msg_rquri: |
| 1262 | case HTTP_MSG_RQURI: |
| 1263 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1264 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1266 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1267 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1268 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1269 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1270 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1271 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1272 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1273 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1274 | http_msg_rquri_sp: |
| 1275 | case HTTP_MSG_RQURI_SP: |
| 1276 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1277 | msg->sl.rq.v = ptr - msg_buf; |
| 1278 | goto http_msg_rqver; |
| 1279 | } |
| 1280 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1281 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1282 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1283 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1285 | http_msg_rqver: |
| 1286 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1287 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1288 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1289 | |
| 1290 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1291 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1292 | http_msg_rqline_eol: |
| 1293 | /* We have seen the end of line. Note that we do not |
| 1294 | * necessarily have the \n yet, but at least we know that we |
| 1295 | * have EITHER \r OR \n, otherwise the request would not be |
| 1296 | * complete. We can then record the request length and return |
| 1297 | * to the caller which will be able to register it. |
| 1298 | */ |
| 1299 | msg->sl.rq.l = ptr - msg->sol; |
| 1300 | return ptr; |
| 1301 | } |
| 1302 | |
| 1303 | /* neither an HTTP_VER token nor a CRLF */ |
| 1304 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1305 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | #ifdef DEBUG_FULL |
| 1307 | default: |
| 1308 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1309 | exit(1); |
| 1310 | #endif |
| 1311 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1312 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1313 | http_msg_ood: |
| 1314 | /* out of data */ |
| 1315 | if (ret_state) |
| 1316 | *ret_state = state; |
| 1317 | if (ret_ptr) |
| 1318 | *ret_ptr = (char *)ptr; |
| 1319 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1320 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | http_msg_invalid: |
| 1322 | /* invalid message */ |
| 1323 | if (ret_state) |
| 1324 | *ret_state = HTTP_MSG_ERROR; |
| 1325 | return NULL; |
| 1326 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1327 | |
| 1328 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | /* |
| 1330 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1331 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | * when data are missing and recalled at the exact same location with no |
| 1333 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1334 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1335 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1338 | { |
| 1339 | __label__ |
| 1340 | http_msg_rqbefore, |
| 1341 | http_msg_rqbefore_cr, |
| 1342 | http_msg_rqmeth, |
| 1343 | http_msg_rqline_end, |
| 1344 | http_msg_hdr_first, |
| 1345 | http_msg_hdr_name, |
| 1346 | http_msg_hdr_l1_sp, |
| 1347 | http_msg_hdr_l1_lf, |
| 1348 | http_msg_hdr_l1_lws, |
| 1349 | http_msg_hdr_val, |
| 1350 | http_msg_hdr_l2_lf, |
| 1351 | http_msg_hdr_l2_lws, |
| 1352 | http_msg_complete_header, |
| 1353 | http_msg_last_lf, |
| 1354 | http_msg_ood, /* out of data */ |
| 1355 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1357 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1359 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1360 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1361 | ptr = buf->lr; |
| 1362 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | if (unlikely(ptr >= end)) |
| 1365 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1367 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1368 | /* |
| 1369 | * First, states that are specific to the response only. |
| 1370 | * We check them first so that request and headers are |
| 1371 | * closer to each other (accessed more often). |
| 1372 | */ |
| 1373 | http_msg_rpbefore: |
| 1374 | case HTTP_MSG_RPBEFORE: |
| 1375 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1376 | if (likely(ptr == buf->data)) { |
| 1377 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1378 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1379 | } else { |
| 1380 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1381 | /* only skip empty leading lines, don't remove them */ |
| 1382 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1383 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1384 | #else |
| 1385 | /* Remove empty leading lines, as recommended by |
| 1386 | * RFC2616. This takes a lot of time because we |
| 1387 | * must move all the buffer backwards, but this |
| 1388 | * is rarely needed. The method above will be |
| 1389 | * cleaner when we'll be able to start sending |
| 1390 | * the request from any place in the buffer. |
| 1391 | */ |
| 1392 | buf->lr = ptr; |
| 1393 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1394 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1395 | msg->sol = buf->data; |
| 1396 | ptr = buf->data; |
| 1397 | end = buf->r; |
| 1398 | #endif |
| 1399 | } |
| 1400 | hdr_idx_init(idx); |
| 1401 | state = HTTP_MSG_RPVER; |
| 1402 | goto http_msg_rpver; |
| 1403 | } |
| 1404 | |
| 1405 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1406 | goto http_msg_invalid; |
| 1407 | |
| 1408 | if (unlikely(*ptr == '\n')) |
| 1409 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1410 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1411 | /* stop here */ |
| 1412 | |
| 1413 | http_msg_rpbefore_cr: |
| 1414 | case HTTP_MSG_RPBEFORE_CR: |
| 1415 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1416 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1417 | /* stop here */ |
| 1418 | |
| 1419 | http_msg_rpver: |
| 1420 | case HTTP_MSG_RPVER: |
| 1421 | case HTTP_MSG_RPVER_SP: |
| 1422 | case HTTP_MSG_RPCODE: |
| 1423 | case HTTP_MSG_RPCODE_SP: |
| 1424 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1425 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1426 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1427 | if (unlikely(!ptr)) |
| 1428 | return; |
| 1429 | |
| 1430 | /* we have a full response and we know that we have either a CR |
| 1431 | * or an LF at <ptr>. |
| 1432 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1433 | //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] | 1434 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1435 | |
| 1436 | msg->sol = ptr; |
| 1437 | if (likely(*ptr == '\r')) |
| 1438 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1439 | goto http_msg_rpline_end; |
| 1440 | |
| 1441 | http_msg_rpline_end: |
| 1442 | case HTTP_MSG_RPLINE_END: |
| 1443 | /* msg->sol must point to the first of CR or LF. */ |
| 1444 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1445 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1446 | /* stop here */ |
| 1447 | |
| 1448 | /* |
| 1449 | * Second, states that are specific to the request only |
| 1450 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | http_msg_rqbefore: |
| 1452 | case HTTP_MSG_RQBEFORE: |
| 1453 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1454 | if (likely(ptr == buf->data)) { |
| 1455 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1456 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | } else { |
| 1458 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1459 | /* only skip empty leading lines, don't remove them */ |
| 1460 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1461 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1462 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | /* Remove empty leading lines, as recommended by |
| 1464 | * RFC2616. This takes a lot of time because we |
| 1465 | * must move all the buffer backwards, but this |
| 1466 | * is rarely needed. The method above will be |
| 1467 | * cleaner when we'll be able to start sending |
| 1468 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1469 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | buf->lr = ptr; |
| 1471 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1472 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | msg->sol = buf->data; |
| 1474 | ptr = buf->data; |
| 1475 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1476 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1478 | /* we will need this when keep-alive will be supported |
| 1479 | hdr_idx_init(idx); |
| 1480 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | state = HTTP_MSG_RQMETH; |
| 1482 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1486 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1487 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | if (unlikely(*ptr == '\n')) |
| 1489 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1490 | 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] | 1491 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1492 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | http_msg_rqbefore_cr: |
| 1494 | case HTTP_MSG_RQBEFORE_CR: |
| 1495 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1498 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1499 | http_msg_rqmeth: |
| 1500 | case HTTP_MSG_RQMETH: |
| 1501 | case HTTP_MSG_RQMETH_SP: |
| 1502 | case HTTP_MSG_RQURI: |
| 1503 | case HTTP_MSG_RQURI_SP: |
| 1504 | case HTTP_MSG_RQVER: |
| 1505 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1506 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | if (unlikely(!ptr)) |
| 1508 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | /* we have a full request and we know that we have either a CR |
| 1511 | * or an LF at <ptr>. |
| 1512 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1513 | //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] | 1514 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1515 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | msg->sol = ptr; |
| 1517 | if (likely(*ptr == '\r')) |
| 1518 | 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] | 1519 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | http_msg_rqline_end: |
| 1522 | case HTTP_MSG_RQLINE_END: |
| 1523 | /* check for HTTP/0.9 request : no version information available. |
| 1524 | * msg->sol must point to the first of CR or LF. |
| 1525 | */ |
| 1526 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1527 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1530 | 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] | 1531 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1532 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1533 | /* |
| 1534 | * Common states below |
| 1535 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | http_msg_hdr_first: |
| 1537 | case HTTP_MSG_HDR_FIRST: |
| 1538 | msg->sol = ptr; |
| 1539 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1540 | goto http_msg_hdr_name; |
| 1541 | } |
| 1542 | |
| 1543 | if (likely(*ptr == '\r')) |
| 1544 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1545 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1546 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1547 | http_msg_hdr_name: |
| 1548 | case HTTP_MSG_HDR_NAME: |
| 1549 | /* assumes msg->sol points to the first char */ |
| 1550 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1551 | 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] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | if (likely(*ptr == ':')) { |
| 1554 | msg->col = ptr - buf->data; |
| 1555 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1556 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1557 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | http_msg_hdr_l1_sp: |
| 1561 | case HTTP_MSG_HDR_L1_SP: |
| 1562 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1563 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1564 | 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] | 1565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | /* header value can be basically anything except CR/LF */ |
| 1567 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1570 | goto http_msg_hdr_val; |
| 1571 | } |
| 1572 | |
| 1573 | if (likely(*ptr == '\r')) |
| 1574 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1575 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1576 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | http_msg_hdr_l1_lf: |
| 1578 | case HTTP_MSG_HDR_L1_LF: |
| 1579 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1580 | 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] | 1581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | http_msg_hdr_l1_lws: |
| 1583 | case HTTP_MSG_HDR_L1_LWS: |
| 1584 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1585 | /* replace HT,CR,LF with spaces */ |
| 1586 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1587 | buf->data[msg->sov] = ' '; |
| 1588 | goto http_msg_hdr_l1_sp; |
| 1589 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1590 | /* we had a header consisting only in spaces ! */ |
| 1591 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | goto http_msg_complete_header; |
| 1593 | |
| 1594 | http_msg_hdr_val: |
| 1595 | case HTTP_MSG_HDR_VAL: |
| 1596 | /* assumes msg->sol points to the first char, msg->col to the |
| 1597 | * colon, and msg->sov points to the first character of the |
| 1598 | * value. |
| 1599 | */ |
| 1600 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1601 | 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] | 1602 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | msg->eol = ptr; |
| 1604 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1605 | * real header end in case it ends with lots of LWS, but is this |
| 1606 | * really needed ? |
| 1607 | */ |
| 1608 | if (likely(*ptr == '\r')) |
| 1609 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1610 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1611 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | http_msg_hdr_l2_lf: |
| 1613 | case HTTP_MSG_HDR_L2_LF: |
| 1614 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1615 | 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] | 1616 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1617 | http_msg_hdr_l2_lws: |
| 1618 | case HTTP_MSG_HDR_L2_LWS: |
| 1619 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1620 | /* LWS: replace HT,CR,LF with spaces */ |
| 1621 | for (; msg->eol < ptr; msg->eol++) |
| 1622 | *msg->eol = ' '; |
| 1623 | goto http_msg_hdr_val; |
| 1624 | } |
| 1625 | http_msg_complete_header: |
| 1626 | /* |
| 1627 | * It was a new header, so the last one is finished. |
| 1628 | * Assumes msg->sol points to the first char, msg->col to the |
| 1629 | * colon, msg->sov points to the first character of the value |
| 1630 | * and msg->eol to the first CR or LF so we know how the line |
| 1631 | * ends. We insert last header into the index. |
| 1632 | */ |
| 1633 | /* |
| 1634 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1635 | write(2, msg->sol, msg->eol-msg->sol); |
| 1636 | fprintf(stderr,"\n"); |
| 1637 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1638 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1640 | idx, idx->tail) < 0)) |
| 1641 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | msg->sol = ptr; |
| 1644 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1645 | goto http_msg_hdr_name; |
| 1646 | } |
| 1647 | |
| 1648 | if (likely(*ptr == '\r')) |
| 1649 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1650 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | http_msg_last_lf: |
| 1653 | case HTTP_MSG_LAST_LF: |
| 1654 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1655 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1656 | ptr++; |
| 1657 | buf->lr = ptr; |
| 1658 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1659 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | return; |
| 1661 | #ifdef DEBUG_FULL |
| 1662 | default: |
| 1663 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1664 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1665 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | } |
| 1667 | http_msg_ood: |
| 1668 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1669 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | buf->lr = ptr; |
| 1671 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1672 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | http_msg_invalid: |
| 1674 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1675 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | return; |
| 1677 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1679 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1680 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1681 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1682 | * 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] | 1683 | * functions. Its behaviour is rather simple : |
| 1684 | * - all enabled analysers are called in turn from the lower to the higher |
| 1685 | * bit. |
| 1686 | * - if an analyser does not have enough data, it must return without calling |
| 1687 | * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure |
| 1688 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1689 | * the protocol. Generally it is not reset in case of errors. |
| 1690 | * - if an analyser has enough data, it just has to pass on to the next |
| 1691 | * analyser without touching BF_MAY_FORWARD (it is enabled prior to |
| 1692 | * analysis). |
| 1693 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1694 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1695 | * |
| 1696 | * In the future, analysers should be able to indicate that they want to be |
| 1697 | * called after XXX bytes have been received (or transfered), and the min of |
| 1698 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1699 | * |
| 1700 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1702 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | struct buffer *req = t->req; |
| 1705 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1706 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1707 | DPRINTF(stderr,"[%u] %s: c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n", |
| 1708 | now_ms, __FUNCTION__, |
| 1709 | cli_stnames[t->cli_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 1710 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 1711 | 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] | 1712 | req->rex, rep->wex, req->flags, rep->flags, req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1714 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1715 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1716 | struct tcp_rule *rule; |
| 1717 | int partial; |
| 1718 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1719 | /* We will abort if we encounter a read error. In theory, we |
| 1720 | * should not abort if we get a close, it might be valid, |
| 1721 | * although very unlikely. FIXME: we'll abort for now, this |
| 1722 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1723 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1724 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1725 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1726 | t->fe->failed_req++; |
| 1727 | if (!(t->flags & SN_ERR_MASK)) |
| 1728 | t->flags |= SN_ERR_CLICL; |
| 1729 | if (!(t->flags & SN_FINST_MASK)) |
| 1730 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1731 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1735 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1736 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1737 | t->fe->failed_req++; |
| 1738 | if (!(t->flags & SN_ERR_MASK)) |
| 1739 | t->flags |= SN_ERR_CLITO; |
| 1740 | if (!(t->flags & SN_FINST_MASK)) |
| 1741 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1742 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /* We don't know whether we have enough data, so must proceed |
| 1746 | * this way : |
| 1747 | * - iterate through all rules in their declaration order |
| 1748 | * - if one rule returns MISS, it means the inspect delay is |
| 1749 | * not over yet, then return immediately, otherwise consider |
| 1750 | * it as a non-match. |
| 1751 | * - if one rule returns OK, then return OK |
| 1752 | * - if one rule returns KO, then return KO |
| 1753 | */ |
| 1754 | |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1755 | 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] | 1756 | partial = 0; |
| 1757 | else |
| 1758 | partial = ACL_PARTIAL; |
| 1759 | |
| 1760 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1761 | int ret = ACL_PAT_PASS; |
| 1762 | |
| 1763 | if (rule->cond) { |
| 1764 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1765 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1766 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1767 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1768 | if (!tick_isset(req->analyse_exp)) |
| 1769 | 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] | 1770 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1771 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1772 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1773 | ret = acl_pass(ret); |
| 1774 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1775 | ret = !ret; |
| 1776 | } |
| 1777 | |
| 1778 | if (ret) { |
| 1779 | /* we have a matching rule. */ |
| 1780 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1781 | buffer_abort(req); |
| 1782 | buffer_abort(rep); |
| 1783 | //FIXME: this delete this |
| 1784 | //fd_delete(t->cli_fd); |
| 1785 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1786 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1787 | t->fe->failed_req++; |
| 1788 | if (!(t->flags & SN_ERR_MASK)) |
| 1789 | t->flags |= SN_ERR_PRXCOND; |
| 1790 | if (!(t->flags & SN_FINST_MASK)) |
| 1791 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1792 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1793 | } |
| 1794 | /* otherwise accept */ |
| 1795 | break; |
| 1796 | } |
| 1797 | } |
| 1798 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1799 | /* if we get there, it means we have no rule which matches, or |
| 1800 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1801 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1802 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1803 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1804 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1805 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1806 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1807 | /* |
| 1808 | * Now parse the partial (or complete) lines. |
| 1809 | * We will check the request syntax, and also join multi-line |
| 1810 | * headers. An index of all the lines will be elaborated while |
| 1811 | * parsing. |
| 1812 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1813 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1816 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | * req->data + req->eoh = end of processed headers / start of current one |
| 1818 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1819 | * req->lr = first non-visited byte |
| 1820 | * req->r = end of data |
| 1821 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1824 | struct http_txn *txn = &t->txn; |
| 1825 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1829 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1830 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | /* 1: we might have to print this header in debug mode */ |
| 1832 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1833 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1834 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1837 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | eol = sol + msg->sl.rq.l; |
| 1839 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1840 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1841 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1842 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1845 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1847 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1848 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | } |
| 1850 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1851 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1855 | * If not so, we check the FD and buffer states before leaving. |
| 1856 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1857 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1858 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1859 | * |
| 1860 | */ |
| 1861 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1862 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | /* |
| 1864 | * First, let's catch bad requests. |
| 1865 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1866 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* 1: Since we are in header mode, if there's no space |
| 1870 | * left for headers, we won't be able to free more |
| 1871 | * later, so the session will never terminate. We |
| 1872 | * must terminate it now. |
| 1873 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1874 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | /* FIXME: check if URI is set and return Status |
| 1876 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1877 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1878 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1881 | /* 2: have we encountered a close ? */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1882 | else if (req->flags & (BF_READ_NULL | BF_SHUTR)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1883 | txn->status = 400; |
| 1884 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
| 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 | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1887 | t->fe->failed_req++; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1888 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1889 | if (!(t->flags & SN_ERR_MASK)) |
| 1890 | t->flags |= SN_ERR_CLICL; |
| 1891 | if (!(t->flags & SN_FINST_MASK)) |
| 1892 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1893 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1897 | 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] | 1898 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1899 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1900 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1901 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1902 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1903 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1904 | if (!(t->flags & SN_ERR_MASK)) |
| 1905 | t->flags |= SN_ERR_CLITO; |
| 1906 | if (!(t->flags & SN_FINST_MASK)) |
| 1907 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1908 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1909 | } |
| 1910 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1911 | /* 4: have we encountered a read error ? */ |
| 1912 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1913 | /* we cannot return any message on error */ |
| 1914 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1915 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1916 | t->fe->failed_req++; |
| 1917 | if (!(t->flags & SN_ERR_MASK)) |
| 1918 | t->flags |= SN_ERR_CLICL; |
| 1919 | if (!(t->flags & SN_FINST_MASK)) |
| 1920 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1921 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1923 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1924 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1925 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1926 | if (!tick_isset(req->analyse_exp)) |
| 1927 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1928 | |
| 1929 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1930 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | /**************************************************************** |
| 1935 | * More interesting part now : we know that we have a complete * |
| 1936 | * request which at least looks like HTTP. We have an indicator * |
| 1937 | * of each header's length, so we can parse them quickly. * |
| 1938 | ****************************************************************/ |
| 1939 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1940 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1941 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1942 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1943 | /* ensure we keep this pointer to the beginning of the message */ |
| 1944 | msg->sol = req->data + msg->som; |
| 1945 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1946 | /* |
| 1947 | * 1: identify the method |
| 1948 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1949 | 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] | 1950 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1951 | /* we can make use of server redirect on GET and HEAD */ |
| 1952 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1953 | t->flags |= SN_REDIRECTABLE; |
| 1954 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1955 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1957 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1958 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1959 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1960 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1961 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1962 | !memcmp(&req->data[msg->sl.rq.u], |
| 1963 | t->fe->monitor_uri, |
| 1964 | t->fe->monitor_uri_len))) { |
| 1965 | /* |
| 1966 | * We have found the monitor URI |
| 1967 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1968 | struct acl_cond *cond; |
| 1969 | cur_proxy = t->fe; |
| 1970 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1971 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* Check if we want to fail this monitor request or not */ |
| 1974 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1975 | 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] | 1976 | |
| 1977 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1978 | if (cond->pol == ACL_COND_UNLESS) |
| 1979 | ret = !ret; |
| 1980 | |
| 1981 | if (ret) { |
| 1982 | /* we fail this request, let's return 503 service unavail */ |
| 1983 | txn->status = 503; |
| 1984 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1985 | goto return_prx_cond; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1990 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1991 | client_retnclose(t, &http_200_chunk); |
| 1992 | goto return_prx_cond; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1997 | * Note: we cannot log anymore if the request has been |
| 1998 | * classified as invalid. |
| 1999 | */ |
| 2000 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2001 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2002 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2003 | int urilen = msg->sl.rq.l; |
| 2004 | |
| 2005 | if (urilen >= REQURI_LEN) |
| 2006 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2007 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2008 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2009 | |
| 2010 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2011 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2012 | } else { |
| 2013 | Alert("HTTP logging : out of memory.\n"); |
| 2014 | } |
| 2015 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2016 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2017 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2018 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2019 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2020 | int delta; |
| 2021 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2022 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2023 | cur_end = msg->sol + msg->sl.rq.l; |
| 2024 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2025 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2026 | if (msg->sl.rq.u_l == 0) { |
| 2027 | /* if no URI was set, add "/" */ |
| 2028 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2029 | cur_end += delta; |
| 2030 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2031 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2032 | /* add HTTP version */ |
| 2033 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2034 | msg->eoh += delta; |
| 2035 | cur_end += delta; |
| 2036 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2037 | HTTP_MSG_RQMETH, |
| 2038 | msg->sol, cur_end + 1, |
| 2039 | NULL, NULL); |
| 2040 | if (unlikely(!cur_end)) |
| 2041 | goto return_bad_req; |
| 2042 | |
| 2043 | /* we have a full HTTP/1.0 request now and we know that |
| 2044 | * we have either a CR or an LF at <ptr>. |
| 2045 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2046 | 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] | 2047 | } |
| 2048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2051 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2052 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2053 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2056 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2057 | * As opposed to version 1.2, now they will be evaluated in the |
| 2058 | * filters order and not in the header order. This means that |
| 2059 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2060 | * |
| 2061 | * We can now check whether we want to switch to another |
| 2062 | * backend, in which case we will re-check the backend's |
| 2063 | * filters and various options. In order to support 3-level |
| 2064 | * switching, here's how we should proceed : |
| 2065 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2066 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2067 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2068 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2069 | * There cannot be any switch from there, so ->be cannot be |
| 2070 | * changed anymore. |
| 2071 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2072 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2073 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2074 | * The response path will be able to apply either ->be, or |
| 2075 | * ->be then ->fe filters in order to match the reverse of |
| 2076 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2077 | */ |
| 2078 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2079 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2080 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2081 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2082 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2083 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2084 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2085 | /* first check whether we have some ACLs set to redirect this request */ |
| 2086 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2087 | 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] | 2088 | |
| 2089 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2090 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2091 | ret = !ret; |
| 2092 | |
| 2093 | if (ret) { |
| 2094 | struct chunk rdr = { trash, 0 }; |
| 2095 | const char *msg_fmt; |
| 2096 | |
| 2097 | /* build redirect message */ |
| 2098 | switch(rule->code) { |
| 2099 | case 303: |
| 2100 | rdr.len = strlen(HTTP_303); |
| 2101 | msg_fmt = HTTP_303; |
| 2102 | break; |
| 2103 | case 301: |
| 2104 | rdr.len = strlen(HTTP_301); |
| 2105 | msg_fmt = HTTP_301; |
| 2106 | break; |
| 2107 | case 302: |
| 2108 | default: |
| 2109 | rdr.len = strlen(HTTP_302); |
| 2110 | msg_fmt = HTTP_302; |
| 2111 | break; |
| 2112 | } |
| 2113 | |
| 2114 | if (unlikely(rdr.len > sizeof(trash))) |
| 2115 | goto return_bad_req; |
| 2116 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2117 | |
| 2118 | switch(rule->type) { |
| 2119 | case REDIRECT_TYPE_PREFIX: { |
| 2120 | const char *path; |
| 2121 | int pathlen; |
| 2122 | |
| 2123 | path = http_get_path(txn); |
| 2124 | /* build message using path */ |
| 2125 | if (path) { |
| 2126 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2127 | } else { |
| 2128 | path = "/"; |
| 2129 | pathlen = 1; |
| 2130 | } |
| 2131 | |
| 2132 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2133 | goto return_bad_req; |
| 2134 | |
| 2135 | /* add prefix */ |
| 2136 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2137 | rdr.len += rule->rdr_len; |
| 2138 | |
| 2139 | /* add path */ |
| 2140 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2141 | rdr.len += pathlen; |
| 2142 | break; |
| 2143 | } |
| 2144 | case REDIRECT_TYPE_LOCATION: |
| 2145 | default: |
| 2146 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2147 | goto return_bad_req; |
| 2148 | |
| 2149 | /* add location */ |
| 2150 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2151 | rdr.len += rule->rdr_len; |
| 2152 | break; |
| 2153 | } |
| 2154 | |
| 2155 | /* add end of headers */ |
| 2156 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2157 | rdr.len += 4; |
| 2158 | |
| 2159 | txn->status = rule->code; |
| 2160 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2161 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2162 | client_retnclose(t, &rdr); |
| 2163 | goto return_prx_cond; |
| 2164 | } |
| 2165 | } |
| 2166 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2167 | /* first check whether we have some ACLs set to block this request */ |
| 2168 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2169 | 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] | 2170 | |
| 2171 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2172 | if (cond->pol == ACL_COND_UNLESS) |
| 2173 | ret = !ret; |
| 2174 | |
| 2175 | if (ret) { |
| 2176 | txn->status = 403; |
| 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 | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2179 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2180 | goto return_prx_cond; |
| 2181 | } |
| 2182 | } |
| 2183 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2184 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2185 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2186 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2187 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2190 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2191 | /* to ensure correct connection accounting on |
| 2192 | * the backend, we count the connection for the |
| 2193 | * one managing the queue. |
| 2194 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2195 | t->be->beconn++; |
| 2196 | if (t->be->beconn > t->be->beconn_max) |
| 2197 | t->be->beconn_max = t->be->beconn; |
| 2198 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2199 | t->flags |= SN_BE_ASSIGNED; |
| 2200 | } |
| 2201 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2202 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2203 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2204 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2205 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2206 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2207 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2208 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2209 | goto return_prx_cond; |
| 2210 | } |
| 2211 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2212 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2213 | 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] | 2214 | !(t->flags & SN_CONN_CLOSED)) { |
| 2215 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2216 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2217 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2218 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2219 | 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] | 2220 | old_idx = 0; |
| 2221 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2222 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2223 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2224 | cur_ptr = cur_next; |
| 2225 | cur_end = cur_ptr + cur_hdr->len; |
| 2226 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2227 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2228 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2229 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2230 | /* 3 possibilities : |
| 2231 | * - we have already set Connection: close, |
| 2232 | * so we remove this line. |
| 2233 | * - we have not yet set Connection: close, |
| 2234 | * but this line indicates close. We leave |
| 2235 | * it untouched and set the flag. |
| 2236 | * - we have not yet set Connection: close, |
| 2237 | * and this line indicates non-close. We |
| 2238 | * replace it. |
| 2239 | */ |
| 2240 | if (t->flags & SN_CONN_CLOSED) { |
| 2241 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2242 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2243 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2244 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2245 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2246 | cur_hdr->len = 0; |
| 2247 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2248 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2249 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2250 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2251 | cur_next += delta; |
| 2252 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2253 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2254 | } |
| 2255 | t->flags |= SN_CONN_CLOSED; |
| 2256 | } |
| 2257 | } |
| 2258 | old_idx = cur_idx; |
| 2259 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2260 | } |
| 2261 | /* add request headers from the rule sets in the same order */ |
| 2262 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2263 | if (unlikely(http_header_add_tail(req, |
| 2264 | &txn->req, |
| 2265 | &txn->hdr_idx, |
| 2266 | rule_set->req_add[cur_idx])) < 0) |
| 2267 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2268 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2269 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2270 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2271 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2272 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2273 | /* we have to check the URI and auth for this request. |
| 2274 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2275 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2276 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2277 | if (stats_check_uri_auth(t, rule_set)) |
| 2278 | return 1; |
| 2279 | } |
| 2280 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2281 | /* now check whether we have some switching rules for this request */ |
| 2282 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2283 | struct switching_rule *rule; |
| 2284 | |
| 2285 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2286 | int ret; |
| 2287 | |
| 2288 | 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] | 2289 | |
| 2290 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2291 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2292 | ret = !ret; |
| 2293 | |
| 2294 | if (ret) { |
| 2295 | t->be = rule->be.backend; |
| 2296 | t->be->beconn++; |
| 2297 | if (t->be->beconn > t->be->beconn_max) |
| 2298 | t->be->beconn_max = t->be->beconn; |
| 2299 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2300 | |
| 2301 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2302 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2303 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2304 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2305 | t->flags |= SN_BE_ASSIGNED; |
| 2306 | break; |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2311 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2312 | /* No backend was set, but there was a default |
| 2313 | * backend set in the frontend, so we use it and |
| 2314 | * loop again. |
| 2315 | */ |
| 2316 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2317 | t->be->beconn++; |
| 2318 | if (t->be->beconn > t->be->beconn_max) |
| 2319 | t->be->beconn_max = t->be->beconn; |
| 2320 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2321 | |
| 2322 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2323 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2324 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2325 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2326 | t->flags |= SN_BE_ASSIGNED; |
| 2327 | } |
| 2328 | } 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] | 2329 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2330 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2331 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2332 | /* To ensure correct connection accounting on |
| 2333 | * the backend, we count the connection for the |
| 2334 | * one managing the queue. |
| 2335 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2336 | t->be->beconn++; |
| 2337 | if (t->be->beconn > t->be->beconn_max) |
| 2338 | t->be->beconn_max = t->be->beconn; |
| 2339 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2340 | t->flags |= SN_BE_ASSIGNED; |
| 2341 | } |
| 2342 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2343 | /* |
| 2344 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2345 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2346 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2347 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2348 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2349 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2350 | /* |
| 2351 | * If HTTP PROXY is set we simply get remote server address |
| 2352 | * parsing incoming request. |
| 2353 | */ |
| 2354 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2355 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2356 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2357 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2358 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2359 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2360 | * so let's do the same now. |
| 2361 | */ |
| 2362 | |
| 2363 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2364 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2365 | 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] | 2366 | } |
| 2367 | |
| 2368 | |
| 2369 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2370 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2371 | * Note that doing so might move headers in the request, but |
| 2372 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2373 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2374 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2375 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2376 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2377 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2378 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2379 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2380 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2381 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2382 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2383 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2384 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2385 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2386 | /* Add an X-Forwarded-For header unless the source IP is |
| 2387 | * in the 'except' network range. |
| 2388 | */ |
| 2389 | if ((!t->fe->except_mask.s_addr || |
| 2390 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2391 | != t->fe->except_net.s_addr) && |
| 2392 | (!t->be->except_mask.s_addr || |
| 2393 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2394 | != t->be->except_net.s_addr)) { |
| 2395 | int len; |
| 2396 | unsigned char *pn; |
| 2397 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2398 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 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, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2412 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2413 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2414 | &txn->hdr_idx, trash, len)) < 0) |
| 2415 | goto return_bad_req; |
| 2416 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2417 | } |
| 2418 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2419 | /* FIXME: for the sake of completeness, we should also support |
| 2420 | * 'except' here, although it is mostly useless in this case. |
| 2421 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2422 | int len; |
| 2423 | char pn[INET6_ADDRSTRLEN]; |
| 2424 | inet_ntop(AF_INET6, |
| 2425 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2426 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* Note: we rely on the backend to get the header name to be used for |
| 2429 | * x-forwarded-for, because the header is really meant for the backends. |
| 2430 | * However, if the backend did not specify any option, we have to rely |
| 2431 | * on the frontend's header name. |
| 2432 | */ |
| 2433 | if (t->be->fwdfor_hdr_len) { |
| 2434 | len = t->be->fwdfor_hdr_len; |
| 2435 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2436 | } else { |
| 2437 | len = t->fe->fwdfor_hdr_len; |
| 2438 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2439 | } |
| 2440 | len += sprintf(trash + len, ": %s", pn); |
| 2441 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2442 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2443 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2444 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2445 | } |
| 2446 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2447 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2448 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2449 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2450 | * 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] | 2451 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2452 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2453 | ((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] | 2454 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2455 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2456 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2457 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2458 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2459 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2460 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2461 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2462 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2463 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2464 | * (unless headers overflowed the buffer?) |
| 2465 | */ |
| 2466 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2467 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2468 | 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] | 2469 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2470 | /* are there enough bytes here? total == l || r || rlim ? |
| 2471 | * len is unsigned, but eoh is int, |
| 2472 | * how many bytes of body have we received? |
| 2473 | * eoh is the first empty line of the header |
| 2474 | */ |
| 2475 | /* 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] | 2476 | 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] | 2477 | |
| 2478 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2479 | * We can't assume responsibility for the server's decision, |
| 2480 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2481 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2482 | */ |
| 2483 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2484 | struct hdr_ctx ctx; |
| 2485 | ctx.idx = 0; |
| 2486 | /* Expect is allowed in 1.1, look for it */ |
| 2487 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2488 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2489 | 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] | 2490 | /* We can't reliablly stall and wait for data, because of |
| 2491 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2492 | * the next block to check length expectations. |
| 2493 | * We could send 100 status back to the client, but then we need to |
| 2494 | * re-write headers, and send the message. And this isn't the right |
| 2495 | * place for that action. |
| 2496 | * TODO: support Expect elsewhere and delete this block. |
| 2497 | */ |
| 2498 | goto end_check_maybe_wait_for_body; |
| 2499 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2500 | |
| 2501 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2502 | /* nothing to do, we got enough */ |
| 2503 | } else { |
| 2504 | /* limit implies we are supposed to need this many bytes |
| 2505 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2506 | */ |
| 2507 | long long hint = len; |
| 2508 | struct hdr_ctx ctx; |
| 2509 | ctx.idx = 0; |
| 2510 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2511 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2512 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2513 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2514 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2515 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2516 | ctx.idx = 0; |
| 2517 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2518 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2519 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2520 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2521 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2522 | hint = 0; /* parse failure, untrusted client */ |
| 2523 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2524 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2525 | msg->hdr_content_len = hint; |
| 2526 | else |
| 2527 | hint = 0; /* bad client, sent negative length */ |
| 2528 | } |
| 2529 | } |
| 2530 | /* 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] | 2531 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2532 | hint = t->be->url_param_post_limit; |
| 2533 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2534 | if (len < hint) { |
| 2535 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2536 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2537 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2538 | /* else... There are no body bytes to wait for */ |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2544 | /************************************************************* |
| 2545 | * OK, that's finished for the headers. We have done what we * |
| 2546 | * could. Let's switch to the DATA state. * |
| 2547 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2548 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2549 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2550 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2551 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2552 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2553 | * which may be the same as the connect timeout if unspecified. |
| 2554 | * 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] | 2555 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2556 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2557 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2558 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2559 | /* flush the request so that we can drop the connection early |
| 2560 | * if the client closes first. |
| 2561 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2562 | req->flags &= ~BF_MAY_FORWARD; |
| 2563 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2564 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2565 | if (!req->analyse_exp) |
| 2566 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2567 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2569 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2570 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2571 | |
| 2572 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2573 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2574 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2575 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2576 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2577 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2578 | return_prx_cond: |
| 2579 | if (!(t->flags & SN_ERR_MASK)) |
| 2580 | t->flags |= SN_ERR_PRXCOND; |
| 2581 | if (!(t->flags & SN_FINST_MASK)) |
| 2582 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2583 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2584 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2585 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2588 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2589 | struct http_txn *txn = &t->txn; |
| 2590 | |
| 2591 | /* This connection is being tarpitted. The CLIENT side has |
| 2592 | * already set the connect expiration date to the right |
| 2593 | * timeout. We just have to check that the client is still |
| 2594 | * there and that the timeout has not expired. |
| 2595 | */ |
| 2596 | if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 && |
| 2597 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2598 | return 0; |
| 2599 | |
| 2600 | /* We will set the queue timer to the time spent, just for |
| 2601 | * logging purposes. We fake a 500 server error, so that the |
| 2602 | * attacker will not suspect his connection has been tarpitted. |
| 2603 | * It will not cause trouble to the logs because we can exclude |
| 2604 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2605 | */ |
| 2606 | trace_term(t, TT_HTTP_SRV_2); |
| 2607 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2608 | |
| 2609 | txn->status = 500; |
| 2610 | if (req->flags != BF_READ_ERROR) |
| 2611 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2612 | |
| 2613 | req->analysers = 0; |
| 2614 | req->analyse_exp = TICK_ETERNITY; |
| 2615 | |
| 2616 | t->fe->failed_req++; |
| 2617 | if (!(t->flags & SN_ERR_MASK)) |
| 2618 | t->flags |= SN_ERR_PRXCOND; |
| 2619 | if (!(t->flags & SN_FINST_MASK)) |
| 2620 | t->flags |= SN_FINST_T; |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2624 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2625 | /* We have to parse the HTTP request body to find any required data. |
| 2626 | * "balance url_param check_post" should have been the only way to get |
| 2627 | * into this. We were brought here after HTTP header analysis, so all |
| 2628 | * related structures are ready. |
| 2629 | */ |
| 2630 | struct http_msg *msg = &t->txn.req; |
| 2631 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2632 | long long limit = t->be->url_param_post_limit; |
| 2633 | struct hdr_ctx ctx; |
| 2634 | |
| 2635 | ctx.idx = 0; |
| 2636 | |
| 2637 | /* now if we have a length, we'll take the hint */ |
| 2638 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2639 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2640 | unsigned int chunk = 0; |
| 2641 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2642 | char c = msg->sol[body]; |
| 2643 | if (ishex(c)) { |
| 2644 | unsigned int hex = toupper(c) - '0'; |
| 2645 | if (hex > 9) |
| 2646 | hex -= 'A' - '9' - 1; |
| 2647 | chunk = (chunk << 4) | hex; |
| 2648 | } else |
| 2649 | break; |
| 2650 | body++; |
| 2651 | } |
| 2652 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2653 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2654 | |
| 2655 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2656 | 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] | 2657 | |
| 2658 | body += 2; // skip CRLF |
| 2659 | |
| 2660 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2661 | * 1. how much entity data do we have? new var |
| 2662 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2663 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2664 | */ |
| 2665 | |
| 2666 | if (chunk < limit) |
| 2667 | limit = chunk; /* only reading one chunk */ |
| 2668 | } else { |
| 2669 | if (msg->hdr_content_len < limit) |
| 2670 | limit = msg->hdr_content_len; |
| 2671 | } |
| 2672 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2673 | http_body_end: |
| 2674 | /* we leave once we know we have nothing left to do. This means that we have |
| 2675 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2676 | * buffer closed). |
| 2677 | */ |
| 2678 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2679 | 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] | 2680 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2681 | /* 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] | 2682 | 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] | 2683 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2684 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2685 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2686 | else { |
| 2687 | /* Not enough data. We'll re-use the http-request |
| 2688 | * timeout here. Ideally, we should set the timeout |
| 2689 | * relative to the accept() date. We just set the |
| 2690 | * request timeout once at the beginning of the |
| 2691 | * request. |
| 2692 | */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2693 | req->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2694 | if (!tick_isset(req->analyse_exp)) |
| 2695 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2696 | return 0; |
| 2697 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2698 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2699 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2700 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2701 | * probably reduce one day's debugging session. |
| 2702 | */ |
| 2703 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2704 | 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] | 2705 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2706 | __FILE__, __LINE__, req->analysers); |
| 2707 | ABORT_NOW(); |
| 2708 | } |
| 2709 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2710 | 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] | 2711 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2712 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2713 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2714 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2715 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2716 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2717 | * 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] | 2718 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2719 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2720 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2721 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2722 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2723 | struct buffer *req = t->req; |
| 2724 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2726 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x analysers=%02x\n", |
| 2727 | now_ms, __FUNCTION__, |
| 2728 | cli_stnames[t->cli_state], |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2729 | req->rex, rep->wex, req->flags, rep->flags, rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2731 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2732 | /* |
| 2733 | * Now parse the partial (or complete) lines. |
| 2734 | * We will check the response syntax, and also join multi-line |
| 2735 | * headers. An index of all the lines will be elaborated while |
| 2736 | * parsing. |
| 2737 | * |
| 2738 | * For the parsing, we use a 28 states FSM. |
| 2739 | * |
| 2740 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2741 | * rep->data + rep->som = beginning of response |
| 2742 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2743 | * 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] | 2744 | * rep->lr = first non-visited byte |
| 2745 | * rep->r = end of data |
| 2746 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2747 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2748 | int cur_idx; |
| 2749 | struct http_msg *msg = &txn->rsp; |
| 2750 | struct proxy *cur_proxy; |
| 2751 | |
| 2752 | if (likely(rep->lr < rep->r)) |
| 2753 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2754 | |
| 2755 | /* 1: we might have to print this header in debug mode */ |
| 2756 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2757 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2758 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2759 | char *eol, *sol; |
| 2760 | |
| 2761 | sol = rep->data + msg->som; |
| 2762 | eol = sol + msg->sl.rq.l; |
| 2763 | debug_hdr("srvrep", t, sol, eol); |
| 2764 | |
| 2765 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2766 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2767 | |
| 2768 | while (cur_idx) { |
| 2769 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2770 | debug_hdr("srvhdr", t, sol, eol); |
| 2771 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2772 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2773 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2774 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2775 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2776 | /* |
| 2777 | * Now we quickly check if we have found a full valid response. |
| 2778 | * If not so, we check the FD and buffer states before leaving. |
| 2779 | * A full response is indicated by the fact that we have seen |
| 2780 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2781 | * responses are checked first. |
| 2782 | * |
| 2783 | * Depending on whether the client is still there or not, we |
| 2784 | * may send an error response back or not. Note that normally |
| 2785 | * we should only check for HTTP status there, and check I/O |
| 2786 | * errors somewhere else. |
| 2787 | */ |
| 2788 | |
| 2789 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2790 | /* Invalid response */ |
| 2791 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2792 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2793 | //buffer_shutr(rep); |
| 2794 | //buffer_shutw(req); |
| 2795 | //fd_delete(req->cons->fd); |
| 2796 | //req->cons->state = SI_ST_CLO; |
| 2797 | buffer_shutr_now(rep); |
| 2798 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2799 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2800 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2801 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2802 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2803 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2804 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2805 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2806 | txn->status = 502; |
| 2807 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2808 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2809 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2810 | if (!(t->flags & SN_FINST_MASK)) |
| 2811 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2812 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2813 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2814 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2815 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2816 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2817 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2818 | /* write error to client, read error or close from server */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2819 | if (rep->flags & (BF_WRITE_ERROR|BF_SHUTW|BF_READ_ERROR|BF_SHUTR|BF_READ_NULL)) { |
| 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 | cebf57e | 2008-08-15 18:16:37 +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 | cebf57e | 2008-08-15 18:16:37 +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 | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2828 | } |
| 2829 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2830 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2831 | txn->status = 502; |
| 2832 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2833 | if (!(t->flags & SN_ERR_MASK)) |
| 2834 | t->flags |= SN_ERR_SRVCL; |
| 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 | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2840 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2841 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2842 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2843 | /* too large response does not fit in buffer. */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2844 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2845 | goto hdr_response_bad; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2846 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2847 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2848 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2849 | buffer_shutr_now(rep); |
| 2850 | buffer_shutw_now(req); |
| 2851 | //fd_delete(req->cons->fd); |
| 2852 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2853 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2854 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2855 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2856 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2857 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2858 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2859 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2860 | txn->status = 504; |
| 2861 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2862 | if (!(t->flags & SN_ERR_MASK)) |
| 2863 | t->flags |= SN_ERR_SRVTO; |
| 2864 | if (!(t->flags & SN_FINST_MASK)) |
| 2865 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2866 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2867 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2868 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2869 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2870 | } |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2871 | |
| 2872 | rep->flags &= ~BF_MAY_FORWARD; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2873 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2874 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2876 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2877 | /***************************************************************** |
| 2878 | * More interesting part now : we know that we have a complete * |
| 2879 | * response which at least looks like HTTP. We have an indicator * |
| 2880 | * of each header's length, so we can parse them quickly. * |
| 2881 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2882 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2883 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2884 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2885 | /* ensure we keep this pointer to the beginning of the message */ |
| 2886 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2887 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2888 | /* |
| 2889 | * 1: get the status code and check for cacheability. |
| 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 | t->logs.logwait &= ~LW_RESP; |
| 2893 | 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] | 2894 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2895 | switch (txn->status) { |
| 2896 | case 200: |
| 2897 | case 203: |
| 2898 | case 206: |
| 2899 | case 300: |
| 2900 | case 301: |
| 2901 | case 410: |
| 2902 | /* RFC2616 @13.4: |
| 2903 | * "A response received with a status code of |
| 2904 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2905 | * by a cache (...) unless a cache-control |
| 2906 | * directive prohibits caching." |
| 2907 | * |
| 2908 | * RFC2616 @9.5: POST method : |
| 2909 | * "Responses to this method are not cacheable, |
| 2910 | * unless the response includes appropriate |
| 2911 | * Cache-Control or Expires header fields." |
| 2912 | */ |
| 2913 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2914 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2915 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2916 | break; |
| 2917 | default: |
| 2918 | break; |
| 2919 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2921 | /* |
| 2922 | * 2: we may need to capture headers |
| 2923 | */ |
| 2924 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2925 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2926 | txn->rsp.cap, t->fe->rsp_cap); |
| 2927 | |
| 2928 | /* |
| 2929 | * 3: we will have to evaluate the filters. |
| 2930 | * As opposed to version 1.2, now they will be evaluated in the |
| 2931 | * filters order and not in the header order. This means that |
| 2932 | * each filter has to be validated among all headers. |
| 2933 | * |
| 2934 | * Filters are tried with ->be first, then with ->fe if it is |
| 2935 | * different from ->be. |
| 2936 | */ |
| 2937 | |
| 2938 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2939 | |
| 2940 | cur_proxy = t->be; |
| 2941 | while (1) { |
| 2942 | struct proxy *rule_set = cur_proxy; |
| 2943 | |
| 2944 | /* try headers filters */ |
| 2945 | if (rule_set->rsp_exp != NULL) { |
| 2946 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2947 | return_bad_resp: |
| 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_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2951 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2952 | } |
| 2953 | cur_proxy->failed_resp++; |
| 2954 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2955 | buffer_shutr_now(rep); |
| 2956 | buffer_shutw_now(req); |
| 2957 | //fd_delete(req->cons->fd); |
| 2958 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2959 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2960 | txn->status = 502; |
| 2961 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2962 | if (!(t->flags & SN_ERR_MASK)) |
| 2963 | t->flags |= SN_ERR_PRXCOND; |
| 2964 | if (!(t->flags & SN_FINST_MASK)) |
| 2965 | t->flags |= SN_FINST_H; |
| 2966 | /* We used to have a free connection slot. Since we'll never use it, |
| 2967 | * we have to inform the server that it may be used by another session. |
| 2968 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2969 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2970 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2971 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2972 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2973 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2974 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2975 | /* has the response been denied ? */ |
| 2976 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2977 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2978 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2979 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2980 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2981 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2982 | cur_proxy->denied_resp++; |
| 2983 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2984 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2985 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2986 | /* We might have to check for "Connection:" */ |
| 2987 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2988 | !(t->flags & SN_CONN_CLOSED)) { |
| 2989 | char *cur_ptr, *cur_end, *cur_next; |
| 2990 | int cur_idx, old_idx, delta, val; |
| 2991 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2992 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2993 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2994 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2995 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2996 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2997 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2998 | cur_ptr = cur_next; |
| 2999 | cur_end = cur_ptr + cur_hdr->len; |
| 3000 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3001 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3002 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3003 | if (val) { |
| 3004 | /* 3 possibilities : |
| 3005 | * - we have already set Connection: close, |
| 3006 | * so we remove this line. |
| 3007 | * - we have not yet set Connection: close, |
| 3008 | * but this line indicates close. We leave |
| 3009 | * it untouched and set the flag. |
| 3010 | * - we have not yet set Connection: close, |
| 3011 | * and this line indicates non-close. We |
| 3012 | * replace it. |
| 3013 | */ |
| 3014 | if (t->flags & SN_CONN_CLOSED) { |
| 3015 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3016 | txn->rsp.eoh += delta; |
| 3017 | cur_next += delta; |
| 3018 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3019 | txn->hdr_idx.used--; |
| 3020 | cur_hdr->len = 0; |
| 3021 | } else { |
| 3022 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3023 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3024 | "close", 5); |
| 3025 | cur_next += delta; |
| 3026 | cur_hdr->len += delta; |
| 3027 | txn->rsp.eoh += delta; |
| 3028 | } |
| 3029 | t->flags |= SN_CONN_CLOSED; |
| 3030 | } |
| 3031 | } |
| 3032 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3033 | } |
| 3034 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3035 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3036 | /* add response headers from the rule sets in the same order */ |
| 3037 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3038 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3039 | rule_set->rsp_add[cur_idx])) < 0) |
| 3040 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3043 | /* check whether we're already working on the frontend */ |
| 3044 | if (cur_proxy == t->fe) |
| 3045 | break; |
| 3046 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3047 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3048 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3049 | /* |
| 3050 | * 4: check for server cookie. |
| 3051 | */ |
| 3052 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3053 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3054 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3055 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3057 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3058 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3059 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3060 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3061 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3063 | /* |
| 3064 | * 6: add server cookie in the response if needed |
| 3065 | */ |
| 3066 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3067 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3068 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3069 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3070 | /* the server is known, it's not the one the client requested, we have to |
| 3071 | * insert a set-cookie here, except if we want to insert only on POST |
| 3072 | * requests and this one isn't. Note that servers which don't have cookies |
| 3073 | * (eg: some backup servers) will return a full cookie removal request. |
| 3074 | */ |
| 3075 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3076 | t->be->cookie_name, |
| 3077 | 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] | 3078 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3079 | if (t->be->cookie_domain) |
| 3080 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3081 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3082 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3083 | trash, len)) < 0) |
| 3084 | goto return_bad_resp; |
| 3085 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3086 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3087 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3088 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3089 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3090 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3091 | */ |
| 3092 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3094 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3095 | |
| 3096 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3097 | "Cache-control: private", 22)) < 0) |
| 3098 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3099 | } |
| 3100 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3102 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3103 | /* |
| 3104 | * 7: check if result will be cacheable with a cookie. |
| 3105 | * We'll block the response if security checks have caught |
| 3106 | * nasty things such as a cacheable cookie. |
| 3107 | */ |
| 3108 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3109 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3110 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3111 | |
| 3112 | /* we're in presence of a cacheable response containing |
| 3113 | * a set-cookie header. We'll block it as requested by |
| 3114 | * the 'checkcache' option, and send an alert. |
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 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3117 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3118 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3119 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3120 | } |
| 3121 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3122 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3123 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3124 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3125 | send_log(t->be, LOG_ALERT, |
| 3126 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3127 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3128 | goto return_srv_prx_502; |
| 3129 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3130 | |
| 3131 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3132 | * 8: add "Connection: close" if needed and not yet set. |
| 3133 | * 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] | 3134 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3135 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3136 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3137 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3138 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3139 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3140 | "Connection: close", 17)) < 0) |
| 3141 | goto return_bad_resp; |
| 3142 | t->flags |= SN_CONN_CLOSED; |
| 3143 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3144 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3145 | /************************************************************* |
| 3146 | * OK, that's finished for the headers. We have done what we * |
| 3147 | * could. Let's switch to the DATA state. * |
| 3148 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3149 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3150 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3151 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3152 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3153 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3154 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3155 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3156 | tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3157 | } |
| 3158 | #endif |
| 3159 | /* if the user wants to log as soon as possible, without counting |
| 3160 | * bytes from the server, then this is the right moment. We have |
| 3161 | * to temporarily assign bytes_out to log what we currently have. |
| 3162 | */ |
| 3163 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3164 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3165 | t->logs.bytes_out = txn->rsp.eoh; |
| 3166 | if (t->fe->to_log & LW_REQ) |
| 3167 | http_sess_log(t); |
| 3168 | else |
| 3169 | tcp_sess_log(t); |
| 3170 | t->logs.bytes_out = 0; |
| 3171 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3172 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3173 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3174 | * otherwise we would not let the client side wake up. |
| 3175 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3176 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3177 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3178 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3179 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3180 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3181 | * probably reduce one day's debugging session. |
| 3182 | */ |
| 3183 | #ifdef DEBUG_DEV |
| 3184 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3185 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3186 | __FILE__, __LINE__, rep->analysers); |
| 3187 | ABORT_NOW(); |
| 3188 | } |
| 3189 | #endif |
| 3190 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3191 | return 0; |
| 3192 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3193 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3194 | /* |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3195 | * Manages the client FSM and its socket. It normally returns zero, but may |
| 3196 | * return 1 if it absolutely wants to be called again. |
| 3197 | * |
| 3198 | * 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] | 3199 | * but CL_STCLOSE. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3200 | */ |
| 3201 | int process_cli(struct session *t) |
| 3202 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3203 | struct buffer *req = t->req; |
| 3204 | struct buffer *rep = t->rep; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3205 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3206 | 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", |
| 3207 | now_ms, __FUNCTION__, |
| 3208 | t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3209 | cli_stnames[t->cli_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3210 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3211 | 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] | 3212 | req->rex, rep->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3213 | req->flags, rep->flags, |
| 3214 | req->l, rep->l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3215 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3216 | update_state: |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3217 | /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3218 | * still set this state (and will do until unix sockets are converted). |
| 3219 | */ |
| 3220 | if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3221 | /* 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] | 3222 | if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3223 | (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3224 | (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3225 | (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3226 | (!(rep->flags & BF_SHUTW) && |
| 3227 | (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3228 | goto update_timeouts; |
| 3229 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3230 | /* read or write error */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3231 | if (fdtab[t->cli_fd].state == FD_STERROR) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3232 | buffer_shutr(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3233 | req->flags |= BF_READ_ERROR; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3234 | buffer_shutw(rep); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3235 | rep->flags |= BF_WRITE_ERROR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3236 | fd_delete(t->cli_fd); |
| 3237 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3238 | trace_term(t, TT_HTTP_CLI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3239 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3240 | if (!(t->flags & SN_ERR_MASK)) |
| 3241 | t->flags |= SN_ERR_CLICL; |
| 3242 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3243 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3244 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3245 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3246 | t->flags |= SN_FINST_C; |
| 3247 | else |
| 3248 | t->flags |= SN_FINST_D; |
| 3249 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3250 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3251 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3252 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3253 | /* last read, or end of server write */ |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3254 | else if (!(req->flags & BF_SHUTR) && /* not already done */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3255 | req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3256 | buffer_shutr(req); |
| 3257 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3258 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3259 | trace_term(t, TT_HTTP_CLI_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3260 | } else { |
| 3261 | /* output was already closed */ |
| 3262 | fd_delete(t->cli_fd); |
| 3263 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3264 | trace_term(t, TT_HTTP_CLI_3); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3265 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3266 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3267 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3268 | /* last server read and buffer empty : we only check them when we're |
| 3269 | * allowed to forward the data. |
| 3270 | */ |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3271 | else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3272 | ((rep->flags & BF_SHUTW_NOW) || |
| 3273 | (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3274 | rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3275 | buffer_shutw(rep); |
| 3276 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3277 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3278 | shutdown(t->cli_fd, SHUT_WR); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3279 | trace_term(t, TT_HTTP_CLI_4); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3280 | } else { |
| 3281 | fd_delete(t->cli_fd); |
| 3282 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3283 | trace_term(t, TT_HTTP_CLI_5); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3284 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3285 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3286 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3287 | /* read timeout */ |
Willy Tarreau | 2500981 | 2008-08-17 18:16:38 +0200 | [diff] [blame] | 3288 | else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3289 | buffer_shutr(req); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3290 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3291 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3292 | trace_term(t, TT_HTTP_CLI_6); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3293 | } else { |
| 3294 | /* output was already closed */ |
| 3295 | fd_delete(t->cli_fd); |
| 3296 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3297 | trace_term(t, TT_HTTP_CLI_7); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3298 | } |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3299 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3300 | if (!(t->flags & SN_ERR_MASK)) |
| 3301 | t->flags |= SN_ERR_CLITO; |
| 3302 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3303 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3304 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3305 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3306 | t->flags |= SN_FINST_C; |
| 3307 | else |
| 3308 | t->flags |= SN_FINST_D; |
| 3309 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3310 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3311 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3312 | } |
| 3313 | /* write timeout */ |
Willy Tarreau | 2500981 | 2008-08-17 18:16:38 +0200 | [diff] [blame] | 3314 | else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3315 | buffer_shutw(rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3316 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3317 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3318 | shutdown(t->cli_fd, SHUT_WR); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3319 | trace_term(t, TT_HTTP_CLI_8); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3320 | } else { |
| 3321 | fd_delete(t->cli_fd); |
| 3322 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3323 | trace_term(t, TT_HTTP_CLI_9); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3324 | } |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3325 | if (!req->analysers) { |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3326 | if (!(t->flags & SN_ERR_MASK)) |
| 3327 | t->flags |= SN_ERR_CLITO; |
| 3328 | if (!(t->flags & SN_FINST_MASK)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3329 | if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3330 | t->flags |= SN_FINST_Q; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3331 | else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 3332 | t->flags |= SN_FINST_C; |
| 3333 | else |
| 3334 | t->flags |= SN_FINST_D; |
| 3335 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3336 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3337 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3340 | update_timeouts: |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3341 | /* manage read timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3342 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3343 | if (req->flags & BF_FULL) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3344 | /* no room to read more data */ |
| 3345 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3346 | /* stop reading until we get some space */ |
| 3347 | req->rex = TICK_ETERNITY; |
| 3348 | } |
| 3349 | } else { |
| 3350 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3351 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | /* manage write timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3356 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3357 | /* first, we may have to produce data (eg: stats). |
| 3358 | * right now, this is limited to the SHUTR state. |
| 3359 | */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3360 | if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3361 | produce_content(t); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3362 | if (rep->flags & BF_EMPTY) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3363 | buffer_shutw(rep); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3364 | fd_delete(t->cli_fd); |
| 3365 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3366 | trace_term(t, TT_HTTP_CLI_10); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3367 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3368 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3369 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3370 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3371 | /* 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] | 3372 | if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3373 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3374 | /* stop writing */ |
| 3375 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3376 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3377 | } else { |
| 3378 | /* buffer not empty */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3379 | EV_FD_COND_S(t->cli_fd, DIR_WR); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3380 | if (!tick_isset(rep->wex)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3381 | /* restart writing */ |
| 3382 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3383 | 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] | 3384 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3385 | * we refresh it, except if it was already infinite. */ |
| 3386 | req->rex = rep->wex; |
| 3387 | } |
| 3388 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3389 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3390 | } |
| 3391 | return 0; /* other cases change nothing */ |
| 3392 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3393 | else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3394 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3395 | int len; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3396 | 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] | 3397 | write(1, trash, len); |
| 3398 | } |
| 3399 | return 0; |
| 3400 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3401 | #ifdef DEBUG_DEV |
| 3402 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3403 | ABORT_NOW(); |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3404 | #endif |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3405 | return 0; |
| 3406 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3407 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3408 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3409 | /* Return 1 if the pending connection has failed and should be retried, |
| 3410 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3411 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3412 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3413 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3414 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3415 | struct buffer *req = t->req; |
| 3416 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3417 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3418 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3419 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3420 | now_ms, __FUNCTION__, |
| 3421 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3422 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3423 | req->flags, rep->flags, |
| 3424 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3425 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3426 | if ((req->flags & BF_SHUTW_NOW) || |
| 3427 | (rep->flags & BF_SHUTW) || |
| 3428 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3429 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
| 3430 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3431 | |
| 3432 | trace_term(t, TT_HTTP_SRV_5); |
| 3433 | req->wex = TICK_ETERNITY; |
| 3434 | fd_delete(req->cons->fd); |
| 3435 | if (t->srv) { |
| 3436 | t->srv->cur_sess--; |
| 3437 | sess_change_server(t, NULL); |
| 3438 | } |
| 3439 | /* note that this must not return any error because it would be able to |
| 3440 | * overwrite the client_retnclose() output. |
| 3441 | */ |
| 3442 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3443 | |
| 3444 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3445 | buffer_shutw(req); |
| 3446 | buffer_shutr(rep); |
| 3447 | req->cons->state = SI_ST_CLO; |
| 3448 | if (!req->cons->err_type) |
| 3449 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3450 | req->cons->err_loc = t->srv; |
| 3451 | return 0; |
| 3452 | } |
| 3453 | |
| 3454 | /* check for timeouts and asynchronous connect errors */ |
| 3455 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3456 | conn_err = SI_ET_CONN_ERR; |
| 3457 | if (!req->cons->err_type) |
| 3458 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3459 | } |
| 3460 | else if (!(req->flags & BF_WRITE_STATUS)) { |
| 3461 | /* nothing happened, maybe we timed out */ |
| 3462 | if (tick_is_expired(req->wex, now_ms)) { |
| 3463 | conn_err = SI_ET_CONN_TO; |
| 3464 | if (!req->cons->err_type) |
| 3465 | req->cons->err_type = SI_ET_CONN_TO; |
| 3466 | } |
| 3467 | else |
| 3468 | return 0; /* let's wait a bit more */ |
| 3469 | } |
| 3470 | |
| 3471 | if (conn_err) { |
| 3472 | fd_delete(req->cons->fd); |
| 3473 | req->cons->state = SI_ST_CLO; |
| 3474 | |
| 3475 | if (t->srv) { |
| 3476 | t->srv->cur_sess--; |
| 3477 | sess_change_server(t, NULL); |
| 3478 | req->cons->err_loc = t->srv; |
| 3479 | } |
| 3480 | |
| 3481 | /* ensure that we have enough retries left */ |
| 3482 | if (srv_count_retry_down(t, conn_err)) |
| 3483 | return 0; |
| 3484 | |
| 3485 | if (conn_err == SI_ET_CONN_ERR) { |
| 3486 | /* we encountered an immediate connection error, and we |
| 3487 | * will have to retry connecting to the same server, most |
| 3488 | * likely leading to the same result. To avoid this, we |
| 3489 | * fake a connection timeout to retry after a turn-around |
| 3490 | * time of 1 second. We will wait in the previous if block. |
| 3491 | */ |
| 3492 | req->cons->state = SI_ST_TAR; |
| 3493 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 3494 | return 0; |
| 3495 | } |
| 3496 | |
| 3497 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3498 | /* We're on our last chance, and the REDISP option was specified. |
| 3499 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3500 | */ |
| 3501 | /* let's try to offer this slot to anybody */ |
| 3502 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3503 | process_srv_queue(t->srv); |
| 3504 | |
| 3505 | /* it's left to the dispatcher to choose a server */ |
| 3506 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3507 | t->prev_srv = t->srv; |
| 3508 | } else { |
| 3509 | /* we just want to retry */ |
| 3510 | if (t->srv) |
| 3511 | t->srv->retries++; |
| 3512 | t->be->retries++; |
| 3513 | |
| 3514 | /* Now we will try to either reconnect to the same server or |
| 3515 | * connect to another server. If the connection gets queued |
| 3516 | * because all servers are saturated, then we will go back to |
| 3517 | * the idle state where the buffer's consumer is marked as |
| 3518 | * unknown. |
| 3519 | */ |
| 3520 | if (srv_retryable_connect(t)) { |
| 3521 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3522 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3523 | return 0; |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | /* We'll rely on the caller to try to get a connection again */ |
| 3528 | return 1; |
| 3529 | } |
| 3530 | else { |
| 3531 | /* no error and write OK : connection succeeded */ |
| 3532 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3533 | req->cons->state = SI_ST_EST; |
| 3534 | req->cons->err_type = SI_ET_NONE; |
| 3535 | req->cons->err_loc = NULL; |
| 3536 | |
| 3537 | if (req->flags & BF_EMPTY) { |
| 3538 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3539 | req->wex = TICK_ETERNITY; |
| 3540 | } else { |
| 3541 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3542 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3543 | if (tick_isset(req->wex)) { |
| 3544 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3545 | * we refresh it. */ |
| 3546 | rep->rex = req->wex; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3551 | if (!(rep->flags & BF_HIJACK)) { |
| 3552 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3553 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3554 | } |
| 3555 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3556 | |
| 3557 | /* if the user wants to log as soon as possible, without counting |
| 3558 | bytes from the server, then this is the right moment. */ |
| 3559 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3560 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3561 | tcp_sess_log(t); |
| 3562 | } |
| 3563 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3564 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3565 | /* TCP splicing supported by both FE and BE */ |
| 3566 | tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
| 3567 | } |
| 3568 | #endif |
| 3569 | } |
| 3570 | else { |
| 3571 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3572 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3573 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3574 | /* reset hdr_idx which was already initialized by the request. |
| 3575 | * right now, the http parser does it. |
| 3576 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3577 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | if (!rep->analysers) |
| 3581 | t->rep->flags |= BF_MAY_FORWARD; |
| 3582 | req->wex = TICK_ETERNITY; |
| 3583 | return 0; |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | |
| 3588 | /* |
| 3589 | * This function tries to assign a server to a stream_sock interface. |
| 3590 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3591 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3592 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3593 | * it returns SI_ST_ASS, otherwise zero. |
| 3594 | */ |
| 3595 | int stream_sock_assign_server(struct session *t) |
| 3596 | { |
| 3597 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3598 | now_ms, __FUNCTION__, |
| 3599 | cli_stnames[t->cli_state], |
| 3600 | t->rep->rex, t->req->wex, |
| 3601 | t->req->flags, t->rep->flags, |
| 3602 | t->req->l, t->rep->l); |
| 3603 | |
| 3604 | if (t->req->cons->state == SI_ST_TAR) { |
| 3605 | /* connection might be aborted */ |
| 3606 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3607 | (t->rep->flags & BF_SHUTW) || |
| 3608 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3609 | (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] | 3610 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3611 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3612 | t->req->wex = TICK_ETERNITY; |
| 3613 | |
| 3614 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3615 | buffer_shutr(t->rep); |
| 3616 | buffer_shutw(t->req); |
| 3617 | if (!t->req->cons->err_type) |
| 3618 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3619 | t->req->cons->state = SI_ST_CLO; |
| 3620 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3621 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3622 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3623 | if (!tick_is_expired(t->req->wex, now_ms)) |
| 3624 | return 0; /* still in turn-around */ |
| 3625 | |
| 3626 | t->req->cons->state = SI_ST_INI; |
| 3627 | } |
| 3628 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3629 | if (t->pend_pos) { |
| 3630 | /* request still in queue... */ |
| 3631 | if (tick_is_expired(t->req->wex, now_ms)) { |
| 3632 | /* ... and timeout expired */ |
| 3633 | trace_term(t, TT_HTTP_SRV_3); |
| 3634 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3635 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3636 | if (t->srv) |
| 3637 | t->srv->failed_conns++; |
| 3638 | t->be->failed_conns++; |
| 3639 | |
| 3640 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3641 | buffer_shutr(t->rep); |
| 3642 | buffer_shutw(t->req); |
| 3643 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3644 | if (!t->req->cons->err_type) |
| 3645 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3646 | t->req->cons->state = SI_ST_CLO; |
| 3647 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3648 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3649 | /* connection remains in queue, check if we have to abort it */ |
| 3650 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3651 | (t->rep->flags & BF_SHUTW) || |
| 3652 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3653 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3654 | /* give up */ |
| 3655 | trace_term(t, TT_HTTP_SRV_1); |
| 3656 | t->req->wex = TICK_ETERNITY; |
| 3657 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3658 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3659 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3660 | buffer_shutr(t->rep); |
| 3661 | buffer_shutw(t->req); |
| 3662 | if (!t->req->cons->err_type) |
| 3663 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3664 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3665 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3666 | return 0; |
| 3667 | } |
| 3668 | /* The connection is not in the queue anymore */ |
| 3669 | t->req->cons->state = SI_ST_INI; |
| 3670 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3671 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3672 | /* we may get here from above */ |
| 3673 | if (t->req->cons->state == SI_ST_INI) { |
| 3674 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3675 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3676 | /* first, check if the connection has been aborted */ |
| 3677 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3678 | (t->rep->flags & BF_SHUTW) || |
| 3679 | ((t->req->flags & BF_SHUTR) && |
| 3680 | (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] | 3681 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3682 | trace_term(t, TT_HTTP_SRV_1); |
| 3683 | t->req->wex = TICK_ETERNITY; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3684 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3685 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3686 | buffer_shutr(t->rep); |
| 3687 | buffer_shutw(t->req); |
| 3688 | if (!t->req->cons->err_type) |
| 3689 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3690 | t->req->cons->state = SI_ST_CLO; |
| 3691 | return 0; |
| 3692 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3694 | /* try to get a server assigned */ |
| 3695 | if (srv_redispatch_connect(t) != 0) { |
| 3696 | /* we did not get any server, let's check the cause */ |
| 3697 | if (t->req->cons->state == SI_ST_QUE) { |
| 3698 | /* the connection was queued, that's OK */ |
| 3699 | return 0; |
| 3700 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3701 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3702 | trace_term(t, TT_HTTP_SRV_2); |
| 3703 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3704 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3705 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3706 | buffer_shutr(t->rep); |
| 3707 | buffer_shutw(t->req); |
| 3708 | t->req->flags |= BF_WRITE_ERROR; |
| 3709 | if (!t->req->cons->err_type) |
| 3710 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3711 | t->req->cons->state = SI_ST_CLO; |
| 3712 | return 0; |
| 3713 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3714 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3715 | t->req->cons->state = SI_ST_ASS; |
| 3716 | /* Once the server is assigned, we have to return because |
| 3717 | * the caller might be interested in checking several |
| 3718 | * things before connecting. |
| 3719 | */ |
| 3720 | return 1; |
| 3721 | } |
| 3722 | return 0; |
| 3723 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3724 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3725 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3726 | /* |
| 3727 | * This function tries to establish a connection to an assigned server. It also |
| 3728 | * performs connection retries. It may only be called with t->req->cons->state |
| 3729 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3730 | * SI_ST_EST, or SI_ST_CLO. |
| 3731 | */ |
| 3732 | int stream_sock_connect_server(struct session *t) |
| 3733 | { |
| 3734 | if (t->req->cons->state == SI_ST_ASS) { |
| 3735 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3736 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3737 | if (!srv_retryable_connect(t)) { |
| 3738 | /* we need to redispatch */ |
| 3739 | t->req->cons->state = SI_ST_INI; |
| 3740 | return 0; |
| 3741 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3742 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3743 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3744 | if (t->req->cons->state != SI_ST_CON) { |
| 3745 | /* it was an error */ |
| 3746 | trace_term(t, TT_HTTP_SRV_4); |
| 3747 | t->req->wex = TICK_ETERNITY; |
| 3748 | |
| 3749 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3750 | buffer_shutr(t->rep); |
| 3751 | buffer_shutw(t->req); |
| 3752 | t->req->flags |= BF_WRITE_ERROR; |
| 3753 | if (!t->req->cons->err_type) |
| 3754 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3755 | t->req->cons->state = SI_ST_CLO; |
| 3756 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3757 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3758 | /* We have a socket and switched to SI_ST_CON */ |
| 3759 | } |
| 3760 | |
| 3761 | /* we may also get here from above */ |
| 3762 | if (t->req->cons->state == SI_ST_CON) { |
| 3763 | /* connection in progress or just completed */ |
| 3764 | if (!tcp_connection_status(t)) |
| 3765 | return 0; |
| 3766 | } |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
| 3770 | |
| 3771 | /* |
| 3772 | * Tries to establish a connection to the server and associate it to the |
| 3773 | * request buffer's consumer side. It is assumed that this function will not be |
| 3774 | * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally |
| 3775 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3776 | */ |
| 3777 | int process_srv_conn(struct session *t) |
| 3778 | { |
| 3779 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3780 | now_ms, __FUNCTION__, |
| 3781 | cli_stnames[t->cli_state], |
| 3782 | t->rep->rex, t->req->wex, |
| 3783 | t->req->flags, t->rep->flags, |
| 3784 | t->req->l, t->rep->l); |
| 3785 | |
| 3786 | do { |
| 3787 | if (t->req->cons->state == SI_ST_INI || |
| 3788 | t->req->cons->state == SI_ST_TAR || |
| 3789 | t->req->cons->state == SI_ST_QUE) { |
| 3790 | /* try to assign a server */ |
| 3791 | if (!stream_sock_assign_server(t)) |
| 3792 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3793 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3794 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3795 | if (t->req->cons->state == SI_ST_ASS && |
| 3796 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3797 | /* Server supporting redirection and it is possible. |
| 3798 | * Invalid requests are reported as such. It concerns all |
| 3799 | * the largest ones. |
| 3800 | */ |
| 3801 | struct http_txn *txn = &t->txn; |
| 3802 | struct chunk rdr; |
| 3803 | char *path; |
| 3804 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3805 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3806 | /* 1: create the response header */ |
| 3807 | rdr.len = strlen(HTTP_302); |
| 3808 | rdr.str = trash; |
| 3809 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3810 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3811 | /* 2: add the server's prefix */ |
| 3812 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3813 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3814 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3815 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3816 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3817 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3818 | /* 3: add the request URI */ |
| 3819 | path = http_get_path(txn); |
| 3820 | if (!path) |
| 3821 | goto cancel_redir; |
| 3822 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3823 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3824 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3825 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3826 | memcpy(rdr.str + rdr.len, path, len); |
| 3827 | rdr.len += len; |
| 3828 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3829 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3830 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3831 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3832 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3834 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3835 | if (t->srv) |
| 3836 | t->srv->cum_sess++; |
| 3837 | |
| 3838 | t->req->cons->state = SI_ST_CLO; |
| 3839 | return 0; |
| 3840 | cancel_redir: |
| 3841 | //txn->status = 400; |
| 3842 | //t->fe->failed_req++; |
| 3843 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3844 | // 400, error_message(t, HTTP_ERR_400)); |
| 3845 | trace_term(t, TT_HTTP_SRV_4); |
| 3846 | |
| 3847 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3848 | buffer_shutw(t->req); |
| 3849 | buffer_shutr(t->rep); |
| 3850 | if (!t->req->cons->err_type) |
| 3851 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3852 | t->req->cons->state = SI_ST_CLO; |
| 3853 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3854 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3855 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3856 | if (t->req->cons->state == SI_ST_CON || |
| 3857 | t->req->cons->state == SI_ST_ASS) { |
| 3858 | stream_sock_connect_server(t); |
| 3859 | } |
| 3860 | } while (t->req->cons->state != SI_ST_CLO && |
| 3861 | t->req->cons->state != SI_ST_CON && |
| 3862 | t->req->cons->state != SI_ST_EST); |
| 3863 | return 0; |
| 3864 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3865 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3866 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3867 | /* |
| 3868 | * Manages the server FSM and its socket during the DATA phase. It must not be |
| 3869 | * called when a file descriptor is not attached to the buffer. It must only be |
| 3870 | * called during SI_ST_EST. It normally returns zero, but may return 1 if it |
| 3871 | * absolutely wants to be called again. |
| 3872 | */ |
| 3873 | int process_srv_data(struct session *t) |
| 3874 | { |
| 3875 | struct buffer *req = t->req; |
| 3876 | struct buffer *rep = t->rep; |
| 3877 | int fd = req->cons->fd; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3878 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3879 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3880 | now_ms, __FUNCTION__, |
| 3881 | cli_stnames[t->cli_state], |
| 3882 | rep->rex, req->wex, |
| 3883 | req->flags, rep->flags, |
| 3884 | req->l, rep->l); |
| 3885 | |
| 3886 | if (req->flags & (BF_WRITE_ERROR | BF_WRITE_TIMEOUT) || |
| 3887 | rep->flags & (BF_READ_ERROR | BF_READ_TIMEOUT)) { |
| 3888 | /* nothing more to be done here */ |
| 3889 | fprintf(stderr, "Hey what are you doing there? t=%p fd=%d state=%d\n", |
| 3890 | t, t->req->cons->fd, t->req->cons->state); |
| 3891 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3892 | } |
Willy Tarreau | 6d2889b | 2008-08-17 16:23:10 +0200 | [diff] [blame] | 3893 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3894 | /* we can skip most of the tests at once if some conditions are not met */ |
| 3895 | /* FIXME: place req->BF_SHUTW_NOW here */ |
| 3896 | //if (!((fdtab[fd].state == FD_STERROR) || |
| 3897 | // (!(req->flags & BF_SHUTW) && |
| 3898 | // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) || |
| 3899 | // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 3900 | // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)))) |
| 3901 | // goto update_timeouts; |
| 3902 | |
| 3903 | /* read or write error */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3904 | if (fdtab[fd].state == FD_STERROR) { |
| 3905 | trace_term(t, TT_HTTP_SRV_6); |
| 3906 | buffer_shutw(req); |
| 3907 | req->flags |= BF_WRITE_ERROR; |
| 3908 | buffer_shutr(rep); |
| 3909 | rep->flags |= BF_READ_ERROR; |
| 3910 | fd_delete(fd); |
| 3911 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3912 | if (!req->cons->err_type) { |
| 3913 | req->cons->err_loc = t->srv; |
| 3914 | req->cons->err_type = SI_ET_DATA_ERR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3915 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | /* last read, or end of client write */ |
| 3920 | if (!(rep->flags & BF_SHUTR) && /* not already done */ |
| 3921 | rep->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3922 | buffer_shutr(rep); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3923 | if (req->flags & BF_SHUTW) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3924 | /* output was already closed */ |
| 3925 | trace_term(t, TT_HTTP_SRV_8); |
| 3926 | fd_delete(fd); |
| 3927 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3928 | return 0; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3929 | } else { |
| 3930 | EV_FD_CLR(fd, DIR_RD); |
| 3931 | trace_term(t, TT_HTTP_SRV_7); |
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 | if (!(req->flags & BF_SHUTW) && /* not already done */ |
| 3942 | (req->flags & BF_SHUTW_NOW || |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3943 | (req->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3944 | buffer_shutw(req); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3945 | if (rep->flags & BF_SHUTR) { |
| 3946 | /* input was already closed */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3947 | trace_term(t, TT_HTTP_SRV_10); |
| 3948 | fd_delete(fd); |
| 3949 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3950 | return 0; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3951 | } else { |
| 3952 | trace_term(t, TT_HTTP_SRV_9); |
| 3953 | EV_FD_CLR(fd, DIR_WR); |
| 3954 | shutdown(fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3955 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3956 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3957 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3958 | /* read timeout */ |
| 3959 | if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == 0 && |
| 3960 | tick_is_expired(rep->rex, now_ms)) { |
| 3961 | rep->flags |= BF_READ_TIMEOUT; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3962 | if (!req->cons->err_type) { |
| 3963 | req->cons->err_loc = t->srv; |
| 3964 | req->cons->err_type = SI_ET_DATA_TO; |
| 3965 | } |
| 3966 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3967 | buffer_shutr(rep); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3968 | if (req->flags & BF_SHUTW) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3969 | trace_term(t, TT_HTTP_SRV_12); |
| 3970 | fd_delete(fd); |
| 3971 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3972 | return 0; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3973 | } else { |
| 3974 | trace_term(t, TT_HTTP_SRV_11); |
| 3975 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3976 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3977 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3978 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3979 | /* write timeout */ |
| 3980 | if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == 0 && |
| 3981 | tick_is_expired(req->wex, now_ms)) { |
| 3982 | req->flags |= BF_WRITE_TIMEOUT; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3983 | if (!req->cons->err_type) { |
| 3984 | req->cons->err_loc = t->srv; |
| 3985 | req->cons->err_type = SI_ET_DATA_TO; |
| 3986 | } |
| 3987 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3988 | buffer_shutw(req); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3989 | if (rep->flags & BF_SHUTR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3990 | trace_term(t, TT_HTTP_SRV_14); |
| 3991 | fd_delete(fd); |
| 3992 | req->cons->state = SI_ST_CLO; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3993 | return 0; |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame^] | 3994 | } else { |
| 3995 | trace_term(t, TT_HTTP_SRV_13); |
| 3996 | EV_FD_CLR(fd, DIR_WR); |
| 3997 | shutdown(fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3998 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3999 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4000 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4001 | update_timeouts: |
| 4002 | /* manage read timeout */ |
| 4003 | if (!(rep->flags & BF_SHUTR)) { |
| 4004 | if (rep->flags & (BF_FULL|BF_HIJACK)) { |
| 4005 | if (EV_FD_COND_C(fd, DIR_RD)) |
| 4006 | rep->rex = TICK_ETERNITY; |
| 4007 | } else { |
| 4008 | EV_FD_COND_S(fd, DIR_RD); |
| 4009 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4010 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4011 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4012 | |
| 4013 | /* manage write timeout */ |
| 4014 | if (!(req->flags & BF_SHUTW)) { |
| 4015 | if ((req->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 4016 | /* stop writing */ |
| 4017 | if (EV_FD_COND_C(fd, DIR_WR)) |
| 4018 | req->wex = TICK_ETERNITY; |
| 4019 | } else { |
| 4020 | /* buffer not empty, there are still data to be transferred */ |
| 4021 | EV_FD_COND_S(fd, DIR_WR); |
| 4022 | if (!tick_isset(req->wex)) { |
| 4023 | /* restart writing */ |
| 4024 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4025 | if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) { |
| 4026 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4027 | * we refresh it, except if it was already infinite. |
| 4028 | */ |
| 4029 | rep->rex = req->wex; |
| 4030 | } |
| 4031 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +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 | return 0; /* other cases change nothing */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4038 | ///* |
| 4039 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 4040 | // * return 1 if it absolutely wants to be called again. |
| 4041 | // * |
| 4042 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 4043 | // * but CL_STCLOSE. |
| 4044 | // */ |
| 4045 | //int process_cli(struct session *t) |
| 4046 | //{ |
| 4047 | // struct buffer *req = t->req; |
| 4048 | // struct buffer *rep = t->rep; |
| 4049 | // |
| 4050 | // 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", |
| 4051 | // now_ms, __FUNCTION__, |
| 4052 | // cli_stnames[t->cli_state], |
| 4053 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 4054 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 4055 | // req->rex, rep->wex, |
| 4056 | // req->flags, rep->flags, |
| 4057 | // req->l, rep->l); |
| 4058 | // |
| 4059 | // update_state: |
| 4060 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 4061 | // * still set this state (and will do until unix sockets are converted). |
| 4062 | // */ |
| 4063 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 4064 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 4065 | // if (!((req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 4066 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) || |
| 4067 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 4068 | // (!(rep->flags & BF_SHUTW) && |
| 4069 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 4070 | // goto update_timeouts; |
| 4071 | // |
| 4072 | // /* read or write error */ |
| 4073 | // if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
| 4074 | // buffer_shutr(req); |
| 4075 | // buffer_shutw(rep); |
| 4076 | // fd_delete(t->cli_fd); |
| 4077 | // t->cli_state = CL_STCLOSE; |
| 4078 | // trace_term(t, TT_HTTP_CLI_1); |
| 4079 | // if (!req->analysers) { |
| 4080 | // if (!(t->flags & SN_ERR_MASK)) |
| 4081 | // t->flags |= SN_ERR_CLICL; |
| 4082 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4083 | // if (t->pend_pos) |
| 4084 | // t->flags |= SN_FINST_Q; |
| 4085 | // else if (!(req->flags & BF_CONNECTED)) |
| 4086 | // t->flags |= SN_FINST_C; |
| 4087 | // else |
| 4088 | // t->flags |= SN_FINST_D; |
| 4089 | // } |
| 4090 | // } |
| 4091 | // goto update_state; |
| 4092 | // } |
| 4093 | // /* last read, or end of server write */ |
| 4094 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 4095 | // req->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 4096 | // buffer_shutr(req); |
| 4097 | // if (!(rep->flags & BF_SHUTW)) { |
| 4098 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 4099 | // trace_term(t, TT_HTTP_CLI_2); |
| 4100 | // } else { |
| 4101 | // /* output was already closed */ |
| 4102 | // fd_delete(t->cli_fd); |
| 4103 | // t->cli_state = CL_STCLOSE; |
| 4104 | // trace_term(t, TT_HTTP_CLI_3); |
| 4105 | // } |
| 4106 | // goto update_state; |
| 4107 | // } |
| 4108 | // /* last server read and buffer empty : we only check them when we're |
| 4109 | // * allowed to forward the data. |
| 4110 | // */ |
| 4111 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 4112 | // rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 4113 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) { |
| 4114 | // buffer_shutw(rep); |
| 4115 | // if (!(req->flags & BF_SHUTR)) { |
| 4116 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 4117 | // shutdown(t->cli_fd, SHUT_WR); |
| 4118 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4119 | // /* FIXME: is this still true ? */ |
| 4120 | // EV_FD_SET(t->cli_fd, DIR_RD); |
| 4121 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4122 | // trace_term(t, TT_HTTP_CLI_4); |
| 4123 | // } else { |
| 4124 | // fd_delete(t->cli_fd); |
| 4125 | // t->cli_state = CL_STCLOSE; |
| 4126 | // trace_term(t, TT_HTTP_CLI_5); |
| 4127 | // } |
| 4128 | // goto update_state; |
| 4129 | // } |
| 4130 | // /* read timeout */ |
| 4131 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 4132 | // buffer_shutr(req); |
| 4133 | // if (!(rep->flags & BF_SHUTW)) { |
| 4134 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 4135 | // trace_term(t, TT_HTTP_CLI_6); |
| 4136 | // } else { |
| 4137 | // /* output was already closed */ |
| 4138 | // fd_delete(t->cli_fd); |
| 4139 | // t->cli_state = CL_STCLOSE; |
| 4140 | // trace_term(t, TT_HTTP_CLI_7); |
| 4141 | // } |
| 4142 | // if (!req->analysers) { |
| 4143 | // if (!(t->flags & SN_ERR_MASK)) |
| 4144 | // t->flags |= SN_ERR_CLITO; |
| 4145 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4146 | // if (t->pend_pos) |
| 4147 | // t->flags |= SN_FINST_Q; |
| 4148 | // else if (!(req->flags & BF_CONNECTED)) |
| 4149 | // t->flags |= SN_FINST_C; |
| 4150 | // else |
| 4151 | // t->flags |= SN_FINST_D; |
| 4152 | // } |
| 4153 | // } |
| 4154 | // goto update_state; |
| 4155 | // } |
| 4156 | // /* write timeout */ |
| 4157 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 4158 | // buffer_shutw(rep); |
| 4159 | // if (!(req->flags & BF_SHUTR)) { |
| 4160 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 4161 | // shutdown(t->cli_fd, SHUT_WR); |
| 4162 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4163 | // /* FIXME: is this still true ? */ |
| 4164 | // EV_FD_SET(t->cli_fd, DIR_RD); |
| 4165 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4166 | // trace_term(t, TT_HTTP_CLI_8); |
| 4167 | // } else { |
| 4168 | // fd_delete(t->cli_fd); |
| 4169 | // t->cli_state = CL_STCLOSE; |
| 4170 | // trace_term(t, TT_HTTP_CLI_9); |
| 4171 | // } |
| 4172 | // if (!req->analysers) { |
| 4173 | // if (!(t->flags & SN_ERR_MASK)) |
| 4174 | // t->flags |= SN_ERR_CLITO; |
| 4175 | // if (!(t->flags & SN_FINST_MASK)) { |
| 4176 | // if (t->pend_pos) |
| 4177 | // t->flags |= SN_FINST_Q; |
| 4178 | // else if (!(req->flags & BF_CONNECTED)) |
| 4179 | // t->flags |= SN_FINST_C; |
| 4180 | // else |
| 4181 | // t->flags |= SN_FINST_D; |
| 4182 | // } |
| 4183 | // } |
| 4184 | // goto update_state; |
| 4185 | // } |
| 4186 | // |
| 4187 | // update_timeouts: |
| 4188 | // /* manage read timeout */ |
| 4189 | // if (!(req->flags & BF_SHUTR)) { |
| 4190 | // if (req->flags & BF_FULL) { |
| 4191 | // /* no room to read more data */ |
| 4192 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 4193 | // /* stop reading until we get some space */ |
| 4194 | // req->rex = TICK_ETERNITY; |
| 4195 | // } |
| 4196 | // } else { |
| 4197 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 4198 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4199 | // } |
| 4200 | // } |
| 4201 | // |
| 4202 | // /* manage write timeout */ |
| 4203 | // if (!(rep->flags & BF_SHUTW)) { |
| 4204 | // /* first, we may have to produce data (eg: stats). |
| 4205 | // * right now, this is limited to the SHUTR state. |
| 4206 | // */ |
| 4207 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 4208 | // produce_content(t); |
| 4209 | // if (rep->flags & BF_EMPTY) { |
| 4210 | // buffer_shutw(rep); |
| 4211 | // fd_delete(t->cli_fd); |
| 4212 | // t->cli_state = CL_STCLOSE; |
| 4213 | // trace_term(t, TT_HTTP_CLI_10); |
| 4214 | // goto update_state; |
| 4215 | // } |
| 4216 | // } |
| 4217 | // |
| 4218 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 4219 | // if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) { |
| 4220 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 4221 | // /* stop writing */ |
| 4222 | // rep->wex = TICK_ETERNITY; |
| 4223 | // } |
| 4224 | // } else { |
| 4225 | // /* buffer not empty */ |
| 4226 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 4227 | // if (!tick_isset(rep->wex)) { |
| 4228 | // /* restart writing */ |
| 4229 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 4230 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 4231 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 4232 | // * we refresh it, except if it was already infinite. */ |
| 4233 | // req->rex = rep->wex; |
| 4234 | // } |
| 4235 | // } |
| 4236 | // } |
| 4237 | // } |
| 4238 | // return 0; /* other cases change nothing */ |
| 4239 | // } |
| 4240 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 4241 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 4242 | // int len; |
| 4243 | // 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); |
| 4244 | // write(1, trash, len); |
| 4245 | // } |
| 4246 | // return 0; |
| 4247 | // } |
| 4248 | //#ifdef DEBUG_DEV |
| 4249 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 4250 | // ABORT_NOW(); |
| 4251 | //#endif |
| 4252 | // return 0; |
| 4253 | //} |
| 4254 | // |
| 4255 | // |
| 4256 | ///* Return 1 if we could get a new connection for session t, otherwise zero */ |
| 4257 | //int tcp_get_connection(struct session *t) |
| 4258 | //{ |
| 4259 | // struct http_txn *txn = &t->txn; |
| 4260 | // struct buffer *req = t->req; |
| 4261 | // struct buffer *rep = t->rep; |
| 4262 | // |
| 4263 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4264 | // now_ms, __FUNCTION__, |
| 4265 | // cli_stnames[t->cli_state], |
| 4266 | // rep->rex, req->wex, |
| 4267 | // req->flags, rep->flags, |
| 4268 | // req->l, rep->l); |
| 4269 | // |
| 4270 | // |
| 4271 | // if ((rep->flags & BF_SHUTW) || |
| 4272 | // ((req->flags & BF_SHUTR) && |
| 4273 | // (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 4274 | // req->wex = TICK_ETERNITY; |
| 4275 | // if (t->pend_pos) |
| 4276 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4277 | // /* note that this must not return any error because it would be able to |
| 4278 | // * overwrite the client_retnclose() output. |
| 4279 | // */ |
| 4280 | // if (txn->flags & TX_CLTARPIT) |
| 4281 | // srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
| 4282 | // else |
| 4283 | // srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
| 4284 | // |
| 4285 | // trace_term(t, TT_HTTP_SRV_1); |
| 4286 | // return 0; |
| 4287 | // } |
| 4288 | // |
| 4289 | // /* stop here if we're not allowed to connect */ |
| 4290 | // if (!(req->flags & BF_MAY_FORWARD)) |
| 4291 | // return 0; |
| 4292 | // |
| 4293 | // /* the client allows the server to connect */ |
| 4294 | // if (txn->flags & TX_CLTARPIT) { |
| 4295 | // /* This connection is being tarpitted. The CLIENT side has |
| 4296 | // * already set the connect expiration date to the right |
| 4297 | // * timeout. We just have to check that it has not expired. |
| 4298 | // */ |
| 4299 | // if (!(req->flags & BF_WRITE_TIMEOUT)) |
| 4300 | // return 0; |
| 4301 | // |
| 4302 | // /* We will set the queue timer to the time spent, just for |
| 4303 | // * logging purposes. We fake a 500 server error, so that the |
| 4304 | // * attacker will not suspect his connection has been tarpitted. |
| 4305 | // * It will not cause trouble to the logs because we can exclude |
| 4306 | // * the tarpitted connections by filtering on the 'PT' status flags. |
| 4307 | // */ |
| 4308 | // req->wex = TICK_ETERNITY; |
| 4309 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4310 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
| 4311 | // 500, error_message(t, HTTP_ERR_500)); |
| 4312 | // trace_term(t, TT_HTTP_SRV_2); |
| 4313 | // return 0; |
| 4314 | // } |
| 4315 | // |
| 4316 | // /* Right now, we will need to create a connection to the server. |
| 4317 | // * We might already have tried, and got a connection pending, in |
| 4318 | // * which case we will not do anything till it's pending. It's up |
| 4319 | // * to any other session to release it and wake us up again. |
| 4320 | // */ |
| 4321 | // if (t->pend_pos) { |
| 4322 | // if (!(req->flags & BF_WRITE_TIMEOUT)) { |
| 4323 | // return 0; |
| 4324 | // } else { |
| 4325 | // /* we've been waiting too long here */ |
| 4326 | // req->wex = TICK_ETERNITY; |
| 4327 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4328 | // srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
| 4329 | // 503, error_message(t, HTTP_ERR_503)); |
| 4330 | // trace_term(t, TT_HTTP_SRV_3); |
| 4331 | // if (t->srv) |
| 4332 | // t->srv->failed_conns++; |
| 4333 | // t->be->failed_conns++; |
| 4334 | // return 0; |
| 4335 | // } |
| 4336 | // } |
| 4337 | // |
| 4338 | // do { |
| 4339 | // if (srv_redispatch_connect(t) != 0) |
| 4340 | // return 0; |
| 4341 | // |
| 4342 | // if (t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 4343 | // /* Server supporting redirection and it is possible. |
| 4344 | // * Invalid requests are reported as such. It concerns all |
| 4345 | // * the largest ones. |
| 4346 | // */ |
| 4347 | // struct chunk rdr; |
| 4348 | // char *path; |
| 4349 | // int len; |
| 4350 | // |
| 4351 | // /* 1: create the response header */ |
| 4352 | // rdr.len = strlen(HTTP_302); |
| 4353 | // rdr.str = trash; |
| 4354 | // memcpy(rdr.str, HTTP_302, rdr.len); |
| 4355 | // |
| 4356 | // /* 2: add the server's prefix */ |
| 4357 | // if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 4358 | // goto cancel_redir; |
| 4359 | // |
| 4360 | // memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 4361 | // rdr.len += t->srv->rdr_len; |
| 4362 | // |
| 4363 | // /* 3: add the request URI */ |
| 4364 | // path = http_get_path(txn); |
| 4365 | // if (!path) |
| 4366 | // goto cancel_redir; |
| 4367 | // len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 4368 | // if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 4369 | // goto cancel_redir; |
| 4370 | // |
| 4371 | // memcpy(rdr.str + rdr.len, path, len); |
| 4372 | // rdr.len += len; |
| 4373 | // memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 4374 | // rdr.len += 4; |
| 4375 | // |
| 4376 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 4377 | // trace_term(t, TT_HTTP_SRV_3); |
| 4378 | // |
| 4379 | // /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4380 | // if (t->srv) |
| 4381 | // t->srv->cum_sess++; |
| 4382 | // return 0; |
| 4383 | // cancel_redir: |
| 4384 | // txn->status = 400; |
| 4385 | // t->fe->failed_req++; |
| 4386 | // srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 4387 | // 400, error_message(t, HTTP_ERR_400)); |
| 4388 | // trace_term(t, TT_HTTP_SRV_4); |
| 4389 | // return 0; |
| 4390 | // } |
| 4391 | // |
| 4392 | // /* try to (re-)connect to the server, and fail if we expire the |
| 4393 | // * number of retries. |
| 4394 | // */ |
| 4395 | // if (srv_retryable_connect(t)) { |
| 4396 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4397 | // if (!(req->cons.flags & BC_KNOWN)) |
| 4398 | // return 0; |
| 4399 | // /* We got an FD */ |
| 4400 | // return 1; |
| 4401 | // } |
| 4402 | // } while (1); |
| 4403 | //} |
| 4404 | // |
| 4405 | // |
| 4406 | ///* Return 1 if the pending connection has failed and should be retried, |
| 4407 | // * otherwise zero. |
| 4408 | // */ |
| 4409 | //int tcp_connection_failed(struct session *t) |
| 4410 | //{ |
| 4411 | // struct buffer *req = t->req; |
| 4412 | // struct buffer *rep = t->rep; |
| 4413 | // int conn_err; |
| 4414 | // |
| 4415 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4416 | // now_ms, __FUNCTION__, |
| 4417 | // cli_stnames[t->cli_state], |
| 4418 | // rep->rex, req->wex, |
| 4419 | // req->flags, rep->flags, |
| 4420 | // req->l, rep->l); |
| 4421 | // |
| 4422 | // if ((rep->flags & BF_SHUTW) || |
| 4423 | // ((req->flags & BF_SHUTR) && |
| 4424 | // ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
| 4425 | // t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 4426 | // req->wex = TICK_ETERNITY; |
| 4427 | // if (!(t->flags & SN_CONN_TAR)) { |
| 4428 | // /* if we are in turn-around, we have already closed the FD */ |
| 4429 | // fd_delete(req->cons->fd); |
| 4430 | // req->cons->state = SI_ST_CLO; |
| 4431 | // if (t->srv) { |
| 4432 | // t->srv->cur_sess--; |
| 4433 | // sess_change_server(t, NULL); |
| 4434 | // } |
| 4435 | // } |
| 4436 | // |
| 4437 | // /* note that this must not return any error because it would be able to |
| 4438 | // * overwrite the client_retnclose() output. |
| 4439 | // */ |
| 4440 | // srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 4441 | // trace_term(t, TT_HTTP_SRV_5); |
| 4442 | // return 0; |
| 4443 | // } |
| 4444 | // |
| 4445 | // if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) |
| 4446 | // return 0; /* nothing changed */ |
| 4447 | // |
| 4448 | // if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
| 4449 | // /* timeout, asynchronous connect error or first write error */ |
| 4450 | // if (t->flags & SN_CONN_TAR) { |
| 4451 | // /* We are doing a turn-around waiting for a new connection attempt. */ |
| 4452 | // if (!(req->flags & BF_WRITE_TIMEOUT)) |
| 4453 | // return 0; |
| 4454 | // t->flags &= ~SN_CONN_TAR; |
| 4455 | // } |
| 4456 | // else { |
| 4457 | // fd_delete(req->cons->fd); |
| 4458 | // req->cons->state = SI_ST_CLO; |
| 4459 | // if (t->srv) { |
| 4460 | // t->srv->cur_sess--; |
| 4461 | // sess_change_server(t, NULL); |
| 4462 | // } |
| 4463 | // |
| 4464 | // if (!(req->flags & BF_WRITE_STATUS)) |
| 4465 | // conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4466 | // else |
| 4467 | // conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 4468 | // |
| 4469 | // /* ensure that we have enough retries left */ |
| 4470 | // if (srv_count_retry_down(t, conn_err)) |
| 4471 | // return 0; |
| 4472 | // |
| 4473 | // if (req->flags & BF_WRITE_ERROR) { |
| 4474 | // /* we encountered an immediate connection error, and we |
| 4475 | // * will have to retry connecting to the same server, most |
| 4476 | // * likely leading to the same result. To avoid this, we |
| 4477 | // * fake a connection timeout to retry after a turn-around |
| 4478 | // * time of 1 second. We will wait in the previous if block. |
| 4479 | // */ |
| 4480 | // t->flags |= SN_CONN_TAR; |
| 4481 | // req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 4482 | // return 0; |
| 4483 | // } |
| 4484 | // } |
| 4485 | // |
| 4486 | // if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 4487 | // /* We're on our last chance, and the REDISP option was specified. |
| 4488 | // * We will ignore cookie and force to balance or use the dispatcher. |
| 4489 | // */ |
| 4490 | // /* let's try to offer this slot to anybody */ |
| 4491 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4492 | // process_srv_queue(t->srv); |
| 4493 | // |
| 4494 | // /* it's left to the dispatcher to choose a server */ |
| 4495 | // t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 4496 | // t->prev_srv = t->srv; |
| 4497 | // |
| 4498 | // /* first, get a connection */ |
| 4499 | // if (srv_redispatch_connect(t)) { |
| 4500 | // if (req->cons.flags & BC_KNOWN) |
| 4501 | // return 0; |
| 4502 | // /* we need to get a connection */ |
| 4503 | // return 1; |
| 4504 | // } |
| 4505 | // } else { |
| 4506 | // if (t->srv) |
| 4507 | // t->srv->retries++; |
| 4508 | // t->be->retries++; |
| 4509 | // } |
| 4510 | // |
| 4511 | // do { |
| 4512 | // /* Now we will try to either reconnect to the same server or |
| 4513 | // * connect to another server. If the connection gets queued |
| 4514 | // * because all servers are saturated, then we will go back to |
| 4515 | // * the idle state where the buffer's consumer is marked as |
| 4516 | // * unknown. |
| 4517 | // */ |
| 4518 | // if (srv_retryable_connect(t)) { |
| 4519 | // t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4520 | // if (req->cons.flags & BC_KNOWN) |
| 4521 | // return 0; |
| 4522 | // /* we did not get a connection */ |
| 4523 | // return 1; |
| 4524 | // } |
| 4525 | // |
| 4526 | // /* we need to redispatch the connection to another server */ |
| 4527 | // if (srv_redispatch_connect(t)) { |
| 4528 | // if (req->cons.flags & BC_KNOWN) |
| 4529 | // return 0; |
| 4530 | // /* we need to get a connection */ |
| 4531 | // return 1; |
| 4532 | // } |
| 4533 | // } while (1); |
| 4534 | // } |
| 4535 | // else { /* no error and write OK */ |
| 4536 | // t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 4537 | // |
| 4538 | // if (req->flags & BF_EMPTY) { |
| 4539 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4540 | // req->wex = TICK_ETERNITY; |
| 4541 | // } else { |
| 4542 | // EV_FD_SET(req->cons->fd, DIR_WR); |
| 4543 | // req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4544 | // if (tick_isset(req->wex)) { |
| 4545 | // /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4546 | // * we refresh it. */ |
| 4547 | // rep->rex = req->wex; |
| 4548 | // } |
| 4549 | // } |
| 4550 | // |
| 4551 | // if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4552 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4553 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4554 | // buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 4555 | // |
| 4556 | // /* if the user wants to log as soon as possible, without counting |
| 4557 | // bytes from the server, then this is the right moment. */ |
| 4558 | // if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4559 | // t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4560 | // tcp_sess_log(t); |
| 4561 | // } |
| 4562 | //#ifdef CONFIG_HAP_TCPSPLICE |
| 4563 | // if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 4564 | // /* TCP splicing supported by both FE and BE */ |
| 4565 | // tcp_splice_splicefd(t->cli_fd, req->cons->fd, 0); |
| 4566 | // } |
| 4567 | //#endif |
| 4568 | // } |
| 4569 | // else { |
| 4570 | // rep->analysers |= AN_RTR_HTTP_HDR; |
| 4571 | // buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 4572 | // t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 4573 | // /* reset hdr_idx which was already initialized by the request. |
| 4574 | // * right now, the http parser does it. |
| 4575 | // * hdr_idx_init(&t->txn.hdr_idx); |
| 4576 | // */ |
| 4577 | // } |
| 4578 | // |
| 4579 | // req->flags |= BF_CONNECTED; |
| 4580 | // if (!rep->analysers) |
| 4581 | // t->rep->flags |= BF_MAY_FORWARD; |
| 4582 | // req->wex = TICK_ETERNITY; |
| 4583 | // return 0; |
| 4584 | // } |
| 4585 | //} |
| 4586 | // |
| 4587 | // |
| 4588 | ///* |
| 4589 | // * Tries to establish a connection to the server and associate it to the |
| 4590 | // * request buffer's consumer side. It normally returns zero, but may return 1 |
| 4591 | // * if it absolutely wants to be called again. |
| 4592 | // */ |
| 4593 | //int process_srv_conn(struct session *t) |
| 4594 | //{ |
| 4595 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4596 | // now_ms, __FUNCTION__, |
| 4597 | // cli_stnames[t->cli_state], |
| 4598 | // t->rep->rex, t->req->wex, |
| 4599 | // t->req->flags, t->rep->flags, |
| 4600 | // t->req->l, t->rep->l); |
| 4601 | // |
| 4602 | // while (!(t->req->flags & BF_CONNECTED)) { |
| 4603 | // if (!(t->req->cons.flags & BC_KNOWN)) { |
| 4604 | // /* no connection in progress, get a new one */ |
| 4605 | // if (!tcp_get_connection(t)) |
| 4606 | // break; |
| 4607 | // } else { |
| 4608 | // /* connection in progress or just completed */ |
| 4609 | // if (!tcp_connection_failed(t)) |
| 4610 | // break; |
| 4611 | // } |
| 4612 | // } |
| 4613 | // return 0; |
| 4614 | //} |
| 4615 | // |
| 4616 | // |
| 4617 | ///* |
| 4618 | // * Manages the server FSM and its socket during the DATA phase. It must not |
| 4619 | // * be called when a file descriptor is not attached to the buffer. It normally |
| 4620 | // * returns zero, but may return 1 if it absolutely wants to be called again. |
| 4621 | // */ |
| 4622 | //int process_srv_data(struct session *t) |
| 4623 | //{ |
| 4624 | // struct buffer *req = t->req; |
| 4625 | // struct buffer *rep = t->rep; |
| 4626 | // |
| 4627 | // DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 4628 | // now_ms, __FUNCTION__, |
| 4629 | // cli_stnames[t->cli_state], |
| 4630 | // rep->rex, req->wex, |
| 4631 | // req->flags, rep->flags, |
| 4632 | // req->l, rep->l); |
| 4633 | // |
| 4634 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 4635 | // if (!((req->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR)) || |
| 4636 | // (!(req->flags & BF_SHUTW) && |
| 4637 | // (req->flags & (BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD)) || |
| 4638 | // (rep->flags & (BF_READ_TIMEOUT|BF_READ_ERROR)) || |
| 4639 | // (!(rep->flags & BF_SHUTR) && rep->flags & (BF_READ_NULL|BF_SHUTW)))) |
| 4640 | // goto update_timeouts; |
| 4641 | // |
| 4642 | // /* read or write error */ |
| 4643 | // /* FIXME: what happens when we have to deal with HTTP ??? */ |
| 4644 | // if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
| 4645 | // buffer_shutr(rep); |
| 4646 | // buffer_shutw(req); |
| 4647 | // fd_delete(req->cons->fd); |
| 4648 | // req->cons->state = SI_ST_CLO; |
| 4649 | // if (t->srv) { |
| 4650 | // t->srv->cur_sess--; |
| 4651 | // t->srv->failed_resp++; |
| 4652 | // sess_change_server(t, NULL); |
| 4653 | // } |
| 4654 | // t->be->failed_resp++; |
| 4655 | // trace_term(t, TT_HTTP_SRV_6); |
| 4656 | // if (!rep->analysers) { |
| 4657 | // if (!(t->flags & SN_ERR_MASK)) |
| 4658 | // t->flags |= SN_ERR_SRVCL; |
| 4659 | // if (!(t->flags & SN_FINST_MASK)) |
| 4660 | // t->flags |= SN_FINST_D; |
| 4661 | // } |
| 4662 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4663 | // process_srv_queue(t->srv); |
| 4664 | // |
| 4665 | // return 0; |
| 4666 | // } |
| 4667 | // |
| 4668 | // /* last read, or end of client write */ |
| 4669 | // if (!(rep->flags & BF_SHUTR) && /* not already done */ |
| 4670 | // rep->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 4671 | // buffer_shutr(rep); |
| 4672 | // if (!(req->flags & BF_SHUTW)) { |
| 4673 | // EV_FD_CLR(req->cons->fd, DIR_RD); |
| 4674 | // trace_term(t, TT_HTTP_SRV_7); |
| 4675 | // } else { |
| 4676 | // /* output was already closed */ |
| 4677 | // fd_delete(req->cons->fd); |
| 4678 | // req->cons->state = SI_ST_CLO; |
| 4679 | // if (t->srv) { |
| 4680 | // t->srv->cur_sess--; |
| 4681 | // sess_change_server(t, NULL); |
| 4682 | // } |
| 4683 | // trace_term(t, TT_HTTP_SRV_8); |
| 4684 | // |
| 4685 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4686 | // process_srv_queue(t->srv); |
| 4687 | // return 0; |
| 4688 | // } |
| 4689 | // } |
| 4690 | // /* end of client read and no more data to send. We can forward |
| 4691 | // * the close when we're allowed to forward data (anytime right |
| 4692 | // * now). If we're using option forceclose, then we may also |
| 4693 | // * shutdown the outgoing write channel once the response starts |
| 4694 | // * coming from the server. |
| 4695 | // */ |
| 4696 | // if (!(req->flags & BF_SHUTW) && /* not already done */ |
| 4697 | // req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD && |
| 4698 | // (req->flags & BF_SHUTR || |
| 4699 | // (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) { |
| 4700 | // buffer_shutw(req); |
| 4701 | // if (!(rep->flags & BF_SHUTR)) { |
| 4702 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4703 | // shutdown(req->cons->fd, SHUT_WR); |
| 4704 | // trace_term(t, TT_HTTP_SRV_9); |
| 4705 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4706 | // /* FIXME: is this still true ? */ |
| 4707 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4708 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4709 | // } else { |
| 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_10); |
| 4717 | // |
| 4718 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4719 | // process_srv_queue(t->srv); |
| 4720 | // return 0; |
| 4721 | // } |
| 4722 | // } |
| 4723 | // |
| 4724 | // /* read timeout */ |
| 4725 | // if ((rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 4726 | // if (!rep->analysers) { |
| 4727 | // if (!(t->flags & SN_ERR_MASK)) |
| 4728 | // t->flags |= SN_ERR_SRVTO; |
| 4729 | // if (!(t->flags & SN_FINST_MASK)) |
| 4730 | // t->flags |= SN_FINST_D; |
| 4731 | // } |
| 4732 | // buffer_shutr(rep); |
| 4733 | // if (!(req->flags & BF_SHUTW)) { |
| 4734 | // EV_FD_CLR(req->cons->fd, DIR_RD); |
| 4735 | // trace_term(t, TT_HTTP_SRV_11); |
| 4736 | // } else { |
| 4737 | // fd_delete(req->cons->fd); |
| 4738 | // req->cons->state = SI_ST_CLO; |
| 4739 | // if (t->srv) { |
| 4740 | // t->srv->cur_sess--; |
| 4741 | // sess_change_server(t, NULL); |
| 4742 | // } |
| 4743 | // trace_term(t, TT_HTTP_SRV_12); |
| 4744 | // |
| 4745 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4746 | // process_srv_queue(t->srv); |
| 4747 | // return 0; |
| 4748 | // } |
| 4749 | // } |
| 4750 | // |
| 4751 | // /* write timeout */ |
| 4752 | // if ((req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 4753 | // if (!rep->analysers) { |
| 4754 | // if (!(t->flags & SN_ERR_MASK)) |
| 4755 | // t->flags |= SN_ERR_SRVTO; |
| 4756 | // if (!(t->flags & SN_FINST_MASK)) |
| 4757 | // t->flags |= SN_FINST_D; |
| 4758 | // } |
| 4759 | // buffer_shutw(req); |
| 4760 | // if (!(rep->flags & BF_SHUTR)) { |
| 4761 | // EV_FD_CLR(req->cons->fd, DIR_WR); |
| 4762 | // shutdown(req->cons->fd, SHUT_WR); |
| 4763 | // /* We must ensure that the read part is still alive when switching to shutw */ |
| 4764 | // /* FIXME: is this still needed ? */ |
| 4765 | // EV_FD_SET(req->cons->fd, DIR_RD); |
| 4766 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4767 | // trace_term(t, TT_HTTP_SRV_13); |
| 4768 | // } else { |
| 4769 | // fd_delete(req->cons->fd); |
| 4770 | // req->cons->state = SI_ST_CLO; |
| 4771 | // if (t->srv) { |
| 4772 | // t->srv->cur_sess--; |
| 4773 | // sess_change_server(t, NULL); |
| 4774 | // } |
| 4775 | // trace_term(t, TT_HTTP_SRV_14); |
| 4776 | // |
| 4777 | // if (may_dequeue_tasks(t->srv, t->be)) |
| 4778 | // process_srv_queue(t->srv); |
| 4779 | // return 0; |
| 4780 | // } |
| 4781 | // } |
| 4782 | // |
| 4783 | // update_timeouts: |
| 4784 | // /* manage read timeout */ |
| 4785 | // if (!(rep->flags & BF_SHUTR)) { |
| 4786 | // if (rep->flags & BF_FULL) { |
| 4787 | // if (EV_FD_COND_C(req->cons->fd, DIR_RD)) |
| 4788 | // rep->rex = TICK_ETERNITY; |
| 4789 | // } else { |
| 4790 | // EV_FD_COND_S(req->cons->fd, DIR_RD); |
| 4791 | // rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4792 | // } |
| 4793 | // } |
| 4794 | // |
| 4795 | // /* manage write timeout */ |
| 4796 | // if (!(req->flags & BF_SHUTW)) { |
| 4797 | // if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) { |
| 4798 | // /* stop writing */ |
| 4799 | // if (EV_FD_COND_C(req->cons->fd, DIR_WR)) |
| 4800 | // req->wex = TICK_ETERNITY; |
| 4801 | // } else { |
| 4802 | // /* buffer not empty, there are still data to be transferred */ |
| 4803 | // EV_FD_COND_S(req->cons->fd, DIR_WR); |
| 4804 | // if (!tick_isset(req->wex)) { |
| 4805 | // /* restart writing */ |
| 4806 | // req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 4807 | // if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) { |
| 4808 | // /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 4809 | // * we refresh it, except if it was already infinite. |
| 4810 | // */ |
| 4811 | // rep->rex = req->wex; |
| 4812 | // } |
| 4813 | // } |
| 4814 | // } |
| 4815 | // } |
| 4816 | // return 0; /* other cases change nothing */ |
| 4817 | //} |
| 4818 | // |
| 4819 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4820 | /* |
| 4821 | * 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] | 4822 | * called with client socket shut down on input. Right now, only statistics can |
| 4823 | * 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] | 4824 | * 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] | 4825 | * 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] | 4826 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4827 | */ |
| 4828 | int produce_content(struct session *s) |
| 4829 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4830 | if (s->data_source == DATA_SRC_NONE) { |
| 4831 | s->flags &= ~SN_SELF_GEN; |
| 4832 | return 1; |
| 4833 | } |
| 4834 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4835 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4836 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4837 | if (ret >= 0) |
| 4838 | return ret; |
| 4839 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4840 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4841 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4842 | /* unknown data source or internal error */ |
| 4843 | s->txn.status = 500; |
| 4844 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4845 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4846 | if (!(s->flags & SN_ERR_MASK)) |
| 4847 | s->flags |= SN_ERR_PRXCOND; |
| 4848 | if (!(s->flags & SN_FINST_MASK)) |
| 4849 | s->flags |= SN_FINST_R; |
| 4850 | s->flags &= ~SN_SELF_GEN; |
| 4851 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4855 | /* Iterate the same filter through all request headers. |
| 4856 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4857 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4858 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4859 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4860 | 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] | 4861 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4862 | char term; |
| 4863 | char *cur_ptr, *cur_end, *cur_next; |
| 4864 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4865 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4866 | struct hdr_idx_elem *cur_hdr; |
| 4867 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4868 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4869 | last_hdr = 0; |
| 4870 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4871 | 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] | 4872 | old_idx = 0; |
| 4873 | |
| 4874 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4875 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4876 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4877 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4878 | (exp->action == ACT_ALLOW || |
| 4879 | exp->action == ACT_DENY || |
| 4880 | exp->action == ACT_TARPIT)) |
| 4881 | return 0; |
| 4882 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4883 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4884 | if (!cur_idx) |
| 4885 | break; |
| 4886 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4887 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4888 | cur_ptr = cur_next; |
| 4889 | cur_end = cur_ptr + cur_hdr->len; |
| 4890 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4891 | |
| 4892 | /* Now we have one header between cur_ptr and cur_end, |
| 4893 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4894 | */ |
| 4895 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4896 | /* The annoying part is that pattern matching needs |
| 4897 | * that we modify the contents to null-terminate all |
| 4898 | * strings before testing them. |
| 4899 | */ |
| 4900 | |
| 4901 | term = *cur_end; |
| 4902 | *cur_end = '\0'; |
| 4903 | |
| 4904 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4905 | switch (exp->action) { |
| 4906 | case ACT_SETBE: |
| 4907 | /* It is not possible to jump a second time. |
| 4908 | * FIXME: should we return an HTTP/500 here so that |
| 4909 | * the admin knows there's a problem ? |
| 4910 | */ |
| 4911 | if (t->be != t->fe) |
| 4912 | break; |
| 4913 | |
| 4914 | /* Swithing Proxy */ |
| 4915 | t->be = (struct proxy *) exp->replace; |
| 4916 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4917 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4918 | * because we have associated req_cap and rsp_cap to the |
| 4919 | * frontend, and the beconn will be updated later. |
| 4920 | */ |
| 4921 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4922 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4923 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4924 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4925 | last_hdr = 1; |
| 4926 | break; |
| 4927 | |
| 4928 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4929 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4930 | last_hdr = 1; |
| 4931 | break; |
| 4932 | |
| 4933 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4934 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4935 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4936 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4937 | break; |
| 4938 | |
| 4939 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4940 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4941 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4942 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4943 | break; |
| 4944 | |
| 4945 | case ACT_REPLACE: |
| 4946 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4947 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4948 | /* FIXME: if the user adds a newline in the replacement, the |
| 4949 | * index will not be recalculated for now, and the new line |
| 4950 | * will not be counted as a new header. |
| 4951 | */ |
| 4952 | |
| 4953 | cur_end += delta; |
| 4954 | cur_next += delta; |
| 4955 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4956 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4957 | break; |
| 4958 | |
| 4959 | case ACT_REMOVE: |
| 4960 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4961 | cur_next += delta; |
| 4962 | |
| 4963 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4964 | txn->req.eoh += delta; |
| 4965 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4966 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4967 | cur_hdr->len = 0; |
| 4968 | cur_end = NULL; /* null-term has been rewritten */ |
| 4969 | break; |
| 4970 | |
| 4971 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4972 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4973 | if (cur_end) |
| 4974 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4975 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4976 | /* keep the link from this header to next one in case of later |
| 4977 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4978 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4979 | old_idx = cur_idx; |
| 4980 | } |
| 4981 | return 0; |
| 4982 | } |
| 4983 | |
| 4984 | |
| 4985 | /* Apply the filter to the request line. |
| 4986 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4987 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4988 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4989 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4990 | */ |
| 4991 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4992 | { |
| 4993 | char term; |
| 4994 | char *cur_ptr, *cur_end; |
| 4995 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4996 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4997 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4998 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4999 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5000 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5001 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5002 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5003 | (exp->action == ACT_ALLOW || |
| 5004 | exp->action == ACT_DENY || |
| 5005 | exp->action == ACT_TARPIT)) |
| 5006 | return 0; |
| 5007 | else if (exp->action == ACT_REMOVE) |
| 5008 | return 0; |
| 5009 | |
| 5010 | done = 0; |
| 5011 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5012 | 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] | 5013 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5014 | |
| 5015 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5016 | |
| 5017 | /* The annoying part is that pattern matching needs |
| 5018 | * that we modify the contents to null-terminate all |
| 5019 | * strings before testing them. |
| 5020 | */ |
| 5021 | |
| 5022 | term = *cur_end; |
| 5023 | *cur_end = '\0'; |
| 5024 | |
| 5025 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5026 | switch (exp->action) { |
| 5027 | case ACT_SETBE: |
| 5028 | /* It is not possible to jump a second time. |
| 5029 | * FIXME: should we return an HTTP/500 here so that |
| 5030 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5031 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5032 | if (t->be != t->fe) |
| 5033 | break; |
| 5034 | |
| 5035 | /* Swithing Proxy */ |
| 5036 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5037 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5038 | /* right now, the backend switch is not too much complicated |
| 5039 | * because we have associated req_cap and rsp_cap to the |
| 5040 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5041 | */ |
| 5042 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 5043 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 5044 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 5045 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5046 | done = 1; |
| 5047 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5049 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5050 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5051 | done = 1; |
| 5052 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5053 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5054 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5055 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5056 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5057 | done = 1; |
| 5058 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5059 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5060 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5061 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5062 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5063 | done = 1; |
| 5064 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5065 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5066 | case ACT_REPLACE: |
| 5067 | *cur_end = term; /* restore the string terminator */ |
| 5068 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5069 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5070 | /* FIXME: if the user adds a newline in the replacement, the |
| 5071 | * index will not be recalculated for now, and the new line |
| 5072 | * will not be counted as a new header. |
| 5073 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5074 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5075 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5076 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5077 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5078 | 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] | 5079 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5080 | HTTP_MSG_RQMETH, |
| 5081 | cur_ptr, cur_end + 1, |
| 5082 | NULL, NULL); |
| 5083 | if (unlikely(!cur_end)) |
| 5084 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5086 | /* we have a full request and we know that we have either a CR |
| 5087 | * or an LF at <ptr>. |
| 5088 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5089 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5090 | 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] | 5091 | /* there is no point trying this regex on headers */ |
| 5092 | return 1; |
| 5093 | } |
| 5094 | } |
| 5095 | *cur_end = term; /* restore the string terminator */ |
| 5096 | return done; |
| 5097 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5098 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5099 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5100 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5101 | /* |
| 5102 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5103 | * 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] | 5104 | * unparsable request. Since it can manage the switch to another backend, it |
| 5105 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5106 | */ |
| 5107 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5108 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5109 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5110 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5111 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5112 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5114 | /* |
| 5115 | * The interleaving of transformations and verdicts |
| 5116 | * makes it difficult to decide to continue or stop |
| 5117 | * the evaluation. |
| 5118 | */ |
| 5119 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5120 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5121 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5122 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5123 | exp = exp->next; |
| 5124 | continue; |
| 5125 | } |
| 5126 | |
| 5127 | /* Apply the filter to the request line. */ |
| 5128 | ret = apply_filter_to_req_line(t, req, exp); |
| 5129 | if (unlikely(ret < 0)) |
| 5130 | return -1; |
| 5131 | |
| 5132 | if (likely(ret == 0)) { |
| 5133 | /* The filter did not match the request, it can be |
| 5134 | * iterated through all headers. |
| 5135 | */ |
| 5136 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5137 | } |
| 5138 | exp = exp->next; |
| 5139 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5140 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5144 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5145 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5146 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5147 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5148 | */ |
| 5149 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5150 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5151 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5152 | char *p1, *p2, *p3, *p4; |
| 5153 | char *del_colon, *del_cookie, *colon; |
| 5154 | int app_cookies; |
| 5155 | |
| 5156 | appsess *asession_temp = NULL; |
| 5157 | appsess local_asession; |
| 5158 | |
| 5159 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5160 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5161 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5162 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5163 | * we start with the start line. |
| 5164 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5165 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5166 | 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] | 5167 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5168 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5169 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5170 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5171 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5172 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5173 | cur_ptr = cur_next; |
| 5174 | cur_end = cur_ptr + cur_hdr->len; |
| 5175 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5176 | |
| 5177 | /* We have one full header between cur_ptr and cur_end, and the |
| 5178 | * next header starts at cur_next. We're only interested in |
| 5179 | * "Cookie:" headers. |
| 5180 | */ |
| 5181 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5182 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5183 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5184 | old_idx = cur_idx; |
| 5185 | continue; |
| 5186 | } |
| 5187 | |
| 5188 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5189 | * attributes whose name begin with a '$', and associate them with |
| 5190 | * the right cookie, if we want to delete this cookie. |
| 5191 | * So there are 3 cases for each cookie read : |
| 5192 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5193 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5194 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5195 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5196 | * "special" cookie. |
| 5197 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5198 | * remove it. If no application cookie persists in the header, we |
| 5199 | * *MUST* delete it |
| 5200 | */ |
| 5201 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5202 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5203 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5204 | /* del_cookie == NULL => nothing to be deleted */ |
| 5205 | del_colon = del_cookie = NULL; |
| 5206 | app_cookies = 0; |
| 5207 | |
| 5208 | while (p1 < cur_end) { |
| 5209 | /* skip spaces and colons, but keep an eye on these ones */ |
| 5210 | while (p1 < cur_end) { |
| 5211 | if (*p1 == ';' || *p1 == ',') |
| 5212 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5213 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5214 | break; |
| 5215 | p1++; |
| 5216 | } |
| 5217 | |
| 5218 | if (p1 == cur_end) |
| 5219 | break; |
| 5220 | |
| 5221 | /* p1 is at the beginning of the cookie name */ |
| 5222 | p2 = p1; |
| 5223 | while (p2 < cur_end && *p2 != '=') |
| 5224 | p2++; |
| 5225 | |
| 5226 | if (p2 == cur_end) |
| 5227 | break; |
| 5228 | |
| 5229 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5230 | if (p3 == cur_end) |
| 5231 | break; |
| 5232 | |
| 5233 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5234 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5235 | p4++; |
| 5236 | |
| 5237 | /* here, we have the cookie name between p1 and p2, |
| 5238 | * and its value between p3 and p4. |
| 5239 | * we can process it : |
| 5240 | * |
| 5241 | * Cookie: NAME=VALUE; |
| 5242 | * | || || | |
| 5243 | * | || || +--> p4 |
| 5244 | * | || |+-------> p3 |
| 5245 | * | || +--------> p2 |
| 5246 | * | |+------------> p1 |
| 5247 | * | +-------------> colon |
| 5248 | * +--------------------> cur_ptr |
| 5249 | */ |
| 5250 | |
| 5251 | if (*p1 == '$') { |
| 5252 | /* skip this one */ |
| 5253 | } |
| 5254 | else { |
| 5255 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5256 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5257 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5258 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5259 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5260 | int log_len = p4 - p1; |
| 5261 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5262 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5263 | Alert("HTTP logging : out of memory.\n"); |
| 5264 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5265 | if (log_len > t->fe->capture_len) |
| 5266 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5267 | memcpy(txn->cli_cookie, p1, log_len); |
| 5268 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5269 | } |
| 5270 | } |
| 5271 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5272 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5273 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5274 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5275 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5276 | char *delim; |
| 5277 | |
| 5278 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5279 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5280 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5281 | * |
| 5282 | * Cookie: NAME=SRV~VALUE; |
| 5283 | * | || || | | |
| 5284 | * | || || | +--> p4 |
| 5285 | * | || || +--------> delim |
| 5286 | * | || |+-----------> p3 |
| 5287 | * | || +------------> p2 |
| 5288 | * | |+----------------> p1 |
| 5289 | * | +-----------------> colon |
| 5290 | * +------------------------> cur_ptr |
| 5291 | */ |
| 5292 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5293 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5294 | for (delim = p3; delim < p4; delim++) |
| 5295 | if (*delim == COOKIE_DELIM) |
| 5296 | break; |
| 5297 | } |
| 5298 | else |
| 5299 | delim = p4; |
| 5300 | |
| 5301 | |
| 5302 | /* Here, we'll look for the first running server which supports the cookie. |
| 5303 | * This allows to share a same cookie between several servers, for example |
| 5304 | * to dedicate backup servers to specific servers only. |
| 5305 | * However, to prevent clients from sticking to cookie-less backup server |
| 5306 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5307 | * empty cookies and mark them as invalid. |
| 5308 | */ |
| 5309 | if (delim == p3) |
| 5310 | srv = NULL; |
| 5311 | |
| 5312 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5313 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5314 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5315 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5316 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5317 | txn->flags &= ~TX_CK_MASK; |
| 5318 | txn->flags |= TX_CK_VALID; |
| 5319 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5320 | t->srv = srv; |
| 5321 | break; |
| 5322 | } else { |
| 5323 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5324 | txn->flags &= ~TX_CK_MASK; |
| 5325 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | srv = srv->next; |
| 5329 | } |
| 5330 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5331 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5332 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5333 | txn->flags &= ~TX_CK_MASK; |
| 5334 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | /* depending on the cookie mode, we may have to either : |
| 5338 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5339 | * the server never sees it ; |
| 5340 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5341 | * application cookie so that it does not get accidentely removed later, |
| 5342 | * if we're in cookie prefix mode |
| 5343 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5344 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5345 | int delta; /* negative */ |
| 5346 | |
| 5347 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5348 | p4 += delta; |
| 5349 | cur_end += delta; |
| 5350 | cur_next += delta; |
| 5351 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5352 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5353 | |
| 5354 | del_cookie = del_colon = NULL; |
| 5355 | app_cookies++; /* protect the header from deletion */ |
| 5356 | } |
| 5357 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5358 | (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] | 5359 | del_cookie = p1; |
| 5360 | del_colon = colon; |
| 5361 | } |
| 5362 | } else { |
| 5363 | /* now we know that we must keep this cookie since it's |
| 5364 | * not ours. But if we wanted to delete our cookie |
| 5365 | * earlier, we cannot remove the complete header, but we |
| 5366 | * can remove the previous block itself. |
| 5367 | */ |
| 5368 | app_cookies++; |
| 5369 | |
| 5370 | if (del_cookie != NULL) { |
| 5371 | int delta; /* negative */ |
| 5372 | |
| 5373 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5374 | p4 += delta; |
| 5375 | cur_end += delta; |
| 5376 | cur_next += delta; |
| 5377 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5378 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5379 | del_cookie = del_colon = NULL; |
| 5380 | } |
| 5381 | } |
| 5382 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5383 | if ((t->be->appsession_name != NULL) && |
| 5384 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5385 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5386 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5387 | /* Cool... it's the right one */ |
| 5388 | |
| 5389 | asession_temp = &local_asession; |
| 5390 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5391 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5392 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5393 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5394 | return; |
| 5395 | } |
| 5396 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5397 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5398 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5399 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5400 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5401 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5402 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5403 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5404 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5405 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5406 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5407 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5408 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5409 | return; |
| 5410 | } |
| 5411 | |
| 5412 | asession_temp->sessid = local_asession.sessid; |
| 5413 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5414 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5415 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5416 | } else { |
| 5417 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5418 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5419 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5420 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5421 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5422 | Alert("Found Application Session without matching server.\n"); |
| 5423 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5424 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5425 | while (srv) { |
| 5426 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5427 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5428 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5429 | txn->flags &= ~TX_CK_MASK; |
| 5430 | txn->flags |= TX_CK_VALID; |
| 5431 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5432 | t->srv = srv; |
| 5433 | break; |
| 5434 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5435 | txn->flags &= ~TX_CK_MASK; |
| 5436 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5437 | } |
| 5438 | } |
| 5439 | srv = srv->next; |
| 5440 | }/* end while(srv) */ |
| 5441 | }/* end else if server == NULL */ |
| 5442 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5443 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5444 | asession_temp->request_count++; |
| 5445 | #if defined(DEBUG_HASH) |
| 5446 | Alert("manage_client_side_cookies\n"); |
| 5447 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5448 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5449 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5450 | } |
| 5451 | |
| 5452 | /* we'll have to look for another cookie ... */ |
| 5453 | p1 = p4; |
| 5454 | } /* while (p1 < cur_end) */ |
| 5455 | |
| 5456 | /* There's no more cookie on this line. |
| 5457 | * We may have marked the last one(s) for deletion. |
| 5458 | * We must do this now in two ways : |
| 5459 | * - if there is no app cookie, we simply delete the header ; |
| 5460 | * - if there are app cookies, we must delete the end of the |
| 5461 | * string properly, including the colon/semi-colon before |
| 5462 | * the cookie name. |
| 5463 | */ |
| 5464 | if (del_cookie != NULL) { |
| 5465 | int delta; |
| 5466 | if (app_cookies) { |
| 5467 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5468 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5469 | cur_hdr->len += delta; |
| 5470 | } else { |
| 5471 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5472 | |
| 5473 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5474 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5475 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5476 | cur_hdr->len = 0; |
| 5477 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5478 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5479 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* keep the link from this header to next one */ |
| 5483 | old_idx = cur_idx; |
| 5484 | } /* end of cookie processing on this header */ |
| 5485 | } |
| 5486 | |
| 5487 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5488 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5489 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5490 | */ |
| 5491 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5492 | { |
| 5493 | char term; |
| 5494 | char *cur_ptr, *cur_end, *cur_next; |
| 5495 | int cur_idx, old_idx, last_hdr; |
| 5496 | struct http_txn *txn = &t->txn; |
| 5497 | struct hdr_idx_elem *cur_hdr; |
| 5498 | int len, delta; |
| 5499 | |
| 5500 | last_hdr = 0; |
| 5501 | |
| 5502 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5503 | old_idx = 0; |
| 5504 | |
| 5505 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5506 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5507 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5508 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5509 | (exp->action == ACT_ALLOW || |
| 5510 | exp->action == ACT_DENY)) |
| 5511 | return 0; |
| 5512 | |
| 5513 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5514 | if (!cur_idx) |
| 5515 | break; |
| 5516 | |
| 5517 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5518 | cur_ptr = cur_next; |
| 5519 | cur_end = cur_ptr + cur_hdr->len; |
| 5520 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5521 | |
| 5522 | /* Now we have one header between cur_ptr and cur_end, |
| 5523 | * and the next header starts at cur_next. |
| 5524 | */ |
| 5525 | |
| 5526 | /* The annoying part is that pattern matching needs |
| 5527 | * that we modify the contents to null-terminate all |
| 5528 | * strings before testing them. |
| 5529 | */ |
| 5530 | |
| 5531 | term = *cur_end; |
| 5532 | *cur_end = '\0'; |
| 5533 | |
| 5534 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5535 | switch (exp->action) { |
| 5536 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5537 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5538 | last_hdr = 1; |
| 5539 | break; |
| 5540 | |
| 5541 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5542 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5543 | last_hdr = 1; |
| 5544 | break; |
| 5545 | |
| 5546 | case ACT_REPLACE: |
| 5547 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5548 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5549 | /* FIXME: if the user adds a newline in the replacement, the |
| 5550 | * index will not be recalculated for now, and the new line |
| 5551 | * will not be counted as a new header. |
| 5552 | */ |
| 5553 | |
| 5554 | cur_end += delta; |
| 5555 | cur_next += delta; |
| 5556 | cur_hdr->len += delta; |
| 5557 | txn->rsp.eoh += delta; |
| 5558 | break; |
| 5559 | |
| 5560 | case ACT_REMOVE: |
| 5561 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5562 | cur_next += delta; |
| 5563 | |
| 5564 | /* FIXME: this should be a separate function */ |
| 5565 | txn->rsp.eoh += delta; |
| 5566 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5567 | txn->hdr_idx.used--; |
| 5568 | cur_hdr->len = 0; |
| 5569 | cur_end = NULL; /* null-term has been rewritten */ |
| 5570 | break; |
| 5571 | |
| 5572 | } |
| 5573 | } |
| 5574 | if (cur_end) |
| 5575 | *cur_end = term; /* restore the string terminator */ |
| 5576 | |
| 5577 | /* keep the link from this header to next one in case of later |
| 5578 | * removal of next header. |
| 5579 | */ |
| 5580 | old_idx = cur_idx; |
| 5581 | } |
| 5582 | return 0; |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5587 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5588 | * or -1 if a replacement resulted in an invalid status line. |
| 5589 | */ |
| 5590 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5591 | { |
| 5592 | char term; |
| 5593 | char *cur_ptr, *cur_end; |
| 5594 | int done; |
| 5595 | struct http_txn *txn = &t->txn; |
| 5596 | int len, delta; |
| 5597 | |
| 5598 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5599 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5600 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5601 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5602 | (exp->action == ACT_ALLOW || |
| 5603 | exp->action == ACT_DENY)) |
| 5604 | return 0; |
| 5605 | else if (exp->action == ACT_REMOVE) |
| 5606 | return 0; |
| 5607 | |
| 5608 | done = 0; |
| 5609 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5610 | 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] | 5611 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5612 | |
| 5613 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5614 | |
| 5615 | /* The annoying part is that pattern matching needs |
| 5616 | * that we modify the contents to null-terminate all |
| 5617 | * strings before testing them. |
| 5618 | */ |
| 5619 | |
| 5620 | term = *cur_end; |
| 5621 | *cur_end = '\0'; |
| 5622 | |
| 5623 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5624 | switch (exp->action) { |
| 5625 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5626 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5627 | done = 1; |
| 5628 | break; |
| 5629 | |
| 5630 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5631 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5632 | done = 1; |
| 5633 | break; |
| 5634 | |
| 5635 | case ACT_REPLACE: |
| 5636 | *cur_end = term; /* restore the string terminator */ |
| 5637 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5638 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5639 | /* FIXME: if the user adds a newline in the replacement, the |
| 5640 | * index will not be recalculated for now, and the new line |
| 5641 | * will not be counted as a new header. |
| 5642 | */ |
| 5643 | |
| 5644 | txn->rsp.eoh += delta; |
| 5645 | cur_end += delta; |
| 5646 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5647 | 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] | 5648 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5649 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5650 | cur_ptr, cur_end + 1, |
| 5651 | NULL, NULL); |
| 5652 | if (unlikely(!cur_end)) |
| 5653 | return -1; |
| 5654 | |
| 5655 | /* we have a full respnse and we know that we have either a CR |
| 5656 | * or an LF at <ptr>. |
| 5657 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5658 | 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] | 5659 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5660 | /* there is no point trying this regex on headers */ |
| 5661 | return 1; |
| 5662 | } |
| 5663 | } |
| 5664 | *cur_end = term; /* restore the string terminator */ |
| 5665 | return done; |
| 5666 | } |
| 5667 | |
| 5668 | |
| 5669 | |
| 5670 | /* |
| 5671 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5672 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5673 | * unparsable response. |
| 5674 | */ |
| 5675 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5676 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5677 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5678 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5679 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5680 | int ret; |
| 5681 | |
| 5682 | /* |
| 5683 | * The interleaving of transformations and verdicts |
| 5684 | * makes it difficult to decide to continue or stop |
| 5685 | * the evaluation. |
| 5686 | */ |
| 5687 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5688 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5689 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5690 | exp->action == ACT_PASS)) { |
| 5691 | exp = exp->next; |
| 5692 | continue; |
| 5693 | } |
| 5694 | |
| 5695 | /* Apply the filter to the status line. */ |
| 5696 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5697 | if (unlikely(ret < 0)) |
| 5698 | return -1; |
| 5699 | |
| 5700 | if (likely(ret == 0)) { |
| 5701 | /* The filter did not match the response, it can be |
| 5702 | * iterated through all headers. |
| 5703 | */ |
| 5704 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5705 | } |
| 5706 | exp = exp->next; |
| 5707 | } |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
| 5711 | |
| 5712 | |
| 5713 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5714 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5715 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5716 | */ |
| 5717 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5718 | { |
| 5719 | struct http_txn *txn = &t->txn; |
| 5720 | char *p1, *p2, *p3, *p4; |
| 5721 | |
| 5722 | appsess *asession_temp = NULL; |
| 5723 | appsess local_asession; |
| 5724 | |
| 5725 | char *cur_ptr, *cur_end, *cur_next; |
| 5726 | int cur_idx, old_idx, delta; |
| 5727 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5728 | /* Iterate through the headers. |
| 5729 | * we start with the start line. |
| 5730 | */ |
| 5731 | old_idx = 0; |
| 5732 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5733 | |
| 5734 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5735 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5736 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5737 | |
| 5738 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5739 | cur_ptr = cur_next; |
| 5740 | cur_end = cur_ptr + cur_hdr->len; |
| 5741 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5742 | |
| 5743 | /* We have one full header between cur_ptr and cur_end, and the |
| 5744 | * next header starts at cur_next. We're only interested in |
| 5745 | * "Cookie:" headers. |
| 5746 | */ |
| 5747 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5748 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5749 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5750 | old_idx = cur_idx; |
| 5751 | continue; |
| 5752 | } |
| 5753 | |
| 5754 | /* 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] | 5755 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5756 | |
| 5757 | |
| 5758 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5759 | if (t->be->cookie_name == NULL && |
| 5760 | t->be->appsession_name == NULL && |
| 5761 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5762 | return; |
| 5763 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5764 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | |
| 5766 | 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] | 5767 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5768 | break; |
| 5769 | |
| 5770 | /* p1 is at the beginning of the cookie name */ |
| 5771 | p2 = p1; |
| 5772 | |
| 5773 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5774 | p2++; |
| 5775 | |
| 5776 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5777 | break; |
| 5778 | |
| 5779 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5780 | if (p3 == cur_end) |
| 5781 | break; |
| 5782 | |
| 5783 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5784 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5785 | p4++; |
| 5786 | |
| 5787 | /* here, we have the cookie name between p1 and p2, |
| 5788 | * and its value between p3 and p4. |
| 5789 | * we can process it. |
| 5790 | */ |
| 5791 | |
| 5792 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5793 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5794 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5795 | (p4 - p1 >= t->be->capture_namelen) && |
| 5796 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5797 | int log_len = p4 - p1; |
| 5798 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5799 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5800 | Alert("HTTP logging : out of memory.\n"); |
| 5801 | } |
| 5802 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5803 | if (log_len > t->be->capture_len) |
| 5804 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5805 | memcpy(txn->srv_cookie, p1, log_len); |
| 5806 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5810 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5811 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5812 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5813 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5816 | * this occurrence because we'll insert another one later. |
| 5817 | * We'll delete it too if the "indirect" option is set and we're in |
| 5818 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5819 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5820 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5821 | /* this header must be deleted */ |
| 5822 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5823 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5824 | txn->hdr_idx.used--; |
| 5825 | cur_hdr->len = 0; |
| 5826 | cur_next += delta; |
| 5827 | txn->rsp.eoh += delta; |
| 5828 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5829 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5830 | } |
| 5831 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5832 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5833 | /* replace bytes p3->p4 with the cookie name associated |
| 5834 | * with this server since we know it. |
| 5835 | */ |
| 5836 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5837 | cur_hdr->len += delta; |
| 5838 | cur_next += delta; |
| 5839 | txn->rsp.eoh += delta; |
| 5840 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5841 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5842 | } |
| 5843 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5844 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5845 | /* insert the cookie name associated with this server |
| 5846 | * before existing cookie, and insert a delimitor between them.. |
| 5847 | */ |
| 5848 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5849 | cur_hdr->len += delta; |
| 5850 | cur_next += delta; |
| 5851 | txn->rsp.eoh += delta; |
| 5852 | |
| 5853 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5854 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5855 | } |
| 5856 | } |
| 5857 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5858 | else if ((t->be->appsession_name != NULL) && |
| 5859 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* Cool... it's the right one */ |
| 5862 | |
| 5863 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5864 | asession_temp = &local_asession; |
| 5865 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5866 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5867 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5868 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5869 | return; |
| 5870 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5871 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5872 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5873 | asession_temp->serverid = NULL; |
| 5874 | |
| 5875 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5876 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5877 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5878 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5879 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5880 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5881 | return; |
| 5882 | } |
| 5883 | asession_temp->sessid = local_asession.sessid; |
| 5884 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5885 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5886 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5887 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5888 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5889 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5890 | } |
| 5891 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5892 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5893 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5894 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5895 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5896 | return; |
| 5897 | } |
| 5898 | asession_temp->serverid[0] = '\0'; |
| 5899 | } |
| 5900 | |
| 5901 | if (asession_temp->serverid[0] == '\0') |
| 5902 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5903 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5904 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5905 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5906 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5907 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5908 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5909 | #endif |
| 5910 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5911 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5912 | } /* we're now at the end of the cookie value */ |
| 5913 | |
| 5914 | /* keep the link from this header to next one */ |
| 5915 | old_idx = cur_idx; |
| 5916 | } /* end of cookie processing on this header */ |
| 5917 | } |
| 5918 | |
| 5919 | |
| 5920 | |
| 5921 | /* |
| 5922 | * Check if response is cacheable or not. Updates t->flags. |
| 5923 | */ |
| 5924 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5925 | { |
| 5926 | struct http_txn *txn = &t->txn; |
| 5927 | char *p1, *p2; |
| 5928 | |
| 5929 | char *cur_ptr, *cur_end, *cur_next; |
| 5930 | int cur_idx; |
| 5931 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5932 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5933 | return; |
| 5934 | |
| 5935 | /* Iterate through the headers. |
| 5936 | * we start with the start line. |
| 5937 | */ |
| 5938 | cur_idx = 0; |
| 5939 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5940 | |
| 5941 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5942 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5943 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5944 | |
| 5945 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5946 | cur_ptr = cur_next; |
| 5947 | cur_end = cur_ptr + cur_hdr->len; |
| 5948 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5949 | |
| 5950 | /* We have one full header between cur_ptr and cur_end, and the |
| 5951 | * next header starts at cur_next. We're only interested in |
| 5952 | * "Cookie:" headers. |
| 5953 | */ |
| 5954 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5955 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5956 | if (val) { |
| 5957 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5958 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5959 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5960 | return; |
| 5961 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5962 | } |
| 5963 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5964 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5965 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5966 | continue; |
| 5967 | |
| 5968 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5969 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5970 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5971 | |
| 5972 | if (p1 >= cur_end) /* no more info */ |
| 5973 | continue; |
| 5974 | |
| 5975 | /* p1 is at the beginning of the value */ |
| 5976 | p2 = p1; |
| 5977 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5978 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5979 | p2++; |
| 5980 | |
| 5981 | /* we have a complete value between p1 and p2 */ |
| 5982 | if (p2 < cur_end && *p2 == '=') { |
| 5983 | /* we have something of the form no-cache="set-cookie" */ |
| 5984 | if ((cur_end - p1 >= 21) && |
| 5985 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5986 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5987 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5988 | continue; |
| 5989 | } |
| 5990 | |
| 5991 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5992 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5993 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5994 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5995 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5996 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5997 | return; |
| 5998 | } |
| 5999 | |
| 6000 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6001 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6002 | continue; |
| 6003 | } |
| 6004 | } |
| 6005 | } |
| 6006 | |
| 6007 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6008 | /* |
| 6009 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6010 | * If the server is found, it's assigned to the session. |
| 6011 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6012 | 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] | 6013 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6014 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6015 | appsess *asession_temp = NULL; |
| 6016 | appsess local_asession; |
| 6017 | char *request_line; |
| 6018 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6019 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 6020 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6021 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6022 | ((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] | 6023 | return; |
| 6024 | |
| 6025 | /* skip ';' */ |
| 6026 | request_line++; |
| 6027 | |
| 6028 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6029 | 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] | 6030 | return; |
| 6031 | |
| 6032 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6033 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6034 | |
| 6035 | /* First try if we already have an appsession */ |
| 6036 | asession_temp = &local_asession; |
| 6037 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6038 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6039 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 6040 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 6041 | return; |
| 6042 | } |
| 6043 | |
| 6044 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6045 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 6046 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6047 | asession_temp->serverid = NULL; |
| 6048 | |
| 6049 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 6050 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 6051 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6052 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6053 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6054 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6055 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 6056 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 6057 | return; |
| 6058 | } |
| 6059 | asession_temp->sessid = local_asession.sessid; |
| 6060 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6061 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6062 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6063 | } |
| 6064 | else { |
| 6065 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 6066 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6067 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6068 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 6069 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6070 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6071 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6072 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6073 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6074 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6075 | #endif |
| 6076 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6077 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6078 | Alert("Found Application Session without matching server.\n"); |
| 6079 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6080 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6081 | while (srv) { |
| 6082 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6083 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6084 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6085 | txn->flags &= ~TX_CK_MASK; |
| 6086 | txn->flags |= TX_CK_VALID; |
| 6087 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6088 | t->srv = srv; |
| 6089 | break; |
| 6090 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6091 | txn->flags &= ~TX_CK_MASK; |
| 6092 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | srv = srv->next; |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6101 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6102 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6103 | * 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] | 6104 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6105 | * 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] | 6106 | * 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] | 6107 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6108 | * |
| 6109 | * Returns 1 if the session's state changes, otherwise 0. |
| 6110 | */ |
| 6111 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6112 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6113 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6114 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6115 | struct user_auth *user; |
| 6116 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6117 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6118 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6119 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6120 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6121 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6122 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6123 | return 0; |
| 6124 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6125 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6126 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6127 | /* the URI is in h */ |
| 6128 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6129 | return 0; |
| 6130 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6131 | h += uri_auth->uri_len; |
| 6132 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 6133 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6134 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6135 | break; |
| 6136 | } |
| 6137 | h++; |
| 6138 | } |
| 6139 | |
| 6140 | if (uri_auth->refresh) { |
| 6141 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6142 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 6143 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6144 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6145 | break; |
| 6146 | } |
| 6147 | h++; |
| 6148 | } |
| 6149 | } |
| 6150 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6151 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6152 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 6153 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6154 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6155 | break; |
| 6156 | } |
| 6157 | h++; |
| 6158 | } |
| 6159 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6160 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6161 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6162 | /* we are in front of a interceptable URI. Let's check |
| 6163 | * if there's an authentication and if it's valid. |
| 6164 | */ |
| 6165 | user = uri_auth->users; |
| 6166 | if (!user) { |
| 6167 | /* no user auth required, it's OK */ |
| 6168 | authenticated = 1; |
| 6169 | } else { |
| 6170 | authenticated = 0; |
| 6171 | |
| 6172 | /* a user list is defined, we have to check. |
| 6173 | * skip 21 chars for "Authorization: Basic ". |
| 6174 | */ |
| 6175 | |
| 6176 | /* FIXME: this should move to an earlier place */ |
| 6177 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6178 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 6179 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6180 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6181 | if (len > 14 && |
| 6182 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6183 | txn->auth_hdr.str = h; |
| 6184 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6185 | break; |
| 6186 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6187 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6188 | } |
| 6189 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6190 | if (txn->auth_hdr.len < 21 || |
| 6191 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6192 | user = NULL; |
| 6193 | |
| 6194 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6195 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6196 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6197 | user->user_pwd, user->user_len)) { |
| 6198 | authenticated = 1; |
| 6199 | break; |
| 6200 | } |
| 6201 | user = user->next; |
| 6202 | } |
| 6203 | } |
| 6204 | |
| 6205 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6206 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6207 | |
| 6208 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6209 | msg.str = trash; |
| 6210 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6211 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6212 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 6213 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6214 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6215 | if (!(t->flags & SN_ERR_MASK)) |
| 6216 | t->flags |= SN_ERR_PRXCOND; |
| 6217 | if (!(t->flags & SN_FINST_MASK)) |
| 6218 | t->flags |= SN_FINST_R; |
| 6219 | return 1; |
| 6220 | } |
| 6221 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6222 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6223 | * data. |
| 6224 | */ |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 6225 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 6226 | buffer_shutr(t->req); |
| 6227 | buffer_shutr(t->rep); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 6228 | buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6229 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6230 | t->data_source = DATA_SRC_STATS; |
| 6231 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6232 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6233 | produce_content(t); |
| 6234 | return 1; |
| 6235 | } |
| 6236 | |
| 6237 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6238 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6239 | * Print a debug line with a header |
| 6240 | */ |
| 6241 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6242 | { |
| 6243 | int len, max; |
| 6244 | 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] | 6245 | dir, (unsigned short)t->cli_fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6246 | max = end - start; |
| 6247 | UBOUND(max, sizeof(trash) - len - 1); |
| 6248 | len += strlcpy2(trash + len, start, max + 1); |
| 6249 | trash[len++] = '\n'; |
| 6250 | write(1, trash, len); |
| 6251 | } |
| 6252 | |
| 6253 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6254 | /************************************************************************/ |
| 6255 | /* The code below is dedicated to ACL parsing and matching */ |
| 6256 | /************************************************************************/ |
| 6257 | |
| 6258 | |
| 6259 | |
| 6260 | |
| 6261 | /* 1. Check on METHOD |
| 6262 | * We use the pre-parsed method if it is known, and store its number as an |
| 6263 | * integer. If it is unknown, we use the pointer and the length. |
| 6264 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6265 | 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] | 6266 | { |
| 6267 | int len, meth; |
| 6268 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6269 | len = strlen(*text); |
| 6270 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6271 | |
| 6272 | pattern->val.i = meth; |
| 6273 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6274 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6275 | if (!pattern->ptr.str) |
| 6276 | return 0; |
| 6277 | pattern->len = len; |
| 6278 | } |
| 6279 | return 1; |
| 6280 | } |
| 6281 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6282 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6283 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6284 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6285 | { |
| 6286 | int meth; |
| 6287 | struct http_txn *txn = l7; |
| 6288 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6289 | if (!txn) |
| 6290 | return 0; |
| 6291 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6292 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6293 | return 0; |
| 6294 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6295 | meth = txn->meth; |
| 6296 | test->i = meth; |
| 6297 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6298 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6299 | /* ensure the indexes are not affected */ |
| 6300 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6301 | test->len = txn->req.sl.rq.m_l; |
| 6302 | test->ptr = txn->req.sol; |
| 6303 | } |
| 6304 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6305 | return 1; |
| 6306 | } |
| 6307 | |
| 6308 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6309 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6310 | int icase; |
| 6311 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6312 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6313 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6314 | |
| 6315 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6316 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6317 | |
| 6318 | /* Other method, we must compare the strings */ |
| 6319 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6320 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6321 | |
| 6322 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6323 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6324 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6325 | return ACL_PAT_FAIL; |
| 6326 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | /* 2. Check on Request/Status Version |
| 6330 | * We simply compare strings here. |
| 6331 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6332 | 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] | 6333 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6334 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6335 | if (!pattern->ptr.str) |
| 6336 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6337 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6338 | return 1; |
| 6339 | } |
| 6340 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6341 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6342 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6343 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6344 | { |
| 6345 | struct http_txn *txn = l7; |
| 6346 | char *ptr; |
| 6347 | int len; |
| 6348 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6349 | if (!txn) |
| 6350 | return 0; |
| 6351 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6352 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6353 | return 0; |
| 6354 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6355 | len = txn->req.sl.rq.v_l; |
| 6356 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 6357 | |
| 6358 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6359 | if (len <= 0) |
| 6360 | return 0; |
| 6361 | |
| 6362 | test->ptr = ptr; |
| 6363 | test->len = len; |
| 6364 | |
| 6365 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6366 | return 1; |
| 6367 | } |
| 6368 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6369 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6370 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6371 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6372 | { |
| 6373 | struct http_txn *txn = l7; |
| 6374 | char *ptr; |
| 6375 | int len; |
| 6376 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6377 | if (!txn) |
| 6378 | return 0; |
| 6379 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6380 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6381 | return 0; |
| 6382 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6383 | len = txn->rsp.sl.st.v_l; |
| 6384 | ptr = txn->rsp.sol; |
| 6385 | |
| 6386 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6387 | if (len <= 0) |
| 6388 | return 0; |
| 6389 | |
| 6390 | test->ptr = ptr; |
| 6391 | test->len = len; |
| 6392 | |
| 6393 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6394 | return 1; |
| 6395 | } |
| 6396 | |
| 6397 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6398 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6399 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6400 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6401 | { |
| 6402 | struct http_txn *txn = l7; |
| 6403 | char *ptr; |
| 6404 | int len; |
| 6405 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6406 | if (!txn) |
| 6407 | return 0; |
| 6408 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6409 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6410 | return 0; |
| 6411 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6412 | len = txn->rsp.sl.st.c_l; |
| 6413 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 6414 | |
| 6415 | test->i = __strl2ui(ptr, len); |
| 6416 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6417 | return 1; |
| 6418 | } |
| 6419 | |
| 6420 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6421 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6422 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6423 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6424 | { |
| 6425 | struct http_txn *txn = l7; |
| 6426 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6427 | if (!txn) |
| 6428 | return 0; |
| 6429 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6430 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6431 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6432 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6433 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6434 | /* ensure the indexes are not affected */ |
| 6435 | return 0; |
| 6436 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6437 | test->len = txn->req.sl.rq.u_l; |
| 6438 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 6439 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6440 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6441 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6442 | return 1; |
| 6443 | } |
| 6444 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6445 | static int |
| 6446 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6447 | struct acl_expr *expr, struct acl_test *test) |
| 6448 | { |
| 6449 | struct http_txn *txn = l7; |
| 6450 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6451 | if (!txn) |
| 6452 | return 0; |
| 6453 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6454 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6455 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6456 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6457 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6458 | /* ensure the indexes are not affected */ |
| 6459 | return 0; |
| 6460 | |
| 6461 | /* Parse HTTP request */ |
| 6462 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 6463 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6464 | test->i = AF_INET; |
| 6465 | |
| 6466 | /* |
| 6467 | * If we are parsing url in frontend space, we prepare backend stage |
| 6468 | * to not parse again the same url ! optimization lazyness... |
| 6469 | */ |
| 6470 | if (px->options & PR_O_HTTP_PROXY) |
| 6471 | l4->flags |= SN_ADDR_SET; |
| 6472 | |
| 6473 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6474 | return 1; |
| 6475 | } |
| 6476 | |
| 6477 | static int |
| 6478 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6479 | struct acl_expr *expr, struct acl_test *test) |
| 6480 | { |
| 6481 | struct http_txn *txn = l7; |
| 6482 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6483 | if (!txn) |
| 6484 | return 0; |
| 6485 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6486 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6487 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6488 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6489 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6490 | /* ensure the indexes are not affected */ |
| 6491 | return 0; |
| 6492 | |
| 6493 | /* Same optimization as url_ip */ |
| 6494 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 6495 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6496 | |
| 6497 | if (px->options & PR_O_HTTP_PROXY) |
| 6498 | l4->flags |= SN_ADDR_SET; |
| 6499 | |
| 6500 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6501 | return 1; |
| 6502 | } |
| 6503 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6504 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6505 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6506 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6507 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6508 | 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] | 6509 | struct acl_expr *expr, struct acl_test *test) |
| 6510 | { |
| 6511 | struct http_txn *txn = l7; |
| 6512 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6513 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6514 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6515 | if (!txn) |
| 6516 | return 0; |
| 6517 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6518 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6519 | /* search for header from the beginning */ |
| 6520 | ctx->idx = 0; |
| 6521 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6522 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6523 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6524 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6525 | test->len = ctx->vlen; |
| 6526 | test->ptr = (char *)ctx->line + ctx->val; |
| 6527 | return 1; |
| 6528 | } |
| 6529 | |
| 6530 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6531 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6532 | return 0; |
| 6533 | } |
| 6534 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6535 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6536 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6537 | struct acl_expr *expr, struct acl_test *test) |
| 6538 | { |
| 6539 | struct http_txn *txn = l7; |
| 6540 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6541 | if (!txn) |
| 6542 | return 0; |
| 6543 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6544 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6545 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6546 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6547 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6548 | /* ensure the indexes are not affected */ |
| 6549 | return 0; |
| 6550 | |
| 6551 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6552 | } |
| 6553 | |
| 6554 | static int |
| 6555 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6556 | struct acl_expr *expr, struct acl_test *test) |
| 6557 | { |
| 6558 | struct http_txn *txn = l7; |
| 6559 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6560 | if (!txn) |
| 6561 | return 0; |
| 6562 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6563 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6564 | return 0; |
| 6565 | |
| 6566 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6567 | } |
| 6568 | |
| 6569 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6570 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6571 | */ |
| 6572 | static int |
| 6573 | 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] | 6574 | struct acl_expr *expr, struct acl_test *test) |
| 6575 | { |
| 6576 | struct http_txn *txn = l7; |
| 6577 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6578 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6579 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6580 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6581 | if (!txn) |
| 6582 | return 0; |
| 6583 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6584 | ctx.idx = 0; |
| 6585 | cnt = 0; |
| 6586 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6587 | cnt++; |
| 6588 | |
| 6589 | test->i = cnt; |
| 6590 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6591 | return 1; |
| 6592 | } |
| 6593 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6594 | static int |
| 6595 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6596 | struct acl_expr *expr, struct acl_test *test) |
| 6597 | { |
| 6598 | struct http_txn *txn = l7; |
| 6599 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6600 | if (!txn) |
| 6601 | return 0; |
| 6602 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6603 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6604 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6605 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6606 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6607 | /* ensure the indexes are not affected */ |
| 6608 | return 0; |
| 6609 | |
| 6610 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6611 | } |
| 6612 | |
| 6613 | static int |
| 6614 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6615 | struct acl_expr *expr, struct acl_test *test) |
| 6616 | { |
| 6617 | struct http_txn *txn = l7; |
| 6618 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6619 | if (!txn) |
| 6620 | return 0; |
| 6621 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6622 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6623 | return 0; |
| 6624 | |
| 6625 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6626 | } |
| 6627 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6628 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6629 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6630 | * 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] | 6631 | */ |
| 6632 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6633 | 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] | 6634 | struct acl_expr *expr, struct acl_test *test) |
| 6635 | { |
| 6636 | struct http_txn *txn = l7; |
| 6637 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6638 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6639 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6640 | if (!txn) |
| 6641 | return 0; |
| 6642 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6643 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6644 | /* search for header from the beginning */ |
| 6645 | ctx->idx = 0; |
| 6646 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6647 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6648 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6649 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6650 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6651 | return 1; |
| 6652 | } |
| 6653 | |
| 6654 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6655 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6656 | return 0; |
| 6657 | } |
| 6658 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6659 | static int |
| 6660 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6661 | struct acl_expr *expr, struct acl_test *test) |
| 6662 | { |
| 6663 | struct http_txn *txn = l7; |
| 6664 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6665 | if (!txn) |
| 6666 | return 0; |
| 6667 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6668 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6669 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6670 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6671 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6672 | /* ensure the indexes are not affected */ |
| 6673 | return 0; |
| 6674 | |
| 6675 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6676 | } |
| 6677 | |
| 6678 | static int |
| 6679 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6680 | struct acl_expr *expr, struct acl_test *test) |
| 6681 | { |
| 6682 | struct http_txn *txn = l7; |
| 6683 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6684 | if (!txn) |
| 6685 | return 0; |
| 6686 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6687 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 6688 | return 0; |
| 6689 | |
| 6690 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6691 | } |
| 6692 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6693 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6694 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6695 | */ |
| 6696 | static int |
| 6697 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6698 | struct acl_expr *expr, struct acl_test *test) |
| 6699 | { |
| 6700 | struct http_txn *txn = l7; |
| 6701 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6702 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6703 | if (!txn) |
| 6704 | return 0; |
| 6705 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6706 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 6707 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6708 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6709 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6710 | /* ensure the indexes are not affected */ |
| 6711 | return 0; |
| 6712 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6713 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 6714 | ptr = http_get_path(txn); |
| 6715 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6716 | return 0; |
| 6717 | |
| 6718 | /* OK, we got the '/' ! */ |
| 6719 | test->ptr = ptr; |
| 6720 | |
| 6721 | while (ptr < end && *ptr != '?') |
| 6722 | ptr++; |
| 6723 | |
| 6724 | test->len = ptr - test->ptr; |
| 6725 | |
| 6726 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6727 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6728 | return 1; |
| 6729 | } |
| 6730 | |
| 6731 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6732 | |
| 6733 | /************************************************************************/ |
| 6734 | /* All supported keywords must be declared here. */ |
| 6735 | /************************************************************************/ |
| 6736 | |
| 6737 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 6738 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6739 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 6740 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6741 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6742 | { "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] | 6743 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6744 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6745 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6746 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6747 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6748 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6749 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6750 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6751 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 6752 | { "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] | 6753 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6754 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 6755 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6756 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6757 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6758 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6759 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6760 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6761 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6762 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 6763 | { "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] | 6764 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6765 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6766 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 6767 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 6768 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 6769 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 6770 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 6771 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 6772 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 6773 | { "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] | 6774 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6775 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6776 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6777 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6778 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6779 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6780 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6781 | { "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] | 6782 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6783 | { NULL, NULL, NULL, NULL }, |
| 6784 | |
| 6785 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6786 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 6787 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 6788 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 6789 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 6790 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 6791 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 6792 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 6793 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6794 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 6795 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 6796 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 6797 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 6798 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 6799 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 6800 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 6801 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 6802 | |
| 6803 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 6804 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 6805 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 6806 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 6807 | { NULL, NULL, NULL, NULL }, |
| 6808 | #endif |
| 6809 | }}; |
| 6810 | |
| 6811 | |
| 6812 | __attribute__((constructor)) |
| 6813 | static void __http_protocol_init(void) |
| 6814 | { |
| 6815 | acl_register_keywords(&acl_kws); |
| 6816 | } |
| 6817 | |
| 6818 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6819 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6820 | * Local variables: |
| 6821 | * c-indent-level: 8 |
| 6822 | * c-basic-offset: 8 |
| 6823 | * End: |
| 6824 | */ |